rules.mk 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # AVR 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. # Output directory and files
  10. ifeq ($(BUILDDIR),)
  11. BUILDDIR = build
  12. endif
  13. ifeq ($(BUILDDIR),.)
  14. BUILDDIR = build
  15. endif
  16. OUTFILES := $(BUILDDIR)/$(PROJECT).elf \
  17. $(BUILDDIR)/$(PROJECT).hex \
  18. $(BUILDDIR)/$(PROJECT).bin \
  19. $(BUILDDIR)/$(PROJECT).eep \
  20. $(BUILDDIR)/$(PROJECT).lss \
  21. $(BUILDDIR)/$(PROJECT).sym
  22. ifdef SREC
  23. OUTFILES += $(BUILDDIR)/$(PROJECT).srec
  24. endif
  25. # Source files groups and paths
  26. ASRC := $(CSRC) $(CPPSRC)
  27. SRCPATHS := $(sort $(dir $(ASMXSRC)) $(dir $(ASMSRC)) $(dir $(ASRC)))
  28. # Various directories
  29. OBJDIR := $(BUILDDIR)/obj
  30. LSTDIR := $(BUILDDIR)/lst
  31. # Object files groups
  32. ACOBJS := $(addprefix $(OBJDIR)/, $(notdir $(CSRC:.c=.o)))
  33. ACPPOBJS := $(addprefix $(OBJDIR)/, $(notdir $(CPPSRC:.cpp=.o)))
  34. ASMOBJS := $(addprefix $(OBJDIR)/, $(notdir $(ASMSRC:.s=.o)))
  35. ASMXOBJS := $(addprefix $(OBJDIR)/, $(notdir $(ASMXSRC:.S=.o)))
  36. OBJS := $(ASMXOBJS) $(ASMOBJS) $(ACOBJS) $(ACPPOBJS)
  37. # Paths
  38. IINCDIR := $(patsubst %, -I%,$(INCDIR) $(DINCDIR) $(UINCDIR))
  39. LLIBDIR := $(patsubst %, -L%,$(DLIBDIR) $(ULIBDIR))
  40. LLIBDIR += -L$(dir $(LDSCRIPT))
  41. # Macros
  42. DEFS := $(DDEFS) $(UDEFS)
  43. ADEFS := $(DADEFS) $(UADEFS)
  44. # Libs
  45. LIBS := $(DLIBS) $(ULIBS)
  46. # Compiler flag to set the C Standard level.
  47. # c89 = "ANSI" C
  48. # gnu89 = c89 plus GCC extensions
  49. # c99 = ISO C99 standard (not yet fully implemented)
  50. # gnu99 = c99 plus GCC extensions
  51. CSTANDARD = -std=gnu11
  52. # Place -D or -U options here for C sources
  53. CDEFS = -DF_CPU=$(F_CPU)UL
  54. # Place -D or -U options here for ASM sources
  55. ADEFS = -DF_CPU=$(F_CPU)
  56. # Place -D or -U options here for C++ sources
  57. CPPDEFS = -DF_CPU=$(F_CPU)UL
  58. # Paths where to search for sources
  59. VPATH = $(SRCPATHS)
  60. # Various settings
  61. MCFLAGS := -mmcu=$(MCU)
  62. CFLAGS = $(MCFLAGS) -I. -gdwarf-2 $(CDEFS) $(OPT) -funsigned-char
  63. CFLAGS += -funsigned-bitfields -fpack-struct -fshort-enums $(CWARN)
  64. CFLAGS += -Wa,-adhlns=$(LSTDIR)/$(notdir $(<:.c=.lst)) $(DEFS)
  65. CFLAGS += -std=gnu11 -mrelax -fdata-sections -ffunction-sections
  66. CFLAGS += -Wundef -MMD -MP #-MF
  67. #---------------- Assembler Options ----------------
  68. # -Wa,...: tell GCC to pass this to the assembler.
  69. # -adhlns: create listing
  70. # -gstabs: have the assembler create line number information; note that
  71. # for use in COFF files, additional information about filenames
  72. # and function names needs to be present in the assembler source
  73. # files -- see avr-libc docs [FIXME: not yet described there]
  74. # -listing-cont-lines: Sets the maximum number of continuation lines of hex
  75. # dump that will be displayed for a given single line of source input.
  76. ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs, \
  77. --listing-cont-lines=100
  78. #---------------- Library Options ----------------
  79. # Minimalistic printf version
  80. PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
  81. # Floating point printf version (requires MATH_LIB = -lm below)
  82. PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
  83. # If this is left blank, then it will use the Standard printf version.
  84. PRINTF_LIB = $(PRINTF_LIB_MIN)
  85. #PRINTF_LIB = $(PRINTF_LIB_MIN)
  86. #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
  87. # Minimalistic scanf version
  88. SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
  89. # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
  90. SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
  91. # If this is left blank, then it will use the Standard scanf version.
  92. SCANF_LIB = $(SCANF_LIB_MIN)
  93. #SCANF_LIB = $(SCANF_LIB_MIN)
  94. #SCANF_LIB = $(SCANF_LIB_FLOAT)
  95. MATH_LIB = -lm
  96. #---------------- Linker Options ----------------
  97. # -Wl,...: tell GCC to pass this to linker.
  98. # -Map: create map file
  99. # --cref: add cross reference to map file
  100. #LDFLAGS = -Wl,-Map=$(TARGET).map,--cref,--gc-sections
  101. #LDFLAGS += $(EXTMEMOPTS)
  102. #LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
  103. #LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
  104. LDFLAGS = -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--gc-sections
  105. LDFLAGS += -Wl,-u,vfprintf -lprintf_min -Wl,-u,vfscanf -lscanf_min -lm
  106. #
  107. # Makefile rules
  108. #
  109. all: PRE_MAKE_ALL_RULE_HOOK $(OBJS) $(OUTFILES) POST_MAKE_ALL_RULE_HOOK
  110. PRE_MAKE_ALL_RULE_HOOK:
  111. POST_MAKE_ALL_RULE_HOOK:
  112. $(OBJS): | $(BUILDDIR) $(OBJDIR) $(LSTDIR)
  113. $(BUILDDIR):
  114. ifneq ($(USE_VERBOSE_COMPILE),yes)
  115. @echo Compiler Options
  116. @echo $(CC) -c $(CFLAGS) -I. $(IINCDIR) main.c -o main.o
  117. @echo
  118. endif
  119. @mkdir -p $(BUILDDIR)
  120. $(OBJDIR):
  121. @mkdir -p $(OBJDIR)
  122. $(LSTDIR):
  123. @mkdir -p $(LSTDIR)
  124. $(ACPPOBJS) : $(OBJDIR)/%.o : %.cpp Makefile
  125. ifeq ($(USE_VERBOSE_COMPILE),yes)
  126. @echo
  127. $(CPPC) -c $(CPPFLAGS) $(MOPT) $(IINCDIR) $< -o $@
  128. else
  129. @echo Compiling $(<F)
  130. @$(CPPC) -c $(CPPFLAGS) $(MOPT) $(IINCDIR) $< -o $@
  131. endif
  132. $(ACOBJS) : $(OBJDIR)/%.o : %.c Makefile
  133. ifeq ($(USE_VERBOSE_COMPILE),yes)
  134. @echo
  135. $(CC) -c $(CFLAGS) $(MOPT) $(IINCDIR) $< -o $@
  136. else
  137. @echo Compiling $(<F)
  138. @$(CC) -c $(CFLAGS) $(MOPT) $(IINCDIR) $< -o $@
  139. endif
  140. $(ASMOBJS) : $(OBJDIR)/%.o : %.s Makefile
  141. ifeq ($(USE_VERBOSE_COMPILE),yes)
  142. @echo
  143. $(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  144. else
  145. @echo Compiling $(<F)
  146. @$(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  147. endif
  148. $(ASMXOBJS) : $(OBJDIR)/%.o : %.S Makefile
  149. ifeq ($(USE_VERBOSE_COMPILE),yes)
  150. @echo
  151. $(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  152. else
  153. @echo Compiling $(<F)
  154. @$(CC) -c $(ASXFLAGS) $(TOPT) -I. $(IINCDIR) $< -o $@
  155. endif
  156. $(BUILDDIR)/$(PROJECT).elf: $(OBJS)
  157. ifeq ($(USE_VERBOSE_COMPILE),yes)
  158. @echo
  159. $(CC) $(CFLAGS) $^ --output $@ $(LDFLAGS)
  160. else
  161. @echo Linking $@
  162. @$(CC) $(CFLAGS) $^ --output $@ $(LDFLAGS)
  163. endif
  164. %.hex: %.elf
  165. ifeq ($(USE_VERBOSE_COMPILE),yes)
  166. @echo
  167. $(HEX) $< $@
  168. else
  169. @echo Creating $@
  170. @$(HEX) $< $@
  171. endif
  172. %.bin: %.elf
  173. ifeq ($(USE_VERBOSE_COMPILE),yes)
  174. @echo
  175. $(BIN) $< $@
  176. else
  177. @echo Creating $@
  178. @$(BIN) $< $@
  179. endif
  180. %.eep: %.elf
  181. ifeq ($(USE_VERBOSE_COMPILE),yes)
  182. @echo
  183. -$(CP) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
  184. --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) \
  185. $< $@ || exit 0
  186. else
  187. @echo Creating $@
  188. @-$(CP) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
  189. --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) \
  190. $< $@ || exit 0
  191. endif
  192. %.lss: %.elf
  193. ifeq ($(USE_VERBOSE_COMPILE),yes)
  194. @echo
  195. $(OD) -h -S $< > $@
  196. else
  197. @echo Creating $@
  198. @$(OD) -h -S $< > $@
  199. endif
  200. %.sym: %.elf
  201. ifeq ($(USE_VERBOSE_COMPILE),yes)
  202. @echo
  203. $(NM) -n $< > $@
  204. $(SZ) $<
  205. @echo
  206. if test -f $(BUILDDIR)/$(PROJECT).elf; then echo; echo $(MSG_SIZE_AFTER); \
  207. $(ELFSIZE); 2>/dev/null; echo; fi
  208. @echo Done
  209. else
  210. @echo Creating $@
  211. @$(NM) -n $< > $@
  212. @echo
  213. @$(SZ) $<
  214. @if test -f $(BUILDDIR)/$(PROJECT).elf; then echo; echo $(MSG_SIZE_AFTER); \
  215. $(ELFSIZE); 2>/dev/null; echo; fi
  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. -rm -fR .dep $(BUILDDIR)
  226. @echo
  227. @echo Done
  228. CLEAN_RULE_HOOK:
  229. #
  230. # Include the dependency files, should be the last of the makefile
  231. #
  232. -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
  233. # *** EOF ***