1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jg Wunsch, et al.
5 # Released to the Public Domain
7 # Additional material for this makefile was written by:
18 # Enable vpath seraching for source files only
19 # Without this, output files, could be read from the wrong .build directories
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)
29 # Output format. (can be srec, ihex, binary)
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.)
42 ERROR_COLOR=\033[31;01m
43 WARN_COLOR=\033[33;01m
50 ifneq ($(shell awk --version 2>/dev/null),)
56 OK_STRING=$(OK_COLOR)[OK]$(NO_COLOR)\n
57 ERROR_STRING=$(ERROR_COLOR)[ERRORS]$(NO_COLOR)\n
58 WARN_STRING=$(WARN_COLOR)[WARNINGS]$(NO_COLOR)\n
64 TAB_LOG = printf "\n$$LOG\n\n" | $(AWK) '{ sub(/^/," | "); print }'
65 TAB_LOG_PLAIN = printf "$$LOG\n"
66 AWK_STATUS = $(AWK) '{ printf " %-10s\n", $$1; }'
67 AWK_CMD = $(AWK) '{ printf "%-99s", $$0; }'
68 PRINT_ERROR = ($(SILENT) ||printf " $(ERROR_STRING)" | $(AWK_STATUS)) && $(TAB_LOG) && exit 1
69 PRINT_WARNING = ($(SILENT) || printf " $(WARN_STRING)" | $(AWK_STATUS)) && $(TAB_LOG)
70 PRINT_ERROR_PLAIN = ($(SILENT) ||printf " $(ERROR_STRING)" | $(AWK_STATUS)) && $(TAB_LOG_PLAIN) && exit 1
71 PRINT_WARNING_PLAIN = ($(SILENT) || printf " $(WARN_STRING)" | $(AWK_STATUS)) && $(TAB_LOG_PLAIN)
72 PRINT_OK = $(SILENT) || printf " $(OK_STRING)" | $(AWK_STATUS)
73 BUILD_CMD = LOG=$$($(CMD) 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING); else $(PRINT_OK); fi;
75 # List any extra directories to look for include files here.
76 # Each directory must be seperated by a space.
77 # Use forward slashes for directory separators.
78 # For a directory that has spaces, enclose it in quotes.
79 EXTRAINCDIRS += $(subst :, ,$(VPATH_SRC))
82 # Compiler flag to set the C Standard level.
84 # gnu89 = c89 plus GCC extensions
85 # c99 = ISO C99 standard (not yet fully implemented)
86 # gnu99 = c99 plus GCC extensions
87 CSTANDARD = -std=gnu99
90 # Place -D or -U options here for C sources
94 # Place -D or -U options here for ASM sources
98 # Place -D or -U options here for C++ sources
99 #CPPDEFS += -D__STDC_LIMIT_MACROS
100 #CPPDEFS += -D__STDC_CONSTANT_MACROS
101 CPPDEFS += $(OPT_DEFS)
105 #---------------- Compiler Options C ----------------
106 # -g*: generate debugging information
107 # -O*: optimization level
108 # -f...: tuning, see GCC manual and avr-libc documentation
109 # -Wall...: warning level
110 # -Wa,...: tell GCC to pass this to the assembler.
111 # -adhlns...: create assembler listing
117 ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
118 CFLAGS+= -fdiagnostics-color
122 CFLAGS += -Wstrict-prototypes
123 #CFLAGS += -mshort-calls
124 #CFLAGS += -fno-unit-at-a-time
126 #CFLAGS += -Wunreachable-code
127 #CFLAGS += -Wsign-compare
128 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
129 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
130 CFLAGS += $(CSTANDARD)
132 CFLAGS += -include $(CONFIG_H)
136 #---------------- Compiler Options C++ ----------------
137 # -g*: generate debugging information
138 # -O*: optimization level
139 # -f...: tuning, see GCC manual and avr-libc documentation
140 # -Wall...: warning level
141 # -Wa,...: tell GCC to pass this to the assembler.
142 # -adhlns...: create assembler listing
143 CPPFLAGS += -g$(DEBUG)
144 CPPFLAGS += $(CPPDEFS)
146 # to supress "warning: only initialized variables can be placed into program memory area"
150 #CPPFLAGS += -mshort-calls
151 #CPPFLAGS += -fno-unit-at-a-time
152 #CPPFLAGS += -Wstrict-prototypes
153 #CPPFLAGS += -Wunreachable-code
154 #CPPFLAGS += -Wsign-compare
155 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
156 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
157 #CPPFLAGS += $(CSTANDARD)
159 CPPFLAGS += -include $(CONFIG_H)
163 #---------------- Assembler Options ----------------
164 # -Wa,...: tell GCC to pass this to the assembler.
165 # -adhlns: create listing
166 # -gstabs: have the assembler create line number information; note that
167 # for use in COFF files, additional information about filenames
168 # and function names needs to be present in the assembler source
169 # files -- see avr-libc docs [FIXME: not yet described there]
170 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
171 # dump that will be displayed for a given single line of source input.
172 ASFLAGS += $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
173 ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
175 ASFLAGS += -include $(CONFIG_H)
178 #---------------- Library Options ----------------
179 # Minimalistic printf version
180 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
182 # Floating point printf version (requires MATH_LIB = -lm below)
183 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
185 # If this is left blank, then it will use the Standard printf version.
187 #PRINTF_LIB = $(PRINTF_LIB_MIN)
188 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
191 # Minimalistic scanf version
192 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
194 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
195 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
197 # If this is left blank, then it will use the Standard scanf version.
199 #SCANF_LIB = $(SCANF_LIB_MIN)
200 #SCANF_LIB = $(SCANF_LIB_FLOAT)
206 #---------------- Linker Options ----------------
207 # -Wl,...: tell GCC to pass this to linker.
208 # -Map: create map file
209 # --cref: add cross reference to map file
211 # Comennt out "--relax" option to avoid a error such:
212 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
214 LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
215 #LDFLAGS += -Wl,--relax
216 LDFLAGS += $(EXTMEMOPTS)
217 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
218 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
219 #LDFLAGS += -T linker_script.x
220 # You can give EXTRALDFLAGS at 'make' command line.
221 LDFLAGS += $(EXTRALDFLAGS)
223 # Define programs and commands.
229 SECHO = $(SILENT) || echo
233 MSG_ERRORS_NONE = Errors: none
234 MSG_BEGIN = -------- begin --------
235 MSG_END = -------- end --------
236 MSG_SIZE_BEFORE = Size before:
237 MSG_SIZE_AFTER = Size after:
238 MSG_COFF = Converting to AVR COFF:
239 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
240 MSG_FLASH = Creating load file for Flash:
241 MSG_EEPROM = Creating load file for EEPROM:
242 MSG_BIN = Creating binary load file for Flash:
243 MSG_EXTENDED_LISTING = Creating Extended Listing:
244 MSG_SYMBOL_TABLE = Creating Symbol Table:
245 MSG_LINKING = Linking:
246 MSG_COMPILING = Compiling:
247 MSG_COMPILING_CPP = Compiling:
248 MSG_ASSEMBLING = Assembling:
249 MSG_CLEANING = Cleaning project:
250 MSG_CREATING_LIBRARY = Creating library:
251 MSG_SUBMODULE_DIRTY = $(WARN_COLOR)WARNING:$(NO_COLOR)\n \
252 Some git sub-modules are out of date or modified, please consider runnning:$(BOLD)\n\
253 git submodule sync --recursive\n\
254 git submodule update --init --recursive$(NO_COLOR)\n\n\
255 You can ignore this warning if you are not compiling any ChibiOS keyboards,\n\
256 or if you have modified the ChibiOS libraries yourself. \n\n
259 # Define all object files.
260 OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
261 # The files in the lib folder are shared between all keymaps, so generate that folder name by removing
262 # the keymap from the name
263 KBOBJDIR=$(subst _$(KEYMAP),,$(OBJDIR))
264 # And fixup the object files to match
265 LIBOBJ = $(foreach v,$(OBJ),$(if $(findstring /lib/,$v),$v))
266 NONLIBOBJ := $(filter-out $(LIBOBJ),$(OBJ))
267 LIBOBJ := $(subst _$(KEYMAP)/,/,$(LIBOBJ))
268 OBJ := $(LIBOBJ) $(NONLIBOBJ)
270 # Define all listing files.
271 LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
274 # Compiler flags to generate dependency files.
275 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
276 GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
279 # Combine all necessary flags and optional flags.
280 # Add target processor to flags.
281 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
282 ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
283 ALL_CPPFLAGS = $(MCUFLAGS) -x c++ $(CPPFLAGS) $(EXTRAFLAGS)
284 ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
286 MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
291 # Change the build target to build a HEX file or a library.
293 #build: elf hex eep lss sym
297 elf: $(BUILD_DIR)/$(TARGET).elf
298 hex: $(BUILD_DIR)/$(TARGET).hex
299 eep: $(BUILD_DIR)/$(TARGET).eep
300 lss: $(BUILD_DIR)/$(TARGET).lss
301 sym: $(BUILD_DIR)/$(TARGET).sym
302 LIBNAME=lib$(TARGET).a
306 git submodule status --recursive | \
307 while IFS= read -r x; do \
310 *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
314 # Display size of file.
315 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
316 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
317 ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
320 @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
321 2>/dev/null; $(SECHO); fi
323 sizeafter: $(BUILD_DIR)/$(TARGET).hex
324 @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
325 2>/dev/null; $(SECHO); fi
326 # test file sizes eventually
327 # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
329 # Display compiler version information.
331 @$(SILENT) || $(CC) --version
333 # Create final output files (.hex, .eep) from ELF output file.
335 @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
336 $(eval CMD=$(HEX) $< $@)
338 @if $(AUTOGEN); then \
339 $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
340 $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
342 $(COPY) $@ $(TARGET).hex; \
346 @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
347 $(eval CMD=$(EEP) $< $@ || exit 0)
350 # Create extended listing file from ELF output file.
352 @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
353 $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
356 # Create a symbol table from ELF output file.
358 @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
359 $(eval CMD=$(NM) -n $< > $@ )
363 @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
364 $(eval CMD=$(BIN) $< $@ || exit 0)
367 # Create library from object files.
368 .SECONDARY : $(BUILD_DIR)/$(TARGET).a
371 @$(SILENT) || printf "$(MSG_CREATING_LIBRARY) $@" | $(AWK_CMD)
372 $(eval CMD=$(AR) $@ $(OBJ) )
375 BEGIN = gccversion check_submodule sizebefore
377 # Link: create ELF output file from object files.
378 .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
380 # Note the obj.txt depeendency is there to force linking if a source file is deleted
381 %.elf: $(OBJ) $(OBJDIR)/cflags.txt $(OBJDIR)/ldflags.txt $(OBJDIR)/obj.txt | $(BEGIN)
382 @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
383 $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
387 # Compile: create object files from C source files.
388 $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
390 @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
391 $$(eval CMD=$$(CC) -c $$(ALL_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
394 # Compile: create object files from C++ source files.
395 $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
397 @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
398 $$(eval CMD=$$(CC) -c $$(ALL_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
401 # Assemble: create object files from assembler source files.
402 $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
404 @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
405 $$(eval CMD=$$(CC) -c $$(ALL_ASFLAGS) $$< -o $$@)
410 $1/cflags.txt: $1/force
411 echo '$$(ALL_CFLAGS)' | cmp -s - $$@ || echo '$$(ALL_CFLAGS)' > $$@
413 $1/cppflags.txt: $1/force
414 echo '$$(ALL_CPPFLAGS)' | cmp -s - $$@ || echo '$$(ALL_CPPFLAGS)' > $$@
416 $1/asflags.txt: $1/force
417 echo '$$(ALL_ASFLAGS)' | cmp -s - $$@ || echo '$$(ALL_ASFLAGS)' > $$@
419 $1/ldflags.txt: $1/force
420 echo '$$(LDFLAGS)' | cmp -s - $$@ || echo '$$(LDFLAGS)' > $$@
423 echo '$$(OBJ)' | cmp -s - $$@ || echo '$$(OBJ)' > $$@
425 $1/compiler.txt: $1/force
426 $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
429 # We have to use static rules for the .d files for some reason
430 DEPS = $(patsubst %.o,%.d,$(OBJ))
433 # Empty rule to force recompilation if the .d file is missing
436 # Since the object files could be in two different folders, generate
437 # separate rules for them, rather than having too generic rules
438 $(eval $(call GEN_OBJRULE,$(OBJDIR)))
439 $(eval $(call GEN_OBJRULE,$(KBOBJDIR)))
441 # Compile: create assembler files from C source files.
443 @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
444 $(eval CMD=$(CC) -S $(ALL_CFLAGS) $< -o $@)
447 # Compile: create assembler files from C++ source files.
448 %.s : %.cpp | $(BEGIN)
449 @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
450 $(eval CMD=$(CC) -S $(ALL_CPPFLAGS) $< -o $@)
453 # Create preprocessed source for use in sending a bug report.
455 $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
457 # Target: clean project.
459 $(REMOVE) -r $(OBJDIR) 2>/dev/null
460 $(REMOVE) -r $(KBOBJDIR) 2>/dev/null
461 $(REMOVE) $(BUILD_DIR)/$(TARGET).*
467 SUBDIRS := $(filter-out %/util/ %/doc/ %/keymaps/ %/old_keymap_files/,$(dir $(wildcard $(TOP_DIR)/keyboards/**/*/Makefile)))
468 SUBDIRS := $(SUBDIRS) $(dir $(wildcard $(TOP_DIR)/keyboards/*/.))
469 SUBDIRS := $(sort $(SUBDIRS))
470 # $(error $(SUBDIRS))
471 all-keyboards-defaults-%:
472 @for x in $(SUBDIRS) ; do \
473 printf "Compiling with default: $$x" | $(AWK_CMD); \
474 LOG=$$($(MAKE) -C $$x $(subst all-keyboards-defaults-,,$@) VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
477 all-keyboards-defaults: all-keyboards-defaults-all
479 KEYBOARDS := $(SUBDIRS:$(TOP_DIR)/keyboards/%/=/keyboards/%)
480 all-keyboards-all: $(addsuffix -all,$(KEYBOARDS))
481 all-keyboards-clean: $(addsuffix -clean,$(KEYBOARDS))
482 all-keyboards: all-keyboards-all
485 $(eval KEYBOARD=$(patsubst /keyboards/%,%,$1))
486 $(eval SUBPROJECT=$(lastword $(subst /, ,$(KEYBOARD))))
487 $(eval KEYBOARD=$(firstword $(subst /, ,$(KEYBOARD))))
488 $(eval KEYMAPS=$(notdir $(patsubst %/.,%,$(wildcard $(TOP_DIR)/keyboards/$(KEYBOARD)/keymaps/*/.))))
489 $(eval KEYMAPS+=$(notdir $(patsubst %/.,%,$(wildcard $(TOP_DIR)/keyboards/$(KEYBOARD)/$(SUBPROJECT)/keymaps/*/.))))
490 @for x in $(KEYMAPS) ; do \
491 printf "Compiling $(BOLD)$(KEYBOARD)/$(SUBPROJECT)$(NO_COLOR) with $(BOLD)$$x$(NO_COLOR)" | $(AWK) '{ printf "%-118s", $$0; }'; \
492 LOG=$$($(MAKE) -C $(TOP_DIR)$1 $2 keymap=$$x VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
496 define make_keyboard_helper
497 # Just remove the -all and so on from the first argument and pass it forward
498 $(call make_keyboard,$(subst -$2,,$1),$2)
502 $(call make_keyboard_helper,$@,all)
504 $(call make_keyboard_helper,$@,clean)
506 $(call make_keyboard_helper,$@,all)
509 $(eval MAKECONFIG=$(call get_target,all-keymaps,$@))
510 $(eval KEYMAPS=$(notdir $(patsubst %/.,%,$(wildcard $(TOP_DIR)/keyboards/$(KEYBOARD)/keymaps/*/.))))
511 @for x in $(KEYMAPS) ; do \
512 printf "Compiling $(BOLD)$(KEYBOARD)$(NO_COLOR) with $(BOLD)$$x$(NO_COLOR)" | $(AWK) '{ printf "%-118s", $$0; }'; \
513 LOG=$$($(MAKE) $(subst all-keymaps-,,$@) keyboard=$(KEYBOARD) keymap=$$x VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
516 all-keymaps: all-keymaps-all
518 # Create build directory
519 $(shell mkdir $(BUILD_DIR) 2>/dev/null)
521 # Create object files directory
522 $(shell mkdir $(OBJDIR) 2>/dev/null)
523 $(shell mkdir $(KBOBJDIR) 2>/dev/null)
525 # Include the dependency files.
526 -include $(patsubst %.o,%.d,$(OBJ))
529 # Listing of phony targets.
530 .PHONY : all finish sizebefore sizeafter gccversion \
531 build elf hex eep lss sym coff extcoff check_submodule \
532 clean clean_list debug gdb-config show_path \
533 program teensy dfu flip dfu-ee flip-ee dfu-start \
534 all-keyboards-defaults all-keyboards all-keymaps \
535 all-keyboards-defaults-% all-keyboards-% all-keymaps-%