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