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