rules.mk 11 KB

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