rules.mk 12 KB

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