chibios_common.mk 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. # FPU options default (Cortex-M4 and Cortex-M7 single precision).
  25. ifeq ($(USE_FPU_OPT),)
  26. USE_FPU_OPT = -mfloat-abi=$(USE_FPU) -mfpu=fpv4-sp-d16 -fsingle-precision-constant
  27. endif
  28. # FPU-related options
  29. ifeq ($(USE_FPU),)
  30. USE_FPU = hard
  31. endif
  32. ifneq ($(USE_FPU),no)
  33. OPT += $(USE_FPU_OPT)
  34. DDEFS += -DCORTEX_USE_FPU=TRUE
  35. DADEFS += -DCORTEX_USE_FPU=TRUE
  36. else
  37. DDEFS += -DCORTEX_USE_FPU=FALSE
  38. DADEFS += -DCORTEX_USE_FPU=FALSE
  39. endif
  40. # Process stack size
  41. ifeq ($(USE_PROCESS_STACKSIZE),)
  42. LDOPT := $(LDOPT),--defsym=__process_stack_size__=0x400
  43. else
  44. LDOPT := $(LDOPT),--defsym=__process_stack_size__=$(USE_PROCESS_STACKSIZE)
  45. endif
  46. # Exceptions stack size
  47. ifeq ($(USE_EXCEPTIONS_STACKSIZE),)
  48. LDOPT := $(LDOPT),--defsym=__main_stack_size__=0x400
  49. else
  50. LDOPT := $(LDOPT),--defsym=__main_stack_size__=$(USE_EXCEPTIONS_STACKSIZE)
  51. endif
  52. # Output directory and files
  53. ifeq ($(BUILDDIR),)
  54. BUILDDIR = build
  55. endif
  56. ifeq ($(BUILDDIR),.)
  57. BUILDDIR = build
  58. endif
  59. OUTFILES := $(BUILDDIR)/$(PROJECT).elf \
  60. $(BUILDDIR)/$(PROJECT).hex \
  61. $(BUILDDIR)/$(PROJECT).bin \
  62. $(BUILDDIR)/$(PROJECT).dmp \
  63. $(BUILDDIR)/$(PROJECT).list
  64. ifdef SREC
  65. OUTFILES += $(BUILDDIR)/$(PROJECT).srec
  66. endif
  67. # Source files groups and paths
  68. ifeq ($(USE_THUMB),yes)
  69. TCSRC += $(CSRC)
  70. TCPPSRC += $(CPPSRC)
  71. else
  72. ACSRC += $(CSRC)
  73. ACPPSRC += $(CPPSRC)
  74. endif
  75. ASRC := $(ACSRC) $(ACPPSRC)
  76. TSRC := $(TCSRC) $(TCPPSRC)
  77. SRCPATHS := $(sort $(dir $(ASMXSRC)) $(dir $(ASMSRC)) $(dir $(ASRC)) $(dir $(TSRC)))
  78. # Various directories
  79. OBJDIR := $(BUILDDIR)/obj
  80. LSTDIR := $(BUILDDIR)/lst
  81. # Object files groups
  82. ACOBJS := $(addprefix $(OBJDIR)/, $(notdir $(ACSRC:.c=.o)))
  83. ACPPOBJS := $(addprefix $(OBJDIR)/, $(notdir $(ACPPSRC:.cpp=.o)))
  84. TCOBJS := $(addprefix $(OBJDIR)/, $(notdir $(TCSRC:.c=.o)))
  85. TCPPOBJS := $(addprefix $(OBJDIR)/, $(notdir $(TCPPSRC:.cpp=.o)))
  86. ASMOBJS := $(addprefix $(OBJDIR)/, $(notdir $(ASMSRC:.s=.o)))
  87. ASMXOBJS := $(addprefix $(OBJDIR)/, $(notdir $(ASMXSRC:.S=.o)))
  88. OBJS := $(ASMXOBJS) $(ASMOBJS) $(ACOBJS) $(TCOBJS) $(ACPPOBJS) $(TCPPOBJS)
  89. # Paths
  90. IINCDIR := $(patsubst %,-I%,$(INCDIR) $(DINCDIR) $(UINCDIR))
  91. LLIBDIR := $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR))
  92. LLIBDIR += -L$(dir $(LDSCRIPT))
  93. # Macros
  94. DEFS := $(DDEFS) $(UDEFS)
  95. ADEFS := $(DADEFS) $(UADEFS)
  96. # Libs
  97. LIBS := $(DLIBS) $(ULIBS)
  98. # Various settings
  99. MCFLAGS := -mcpu=$(MCU)
  100. ODFLAGS = -x --syms
  101. ASFLAGS = $(MCFLAGS) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.s=.lst)) $(ADEFS)
  102. ASXFLAGS = $(MCFLAGS) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.S=.lst)) $(ADEFS)
  103. CFLAGS = $(MCFLAGS) $(OPT) $(COPT) $(CWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.c=.lst)) $(DEFS)
  104. CPPFLAGS = $(MCFLAGS) $(OPT) $(CPPOPT) $(CPPWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.cpp=.lst)) $(DEFS)
  105. LDFLAGS = $(MCFLAGS) $(OPT) -nostartfiles $(LLIBDIR) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(RULESPATH)/ld,--script=$(LDSCRIPT)$(LDOPT)
  106. # provide a marker for ArduPilot build options in ChibiOS
  107. CFLAGS += -D_ARDUPILOT_
  108. ifeq ($(ENABLE_ASSERTS),yes)
  109. ASXFLAGS += -DHAL_CHIBIOS_ENABLE_ASSERTS
  110. endif
  111. # Thumb interwork enabled only if needed because it kills performance.
  112. ifneq ($(strip $(TSRC)),)
  113. CFLAGS += -DTHUMB_PRESENT
  114. CPPFLAGS += -DTHUMB_PRESENT
  115. ASFLAGS += -DTHUMB_PRESENT
  116. ASXFLAGS += -DTHUMB_PRESENT
  117. ifneq ($(strip $(ASRC)),)
  118. # Mixed ARM and THUMB mode.
  119. CFLAGS += -mthumb-interwork
  120. CPPFLAGS += -mthumb-interwork
  121. ASFLAGS += -mthumb-interwork
  122. ASXFLAGS += -mthumb-interwork
  123. LDFLAGS += -mthumb-interwork
  124. else
  125. # Pure THUMB mode, THUMB C code cannot be called by ARM asm code directly.
  126. CFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING
  127. CPPFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING
  128. ASFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb
  129. ASXFLAGS += -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb
  130. LDFLAGS += -mno-thumb-interwork -mthumb
  131. endif
  132. else
  133. # Pure ARM mode
  134. CFLAGS += -mno-thumb-interwork
  135. CPPFLAGS += -mno-thumb-interwork
  136. ASFLAGS += -mno-thumb-interwork
  137. ASXFLAGS += -mno-thumb-interwork
  138. LDFLAGS += -mno-thumb-interwork
  139. endif
  140. # Generate dependency information
  141. ASFLAGS += -MD -MP -MF .dep/$(@F).d
  142. ASXFLAGS += -MD -MP -MF .dep/$(@F).d
  143. CFLAGS += -MD -MP -MF .dep/$(@F).d
  144. CPPFLAGS += -MD -MP -MF .dep/$(@F).d
  145. # Paths where to search for sources
  146. VPATH = $(SRCPATHS)
  147. ifndef ECHO
  148. T := $(shell $(MAKE) $(MAKECMDGOALS) --no-print-directory \
  149. -nrRf $(firstword $(MAKEFILE_LIST)) \
  150. ECHO="COUNTTHIS" | grep -c "COUNTTHIS")
  151. N := x
  152. C = $(words $N)$(eval N := x $N)
  153. ECHO = echo "[$C/$T] ChibiOS:"
  154. endif
  155. all: PRE_MAKE_ALL_RULE_HOOK $(OBJS) $(OUTFILES) POST_MAKE_ALL_RULE_HOOK
  156. PRE_MAKE_ALL_RULE_HOOK:
  157. POST_MAKE_ALL_RULE_HOOK:
  158. $(OBJS): | $(BUILDDIR) $(OBJDIR) $(LSTDIR)
  159. $(BUILDDIR):
  160. ifneq ($(USE_VERBOSE_COMPILE),yes)
  161. @echo Compiler Options
  162. @echo $(CC) -c $(CFLAGS) -I. $(IINCDIR) main.c -o main.o
  163. @echo
  164. endif
  165. @mkdir -p $(BUILDDIR)
  166. $(OBJDIR):
  167. @mkdir -p $(OBJDIR)
  168. $(LSTDIR):
  169. @mkdir -p $(LSTDIR)
  170. $(ACPPOBJS) : $(OBJDIR)/%.o : %.cpp
  171. ifeq ($(USE_VERBOSE_COMPILE),yes)
  172. @echo
  173. $(CPPC) -c $(CPPFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  174. else
  175. @$(ECHO) Compiling $(<F)
  176. @$(CPPC) -c $(CPPFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  177. endif
  178. $(TCPPOBJS) : $(OBJDIR)/%.o : %.cpp
  179. ifeq ($(USE_VERBOSE_COMPILE),yes)
  180. @echo
  181. $(CPPC) -c $(CPPFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  182. else
  183. @$(ECHO) Compiling $(<F)
  184. @$(CPPC) -c $(CPPFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  185. endif
  186. $(ACOBJS) : $(OBJDIR)/%.o : %.c
  187. ifeq ($(USE_VERBOSE_COMPILE),yes)
  188. @echo
  189. $(CC) -c $(CFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  190. else
  191. @$(ECHO) Compiling $(<F)
  192. @$(CC) -c $(CFLAGS) $(AOPT) -I. $(IINCDIR) $< -o $@
  193. endif
  194. $(TCOBJS) : $(OBJDIR)/%.o : %.c
  195. ifeq ($(USE_VERBOSE_COMPILE),yes)
  196. @echo
  197. $(CC) -c $(CFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  198. else
  199. @$(ECHO) Compiling $(<F)
  200. @$(CC) -c $(CFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  201. endif
  202. $(ASMOBJS) : $(OBJDIR)/%.o : %.s
  203. ifeq ($(USE_VERBOSE_COMPILE),yes)
  204. @echo
  205. $(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  206. else
  207. @$(ECHO) Compiling $(<F)
  208. @$(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  209. endif
  210. $(ASMXOBJS) : $(OBJDIR)/%.o : %.S
  211. ifeq ($(USE_VERBOSE_COMPILE),yes)
  212. @echo
  213. $(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  214. else
  215. @$(ECHO) Compiling $(<F)
  216. @$(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  217. endif
  218. $(BUILDDIR)/$(PROJECT).elf: $(OBJS) $(LDSCRIPT)
  219. ifeq ($(USE_VERBOSE_COMPILE),yes)
  220. @echo
  221. $(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  222. else
  223. @echo Linking $@
  224. @$(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  225. endif
  226. %.hex: %.elf
  227. ifeq ($(USE_VERBOSE_COMPILE),yes)
  228. $(HEX) $< $@
  229. else
  230. @echo Creating $@
  231. @$(HEX) $< $@
  232. endif
  233. %.bin: %.elf
  234. ifeq ($(USE_VERBOSE_COMPILE),yes)
  235. $(BIN) $< $@
  236. else
  237. @echo Creating $@
  238. @$(BIN) $< $@
  239. endif
  240. %.srec: %.elf
  241. ifdef SREC
  242. ifeq ($(USE_VERBOSE_COMPILE),yes)
  243. $(SREC) $< $@
  244. else
  245. @echo Creating $@
  246. @$(SREC) $< $@
  247. endif
  248. endif
  249. %.dmp: %.elf
  250. ifeq ($(USE_VERBOSE_COMPILE),yes)
  251. $(OD) $(ODFLAGS) $< > $@
  252. $(SZ) $<
  253. else
  254. @echo Creating $@
  255. @$(OD) $(ODFLAGS) $< > $@
  256. @echo
  257. @$(SZ) $<
  258. endif
  259. %.list: %.elf
  260. ifeq ($(USE_VERBOSE_COMPILE),yes)
  261. $(OD) -S $< > $@
  262. else
  263. @echo Creating $@
  264. @$(OD) -S $< > $@
  265. @echo
  266. @echo Done
  267. endif
  268. lib: $(OBJS) $(BUILDDIR)/lib$(PROJECT).a pass
  269. $(BUILDDIR)/lib$(PROJECT).a: $(OBJS)
  270. @$(AR) -r $@ $^
  271. @echo
  272. @echo ChibiOS: Done!
  273. pass: $(BUILDDIR)
  274. @echo $(foreach f,$(IINCDIR),"$(f);") > $(BUILDDIR)/include_dirs
  275. clean: CLEAN_RULE_HOOK
  276. @echo Cleaning
  277. -rm -fR .dep $(BUILDDIR)
  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 $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
  285. # *** EOF ***