rules.mk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. # Output format. (can be srec, ihex, binary)
  27. FORMAT = ihex
  28. # Optimization level, can be [0, 1, 2, 3, s].
  29. # 0 = turn off optimization. s = optimize for size.
  30. # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
  31. OPT = s
  32. AUTOGEN ?= false
  33. # List any extra directories to look for include files here.
  34. # Each directory must be seperated by a space.
  35. # Use forward slashes for directory separators.
  36. # For a directory that has spaces, enclose it in quotes.
  37. EXTRAINCDIRS += $(subst :, ,$(VPATH_SRC))
  38. # Compiler flag to set the C Standard level.
  39. # c89 = "ANSI" C
  40. # gnu89 = c89 plus GCC extensions
  41. # c99 = ISO C99 standard (not yet fully implemented)
  42. # gnu99 = c99 plus GCC extensions
  43. CSTANDARD = -std=gnu99
  44. # Place -D or -U options here for C sources
  45. CDEFS += $(OPT_DEFS)
  46. # Place -D or -U options here for ASM sources
  47. ADEFS += $(OPT_DEFS)
  48. # Place -D or -U options here for C++ sources
  49. #CPPDEFS += -D__STDC_LIMIT_MACROS
  50. #CPPDEFS += -D__STDC_CONSTANT_MACROS
  51. CPPDEFS += $(OPT_DEFS)
  52. #---------------- Compiler Options C ----------------
  53. # -g*: generate debugging information
  54. # -O*: optimization level
  55. # -f...: tuning, see GCC manual and avr-libc documentation
  56. # -Wall...: warning level
  57. # -Wa,...: tell GCC to pass this to the assembler.
  58. # -adhlns...: create assembler listing
  59. CFLAGS += -g$(DEBUG)
  60. CFLAGS += $(CDEFS)
  61. CFLAGS += -O$(OPT)
  62. # add color
  63. ifeq ($(COLOR),true)
  64. ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
  65. CFLAGS+= -fdiagnostics-color
  66. endif
  67. endif
  68. CFLAGS += -Wall
  69. CFLAGS += -Wstrict-prototypes
  70. #CFLAGS += -mshort-calls
  71. #CFLAGS += -fno-unit-at-a-time
  72. #CFLAGS += -Wundef
  73. #CFLAGS += -Wunreachable-code
  74. #CFLAGS += -Wsign-compare
  75. CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
  76. CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
  77. CFLAGS += $(CSTANDARD)
  78. ifdef CONFIG_H
  79. CFLAGS += -include $(CONFIG_H)
  80. endif
  81. #---------------- Compiler Options C++ ----------------
  82. # -g*: generate debugging information
  83. # -O*: optimization level
  84. # -f...: tuning, see GCC manual and avr-libc documentation
  85. # -Wall...: warning level
  86. # -Wa,...: tell GCC to pass this to the assembler.
  87. # -adhlns...: create assembler listing
  88. CPPFLAGS += -g$(DEBUG)
  89. CPPFLAGS += $(CPPDEFS)
  90. CPPFLAGS += -O$(OPT)
  91. # to supress "warning: only initialized variables can be placed into program memory area"
  92. CPPFLAGS += -w
  93. CPPFLAGS += -Wall
  94. CPPFLAGS += -Wundef
  95. #CPPFLAGS += -mshort-calls
  96. #CPPFLAGS += -fno-unit-at-a-time
  97. #CPPFLAGS += -Wstrict-prototypes
  98. #CPPFLAGS += -Wunreachable-code
  99. #CPPFLAGS += -Wsign-compare
  100. CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
  101. CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
  102. #CPPFLAGS += $(CSTANDARD)
  103. ifdef CONFIG_H
  104. CPPFLAGS += -include $(CONFIG_H)
  105. endif
  106. #---------------- Assembler Options ----------------
  107. # -Wa,...: tell GCC to pass this to the assembler.
  108. # -adhlns: create listing
  109. # -gstabs: have the assembler create line number information; note that
  110. # for use in COFF files, additional information about filenames
  111. # and function names needs to be present in the assembler source
  112. # files -- see avr-libc docs [FIXME: not yet described there]
  113. # -listing-cont-lines: Sets the maximum number of continuation lines of hex
  114. # dump that will be displayed for a given single line of source input.
  115. ASFLAGS += $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
  116. ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
  117. ifdef CONFIG_H
  118. ASFLAGS += -include $(CONFIG_H)
  119. endif
  120. #---------------- Library Options ----------------
  121. # Minimalistic printf version
  122. PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
  123. # Floating point printf version (requires MATH_LIB = -lm below)
  124. PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
  125. # If this is left blank, then it will use the Standard printf version.
  126. PRINTF_LIB =
  127. #PRINTF_LIB = $(PRINTF_LIB_MIN)
  128. #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
  129. # Minimalistic scanf version
  130. SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
  131. # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
  132. SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
  133. # If this is left blank, then it will use the Standard scanf version.
  134. SCANF_LIB =
  135. #SCANF_LIB = $(SCANF_LIB_MIN)
  136. #SCANF_LIB = $(SCANF_LIB_FLOAT)
  137. MATH_LIB = -lm
  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. LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
  147. #LDFLAGS += -Wl,--relax
  148. LDFLAGS += $(EXTMEMOPTS)
  149. LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
  150. LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
  151. #LDFLAGS += -T linker_script.x
  152. # You can give EXTRALDFLAGS at 'make' command line.
  153. LDFLAGS += $(EXTRALDFLAGS)
  154. # Define programs and commands.
  155. SHELL = sh
  156. REMOVE = rm -f
  157. REMOVEDIR = rmdir
  158. COPY = cp
  159. WINSHELL = cmd
  160. SECHO = $(SILENT) || echo
  161. # Define Messages
  162. # English
  163. MSG_ERRORS_NONE = Errors: none
  164. MSG_BEGIN = -------- begin --------
  165. MSG_END = -------- end --------
  166. MSG_SIZE_BEFORE = Size before:
  167. MSG_SIZE_AFTER = Size after:
  168. MSG_COFF = Converting to AVR COFF:
  169. MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
  170. MSG_FLASH = Creating load file for Flash:
  171. MSG_EEPROM = Creating load file for EEPROM:
  172. MSG_BIN = Creating binary load file for Flash:
  173. MSG_EXTENDED_LISTING = Creating Extended Listing:
  174. MSG_SYMBOL_TABLE = Creating Symbol Table:
  175. MSG_LINKING = Linking:
  176. MSG_COMPILING = Compiling:
  177. MSG_COMPILING_CPP = Compiling:
  178. MSG_ASSEMBLING = Assembling:
  179. MSG_CLEANING = Cleaning project:
  180. MSG_CREATING_LIBRARY = Creating library:
  181. MSG_SUBMODULE_DIRTY = $(WARN_COLOR)WARNING:$(NO_COLOR)\n \
  182. Some git sub-modules are out of date or modified, please consider runnning:$(BOLD)\n\
  183. git submodule sync --recursive\n\
  184. git submodule update --init --recursive$(NO_COLOR)\n\n\
  185. You can ignore this warning if you are not compiling any ChibiOS keyboards,\n\
  186. or if you have modified the ChibiOS libraries yourself. \n\n
  187. # Define all object files.
  188. OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
  189. # The files in the lib folder are shared between all keymaps, so generate that folder name by removing
  190. # the keymap from the name
  191. KBOBJDIR=$(subst _$(KEYMAP),,$(OBJDIR))
  192. # And fixup the object files to match
  193. LIBOBJ = $(foreach v,$(OBJ),$(if $(findstring /lib/,$v),$v))
  194. NONLIBOBJ := $(filter-out $(LIBOBJ),$(OBJ))
  195. LIBOBJ := $(subst _$(KEYMAP)/,/,$(LIBOBJ))
  196. OBJ := $(LIBOBJ) $(NONLIBOBJ)
  197. # Define all listing files.
  198. LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
  199. # Compiler flags to generate dependency files.
  200. #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
  201. GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
  202. # Combine all necessary flags and optional flags.
  203. # Add target processor to flags.
  204. # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
  205. ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
  206. ALL_CPPFLAGS = $(MCUFLAGS) -x c++ $(CPPFLAGS) $(EXTRAFLAGS)
  207. ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
  208. MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
  209. # Default target.
  210. all: build sizeafter
  211. # Change the build target to build a HEX file or a library.
  212. build: elf hex
  213. #build: elf hex eep lss sym
  214. #build: lib
  215. elf: $(BUILD_DIR)/$(TARGET).elf
  216. hex: $(BUILD_DIR)/$(TARGET).hex
  217. eep: $(BUILD_DIR)/$(TARGET).eep
  218. lss: $(BUILD_DIR)/$(TARGET).lss
  219. sym: $(BUILD_DIR)/$(TARGET).sym
  220. LIBNAME=lib$(TARGET).a
  221. lib: $(LIBNAME)
  222. # Display size of file.
  223. HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
  224. #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
  225. ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
  226. sizebefore:
  227. @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
  228. 2>/dev/null; $(SECHO); fi
  229. sizeafter: $(BUILD_DIR)/$(TARGET).hex
  230. @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
  231. 2>/dev/null; $(SECHO); fi
  232. # test file sizes eventually
  233. # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
  234. # Display compiler version information.
  235. gccversion :
  236. @$(SILENT) || $(CC) --version
  237. # Create final output files (.hex, .eep) from ELF output file.
  238. %.hex: %.elf
  239. @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
  240. $(eval CMD=$(HEX) $< $@)
  241. @$(BUILD_CMD)
  242. @if $(AUTOGEN); then \
  243. $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
  244. $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
  245. else \
  246. $(COPY) $@ $(TARGET).hex; \
  247. fi
  248. %.eep: %.elf
  249. @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
  250. $(eval CMD=$(EEP) $< $@ || exit 0)
  251. @$(BUILD_CMD)
  252. # Create extended listing file from ELF output file.
  253. %.lss: %.elf
  254. @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
  255. $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
  256. @$(BUILD_CMD)
  257. # Create a symbol table from ELF output file.
  258. %.sym: %.elf
  259. @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
  260. $(eval CMD=$(NM) -n $< > $@ )
  261. @$(BUILD_CMD)
  262. %.bin: %.elf
  263. @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
  264. $(eval CMD=$(BIN) $< $@ || exit 0)
  265. @$(BUILD_CMD)
  266. # Create library from object files.
  267. .SECONDARY : $(BUILD_DIR)/$(TARGET).a
  268. .PRECIOUS : $(OBJ)
  269. %.a: $(OBJ)
  270. @$(SILENT) || printf "$(MSG_CREATING_LIBRARY) $@" | $(AWK_CMD)
  271. $(eval CMD=$(AR) $@ $(OBJ) )
  272. @$(BUILD_CMD)
  273. BEGIN = gccversion sizebefore
  274. # Link: create ELF output file from object files.
  275. .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
  276. .PRECIOUS : $(OBJ)
  277. # Note the obj.txt depeendency is there to force linking if a source file is deleted
  278. %.elf: $(OBJ) $(OBJDIR)/cflags.txt $(OBJDIR)/ldflags.txt $(OBJDIR)/obj.txt | $(BEGIN)
  279. @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
  280. $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
  281. @$(BUILD_CMD)
  282. define GEN_OBJRULE
  283. # Compile: create object files from C source files.
  284. $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
  285. @mkdir -p $$(@D)
  286. @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
  287. $$(eval CMD=$$(CC) -c $$(ALL_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
  288. @$$(BUILD_CMD)
  289. # Compile: create object files from C++ source files.
  290. $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
  291. @mkdir -p $$(@D)
  292. @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
  293. $$(eval CMD=$$(CC) -c $$(ALL_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
  294. @$(BUILD_CMD)
  295. # Assemble: create object files from assembler source files.
  296. $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
  297. @mkdir -p $$(@D)
  298. @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
  299. $$(eval CMD=$$(CC) -c $$(ALL_ASFLAGS) $$< -o $$@)
  300. @$$(BUILD_CMD)
  301. $1/force:
  302. $1/cflags.txt: $1/force
  303. echo '$$(ALL_CFLAGS)' | cmp -s - $$@ || echo '$$(ALL_CFLAGS)' > $$@
  304. $1/cppflags.txt: $1/force
  305. echo '$$(ALL_CPPFLAGS)' | cmp -s - $$@ || echo '$$(ALL_CPPFLAGS)' > $$@
  306. $1/asflags.txt: $1/force
  307. echo '$$(ALL_ASFLAGS)' | cmp -s - $$@ || echo '$$(ALL_ASFLAGS)' > $$@
  308. $1/ldflags.txt: $1/force
  309. echo '$$(LDFLAGS)' | cmp -s - $$@ || echo '$$(LDFLAGS)' > $$@
  310. $1/obj.txt: $1/force
  311. echo '$$(OBJ)' | cmp -s - $$@ || echo '$$(OBJ)' > $$@
  312. $1/compiler.txt: $1/force
  313. $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
  314. endef
  315. # We have to use static rules for the .d files for some reason
  316. DEPS = $(patsubst %.o,%.d,$(OBJ))
  317. # Keep the .d files
  318. .PRECIOUS: $(DEPS)
  319. # Empty rule to force recompilation if the .d file is missing
  320. $(DEPS):
  321. # Since the object files could be in two different folders, generate
  322. # separate rules for them, rather than having too generic rules
  323. $(eval $(call GEN_OBJRULE,$(OBJDIR)))
  324. $(eval $(call GEN_OBJRULE,$(KBOBJDIR)))
  325. # Compile: create assembler files from C source files.
  326. %.s : %.c | $(BEGIN)
  327. @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
  328. $(eval CMD=$(CC) -S $(ALL_CFLAGS) $< -o $@)
  329. @$(BUILD_CMD)
  330. # Compile: create assembler files from C++ source files.
  331. %.s : %.cpp | $(BEGIN)
  332. @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
  333. $(eval CMD=$(CC) -S $(ALL_CPPFLAGS) $< -o $@)
  334. @$(BUILD_CMD)
  335. # Create preprocessed source for use in sending a bug report.
  336. %.i : %.c | $(BEGIN)
  337. $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
  338. # Target: clean project.
  339. clean:
  340. $(REMOVE) -r $(OBJDIR) 2>/dev/null
  341. $(REMOVE) -r $(KBOBJDIR) 2>/dev/null
  342. $(REMOVE) $(BUILD_DIR)/$(TARGET).*
  343. show_path:
  344. @echo VPATH=$(VPATH)
  345. @echo SRC=$(SRC)
  346. # Create build directory
  347. $(shell mkdir $(BUILD_DIR) 2>/dev/null)
  348. # Create object files directory
  349. $(shell mkdir $(OBJDIR) 2>/dev/null)
  350. $(shell mkdir $(KBOBJDIR) 2>/dev/null)
  351. # Include the dependency files.
  352. -include $(patsubst %.o,%.d,$(OBJ))
  353. # Listing of phony targets.
  354. .PHONY : all finish sizebefore sizeafter gccversion \
  355. build elf hex eep lss sym coff extcoff \
  356. clean clean_list debug gdb-config show_path \
  357. program teensy dfu flip dfu-ee flip-ee dfu-start