5 # Allow the silent with lower caps to work the same way as upper caps
11 SUB_IS_SILENT := $(SILENT)
14 # We need to make sure that silent is always turned off at the top level
15 # Otherwise the [OK], [ERROR] and [WARN] messags won't be displayed correctly
16 override SILENT := false
18 ON_ERROR := error_occured=1
20 STARTING_MAKEFILE := $(firstword $(MAKEFILE_LIST))
21 ROOT_MAKEFILE := $(lastword $(MAKEFILE_LIST))
22 ROOT_DIR := $(dir $(ROOT_MAKEFILE))
26 ABS_STARTING_MAKEFILE := $(abspath $(STARTING_MAKEFILE))
27 ABS_ROOT_MAKEFILE := $(abspath $(ROOT_MAKEFILE))
28 ABS_STARTING_DIR := $(dir $(ABS_STARTING_MAKEFILE))
29 ABS_ROOT_DIR := $(dir $(ABS_ROOT_MAKEFILE))
30 STARTING_DIR := $(subst $(ABS_ROOT_DIR),,$(ABS_STARTING_DIR))
31 TEST_DIR := $(ROOT_DIR)/.build/test
35 # Helper function to process the newt element of a space separated path
36 # It works a bit like the traditional functional head tail
37 # so the CURRENT_PATH_ELEMENT will beome the new head
38 # and the PATH_ELEMENTS are the rest that are still unprocessed
39 define NEXT_PATH_ELEMENT
40 $$(eval CURRENT_PATH_ELEMENT := $$(firstword $$(PATH_ELEMENTS)))
41 $$(eval PATH_ELEMENTS := $$(wordlist 2,9999,$$(PATH_ELEMENTS)))
44 # We change the / to spaces so that we more easily can work with the elements
46 PATH_ELEMENTS := $(subst /, ,$(STARTING_DIR))
47 # Initialize the path elements list for further processing
48 $(eval $(call NEXT_PATH_ELEMENT))
50 # This function sets the KEYBOARD; KEYMAP and SUBPROJECT to the correct
51 # variables depending on which directory you stand in.
52 # It's really a very simple if else chain, if you squint enough,
53 # but the makefile syntax makes it very verbose.
54 # If we are in a subfolder of keyboards
55 ifeq ($(CURRENT_PATH_ELEMENT),keyboards)
56 $(eval $(call NEXT_PATH_ELEMENT))
57 KEYBOARD := $(CURRENT_PATH_ELEMENT)
58 $(eval $(call NEXT_PATH_ELEMENT))
59 # If we are in a subfolder of keymaps, or in other words in a keymap
61 ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
62 $(eval $(call NEXT_PATH_ELEMENT))
63 KEYMAP := $(CURRENT_PATH_ELEMENT)
64 # else if we are not in the keyboard folder itself
65 else ifneq ($(CURRENT_PATH_ELEMENT),)
66 # the we can assume it's a subproject, as no other folders
67 # should have make files in them
68 SUBPROJECT := $(CURRENT_PATH_ELEMENT)
69 $(eval $(call NEXT_PATH_ELEMENT))
70 # if we are inside a keymap folder of a subproject
71 ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
72 $(eval $(call NEXT_PATH_ELEMENT))
73 KEYMAP := $(CURRENT_PATH_ELEMENT)
78 # Only consider folders with makefiles, to prevent errors in case there are extra folders
79 KEYBOARDS := $(notdir $(patsubst %/Makefile,%,$(wildcard $(ROOT_DIR)/keyboards/*/Makefile)))
81 #Compability with the old make variables, anything you specify directly on the command line
82 # always overrides the detected folders
84 KEYBOARD := $(keyboard)
90 SUBPROJECT := $(subproject)
96 # Uncomment these for debugging
97 #$(info Keyboard: $(KEYBOARD))
98 #$(info Keymap: $(KEYMAP))
99 #$(info Subproject: $(SUBPROJECT))
100 #$(info Keyboards: $(KEYBOARDS))
103 # Set the default goal depening on where we are running make from
104 # this handles the case where you run make without any arguments
107 ifeq ($(SUBPROJECT),)
108 # Inside a keymap folder, just build the keymap, with the
110 .DEFAULT_GOAL := $(KEYBOARD)-$(KEYMAP)
112 # Inside a subproject keyamp folder, build the keymap
113 # for that subproject
114 .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-$(KEYMAP)
116 else ifneq ($(SUBPROJECT),)
117 # Inside a subproject folder, build all keymaps for that subproject
118 .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-allkm
119 else ifneq ($(KEYBOARD),)
120 # Inside a keyboard folder, build all keymaps for all subprojects
121 # Note that this is different from the old behaviour, which would
122 # build only the default keymap of the default keyboard
123 .DEFAULT_GOAL := $(KEYBOARD)-allsp-allkm
127 # Compare the start of the RULE variable with the first argument($1)
128 # If the rules equals $1 or starts with $1-, RULE_FOUND is set to true
129 # and $1 is removed from the RULE variable
130 # Otherwise the RULE_FOUND variable is set to false, and RULE left as it was
131 # The function is a bit tricky, since there's no built in $(startswith) function
132 define COMPARE_AND_REMOVE_FROM_RULE_HELPER
137 STARTDASH_REMOVED=$$(subst START$1-,,START$$(RULE))
138 ifneq ($$(STARTDASH_REMOVED),START$$(RULE))
140 RULE := $$(STARTDASH_REMOVED)
147 # This makes it easier to call COMPARE_AND_REMOVE_FROM_RULE, since it makes it behave like
148 # a function that returns the value
149 COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND)
152 # Recursively try to find a match for the start of the rule to be checked
153 # $1 The list to be checked
154 # If a match is found, then RULE_FOUND is set to true
155 # and MATCHED_ITEM to the item that was matched
156 define TRY_TO_MATCH_RULE_FROM_LIST_HELPER3
158 ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,$$(firstword $1)),true)
159 MATCHED_ITEM := $$(firstword $1)
161 $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$$(wordlist 2,9999,$1)))
166 # A recursive helper function for finding the longest match
167 # $1 The list to be checed
168 # It works by always removing the currently matched item from the list
169 # and call itself recursively, until a match is found
170 define TRY_TO_MATCH_RULE_FROM_LIST_HELPER2
171 # Stop the recursion when the list is empty
173 RULE_BEFORE := $$(RULE)
174 $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$1))
175 # If a match is found in the current list, otherwise just return what we had before
176 ifeq ($$(RULE_FOUND),true)
177 # Save the best match so far and call itself recursivel
178 BEST_MATCH := $$(MATCHED_ITEM)
179 BEST_MATCH_RULE := $$(RULE)
181 RULE := $$(RULE_BEFORE)
182 $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$$(filter-out $$(MATCHED_ITEM),$1)))
188 # Recursively try to find the longest match for the start of the rule to be checked
189 # $1 The list to be checked
190 # If a match is found, then RULE_FOUND is set to true
191 # and MATCHED_ITEM to the item that was matched
192 define TRY_TO_MATCH_RULE_FROM_LIST_HELPER
194 $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$1))
195 ifneq ($$(BEST_MATCH),)
197 RULE := $$(BEST_MATCH_RULE)
198 MATCHED_ITEM := $$(BEST_MATCH)
205 # Make it easier to call TRY_TO_MATCH_RULE_FROM_LIST
206 TRY_TO_MATCH_RULE_FROM_LIST = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$1))$(RULE_FOUND)
208 define ALL_IN_LIST_LOOP
209 OLD_RULE$1 := $$(RULE)
210 $$(eval $$(call $1,$$(ITEM$1)))
211 RULE := $$(OLD_RULE$1)
214 define PARSE_ALL_IN_LIST
215 $$(foreach ITEM$1,$2,$$(eval $$(call ALL_IN_LIST_LOOP,$1)))
218 # The entry point for rule parsing
219 # parses a rule in the format <keyboard>-<subproject>-<keymap>-<target>
220 # but this particular function only deals with the first <keyboard> part
224 # If the rule starts with allkb, then continue the parsing from
225 # PARSE_ALL_KEYBOARDS
226 ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkb),true)
227 $$(eval $$(call PARSE_ALL_KEYBOARDS))
228 else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,test),true)
229 $$(eval $$(call PARSE_TEST))
230 # If the rule starts with the name of a known keyboard, then continue
231 # the parsing from PARSE_KEYBOARD
232 else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYBOARDS)),true)
233 $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM)))
234 # Otherwise use the KEYBOARD variable, which is determined either by
235 # the current directory you run make from, or passed in as an argument
236 else ifneq ($$(KEYBOARD),)
237 $$(eval $$(call PARSE_KEYBOARD,$$(KEYBOARD)))
239 $$(info make: *** No rule to make target '$1'. Stop.)
240 # Notice the tab instead of spaces below!
246 # Parses a rule in the format <subproject>-<keymap>-<target>
247 # the keyboard is already known when entering this function
248 define PARSE_KEYBOARD
250 # A subproject is any keyboard subfolder with a makefile
251 SUBPROJECTS := $$(notdir $$(patsubst %/Makefile,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/*/Makefile)))
252 # if the rule starts with allsp, then continue with looping over all subprojects
253 ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allsp),true)
254 $$(eval $$(call PARSE_ALL_SUBPROJECTS))
255 # A special case for matching the defaultsp (default subproject)
256 else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,defaultsp),true)
257 $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
258 # If the rule starts with the name of a known subproject
259 else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(SUBPROJECTS)),true)
260 $$(eval $$(call PARSE_SUBPROJECT,$$(MATCHED_ITEM)))
261 # Try to use the SUBPROJECT variable, which is either determined by the
262 # directory which invoked make, or passed as an argument to make
263 else ifneq ($$(SUBPROJECT),)
264 $$(eval $$(call PARSE_SUBPROJECT,$$(SUBPROJECT)))
265 # If there's no matching subproject, we assume it's the default
266 # This will allow you to leave the subproject part of the target out
268 $$(eval $$(call PARSE_SUBPROJECT,))
272 # if we are going to compile all keyboards, match the rest of the rule
274 define PARSE_ALL_KEYBOARDS
275 $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(KEYBOARDS)))
279 # When entering this, the keyboard and subproject are known, so now we need
280 # to determine which keymaps are going to get compiled
281 define PARSE_SUBPROJECT
282 # If we want to compile the default subproject, then we need to
283 # include the correct makefile to determine the actual name of it
285 ifeq ($$(CURRENT_SP),)
286 CURRENT_SP := defaultsp
288 ifeq ($$(CURRENT_SP),defaultsp)
290 $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/Makefile)
291 CURRENT_SP := $$(SUBPROJECT_DEFAULT)
293 # If current subproject is empty (the default was not defined), and we have a list of subproject
294 # then make all of them
295 ifeq ($$(CURRENT_SP),)
296 ifneq ($$(SUBPROJECTS),)
300 # The special allsp is handled later
301 ifneq ($$(CURRENT_SP),allsp)
302 # get a list of all keymaps
303 KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/keymaps/*/.)))
304 ifneq ($$(CURRENT_SP),)
305 # if the subproject is defined, then also look for keymaps inside the subproject folder
306 SP_KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/$$(CURRENT_SP)/keymaps/*/.)))
307 KEYMAPS := $$(sort $$(KEYMAPS) $$(SP_KEYMAPS))
309 # if the rule after removing the start of it is empty (we haven't specified a kemap or target)
310 # compile all the keymaps
312 $$(eval $$(call PARSE_ALL_KEYMAPS))
313 # The same if allkm was specified
314 else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkm),true)
315 $$(eval $$(call PARSE_ALL_KEYMAPS))
316 # Try to match the specified keyamp with the list of known keymaps
317 else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYMAPS)),true)
318 $$(eval $$(call PARSE_KEYMAP,$$(MATCHED_ITEM)))
319 # Otherwise try to match the keymap from the current folder, or arguments to the make command
320 else ifneq ($$(KEYMAP),)
321 $$(eval $$(call PARSE_KEYMAP,$$(KEYMAP)))
322 # No matching keymap found, so we assume that the rest of the rule is the target
323 # If we haven't been able to parse out a subproject, then make all of them
324 # This is consistent with running make without any arguments from the keyboard
327 $$(eval $$(call PARSE_ALL_SUBPROJECTS))
328 # Otherwise, make all keymaps, again this is consistent with how it works without
331 $$(eval $$(call PARSE_ALL_KEYMAPS))
334 # As earlier mentione,d when allsb is specified, we call our self recursively
335 # for all of the subprojects
336 $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$(SUBPROJECTS)))
340 # If we want to parse all subprojects, but the keyboard doesn't have any,
341 # then use defaultsp instead
342 define PARSE_ALL_SUBPROJECTS
343 ifeq ($$(SUBPROJECTS),)
344 $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
346 $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$$(SUBPROJECTS)))
351 # This is the meat of compiling a keyboard, when entering this, everything is known
352 # keyboard, subproject, and keymap
353 # Note that we are not directly calling the command here, but instead building a list,
354 # which will later be processed
357 # The rest of the rule is the target
358 # Remove the leading "-" from the target, as it acts as a separator
359 MAKE_TARGET := $$(patsubst -%,%,$$(RULE))
360 # We need to generate an unique indentifer to append to the COMMANDS list
361 COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM)
362 # If we are compiling a keyboard without a subproject, we want to display just the name
363 # of the keyboard, otherwise keyboard/subproject
364 ifeq ($$(CURRENT_SP),)
365 KB_SP := $(CURRENT_KB)
367 KB_SP := $(CURRENT_KB)/$$(CURRENT_SP)
370 KB_SP := $(BOLD)$$(KB_SP)$(NO_COLOR)
371 # Specify the variables that we are passing forward to submake
372 MAKE_VARS := KEYBOARD=$$(CURRENT_KB) SUBPROJECT=$$(CURRENT_SP) KEYMAP=$$(CURRENT_KM)
373 # And the first part of the make command
374 MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_keyboard.mk $$(MAKE_TARGET)
375 # The message to display
376 MAKE_MSG := $$(MSG_MAKE_KB)
377 # We run the command differently, depending on if we want more output or not
378 # The true version for silent output and the false version otherwise
379 $$(eval $$(call BUILD))
383 MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
384 COMMANDS += $$(COMMAND)
385 COMMAND_true_$$(COMMAND) := \
386 printf "$$(MAKE_MSG)" | \
387 $$(MAKE_MSG_FORMAT); \
388 LOG=$$$$($$(MAKE_CMD) $$(MAKE_VARS) SILENT=true 2>&1) ; \
389 if [ $$$$? -gt 0 ]; \
390 then $$(PRINT_ERROR_PLAIN); \
391 elif [ "$$$$LOG" != "" ] ; \
392 then $$(PRINT_WARNING_PLAIN); \
396 COMMAND_false_$$(COMMAND) := \
397 printf "$$(MAKE_MSG)\n\n"; \
398 $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false; \
399 if [ $$$$? -gt 0 ]; \
400 then error_occured=1; \
404 # Just parse all the keymaps for a specifc keyboard
405 define PARSE_ALL_KEYMAPS
406 $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
413 MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_test.mk $$(MAKE_TARGET)
414 MAKE_VARS := TEST=$$(TEST_NAME)
415 MAKE_MSG := $$(MSG_MAKE_TEST)
416 $$(eval $$(call BUILD))
417 TESTS += $$(TEST_DIR)/$$(TEST_NAME).elf
421 TEST_NAME := $$(firstword $$(subst -, ,$$(RULE)))
422 TEST_TARGET := $$(subst $$(TEST_NAME),,$$(subst $$(TEST_NAME)-,,$$(RULE)))
423 MATCHED_TESTS := $$(foreach TEST,$$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME),$$(TEST)),$$(TEST),))
424 $$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
428 # Set the silent mode depending on if we are trying to compile multiple keyboards or not
429 # By default it's on in that case, but it can be overriden by specifying silent=false
430 # from the command line
431 define SET_SILENT_MODE
433 SILENT_MODE := $(SUB_IS_SILENT)
434 else ifeq ($$(words $$(COMMANDS)),1)
441 include $(ROOT_DIR)/message.mk
444 $(COMMAND_$(SILENT_MODE)_$(COMMAND))
446 # Allow specifying just the subproject, in the keyboard directory, which will compile all keymaps
447 SUBPROJECTS := $(notdir $(patsubst %/Makefile,%,$(wildcard ./*/Makefile)))
448 .PHONY: $(SUBPROJECTS)
449 $(SUBPROJECTS): %: %-allkm
451 # Let's match everything, we handle all the rule parsing ourselves
454 # Check if we have the CMP tool installed
455 cmp --version >/dev/null 2>&1; if [ $$? -gt 0 ]; then printf "$(MSG_NO_CMP)"; exit 1; fi;
456 # Check if the submodules are dirty, and display a warning if they are
457 git submodule status --recursive 2>/dev/null | \
458 while IFS= read -r x; do \
461 *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
464 $(eval $(call PARSE_RULE,$@))
465 $(eval $(call SET_SILENT_MODE))
466 # Run all the commands in the same shell, notice the + at the first line
467 # it has to be there to allow parallel execution of the submake
468 # This always tries to compile everything, even if error occurs in the middle
469 # But we return the error code at the end, to trigger travis failures
471 $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND)) \
472 if [ $$error_occured -gt 0 ]; then printf "$(MSG_ERRORS)" & exit $$error_occured; fi;\
473 $(foreach TEST,$(TESTS),$(TEST))
475 # All should compile everything
479 # Define some shortcuts, mostly for compability with the old syntax
480 .PHONY: all-keyboards
481 all-keyboards: allkb-allsp-allkm
483 .PHONY: all-keyboards-defaults
484 all-keyboards-defaults: allkb-allsp-default
487 # Generate the version.h file
488 GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
489 BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
490 $(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
491 $(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
493 include $(ROOT_DIR)/testlist.mk