rules.mk 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. # Monitor stack size
  61. ifeq ($(USE_MONITOR_STACKSIZE),)
  62. LDOPT := $(LDOPT),--defsym=__mon_stack_size__=0x64
  63. else
  64. LDOPT := $(LDOPT),--defsym=__mon_stack_size__=$(USE_MONITOR_STACKSIZE)
  65. endif
  66. # Output directory and files
  67. ifeq ($(BUILDDIR),)
  68. BUILDDIR = build
  69. endif
  70. ifeq ($(BUILDDIR),.)
  71. BUILDDIR = build
  72. endif
  73. # Dependencies directory
  74. ifeq ($(DEPDIR),)
  75. DEPDIR = .dep
  76. endif
  77. ifeq ($(DEPDIR),.)
  78. DEPDIR = .dep
  79. endif
  80. OUTFILES = $(BUILDDIR)/$(PROJECT).elf $(BUILDDIR)/$(PROJECT).hex \
  81. $(BUILDDIR)/$(PROJECT).bin $(BUILDDIR)/$(PROJECT).dmp \
  82. $(BUILDDIR)/$(PROJECT).list
  83. # Source files groups and paths
  84. ifeq ($(USE_THUMB),yes)
  85. TCSRC += $(CSRC)
  86. TCPPSRC += $(CPPSRC)
  87. else
  88. ACSRC += $(CSRC)
  89. ACPPSRC += $(CPPSRC)
  90. endif
  91. ASRC = $(ACSRC) $(ACPPSRC)
  92. TSRC = $(TCSRC) $(TCPPSRC)
  93. SRCPATHS = $(sort $(dir $(ASMXSRC)) $(dir $(ASMSRC)) $(dir $(ASRC)) $(dir $(TSRC)))
  94. # Various directories
  95. OBJDIR = $(BUILDDIR)/obj
  96. LSTDIR = $(BUILDDIR)/lst
  97. # Object files groups
  98. ACOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ACSRC:.c=.o)))
  99. ACPPOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ACPPSRC:.cpp=.o)))
  100. TCOBJS = $(addprefix $(OBJDIR)/, $(notdir $(TCSRC:.c=.o)))
  101. TCPPOBJS = $(addprefix $(OBJDIR)/, $(notdir $(TCPPSRC:.cpp=.o)))
  102. ASMOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ASMSRC:.s=.o)))
  103. ASMXOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ASMXSRC:.S=.o)))
  104. OBJS = $(ASMXOBJS) $(ASMOBJS) $(ACOBJS) $(TCOBJS) $(ACPPOBJS) $(TCPPOBJS)
  105. # Paths
  106. IINCDIR = $(patsubst %,-I%,$(INCDIR) $(DINCDIR) $(UINCDIR))
  107. LLIBDIR = $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR))
  108. # Macros
  109. DEFS = $(DDEFS) $(UDEFS)
  110. ADEFS = $(DADEFS) $(UADEFS)
  111. # Libs
  112. LIBS = $(DLIBS) $(ULIBS)
  113. # Various settings
  114. MCFLAGS = -mcpu=$(MCU)
  115. ODFLAGS = -x --syms
  116. ASFLAGS = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.s=.lst)) $(ADEFS)
  117. ASXFLAGS = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.S=.lst)) $(ADEFS)
  118. CFLAGS = $(MCFLAGS) $(OPT) $(COPT) $(CWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.c=.lst)) $(DEFS)
  119. CPPFLAGS = $(MCFLAGS) $(OPT) $(CPPOPT) $(CPPWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.cpp=.lst)) $(DEFS)
  120. LDFLAGS = $(MCFLAGS) $(OPT) -nostartfiles $(LLIBDIR) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(RULESPATH)/ld,--script=$(LDSCRIPT)$(LDOPT)
  121. # Thumb interwork enabled only if needed because it kills performance.
  122. ifneq ($(strip $(TSRC)),)
  123. CFLAGS += -DTHUMB_PRESENT
  124. CPPFLAGS += -DTHUMB_PRESENT
  125. ASFLAGS += -DTHUMB_PRESENT
  126. ASXFLAGS += -DTHUMB_PRESENT
  127. ifneq ($(strip $(ASRC)),)
  128. # Mixed ARM and THUMB mode.
  129. CFLAGS += -mthumb-interwork
  130. CPPFLAGS += -mthumb-interwork
  131. ASFLAGS += -mthumb-interwork
  132. ASXFLAGS += -mthumb-interwork
  133. LDFLAGS += -mthumb-interwork
  134. else
  135. # Pure THUMB mode, THUMB C code cannot be called by ARM asm code directly.
  136. CFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING
  137. CPPFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING
  138. ASFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb
  139. ASXFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb
  140. LDFLAGS += -mno-thumb-interwork -mthumb
  141. endif
  142. else
  143. # Pure ARM mode
  144. CFLAGS += -mno-thumb-interwork
  145. CPPFLAGS += -mno-thumb-interwork
  146. ASFLAGS += -mno-thumb-interwork
  147. ASXFLAGS += -mno-thumb-interwork
  148. LDFLAGS += -mno-thumb-interwork
  149. endif
  150. # Generate dependency information
  151. ASFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  152. ASXFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  153. CFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  154. CPPFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  155. # Paths where to search for sources
  156. VPATH = $(SRCPATHS)
  157. #
  158. # Makefile rules
  159. #
  160. all: PRE_MAKE_ALL_RULE_HOOK $(OBJS) $(OUTFILES) POST_MAKE_ALL_RULE_HOOK
  161. PRE_MAKE_ALL_RULE_HOOK:
  162. POST_MAKE_ALL_RULE_HOOK:
  163. $(OBJS): | PRE_MAKE_ALL_RULE_HOOK $(BUILDDIR) $(OBJDIR) $(LSTDIR) $(DEPDIR)
  164. $(BUILDDIR):
  165. ifneq ($(USE_VERBOSE_COMPILE),yes)
  166. @echo Compiler Options
  167. @echo $(CC) -c $(CFLAGS) -I. $(IINCDIR) main.c -o main.o
  168. @echo
  169. endif
  170. @mkdir -p $(BUILDDIR)
  171. $(OBJDIR):
  172. @mkdir -p $(OBJDIR)
  173. $(LSTDIR):
  174. @mkdir -p $(LSTDIR)
  175. $(DEPDIR):
  176. @mkdir -p $(DEPDIR)
  177. $(ACPPOBJS) : $(OBJDIR)/%.o : %.cpp $(MAKEFILE_LIST)
  178. ifeq ($(USE_VERBOSE_COMPILE),yes)
  179. @echo
  180. $(CPPC) -c $(CPPFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  181. else
  182. @echo Compiling $(<F)
  183. @$(CPPC) -c $(CPPFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  184. endif
  185. $(TCPPOBJS) : $(OBJDIR)/%.o : %.cpp $(MAKEFILE_LIST)
  186. ifeq ($(USE_VERBOSE_COMPILE),yes)
  187. @echo
  188. $(CPPC) -c $(CPPFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  189. else
  190. @echo Compiling $(<F)
  191. @$(CPPC) -c $(CPPFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  192. endif
  193. $(ACOBJS) : $(OBJDIR)/%.o : %.c $(MAKEFILE_LIST)
  194. ifeq ($(USE_VERBOSE_COMPILE),yes)
  195. @echo
  196. $(CC) -c $(CFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  197. else
  198. @echo Compiling $(<F)
  199. @$(CC) -c $(CFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  200. endif
  201. $(TCOBJS) : $(OBJDIR)/%.o : %.c $(MAKEFILE_LIST)
  202. ifeq ($(USE_VERBOSE_COMPILE),yes)
  203. @echo
  204. $(CC) -c $(CFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  205. else
  206. @echo Compiling $(<F)
  207. @$(CC) -c $(CFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  208. endif
  209. $(ASMOBJS) : $(OBJDIR)/%.o : %.s $(MAKEFILE_LIST)
  210. ifeq ($(USE_VERBOSE_COMPILE),yes)
  211. @echo
  212. $(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  213. else
  214. @echo Compiling $(<F)
  215. @$(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  216. endif
  217. $(ASMXOBJS) : $(OBJDIR)/%.o : %.S $(MAKEFILE_LIST)
  218. ifeq ($(USE_VERBOSE_COMPILE),yes)
  219. @echo
  220. $(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  221. else
  222. @echo Compiling $(<F)
  223. @$(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  224. endif
  225. $(BUILDDIR)/$(PROJECT).elf: $(OBJS) $(LDSCRIPT)
  226. ifeq ($(USE_VERBOSE_COMPILE),yes)
  227. @echo
  228. $(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  229. else
  230. @echo Linking $@
  231. @$(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  232. endif
  233. %.hex: %.elf
  234. ifeq ($(USE_VERBOSE_COMPILE),yes)
  235. $(HEX) $< $@
  236. else
  237. @echo Creating $@
  238. @$(HEX) $< $@
  239. endif
  240. %.bin: %.elf
  241. ifeq ($(USE_VERBOSE_COMPILE),yes)
  242. $(BIN) $< $@
  243. else
  244. @echo Creating $@
  245. @$(BIN) $< $@
  246. endif
  247. %.dmp: %.elf
  248. ifeq ($(USE_VERBOSE_COMPILE),yes)
  249. $(OD) $(ODFLAGS) $< > $@
  250. $(SZ) $<
  251. else
  252. @echo Creating $@
  253. @$(OD) $(ODFLAGS) $< > $@
  254. @echo
  255. @$(SZ) $<
  256. endif
  257. %.list: %.elf
  258. ifeq ($(USE_VERBOSE_COMPILE),yes)
  259. $(OD) -S $< > $@
  260. else
  261. @echo Creating $@
  262. @$(OD) -S $< > $@
  263. @echo
  264. @echo Done
  265. endif
  266. lib: $(OBJS) $(BUILDDIR)/lib$(PROJECT).a
  267. $(BUILDDIR)/lib$(PROJECT).a: $(OBJS)
  268. @$(AR) -r $@ $^
  269. @echo
  270. @echo Done
  271. clean: CLEAN_RULE_HOOK
  272. @echo Cleaning
  273. @echo - $(DEPDIR)
  274. @-rm -fR $(DEPDIR)/* $(BUILDDIR)/* 2>/dev/null
  275. @-if [ -d "$(DEPDIR)" ]; then rmdir -p --ignore-fail-on-non-empty $(subst ./,,$(DEPDIR)) 2>/dev/null; fi
  276. @echo - $(BUILDDIR)
  277. @-if [ -d "$(BUILDDIR)" ]; then rmdir -p --ignore-fail-on-non-empty $(subst ./,,$(BUILDDIR)) 2>/dev/null; fi
  278. @echo
  279. @echo Done
  280. CLEAN_RULE_HOOK:
  281. #
  282. # Include the dependency files, should be the last of the makefile
  283. #
  284. -include $(wildcard $(DEPDIR)/*)
  285. # *** EOF ***