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