common_rules.mk 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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,$(patsubst %.clib,$1/%.a,$($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. NO_LTO_OBJ := $(filter %.a,$(OBJ))
  35. MASTER_OUTPUT := $(firstword $(OUTPUTS))
  36. # Output format. (can be srec, ihex, binary)
  37. FORMAT = ihex
  38. # Optimization level, can be [0, 1, 2, 3, s].
  39. # 0 = turn off optimization. s = optimize for size.
  40. # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
  41. OPT ?= s
  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. #CXXDEFS += -D__STDC_LIMIT_MACROS
  54. #CXXDEFS += -D__STDC_CONSTANT_MACROS
  55. #CXXDEFS +=
  56. # Speed up recompilations by opt-in usage of ccache
  57. USE_CCACHE ?= no
  58. ifneq ($(USE_CCACHE),no)
  59. CC_PREFIX ?= ccache
  60. endif
  61. #---------------- Compiler Options C ----------------
  62. # -g*: generate debugging information
  63. # -O*: optimization level
  64. # -f...: tuning, see GCC manual and avr-libc documentation
  65. # -Wall...: warning level
  66. # -Wa,...: tell GCC to pass this to the assembler.
  67. ifeq ($(strip $(LTO_ENABLE)), yes)
  68. ifeq ($(PLATFORM),ARM_ATSAM)
  69. $(info Enabling LTO on arm_atsam-targeting boards is known to have a high likelihood of failure.)
  70. $(info If unsure, set LTO_ENABLE = no.)
  71. endif
  72. CDEFS += -flto
  73. CDEFS += -DLTO_ENABLE
  74. endif
  75. DEBUG_ENABLE ?= yes
  76. ifeq ($(strip $(SKIP_DEBUG_INFO)),yes)
  77. DEBUG_ENABLE=no
  78. endif
  79. ifeq ($(strip $(DEBUG_ENABLE)),yes)
  80. CFLAGS += -g$(DEBUG)
  81. endif
  82. CFLAGS += $(CDEFS)
  83. CFLAGS += -O$(OPT)
  84. # add color
  85. ifeq ($(COLOR),true)
  86. ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
  87. CFLAGS+= -fdiagnostics-color
  88. endif
  89. endif
  90. CFLAGS += -Wall
  91. CFLAGS += -Wstrict-prototypes
  92. ifneq ($(strip $(ALLOW_WARNINGS)), yes)
  93. CFLAGS += -Werror
  94. endif
  95. #CFLAGS += -mshort-calls
  96. #CFLAGS += -fno-unit-at-a-time
  97. #CFLAGS += -Wundef
  98. #CFLAGS += -Wunreachable-code
  99. #CFLAGS += -Wsign-compare
  100. CFLAGS += $(CSTANDARD)
  101. # This fixes lots of keyboards linking errors but SHOULDN'T BE A FINAL SOLUTION
  102. # Fixing of multiple variable definitions must be made.
  103. CFLAGS += -fcommon
  104. #---------------- Compiler Options C++ ----------------
  105. # -g*: generate debugging information
  106. # -O*: optimization level
  107. # -f...: tuning, see GCC manual and avr-libc documentation
  108. # -Wall...: warning level
  109. # -Wa,...: tell GCC to pass this to the assembler.
  110. ifeq ($(strip $(DEBUG_ENABLE)),yes)
  111. CXXFLAGS += -g$(DEBUG)
  112. endif
  113. CXXFLAGS += $(CXXDEFS)
  114. CXXFLAGS += -O$(OPT)
  115. # to supress "warning: only initialized variables can be placed into program memory area"
  116. CXXFLAGS += -w
  117. CXXFLAGS += -Wall
  118. CXXFLAGS += -Wundef
  119. ifneq ($(strip $(ALLOW_WARNINGS)), yes)
  120. CXXFLAGS += -Werror
  121. endif
  122. #CXXFLAGS += -mshort-calls
  123. #CXXFLAGS += -fno-unit-at-a-time
  124. #CXXFLAGS += -Wstrict-prototypes
  125. #CXXFLAGS += -Wunreachable-code
  126. #CXXFLAGS += -Wsign-compare
  127. #CXXFLAGS += $(CSTANDARD)
  128. #---------------- Assembler Options ----------------
  129. ASFLAGS += $(ADEFS)
  130. ifeq ($(VERBOSE_AS_CMD),yes)
  131. ASFLAGS += -v
  132. endif
  133. #---------------- Library Options ----------------
  134. # Minimalistic printf version
  135. PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
  136. # Floating point printf version (requires MATH_LIB = -lm below)
  137. PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
  138. # If this is left blank, then it will use the Standard printf version.
  139. PRINTF_LIB =
  140. #PRINTF_LIB = $(PRINTF_LIB_MIN)
  141. #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
  142. # Minimalistic scanf version
  143. SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
  144. # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
  145. SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
  146. # If this is left blank, then it will use the Standard scanf version.
  147. SCANF_LIB =
  148. #SCANF_LIB = $(SCANF_LIB_MIN)
  149. #SCANF_LIB = $(SCANF_LIB_FLOAT)
  150. MATH_LIB = -lm
  151. CREATE_MAP ?= yes
  152. #---------------- Linker Options ----------------
  153. # -Wl,...: tell GCC to pass this to linker.
  154. # -Map: create map file
  155. # --cref: add cross reference to map file
  156. #
  157. # Comennt out "--relax" option to avoid a error such:
  158. # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
  159. #
  160. ifeq ($(CREATE_MAP),yes)
  161. LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
  162. endif
  163. ifeq ($(VERBOSE_LD_CMD),yes)
  164. LDFLAGS += -v
  165. endif
  166. #LDFLAGS += -Wl,--relax
  167. LDFLAGS += $(EXTMEMOPTS)
  168. LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
  169. LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
  170. #LDFLAGS += -T linker_script.x
  171. # You can give EXTRALDFLAGS at 'make' command line.
  172. LDFLAGS += $(EXTRALDFLAGS)
  173. #---------------- Assembler Listings ----------------
  174. # -Wa,...: tell GCC to pass this to the assembler.
  175. # -adhlns: create listing
  176. # -gstabs: have the assembler create line number information; note that
  177. # for use in COFF files, additional information about filenames
  178. # and function names needs to be present in the assembler source
  179. # files -- see avr-libc docs [FIXME: not yet described there]
  180. # -listing-cont-lines: Sets the maximum number of continuation lines of hex
  181. # dump that will be displayed for a given single line of source input.
  182. ADHLNS_ENABLE ?= no
  183. ifeq ($(ADHLNS_ENABLE),yes)
  184. # Avoid "Options to '-Xassembler' do not match" - only specify assembler options at LTO link time
  185. ifeq ($(strip $(LTO_ENABLE)), yes)
  186. LDFLAGS += -Wa,-adhlns=$(BUILD_DIR)/$(TARGET).lst
  187. else
  188. CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
  189. CXXFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
  190. ifeq ($(strip $(DEBUG_ENABLE)),yes)
  191. ASFLAGS = -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
  192. else
  193. ASFLAGS = -Wa,-adhlns=$(@:%.o=%.lst),--listing-cont-lines=100
  194. endif
  195. endif
  196. endif
  197. # Define programs and commands.
  198. SHELL = sh
  199. SED = sed
  200. REMOVE = rm -f
  201. REMOVEDIR = rmdir
  202. COPY = cp
  203. WINSHELL = cmd
  204. SECHO = $(SILENT) || echo
  205. MD5SUM ?= md5sum
  206. ifneq ($(filter Darwin FreeBSD,$(shell uname -s)),)
  207. MD5SUM = md5
  208. endif
  209. # UF2 format settings
  210. # To produce a UF2 file in your build, add to your keyboard's rules.mk:
  211. # FIRMWARE_FORMAT = uf2
  212. UF2CONV = $(TOP_DIR)/util/uf2conv.py
  213. UF2_FAMILY ?= 0x0
  214. # Compiler flags to generate dependency files.
  215. #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
  216. GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
  217. # Combine all necessary flags and optional flags.
  218. # Add target processor to flags.
  219. # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
  220. ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
  221. ALL_CXXFLAGS = $(MCUFLAGS) -x c++ $(CXXFLAGS) $(EXTRAFLAGS)
  222. ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
  223. define NO_LTO
  224. $(patsubst %.a,%.o,$1): NOLTO_CFLAGS += -fno-lto
  225. endef
  226. $(foreach LOBJ, $(NO_LTO_OBJ), $(eval $(call NO_LTO,$(LOBJ))))
  227. MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
  228. # For a ChibiOS build, ensure that the board files have the hook overrides injected
  229. define BOARDSRC_INJECT_HOOKS
  230. $(KEYBOARD_OUTPUT)/$(patsubst %.c,%.o,$(patsubst ./%,%,$1)): INIT_HOOK_CFLAGS += -include $(TOP_DIR)/tmk_core/protocol/chibios/init_hooks.h
  231. endef
  232. $(foreach LOBJ, $(BOARDSRC), $(eval $(call BOARDSRC_INJECT_HOOKS,$(LOBJ))))
  233. # Add QMK specific flags
  234. DFU_SUFFIX ?= dfu-suffix
  235. DFU_SUFFIX_ARGS ?=
  236. elf: $(BUILD_DIR)/$(TARGET).elf
  237. hex: $(BUILD_DIR)/$(TARGET).hex
  238. uf2: $(BUILD_DIR)/$(TARGET).uf2
  239. cpfirmware: $(FIRMWARE_FORMAT)
  240. $(SILENT) || printf "Copying $(TARGET).$(FIRMWARE_FORMAT) to qmk_firmware folder" | $(AWK_CMD)
  241. $(COPY) $(BUILD_DIR)/$(TARGET).$(FIRMWARE_FORMAT) $(TARGET).$(FIRMWARE_FORMAT) && $(PRINT_OK)
  242. eep: $(BUILD_DIR)/$(TARGET).eep
  243. lss: $(BUILD_DIR)/$(TARGET).lss
  244. sym: $(BUILD_DIR)/$(TARGET).sym
  245. LIBNAME=lib$(TARGET).a
  246. lib: $(LIBNAME)
  247. # Display size of file, modifying the output so people don't mistakenly grab the hex output
  248. BINARY_SIZE = $(SIZE) --target=$(FORMAT) $(BUILD_DIR)/$(TARGET).hex | $(SED) -e 's/\.build\/.*$$/$(TARGET).$(FIRMWARE_FORMAT)/g'
  249. sizebefore:
  250. @if test -f $(BUILD_DIR)/$(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(BINARY_SIZE); \
  251. 2>/dev/null; $(SECHO); fi
  252. sizeafter: $(BUILD_DIR)/$(TARGET).hex
  253. @if test -f $(BUILD_DIR)/$(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(BINARY_SIZE); \
  254. 2>/dev/null; $(SECHO); fi
  255. # Display compiler version information.
  256. gccversion :
  257. @$(SILENT) || $(CC) --version
  258. # Create final output files (.hex, .eep) from ELF output file.
  259. %.hex: %.elf
  260. $(eval CMD=$(HEX) $< $@)
  261. #@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
  262. @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
  263. @$(BUILD_CMD)
  264. %.uf2: %.hex
  265. $(eval CMD=$(UF2CONV) $(BUILD_DIR)/$(TARGET).hex --output $(BUILD_DIR)/$(TARGET).uf2 --convert --family $(UF2_FAMILY) >/dev/null 2>&1)
  266. #@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
  267. @$(SILENT) || printf "$(MSG_UF2) $@" | $(AWK_CMD)
  268. @$(BUILD_CMD)
  269. %.eep: %.elf
  270. $(eval CMD=$(EEP) $< $@ || exit 0)
  271. #@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
  272. @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
  273. @$(BUILD_CMD)
  274. # Create extended listing file from ELF output file.
  275. %.lss: %.elf
  276. $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
  277. #@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
  278. @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
  279. @$(BUILD_CMD)
  280. # Create a symbol table from ELF output file.
  281. %.sym: %.elf
  282. $(eval CMD=$(NM) -n $< > $@ )
  283. #@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
  284. @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
  285. @$(BUILD_CMD)
  286. %.bin: %.elf
  287. $(eval CMD=$(BIN) $< $@ || exit 0)
  288. #@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
  289. @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
  290. @$(BUILD_CMD)
  291. if [ ! -z "$(DFU_SUFFIX_ARGS)" ]; then \
  292. $(DFU_SUFFIX) $(DFU_SUFFIX_ARGS) -a $(BUILD_DIR)/$(TARGET).bin 1>/dev/null ;\
  293. fi
  294. #$(SILENT) || printf "$(MSG_EXECUTING) '$(DFU_SUFFIX) $(DFU_SUFFIX_ARGS) -a $(BUILD_DIR)/$(TARGET).bin 1>/dev/null':\n" ;\
  295. $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
  296. BEGIN = gccversion sizebefore
  297. # Link: create ELF output file from object files.
  298. .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
  299. .PRECIOUS : $(OBJ)
  300. # Note the obj.txt depeendency is there to force linking if a source file is deleted
  301. %.elf: $(OBJ) $(MASTER_OUTPUT)/cflags.txt $(MASTER_OUTPUT)/ldflags.txt $(MASTER_OUTPUT)/obj.txt | $(BEGIN)
  302. @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
  303. $(eval CMD=MAKE=$(MAKE) $(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
  304. @$(BUILD_CMD)
  305. define GEN_OBJRULE
  306. $1_INCFLAGS := $$(patsubst %,-I%,$$($1_INC))
  307. ifdef $1_CONFIG
  308. $1_CONFIG_FLAGS += $$(patsubst %,-include %,$$($1_CONFIG))
  309. endif
  310. $1_CFLAGS = $$(ALL_CFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS) $$(NOLTO_CFLAGS)
  311. $1_CXXFLAGS = $$(ALL_CXXFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS) $$(NOLTO_CFLAGS)
  312. $1_ASFLAGS = $$(ALL_ASFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
  313. # Compile: create object files from C source files.
  314. $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
  315. @mkdir -p $$(@D)
  316. @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
  317. $$(eval CC_EXEC := $$(CC))
  318. ifneq ($$(VERBOSE_C_CMD),)
  319. $$(if $$(filter $$(notdir $$(VERBOSE_C_CMD)),$$(notdir $$<)),$$(eval CC_EXEC += -v))
  320. endif
  321. ifneq ($$(VERBOSE_C_INCLUDE),)
  322. $$(if $$(filter $$(notdir $$(VERBOSE_C_INCLUDE)),$$(notdir $$<)),$$(eval CC_EXEC += -H))
  323. endif
  324. $$(eval CMD := $$(CC_EXEC) -c $$($1_CFLAGS) $$(INIT_HOOK_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
  325. @$$(BUILD_CMD)
  326. ifneq ($$(DUMP_C_MACROS),)
  327. $$(eval CMD := $$(CC) -E -dM $$($1_CFLAGS) $$(INIT_HOOK_CFLAGS) $$(GENDEPFLAGS) $$<)
  328. @$$(if $$(filter $$(notdir $$(DUMP_C_MACROS)),$$(notdir $$<)),$$(BUILD_CMD))
  329. endif
  330. # Compile: create object files from C++ source files.
  331. $1/%.o : %.cpp $1/%.d $1/cxxflags.txt $1/compiler.txt | $(BEGIN)
  332. @mkdir -p $$(@D)
  333. @$$(SILENT) || printf "$$(MSG_COMPILING_CXX) $$<" | $$(AWK_CMD)
  334. $$(eval CMD=$$(CC) -c $$($1_CXXFLAGS) $$(INIT_HOOK_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
  335. @$$(BUILD_CMD)
  336. $1/%.o : %.cc $1/%.d $1/cxxflags.txt $1/compiler.txt | $(BEGIN)
  337. @mkdir -p $$(@D)
  338. @$$(SILENT) || printf "$$(MSG_COMPILING_CXX) $$<" | $$(AWK_CMD)
  339. $$(eval CMD=$$(CC) -c $$($1_CXXFLAGS) $$(INIT_HOOK_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
  340. @$$(BUILD_CMD)
  341. # Assemble: create object files from assembler source files.
  342. $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
  343. @mkdir -p $$(@D)
  344. @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
  345. $$(eval CMD=$$(CC) -c $$($1_ASFLAGS) $$< -o $$@)
  346. @$$(BUILD_CMD)
  347. $1/%.a : $1/%.o
  348. @mkdir -p $$(@D)
  349. @$(SILENT) || printf "Archiving: $$<" | $$(AWK_CMD)
  350. $$(eval CMD=$$(AR) rcs $$@ $$<)
  351. @$$(BUILD_CMD)
  352. $1/force:
  353. $1/cflags.txt: $1/force
  354. echo '$$($1_CFLAGS)' | cmp -s - $$@ || echo '$$($1_CFLAGS)' > $$@
  355. $1/cxxflags.txt: $1/force
  356. echo '$$($1_CXXFLAGS)' | cmp -s - $$@ || echo '$$($1_CXXFLAGS)' > $$@
  357. $1/asflags.txt: $1/force
  358. echo '$$($1_ASFLAGS)' | cmp -s - $$@ || echo '$$($1_ASFLAGS)' > $$@
  359. $1/compiler.txt: $1/force
  360. $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
  361. endef
  362. .PRECIOUS: $(MASTER_OUTPUT)/obj.txt
  363. $(MASTER_OUTPUT)/obj.txt: $(MASTER_OUTPUT)/force
  364. echo '$(OBJ)' | cmp -s - $@ || echo '$(OBJ)' > $@
  365. .PRECIOUS: $(MASTER_OUTPUT)/ldflags.txt
  366. $(MASTER_OUTPUT)/ldflags.txt: $(MASTER_OUTPUT)/force
  367. echo '$(LDFLAGS)' | cmp -s - $@ || echo '$(LDFLAGS)' > $@
  368. # We have to use static rules for the .d files for some reason
  369. DEPS = $(patsubst %.o,%.d,$(patsubst %.a,%.o,$(OBJ)))
  370. # Keep the .d files
  371. .PRECIOUS: $(DEPS)
  372. # Empty rule to force recompilation if the .d file is missing
  373. $(DEPS):
  374. $(foreach OUTPUT,$(OUTPUTS),$(eval $(call GEN_OBJRULE,$(OUTPUT))))
  375. # Create preprocessed source for use in sending a bug report.
  376. %.i : %.c | $(BEGIN)
  377. $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
  378. # Target: clean project.
  379. clean:
  380. $(foreach OUTPUT,$(OUTPUTS), $(REMOVE) -r $(OUTPUT) 2>/dev/null)
  381. $(REMOVE) $(BUILD_DIR)/$(TARGET).*
  382. show_path:
  383. @echo VPATH=$(VPATH)
  384. @echo SRC=$(SRC)
  385. @echo OBJ=$(OBJ)
  386. dump_vars: ERROR_IF_EMPTY=""
  387. dump_vars: ERROR_IF_NONBOOL=""
  388. dump_vars: ERROR_IF_UNSET=""
  389. dump_vars:
  390. @$(foreach V,$(sort $(.VARIABLES)),$(if $(filter-out environment% default automatic,$(origin $V)),$(info $V=$($V))))
  391. objs-size:
  392. for i in $(OBJ); do echo $$i; done | sort | xargs $(SIZE)
  393. ifeq ($(findstring avr-gcc,$(CC)),avr-gcc)
  394. SIZE_MARGIN = 1024
  395. check-size:
  396. $(eval MAX_SIZE=$(shell n=`$(CC) -E -mmcu=$(MCU) -D__ASSEMBLER__ $(CFLAGS) $(OPT_DEFS) platforms/avr/bootloader_size.c 2> /dev/null | $(SED) -ne 's/\r//;/^#/n;/^AVR_SIZE:/,$${s/^AVR_SIZE: //;p;}'` && echo $$(($$n)) || echo 0))
  397. $(eval CURRENT_SIZE=$(shell if [ -f $(BUILD_DIR)/$(TARGET).hex ]; then $(SIZE) --target=$(FORMAT) $(BUILD_DIR)/$(TARGET).hex | $(AWK) 'NR==2 {print $$4}'; else printf 0; fi))
  398. $(eval FREE_SIZE=$(shell expr $(MAX_SIZE) - $(CURRENT_SIZE)))
  399. $(eval OVER_SIZE=$(shell expr $(CURRENT_SIZE) - $(MAX_SIZE)))
  400. $(eval PERCENT_SIZE=$(shell expr $(CURRENT_SIZE) \* 100 / $(MAX_SIZE)))
  401. if [ $(MAX_SIZE) -gt 0 ] && [ $(CURRENT_SIZE) -gt 0 ]; then \
  402. $(SILENT) || printf "$(MSG_CHECK_FILESIZE)" | $(AWK_CMD); \
  403. if [ $(CURRENT_SIZE) -gt $(MAX_SIZE) ]; then \
  404. printf "\n * $(MSG_FILE_TOO_BIG)"; $(PRINT_ERROR_PLAIN); \
  405. else \
  406. if [ $(FREE_SIZE) -lt $(SIZE_MARGIN) ]; then \
  407. $(PRINT_WARNING_PLAIN); printf " * $(MSG_FILE_NEAR_LIMIT)"; \
  408. else \
  409. $(PRINT_OK); $(SILENT) || printf " * $(MSG_FILE_JUST_RIGHT)"; \
  410. fi ; \
  411. fi ; \
  412. fi
  413. else
  414. check-size:
  415. $(SILENT) || echo "$(MSG_CHECK_FILESIZE_SKIPPED)"
  416. endif
  417. check-md5:
  418. $(MD5SUM) $(BUILD_DIR)/$(TARGET).$(FIRMWARE_FORMAT)
  419. # Create build directory
  420. $(shell mkdir -p $(BUILD_DIR) 2>/dev/null)
  421. # Create object files directory
  422. $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir -p $(OUTPUT) 2>/dev/null)))
  423. # Include the dependency files.
  424. -include $(patsubst %.o,%.d,$(patsubst %.a,%.o,$(OBJ)))
  425. # Listing of phony targets.
  426. .PHONY : all dump_vars finish sizebefore sizeafter qmkversion \
  427. gccversion build elf hex uf2 eep lss sym coff extcoff \
  428. clean clean_list debug gdb-config show_path \
  429. program teensy dfu dfu-ee dfu-start \
  430. flash dfu-split-left dfu-split-right \
  431. avrdude-split-left avrdude-split-right \
  432. avrdude-loop usbasp