rules.mk 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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
  27. endif
  28. # FPU-related options
  29. ifeq ($(USE_FPU),)
  30. USE_FPU = no
  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. OPT += -mfloat-abi=soft
  38. DDEFS += -DCORTEX_USE_FPU=FALSE
  39. DADEFS += -DCORTEX_USE_FPU=FALSE
  40. endif
  41. # Process stack size
  42. ifeq ($(USE_PROCESS_STACKSIZE),)
  43. LDOPT := $(LDOPT),--defsym=__process_stack_size__=0x400
  44. else
  45. LDOPT := $(LDOPT),--defsym=__process_stack_size__=$(USE_PROCESS_STACKSIZE)
  46. endif
  47. # Exceptions stack size
  48. ifeq ($(USE_EXCEPTIONS_STACKSIZE),)
  49. LDOPT := $(LDOPT),--defsym=__main_stack_size__=0x400
  50. else
  51. LDOPT := $(LDOPT),--defsym=__main_stack_size__=$(USE_EXCEPTIONS_STACKSIZE)
  52. endif
  53. # Output directory and files
  54. ifeq ($(BUILDDIR),)
  55. BUILDDIR = build
  56. endif
  57. ifeq ($(BUILDDIR),.)
  58. BUILDDIR = build
  59. endif
  60. # Dependencies directory
  61. ifeq ($(DEPDIR),)
  62. DEPDIR = .dep
  63. endif
  64. ifeq ($(DEPDIR),.)
  65. DEPDIR = .dep
  66. endif
  67. OUTFILES := $(BUILDDIR)/$(PROJECT).elf \
  68. $(BUILDDIR)/$(PROJECT).hex \
  69. $(BUILDDIR)/$(PROJECT).bin \
  70. $(BUILDDIR)/$(PROJECT).dmp \
  71. $(BUILDDIR)/$(PROJECT).list
  72. ifdef SREC
  73. OUTFILES += $(BUILDDIR)/$(PROJECT).srec
  74. endif
  75. # Source files groups and paths
  76. TCSRC += $(CSRC)
  77. TCPPSRC += $(CPPSRC)
  78. TSRC := $(TCSRC) $(TCPPSRC)
  79. SRCPATHS := $(sort $(dir $(ASMXSRC)) $(dir $(ASMSRC)) $(dir $(TSRC)))
  80. # Various directories
  81. OBJDIR := $(BUILDDIR)/obj
  82. LSTDIR := $(BUILDDIR)/lst
  83. # Object files groups
  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. # Macros
  93. DEFS := $(DDEFS) $(UDEFS)
  94. ADEFS := $(DADEFS) $(UADEFS)
  95. # Libs
  96. LIBS := $(DLIBS) $(ULIBS)
  97. # Various settings
  98. MCFLAGS := -mcpu=$(MCU) -mthumb
  99. ODFLAGS = -x --syms
  100. ASFLAGS = $(MCFLAGS) $(OPT) $(ADEFS)
  101. ASXFLAGS = $(MCFLAGS) $(OPT) $(ADEFS)
  102. CFLAGS = $(MCFLAGS) $(OPT) $(COPT) $(CWARN) $(DEFS)
  103. CPPFLAGS = $(MCFLAGS) $(OPT) $(CPPOPT) $(CPPWARN) $(DEFS)
  104. #ASFLAGS = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.s=.lst)) $(ADEFS)
  105. #ASXFLAGS = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.S=.lst)) $(ADEFS)
  106. #CFLAGS = $(MCFLAGS) $(OPT) $(COPT) $(CWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.c=.lst)) $(DEFS)
  107. #CPPFLAGS = $(MCFLAGS) $(OPT) $(CPPOPT) $(CPPWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.cpp=.lst)) $(DEFS)
  108. LDFLAGS = $(MCFLAGS) $(OPT) -nostartfiles $(LLIBDIR) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(STARTUPLD),--script=$(LDSCRIPT)$(LDOPT)
  109. # Generate dependency information
  110. ASFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  111. ASXFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  112. CFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  113. CPPFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  114. # Paths where to search for sources
  115. VPATH = $(SRCPATHS)
  116. #
  117. # Makefile rules
  118. #
  119. all: PRE_MAKE_ALL_RULE_HOOK $(OBJS) $(OUTFILES) POST_MAKE_ALL_RULE_HOOK
  120. PRE_MAKE_ALL_RULE_HOOK:
  121. POST_MAKE_ALL_RULE_HOOK:
  122. $(OBJS): | PRE_MAKE_ALL_RULE_HOOK $(BUILDDIR) $(OBJDIR) $(LSTDIR) $(DEPDIR)
  123. $(BUILDDIR):
  124. ifneq ($(USE_VERBOSE_COMPILE),yes)
  125. @echo Compiler Options
  126. @echo $(CC) -c $(CFLAGS) -I. $(IINCDIR) main.c -o main.o
  127. @echo
  128. endif
  129. @mkdir -p $(BUILDDIR)
  130. $(OBJDIR):
  131. @mkdir -p $(OBJDIR)
  132. $(LSTDIR):
  133. @mkdir -p $(LSTDIR)
  134. $(DEPDIR):
  135. @mkdir -p $(DEPDIR)
  136. $(TCPPOBJS) : $(OBJDIR)/%.o : %.cpp $(MAKEFILE_LIST)
  137. ifeq ($(USE_VERBOSE_COMPILE),yes)
  138. @echo
  139. $(CPPC) -c $(CPPFLAGS) -I. $(IINCDIR) $< -o $@
  140. else
  141. @echo Compiling $(<F)
  142. @$(CPPC) -c $(CPPFLAGS) -I. $(IINCDIR) $< -o $@
  143. endif
  144. $(TCOBJS) : $(OBJDIR)/%.o : %.c $(MAKEFILE_LIST)
  145. ifeq ($(USE_VERBOSE_COMPILE),yes)
  146. @echo
  147. $(CC) -c $(CFLAGS) -I. $(IINCDIR) $< -o $@
  148. else
  149. @echo Compiling $(<F)
  150. @$(CC) -c $(CFLAGS) -I. $(IINCDIR) $< -o $@
  151. endif
  152. $(ASMOBJS) : $(OBJDIR)/%.o : %.s $(MAKEFILE_LIST)
  153. ifeq ($(USE_VERBOSE_COMPILE),yes)
  154. @echo
  155. $(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  156. else
  157. @echo Compiling $(<F)
  158. @$(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  159. endif
  160. $(ASMXOBJS) : $(OBJDIR)/%.o : %.S $(MAKEFILE_LIST)
  161. ifeq ($(USE_VERBOSE_COMPILE),yes)
  162. @echo
  163. $(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  164. else
  165. @echo Compiling $(<F)
  166. @$(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  167. endif
  168. $(BUILDDIR)/$(PROJECT).elf: $(OBJS) $(LDSCRIPT)
  169. ifeq ($(USE_VERBOSE_COMPILE),yes)
  170. @echo
  171. $(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  172. else
  173. @echo Linking $@
  174. @$(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  175. endif
  176. %.hex: %.elf
  177. ifeq ($(USE_VERBOSE_COMPILE),yes)
  178. $(HEX) $< $@
  179. else
  180. @echo Creating $@
  181. @$(HEX) $< $@
  182. endif
  183. %.bin: %.elf
  184. ifeq ($(USE_VERBOSE_COMPILE),yes)
  185. $(BIN) $< $@
  186. else
  187. @echo Creating $@
  188. @$(BIN) $< $@
  189. endif
  190. %.srec: %.elf
  191. ifdef SREC
  192. ifeq ($(USE_VERBOSE_COMPILE),yes)
  193. $(SREC) $< $@
  194. else
  195. @echo Creating $@
  196. @$(SREC) $< $@
  197. endif
  198. endif
  199. %.dmp: %.elf
  200. ifeq ($(USE_VERBOSE_COMPILE),yes)
  201. $(OD) $(ODFLAGS) $< > $@
  202. $(SZ) $<
  203. else
  204. @echo Creating $@
  205. @$(OD) $(ODFLAGS) $< > $@
  206. @echo
  207. @$(SZ) $<
  208. endif
  209. %.list: %.elf
  210. ifeq ($(USE_VERBOSE_COMPILE),yes)
  211. $(OD) -S $< > $@
  212. else
  213. @echo Creating $@
  214. @$(OD) -S $< > $@
  215. @echo
  216. @echo Done
  217. endif
  218. lib: $(OBJS) $(BUILDDIR)/lib$(PROJECT).a
  219. $(BUILDDIR)/lib$(PROJECT).a: $(OBJS)
  220. @$(AR) -r $@ $^
  221. @echo
  222. @echo Done
  223. clean: CLEAN_RULE_HOOK
  224. @echo Cleaning
  225. @echo - $(DEPDIR)
  226. @-rm -fR $(DEPDIR)/* $(BUILDDIR)/* 2>/dev/null
  227. @-if [ -d "$(DEPDIR)" ]; then rmdir -p --ignore-fail-on-non-empty $(subst ./,,$(DEPDIR)) 2>/dev/null; fi
  228. @echo - $(BUILDDIR)
  229. @-if [ -d "$(BUILDDIR)" ]; then rmdir -p --ignore-fail-on-non-empty $(subst ./,,$(BUILDDIR)) 2>/dev/null; fi
  230. @echo
  231. @echo Done
  232. CLEAN_RULE_HOOK:
  233. #
  234. # Include the dependency files, should be the last of the makefile
  235. #
  236. -include $(wildcard $(DEPDIR)/*)
  237. # *** EOF ***