rules.mk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. # Hey Emacs, this is a -*- makefile -*-
  2. #----------------------------------------------------------------------------
  3. # WinAVR Makefile Template written by Eric B. Weddington, Jg Wunsch, et al.
  4. #
  5. # Released to the Public Domain
  6. #
  7. # Additional material for this makefile was written by:
  8. # Peter Fleury
  9. # Tim Henigan
  10. # Colin O'Flynn
  11. # Reiner Patommel
  12. # Markus Pfaff
  13. # Sander Pool
  14. # Frederik Rouleau
  15. # Carlos Lamas
  16. #
  17. # Enable vpath seraching for source files only
  18. # Without this, output files, could be read from the wrong .build directories
  19. VPATH_SRC := $(VPATH)
  20. vpath %.c $(VPATH_SRC)
  21. vpath %.h $(VPATH_SRC)
  22. vpath %.cpp $(VPATH_SRC)
  23. vpath %.cc $(VPATH_SRC)
  24. vpath %.hpp $(VPATH_SRC)
  25. vpath %.S $(VPATH_SRC)
  26. VPATH :=
  27. # Convert all SRC to OBJ
  28. define OBJ_FROM_SRC
  29. $(patsubst %.c,$1/%.o,$(patsubst %.cpp,$1/%.o,$(patsubst %.cc,$1/%.o,$(patsubst %.S,$1/%.o,$($1_SRC)))))
  30. endef
  31. $(foreach OUTPUT,$(OUTPUTS),$(eval $(OUTPUT)_OBJ +=$(call OBJ_FROM_SRC,$(OUTPUT))))
  32. # Define a list of all objects
  33. OBJ := $(foreach OUTPUT,$(OUTPUTS),$($(OUTPUT)_OBJ))
  34. MASTER_OUTPUT := $(firstword $(OUTPUTS))
  35. # Output format. (can be srec, ihex, binary)
  36. FORMAT = ihex
  37. # Optimization level, can be [0, 1, 2, 3, s].
  38. # 0 = turn off optimization. s = optimize for size.
  39. # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
  40. OPT = s
  41. AUTOGEN ?= false
  42. # Compiler flag to set the C Standard level.
  43. # c89 = "ANSI" C
  44. # gnu89 = c89 plus GCC extensions
  45. # c99 = ISO C99 standard (not yet fully implemented)
  46. # gnu99 = c99 plus GCC extensions
  47. CSTANDARD = -std=gnu99
  48. # Place -D or -U options here for C sources
  49. #CDEFS +=
  50. # Place -D or -U options here for ASM sources
  51. #ADEFS +=
  52. # Place -D or -U options here for C++ sources
  53. #CPPDEFS += -D__STDC_LIMIT_MACROS
  54. #CPPDEFS += -D__STDC_CONSTANT_MACROS
  55. #CPPDEFS +=
  56. #---------------- Compiler Options C ----------------
  57. # -g*: generate debugging information
  58. # -O*: optimization level
  59. # -f...: tuning, see GCC manual and avr-libc documentation
  60. # -Wall...: warning level
  61. # -Wa,...: tell GCC to pass this to the assembler.
  62. # -adhlns...: create assembler listing
  63. CFLAGS += -g$(DEBUG)
  64. CFLAGS += $(CDEFS)
  65. CFLAGS += -O$(OPT)
  66. # add color
  67. ifeq ($(COLOR),true)
  68. ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
  69. CFLAGS+= -fdiagnostics-color
  70. endif
  71. endif
  72. CFLAGS += -Wall
  73. CFLAGS += -Wstrict-prototypes
  74. ifneq ($(strip $(ALLOW_WARNINGS)), yes)
  75. CFLAGS += -Werror
  76. endif
  77. #CFLAGS += -mshort-calls
  78. #CFLAGS += -fno-unit-at-a-time
  79. #CFLAGS += -Wundef
  80. #CFLAGS += -Wunreachable-code
  81. #CFLAGS += -Wsign-compare
  82. CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
  83. CFLAGS += $(CSTANDARD)
  84. #---------------- Compiler Options C++ ----------------
  85. # -g*: generate debugging information
  86. # -O*: optimization level
  87. # -f...: tuning, see GCC manual and avr-libc documentation
  88. # -Wall...: warning level
  89. # -Wa,...: tell GCC to pass this to the assembler.
  90. # -adhlns...: create assembler listing
  91. CPPFLAGS += -g$(DEBUG)
  92. CPPFLAGS += $(CPPDEFS)
  93. CPPFLAGS += -O$(OPT)
  94. # to supress "warning: only initialized variables can be placed into program memory area"
  95. CPPFLAGS += -w
  96. CPPFLAGS += -Wall
  97. CPPFLAGS += -Wundef
  98. ifneq ($(strip $(ALLOW_WARNINGS)), yes)
  99. CPPFLAGS += -Werror
  100. endif
  101. #CPPFLAGS += -mshort-calls
  102. #CPPFLAGS += -fno-unit-at-a-time
  103. #CPPFLAGS += -Wstrict-prototypes
  104. #CPPFLAGS += -Wunreachable-code
  105. #CPPFLAGS += -Wsign-compare
  106. CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
  107. #CPPFLAGS += $(CSTANDARD)
  108. #---------------- Assembler Options ----------------
  109. # -Wa,...: tell GCC to pass this to the assembler.
  110. # -adhlns: create listing
  111. # -gstabs: have the assembler create line number information; note that
  112. # for use in COFF files, additional information about filenames
  113. # and function names needs to be present in the assembler source
  114. # files -- see avr-libc docs [FIXME: not yet described there]
  115. # -listing-cont-lines: Sets the maximum number of continuation lines of hex
  116. # dump that will be displayed for a given single line of source input.
  117. ASFLAGS += $(ADEFS)
  118. ASFLAGS += -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
  119. #---------------- Library Options ----------------
  120. # Minimalistic printf version
  121. PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
  122. # Floating point printf version (requires MATH_LIB = -lm below)
  123. PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
  124. # If this is left blank, then it will use the Standard printf version.
  125. PRINTF_LIB =
  126. #PRINTF_LIB = $(PRINTF_LIB_MIN)
  127. #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
  128. # Minimalistic scanf version
  129. SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
  130. # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
  131. SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
  132. # If this is left blank, then it will use the Standard scanf version.
  133. SCANF_LIB =
  134. #SCANF_LIB = $(SCANF_LIB_MIN)
  135. #SCANF_LIB = $(SCANF_LIB_FLOAT)
  136. MATH_LIB = -lm
  137. CREATE_MAP ?= yes
  138. #---------------- Linker Options ----------------
  139. # -Wl,...: tell GCC to pass this to linker.
  140. # -Map: create map file
  141. # --cref: add cross reference to map file
  142. #
  143. # Comennt out "--relax" option to avoid a error such:
  144. # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
  145. #
  146. ifeq ($(CREATE_MAP),yes)
  147. LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
  148. endif
  149. #LDFLAGS += -Wl,--relax
  150. LDFLAGS += $(EXTMEMOPTS)
  151. LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
  152. LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
  153. #LDFLAGS += -T linker_script.x
  154. # You can give EXTRALDFLAGS at 'make' command line.
  155. LDFLAGS += $(EXTRALDFLAGS)
  156. # Define programs and commands.
  157. SHELL = sh
  158. REMOVE = rm -f
  159. REMOVEDIR = rmdir
  160. COPY = cp
  161. WINSHELL = cmd
  162. SECHO = $(SILENT) || echo
  163. # Compiler flags to generate dependency files.
  164. #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
  165. GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
  166. # Combine all necessary flags and optional flags.
  167. # Add target processor to flags.
  168. # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
  169. ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
  170. ALL_CPPFLAGS = $(MCUFLAGS) -x c++ $(CPPFLAGS) $(EXTRAFLAGS)
  171. ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
  172. MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
  173. elf: $(BUILD_DIR)/$(TARGET).elf
  174. hex: $(BUILD_DIR)/$(TARGET).hex
  175. eep: $(BUILD_DIR)/$(TARGET).eep
  176. lss: $(BUILD_DIR)/$(TARGET).lss
  177. sym: $(BUILD_DIR)/$(TARGET).sym
  178. LIBNAME=lib$(TARGET).a
  179. lib: $(LIBNAME)
  180. # Display size of file.
  181. HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
  182. #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
  183. ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
  184. sizebefore:
  185. @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
  186. 2>/dev/null; $(SECHO); fi
  187. sizeafter: $(BUILD_DIR)/$(TARGET).hex
  188. @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
  189. 2>/dev/null; $(SECHO); fi
  190. # test file sizes eventually
  191. # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
  192. # Display compiler version information.
  193. gccversion :
  194. @$(SILENT) || $(CC) --version
  195. # Create final output files (.hex, .eep) from ELF output file.
  196. %.hex: %.elf
  197. @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
  198. $(eval CMD=$(HEX) $< $@)
  199. @$(BUILD_CMD)
  200. @if $(AUTOGEN); then \
  201. $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
  202. $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
  203. else \
  204. $(COPY) $@ $(TARGET).hex; \
  205. fi
  206. %.eep: %.elf
  207. @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
  208. $(eval CMD=$(EEP) $< $@ || exit 0)
  209. @$(BUILD_CMD)
  210. # Create extended listing file from ELF output file.
  211. %.lss: %.elf
  212. @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
  213. $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
  214. @$(BUILD_CMD)
  215. # Create a symbol table from ELF output file.
  216. %.sym: %.elf
  217. @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
  218. $(eval CMD=$(NM) -n $< > $@ )
  219. @$(BUILD_CMD)
  220. %.bin: %.elf
  221. @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
  222. $(eval CMD=$(BIN) $< $@ || exit 0)
  223. @$(BUILD_CMD)
  224. BEGIN = gccversion sizebefore
  225. # Link: create ELF output file from object files.
  226. .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
  227. .PRECIOUS : $(OBJ)
  228. # Note the obj.txt depeendency is there to force linking if a source file is deleted
  229. %.elf: $(OBJ) $(MASTER_OUTPUT)/cflags.txt $(MASTER_OUTPUT)/ldflags.txt $(MASTER_OUTPUT)/obj.txt | $(BEGIN)
  230. @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
  231. $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
  232. @$(BUILD_CMD)
  233. define GEN_OBJRULE
  234. $1_INCFLAGS := $$(patsubst %,-I%,$$($1_INC))
  235. ifdef $1_CONFIG
  236. $1_CONFIG_FLAGS += -include $$($1_CONFIG)
  237. endif
  238. $1_CFLAGS = $$(ALL_CFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
  239. $1_CPPFLAGS= $$(ALL_CPPFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
  240. $1_ASFLAGS= $$(ALL_ASFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
  241. # Compile: create object files from C source files.
  242. $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
  243. @mkdir -p $$(@D)
  244. @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
  245. $$(eval CMD := $$(CC) -c $$($1_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
  246. @$$(BUILD_CMD)
  247. # Compile: create object files from C++ source files.
  248. $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
  249. @mkdir -p $$(@D)
  250. @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
  251. $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
  252. @$$(BUILD_CMD)
  253. $1/%.o : %.cc $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
  254. @mkdir -p $$(@D)
  255. @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
  256. $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
  257. @$$(BUILD_CMD)
  258. # Assemble: create object files from assembler source files.
  259. $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
  260. @mkdir -p $$(@D)
  261. @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
  262. $$(eval CMD=$$(CC) -c $$($1_ASFLAGS) $$< -o $$@)
  263. @$$(BUILD_CMD)
  264. $1/force:
  265. $1/cflags.txt: $1/force
  266. echo '$$($1_CFLAGS)' | cmp -s - $$@ || echo '$$($1_CFLAGS)' > $$@
  267. $1/cppflags.txt: $1/force
  268. echo '$$($1_CPPFLAGS)' | cmp -s - $$@ || echo '$$($1_CPPFLAGS)' > $$@
  269. $1/asflags.txt: $1/force
  270. echo '$$($1_ASFLAGS)' | cmp -s - $$@ || echo '$$($1_ASFLAGS)' > $$@
  271. $1/compiler.txt: $1/force
  272. $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
  273. endef
  274. .PRECIOUS: $(MASTER_OUTPUT)/obj.txt
  275. $(MASTER_OUTPUT)/obj.txt: $(MASTER_OUTPUT)/force
  276. echo '$(OBJ)' | cmp -s - $@ || echo '$(OBJ)' > $@
  277. .PRECIOUS: $(MASTER_OUTPUT)/ldflags.txt
  278. $(MASTER_OUTPUT)/ldflags.txt: $(MASTER_OUTPUT)/force
  279. echo '$(LDFLAGS)' | cmp -s - $@ || echo '$(LDFLAGS)' > $@
  280. # We have to use static rules for the .d files for some reason
  281. DEPS = $(patsubst %.o,%.d,$(OBJ))
  282. # Keep the .d files
  283. .PRECIOUS: $(DEPS)
  284. # Empty rule to force recompilation if the .d file is missing
  285. $(DEPS):
  286. $(foreach OUTPUT,$(OUTPUTS),$(eval $(call GEN_OBJRULE,$(OUTPUT))))
  287. # Create preprocessed source for use in sending a bug report.
  288. %.i : %.c | $(BEGIN)
  289. $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
  290. # Target: clean project.
  291. clean:
  292. $(foreach OUTPUT,$(OUTPUTS), $(REMOVE) -r $(OUTPUT) 2>/dev/null)
  293. $(REMOVE) $(BUILD_DIR)/$(TARGET).*
  294. show_path:
  295. @echo VPATH=$(VPATH)
  296. @echo SRC=$(SRC)
  297. @echo OBJ=$(OBJ)
  298. # Create build directory
  299. $(shell mkdir -p $(BUILD_DIR) 2>/dev/null)
  300. # Create object files directory
  301. $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir -p $(OUTPUT) 2>/dev/null)))
  302. # Include the dependency files.
  303. -include $(patsubst %.o,%.d,$(OBJ))
  304. # Listing of phony targets.
  305. .PHONY : all finish sizebefore sizeafter gccversion \
  306. build elf hex eep lss sym coff extcoff \
  307. clean clean_list debug gdb-config show_path \
  308. program teensy dfu flip dfu-ee flip-ee dfu-start