rules.mk 6.3 KB

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