rules.mk 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # ARM Cortex-Mx common makefile scripts and rules.
  2. ##############################################################################
  3. # Processing options coming from the upper Makefile.
  4. #
  5. # Compiler options
  6. OPT = $(USE_OPT)
  7. COPT = $(USE_COPT)
  8. CPPOPT = $(USE_CPPOPT)
  9. # Garbage collection
  10. ifeq ($(USE_LINK_GC),yes)
  11. OPT += -ffunction-sections -fdata-sections -fno-common
  12. LDOPT := ,--gc-sections
  13. else
  14. LDOPT :=
  15. endif
  16. # Linker extra options
  17. ifneq ($(USE_LDOPT),)
  18. LDOPT := $(LDOPT),$(USE_LDOPT)
  19. endif
  20. # Link time optimizations
  21. ifeq ($(USE_LTO),yes)
  22. OPT += -flto
  23. endif
  24. # Undefined state stack size
  25. ifeq ($(USE_UND_STACKSIZE),)
  26. LDOPT := $(LDOPT),--defsym=__und_stack_size__=8
  27. else
  28. LDOPT := $(LDOPT),--defsym=__und_stack_size__=$(USE_UND_STACKSIZE)
  29. endif
  30. # Abort stack size
  31. ifeq ($(USE_ABT_STACKSIZE),)
  32. LDOPT := $(LDOPT),--defsym=__abt_stack_size__=8
  33. else
  34. LDOPT := $(LDOPT),--defsym=__abt_stack_size__=$(USE_ABT_STACKSIZE)
  35. endif
  36. # FIQ stack size
  37. ifeq ($(USE_FIQ_STACKSIZE),)
  38. LDOPT := $(LDOPT),--defsym=__fiq_stack_size__=64
  39. else
  40. LDOPT := $(LDOPT),--defsym=__fiq_stack_size__=$(USE_FIQ_STACKSIZE)
  41. endif
  42. # IRQ stack size
  43. ifeq ($(USE_IRQ_STACKSIZE),)
  44. LDOPT := $(LDOPT),--defsym=__irq_stack_size__=0x400
  45. else
  46. LDOPT := $(LDOPT),--defsym=__irq_stack_size__=$(USE_IRQ_STACKSIZE)
  47. endif
  48. # Supervisor stack size
  49. ifeq ($(USE_SUPERVISOR_STACKSIZE),)
  50. LDOPT := $(LDOPT),--defsym=__svc_stack_size__=8
  51. else
  52. LDOPT := $(LDOPT),--defsym=__svc_stack_size__=$(USE_SUPERVISOR_STACKSIZE)
  53. endif
  54. # System stack size
  55. ifeq ($(USE_SYSTEM_STACKSIZE),)
  56. LDOPT := $(LDOPT),--defsym=__sys_stack_size__=0x400
  57. else
  58. LDOPT := $(LDOPT),--defsym=__sys_stack_size__=$(USE_SYSTEM_STACKSIZE)
  59. endif
  60. # Output directory and files
  61. ifeq ($(BUILDDIR),)
  62. BUILDDIR = build
  63. endif
  64. ifeq ($(BUILDDIR),.)
  65. BUILDDIR = build
  66. endif
  67. # Dependencies directory
  68. ifeq ($(DEPDIR),)
  69. DEPDIR = .dep
  70. endif
  71. ifeq ($(DEPDIR),.)
  72. DEPDIR = .dep
  73. endif
  74. OUTFILES = $(BUILDDIR)/$(PROJECT).elf $(BUILDDIR)/$(PROJECT).hex \
  75. $(BUILDDIR)/$(PROJECT).bin $(BUILDDIR)/$(PROJECT).dmp \
  76. $(BUILDDIR)/$(PROJECT).list
  77. # Source files groups and paths
  78. ifeq ($(USE_THUMB),yes)
  79. TCSRC += $(CSRC)
  80. TCPPSRC += $(CPPSRC)
  81. else
  82. ACSRC += $(CSRC)
  83. ACPPSRC += $(CPPSRC)
  84. endif
  85. ASRC = $(ACSRC) $(ACPPSRC)
  86. TSRC = $(TCSRC) $(TCPPSRC)
  87. SRCPATHS = $(sort $(dir $(ASMXSRC)) $(dir $(ASMSRC)) $(dir $(ASRC)) $(dir $(TSRC)))
  88. # Various directories
  89. OBJDIR = $(BUILDDIR)/obj
  90. LSTDIR = $(BUILDDIR)/lst
  91. # Object files groups
  92. ACOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ACSRC:.c=.o)))
  93. ACPPOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ACPPSRC:.cpp=.o)))
  94. TCOBJS = $(addprefix $(OBJDIR)/, $(notdir $(TCSRC:.c=.o)))
  95. TCPPOBJS = $(addprefix $(OBJDIR)/, $(notdir $(TCPPSRC:.cpp=.o)))
  96. ASMOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ASMSRC:.s=.o)))
  97. ASMXOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ASMXSRC:.S=.o)))
  98. OBJS = $(ASMXOBJS) $(ASMOBJS) $(ACOBJS) $(TCOBJS) $(ACPPOBJS) $(TCPPOBJS)
  99. # Paths
  100. IINCDIR = $(patsubst %,-I%,$(INCDIR) $(DINCDIR) $(UINCDIR))
  101. LLIBDIR = $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR))
  102. # Macros
  103. DEFS = $(DDEFS) $(UDEFS)
  104. ADEFS = $(DADEFS) $(UADEFS)
  105. # Libs
  106. LIBS = $(DLIBS) $(ULIBS)
  107. # Various settings
  108. MCFLAGS = -mcpu=$(MCU)
  109. ODFLAGS = -x --syms
  110. ASFLAGS = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.s=.lst)) $(ADEFS)
  111. ASXFLAGS = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.S=.lst)) $(ADEFS)
  112. CFLAGS = $(MCFLAGS) $(OPT) $(COPT) $(CWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.c=.lst)) $(DEFS)
  113. CPPFLAGS = $(MCFLAGS) $(OPT) $(CPPOPT) $(CPPWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.cpp=.lst)) $(DEFS)
  114. LDFLAGS = $(MCFLAGS) $(OPT) -nostartfiles $(LLIBDIR) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(RULESPATH)/ld,--script=$(LDSCRIPT)$(LDOPT)
  115. # Thumb interwork enabled only if needed because it kills performance.
  116. ifneq ($(strip $(TSRC)),)
  117. CFLAGS += -DTHUMB_PRESENT
  118. CPPFLAGS += -DTHUMB_PRESENT
  119. ASFLAGS += -DTHUMB_PRESENT
  120. ASXFLAGS += -DTHUMB_PRESENT
  121. ifneq ($(strip $(ASRC)),)
  122. # Mixed ARM and THUMB mode.
  123. CFLAGS += -mthumb-interwork
  124. CPPFLAGS += -mthumb-interwork
  125. ASFLAGS += -mthumb-interwork
  126. ASXFLAGS += -mthumb-interwork
  127. LDFLAGS += -mthumb-interwork
  128. else
  129. # Pure THUMB mode, THUMB C code cannot be called by ARM asm code directly.
  130. CFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING
  131. CPPFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING
  132. ASFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb
  133. ASXFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb
  134. LDFLAGS += -mno-thumb-interwork -mthumb
  135. endif
  136. else
  137. # Pure ARM mode
  138. CFLAGS += -mno-thumb-interwork
  139. CPPFLAGS += -mno-thumb-interwork
  140. ASFLAGS += -mno-thumb-interwork
  141. ASXFLAGS += -mno-thumb-interwork
  142. LDFLAGS += -mno-thumb-interwork
  143. endif
  144. # Generate dependency information
  145. ASFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  146. ASXFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  147. CFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  148. CPPFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  149. # Paths where to search for sources
  150. VPATH = $(SRCPATHS)
  151. #
  152. # Makefile rules
  153. #
  154. all: PRE_MAKE_ALL_RULE_HOOK $(OBJS) $(OUTFILES) POST_MAKE_ALL_RULE_HOOK
  155. PRE_MAKE_ALL_RULE_HOOK:
  156. POST_MAKE_ALL_RULE_HOOK:
  157. $(OBJS): | PRE_MAKE_ALL_RULE_HOOK $(BUILDDIR) $(OBJDIR) $(LSTDIR) $(DEPDIR)
  158. $(BUILDDIR):
  159. ifneq ($(USE_VERBOSE_COMPILE),yes)
  160. @echo Compiler Options
  161. @echo $(CC) -c $(CFLAGS) -I. $(IINCDIR) main.c -o main.o
  162. @echo
  163. endif
  164. @mkdir -p $(BUILDDIR)
  165. $(OBJDIR):
  166. @mkdir -p $(OBJDIR)
  167. $(LSTDIR):
  168. @mkdir -p $(LSTDIR)
  169. $(DEPDIR):
  170. @mkdir -p $(DEPDIR)
  171. $(ACPPOBJS) : $(OBJDIR)/%.o : %.cpp $(MAKEFILE_LIST)
  172. ifeq ($(USE_VERBOSE_COMPILE),yes)
  173. @echo
  174. $(CPPC) -c $(CPPFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  175. else
  176. @echo Compiling $(<F)
  177. @$(CPPC) -c $(CPPFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  178. endif
  179. $(TCPPOBJS) : $(OBJDIR)/%.o : %.cpp $(MAKEFILE_LIST)
  180. ifeq ($(USE_VERBOSE_COMPILE),yes)
  181. @echo
  182. $(CPPC) -c $(CPPFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  183. else
  184. @echo Compiling $(<F)
  185. @$(CPPC) -c $(CPPFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  186. endif
  187. $(ACOBJS) : $(OBJDIR)/%.o : %.c $(MAKEFILE_LIST)
  188. ifeq ($(USE_VERBOSE_COMPILE),yes)
  189. @echo
  190. $(CC) -c $(CFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  191. else
  192. @echo Compiling $(<F)
  193. @$(CC) -c $(CFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  194. endif
  195. $(TCOBJS) : $(OBJDIR)/%.o : %.c $(MAKEFILE_LIST)
  196. ifeq ($(USE_VERBOSE_COMPILE),yes)
  197. @echo
  198. $(CC) -c $(CFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  199. else
  200. @echo Compiling $(<F)
  201. @$(CC) -c $(CFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  202. endif
  203. $(ASMOBJS) : $(OBJDIR)/%.o : %.s $(MAKEFILE_LIST)
  204. ifeq ($(USE_VERBOSE_COMPILE),yes)
  205. @echo
  206. $(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  207. else
  208. @echo Compiling $(<F)
  209. @$(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  210. endif
  211. $(ASMXOBJS) : $(OBJDIR)/%.o : %.S $(MAKEFILE_LIST)
  212. ifeq ($(USE_VERBOSE_COMPILE),yes)
  213. @echo
  214. $(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  215. else
  216. @echo Compiling $(<F)
  217. @$(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  218. endif
  219. $(BUILDDIR)/$(PROJECT).elf: $(OBJS) $(LDSCRIPT)
  220. ifeq ($(USE_VERBOSE_COMPILE),yes)
  221. @echo
  222. $(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  223. else
  224. @echo Linking $@
  225. @$(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  226. endif
  227. %.hex: %.elf
  228. ifeq ($(USE_VERBOSE_COMPILE),yes)
  229. $(HEX) $< $@
  230. else
  231. @echo Creating $@
  232. @$(HEX) $< $@
  233. endif
  234. %.bin: %.elf
  235. ifeq ($(USE_VERBOSE_COMPILE),yes)
  236. $(BIN) $< $@
  237. else
  238. @echo Creating $@
  239. @$(BIN) $< $@
  240. endif
  241. %.dmp: %.elf
  242. ifeq ($(USE_VERBOSE_COMPILE),yes)
  243. $(OD) $(ODFLAGS) $< > $@
  244. $(SZ) $<
  245. else
  246. @echo Creating $@
  247. @$(OD) $(ODFLAGS) $< > $@
  248. @echo
  249. @$(SZ) $<
  250. endif
  251. %.list: %.elf
  252. ifeq ($(USE_VERBOSE_COMPILE),yes)
  253. $(OD) -S $< > $@
  254. else
  255. @echo Creating $@
  256. @$(OD) -S $< > $@
  257. @echo
  258. @echo Done
  259. endif
  260. lib: $(OBJS) $(BUILDDIR)/lib$(PROJECT).a
  261. $(BUILDDIR)/lib$(PROJECT).a: $(OBJS)
  262. @$(AR) -r $@ $^
  263. @echo
  264. @echo Done
  265. clean: CLEAN_RULE_HOOK
  266. @echo Cleaning
  267. @echo - $(DEPDIR)
  268. @-rm -fR $(DEPDIR)/* $(BUILDDIR)/* 2>/dev/null
  269. @-if [ -d "$(DEPDIR)" ]; then rmdir -p --ignore-fail-on-non-empty $(subst ./,,$(DEPDIR)) 2>/dev/null; fi
  270. @echo - $(BUILDDIR)
  271. @-if [ -d "$(BUILDDIR)" ]; then rmdir -p --ignore-fail-on-non-empty $(subst ./,,$(BUILDDIR)) 2>/dev/null; fi
  272. @echo
  273. @echo Done
  274. CLEAN_RULE_HOOK:
  275. #
  276. # Include the dependency files, should be the last of the makefile
  277. #
  278. -include $(wildcard $(DEPDIR)/*)
  279. # *** EOF ***