5 # Never run this makefile in parallel, as it could screw things up
6 # It won't affect the submakes, so you still get the speedup from specifying -jx
9 # Allow the silent with lower caps to work the same way as upper caps
15 SUB_IS_SILENT := $(SILENT)
18 # We need to make sure that silent is always turned off at the top level
19 # Otherwise the [OK], [ERROR] and [WARN] messags won't be displayed correctly
20 override SILENT := false
22 ON_ERROR := error_occured=1
24 STARTING_MAKEFILE := $(firstword $(MAKEFILE_LIST))
25 ROOT_MAKEFILE := $(lastword $(MAKEFILE_LIST))
26 ROOT_DIR := $(dir $(ROOT_MAKEFILE))
30 ABS_STARTING_MAKEFILE := $(abspath $(STARTING_MAKEFILE))
31 ABS_ROOT_MAKEFILE := $(abspath $(ROOT_MAKEFILE))
32 ABS_STARTING_DIR := $(dir $(ABS_STARTING_MAKEFILE))
33 ABS_ROOT_DIR := $(dir $(ABS_ROOT_MAKEFILE))
34 STARTING_DIR := $(subst $(ABS_ROOT_DIR),,$(ABS_STARTING_DIR))
35 TEST_DIR := $(ROOT_DIR)/.build/test
39 # Helper function to process the newt element of a space separated path
40 # It works a bit like the traditional functional head tail
41 # so the CURRENT_PATH_ELEMENT will beome the new head
42 # and the PATH_ELEMENTS are the rest that are still unprocessed
43 define NEXT_PATH_ELEMENT
44 $$(eval CURRENT_PATH_ELEMENT := $$(firstword $$(PATH_ELEMENTS)))
45 $$(eval PATH_ELEMENTS := $$(wordlist 2,9999,$$(PATH_ELEMENTS)))
48 # We change the / to spaces so that we more easily can work with the elements
50 PATH_ELEMENTS := $(subst /, ,$(STARTING_DIR))
51 # Initialize the path elements list for further processing
52 $(eval $(call NEXT_PATH_ELEMENT))
54 # This function sets the KEYBOARD; KEYMAP and SUBPROJECT to the correct
55 # variables depending on which directory you stand in.
56 # It's really a very simple if else chain, if you squint enough,
57 # but the makefile syntax makes it very verbose.
58 # If we are in a subfolder of keyboards
59 ifeq ($(CURRENT_PATH_ELEMENT),keyboards)
60 $(eval $(call NEXT_PATH_ELEMENT))
61 KEYBOARD := $(CURRENT_PATH_ELEMENT)
62 $(eval $(call NEXT_PATH_ELEMENT))
63 # If we are in a subfolder of keymaps, or in other words in a keymap
65 ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
66 $(eval $(call NEXT_PATH_ELEMENT))
67 KEYMAP := $(CURRENT_PATH_ELEMENT)
68 # else if we are not in the keyboard folder itself
69 else ifneq ($(CURRENT_PATH_ELEMENT),)
70 # the we can assume it's a subproject, as no other folders
71 # should have make files in them
72 SUBPROJECT := $(CURRENT_PATH_ELEMENT)
73 $(eval $(call NEXT_PATH_ELEMENT))
74 # if we are inside a keymap folder of a subproject
75 ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
76 $(eval $(call NEXT_PATH_ELEMENT))
77 KEYMAP := $(CURRENT_PATH_ELEMENT)
82 # Only consider folders with makefiles, to prevent errors in case there are extra folders
83 KEYBOARDS := $(notdir $(patsubst %/Makefile,%,$(wildcard $(ROOT_DIR)/keyboards/*/Makefile)))
85 #Compability with the old make variables, anything you specify directly on the command line
86 # always overrides the detected folders
88 KEYBOARD := $(keyboard)
94 SUBPROJECT := $(subproject)
100 # Uncomment these for debugging
101 #$(info Keyboard: $(KEYBOARD))
102 #$(info Keymap: $(KEYMAP))
103 #$(info Subproject: $(SUBPROJECT))
104 #$(info Keyboards: $(KEYBOARDS))
107 # Set the default goal depening on where we are running make from
108 # this handles the case where you run make without any arguments
111 ifeq ($(SUBPROJECT),)
112 # Inside a keymap folder, just build the keymap, with the
114 .DEFAULT_GOAL := $(KEYBOARD)-$(KEYMAP)
116 # Inside a subproject keyamp folder, build the keymap
117 # for that subproject
118 .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-$(KEYMAP)
120 else ifneq ($(SUBPROJECT),)
121 # Inside a subproject folder, build all keymaps for that subproject
122 .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-allkm
123 else ifneq ($(KEYBOARD),)
124 # Inside a keyboard folder, build all keymaps for all subprojects
125 # Note that this is different from the old behaviour, which would
126 # build only the default keymap of the default keyboard
127 .DEFAULT_GOAL := $(KEYBOARD)-allsp-allkm
131 # Compare the start of the RULE variable with the first argument($1)
132 # If the rules equals $1 or starts with $1-, RULE_FOUND is set to true
133 # and $1 is removed from the RULE variable
134 # Otherwise the RULE_FOUND variable is set to false, and RULE left as it was
135 # The function is a bit tricky, since there's no built in $(startswith) function
136 define COMPARE_AND_REMOVE_FROM_RULE_HELPER
141 STARTDASH_REMOVED=$$(subst START$1-,,START$$(RULE))
142 ifneq ($$(STARTDASH_REMOVED),START$$(RULE))
144 RULE := $$(STARTDASH_REMOVED)
151 # This makes it easier to call COMPARE_AND_REMOVE_FROM_RULE, since it makes it behave like
152 # a function that returns the value
153 COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND)
156 # Recursively try to find a match for the start of the rule to be checked
157 # $1 The list to be checked
158 # If a match is found, then RULE_FOUND is set to true
159 # and MATCHED_ITEM to the item that was matched
160 define TRY_TO_MATCH_RULE_FROM_LIST_HELPER3
162 ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,$$(firstword $1)),true)
163 MATCHED_ITEM := $$(firstword $1)
165 $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$$(wordlist 2,9999,$1)))
170 # A recursive helper function for finding the longest match
171 # $1 The list to be checed
172 # It works by always removing the currently matched item from the list
173 # and call itself recursively, until a match is found
174 define TRY_TO_MATCH_RULE_FROM_LIST_HELPER2
175 # Stop the recursion when the list is empty
177 RULE_BEFORE := $$(RULE)
178 $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$1))
179 # If a match is found in the current list, otherwise just return what we had before
180 ifeq ($$(RULE_FOUND),true)
181 # Save the best match so far and call itself recursivel
182 BEST_MATCH := $$(MATCHED_ITEM)
183 BEST_MATCH_RULE := $$(RULE)
185 RULE := $$(RULE_BEFORE)
186 $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$$(filter-out $$(MATCHED_ITEM),$1)))
192 # Recursively try to find the longest match for the start of the rule to be checked
193 # $1 The list to be checked
194 # If a match is found, then RULE_FOUND is set to true
195 # and MATCHED_ITEM to the item that was matched
196 define TRY_TO_MATCH_RULE_FROM_LIST_HELPER
198 $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$1))
199 ifneq ($$(BEST_MATCH),)
201 RULE := $$(BEST_MATCH_RULE)
202 MATCHED_ITEM := $$(BEST_MATCH)
209 # Make it easier to call TRY_TO_MATCH_RULE_FROM_LIST
210 TRY_TO_MATCH_RULE_FROM_LIST = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$1))$(RULE_FOUND)
212 define ALL_IN_LIST_LOOP
213 OLD_RULE$1 := $$(RULE)
214 $$(eval $$(call $1,$$(ITEM$1)))
215 RULE := $$(OLD_RULE$1)
218 define PARSE_ALL_IN_LIST
219 $$(foreach ITEM$1,$2,$$(eval $$(call ALL_IN_LIST_LOOP,$1)))
222 # The entry point for rule parsing
223 # parses a rule in the format <keyboard>-<subproject>-<keymap>-<target>
224 # but this particular function only deals with the first <keyboard> part
228 # If the rule starts with allkb, then continue the parsing from
229 # PARSE_ALL_KEYBOARDS
230 ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkb),true)
231 $$(eval $$(call PARSE_ALL_KEYBOARDS))
232 else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,test),true)
233 $$(eval $$(call PARSE_TEST))
234 # If the rule starts with the name of a known keyboard, then continue
235 # the parsing from PARSE_KEYBOARD
236 else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYBOARDS)),true)
237 $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM)))
238 # Otherwise use the KEYBOARD variable, which is determined either by
239 # the current directory you run make from, or passed in as an argument
240 else ifneq ($$(KEYBOARD),)
241 $$(eval $$(call PARSE_KEYBOARD,$$(KEYBOARD)))
243 $$(info make: *** No rule to make target '$1'. Stop.)
244 # Notice the tab instead of spaces below!
250 # Parses a rule in the format <subproject>-<keymap>-<target>
251 # the keyboard is already known when entering this function
252 define PARSE_KEYBOARD
254 # A subproject is any keyboard subfolder with a makefile
255 SUBPROJECTS := $$(notdir $$(patsubst %/Makefile,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/*/Makefile)))
256 # if the rule starts with allsp, then continue with looping over all subprojects
257 ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allsp),true)
258 $$(eval $$(call PARSE_ALL_SUBPROJECTS))
259 # A special case for matching the defaultsp (default subproject)
260 else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,defaultsp),true)
261 $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
262 # If the rule starts with the name of a known subproject
263 else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(SUBPROJECTS)),true)
264 $$(eval $$(call PARSE_SUBPROJECT,$$(MATCHED_ITEM)))
265 # Try to use the SUBPROJECT variable, which is either determined by the
266 # directory which invoked make, or passed as an argument to make
267 else ifneq ($$(SUBPROJECT),)
268 $$(eval $$(call PARSE_SUBPROJECT,$$(SUBPROJECT)))
269 # If there's no matching subproject, we assume it's the default
270 # This will allow you to leave the subproject part of the target out
272 $$(eval $$(call PARSE_SUBPROJECT,))
276 # if we are going to compile all keyboards, match the rest of the rule
278 define PARSE_ALL_KEYBOARDS
279 $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(KEYBOARDS)))
283 # When entering this, the keyboard and subproject are known, so now we need
284 # to determine which keymaps are going to get compiled
285 define PARSE_SUBPROJECT
286 # If we want to compile the default subproject, then we need to
287 # include the correct makefile to determine the actual name of it
289 ifeq ($$(CURRENT_SP),)
290 CURRENT_SP := defaultsp
292 ifeq ($$(CURRENT_SP),defaultsp)
294 $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/Makefile)
295 CURRENT_SP := $$(SUBPROJECT_DEFAULT)
297 # If current subproject is empty (the default was not defined), and we have a list of subproject
298 # then make all of them
299 ifeq ($$(CURRENT_SP),)
300 ifneq ($$(SUBPROJECTS),)
304 # The special allsp is handled later
305 ifneq ($$(CURRENT_SP),allsp)
306 # get a list of all keymaps
307 KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/keymaps/*/.)))
308 ifneq ($$(CURRENT_SP),)
309 # if the subproject is defined, then also look for keymaps inside the subproject folder
310 SP_KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/$$(CURRENT_SP)/keymaps/*/.)))
311 KEYMAPS := $$(sort $$(KEYMAPS) $$(SP_KEYMAPS))
313 # if the rule after removing the start of it is empty (we haven't specified a kemap or target)
314 # compile all the keymaps
316 $$(eval $$(call PARSE_ALL_KEYMAPS))
317 # The same if allkm was specified
318 else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkm),true)
319 $$(eval $$(call PARSE_ALL_KEYMAPS))
320 # Try to match the specified keyamp with the list of known keymaps
321 else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYMAPS)),true)
322 $$(eval $$(call PARSE_KEYMAP,$$(MATCHED_ITEM)))
323 # Otherwise try to match the keymap from the current folder, or arguments to the make command
324 else ifneq ($$(KEYMAP),)
325 $$(eval $$(call PARSE_KEYMAP,$$(KEYMAP)))
326 # No matching keymap found, so we assume that the rest of the rule is the target
327 # If we haven't been able to parse out a subproject, then make all of them
328 # This is consistent with running make without any arguments from the keyboard
331 $$(eval $$(call PARSE_ALL_SUBPROJECTS))
332 # Otherwise, make all keymaps, again this is consistent with how it works without
335 $$(eval $$(call PARSE_ALL_KEYMAPS))
338 # As earlier mentione,d when allsb is specified, we call our self recursively
339 # for all of the subprojects
340 $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$(SUBPROJECTS)))
344 # If we want to parse all subprojects, but the keyboard doesn't have any,
345 # then use defaultsp instead
346 define PARSE_ALL_SUBPROJECTS
347 ifeq ($$(SUBPROJECTS),)
348 $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
350 $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$$(SUBPROJECTS)))
355 # This is the meat of compiling a keyboard, when entering this, everything is known
356 # keyboard, subproject, and keymap
357 # Note that we are not directly calling the command here, but instead building a list,
358 # which will later be processed
361 # The rest of the rule is the target
362 # Remove the leading "-" from the target, as it acts as a separator
363 MAKE_TARGET := $$(patsubst -%,%,$$(RULE))
364 # We need to generate an unique indentifer to append to the COMMANDS list
365 COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM)
366 # If we are compiling a keyboard without a subproject, we want to display just the name
367 # of the keyboard, otherwise keyboard/subproject
368 ifeq ($$(CURRENT_SP),)
369 KB_SP := $(CURRENT_KB)
371 KB_SP := $(CURRENT_KB)/$$(CURRENT_SP)
374 KB_SP := $(BOLD)$$(KB_SP)$(NO_COLOR)
375 # Specify the variables that we are passing forward to submake
376 MAKE_VARS := KEYBOARD=$$(CURRENT_KB) SUBPROJECT=$$(CURRENT_SP) KEYMAP=$$(CURRENT_KM)
377 # And the first part of the make command
378 MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_keyboard.mk $$(MAKE_TARGET)
379 # The message to display
380 MAKE_MSG := $$(MSG_MAKE_KB)
381 # We run the command differently, depending on if we want more output or not
382 # The true version for silent output and the false version otherwise
383 $$(eval $$(call BUILD))
387 MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
388 COMMANDS += $$(COMMAND)
389 COMMAND_true_$$(COMMAND) := \
390 printf "$$(MAKE_MSG)" | \
391 $$(MAKE_MSG_FORMAT); \
392 LOG=$$$$($$(MAKE_CMD) $$(MAKE_VARS) SILENT=true 2>&1) ; \
393 if [ $$$$? -gt 0 ]; \
394 then $$(PRINT_ERROR_PLAIN); \
395 elif [ "$$$$LOG" != "" ] ; \
396 then $$(PRINT_WARNING_PLAIN); \
400 COMMAND_false_$$(COMMAND) := \
401 printf "$$(MAKE_MSG)\n\n"; \
402 $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false; \
403 if [ $$$$? -gt 0 ]; \
404 then error_occured=1; \
408 # Just parse all the keymaps for a specifc keyboard
409 define PARSE_ALL_KEYMAPS
410 $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
417 MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_test.mk $$(MAKE_TARGET)
418 MAKE_VARS := TEST=$$(TEST_NAME)
419 MAKE_MSG := $$(MSG_MAKE_TEST)
420 $$(eval $$(call BUILD))
421 ifneq ($$(MAKE_TARGET),clean)
422 TEST_EXECUTABLE := $$(TEST_DIR)/$$(TEST_NAME).elf
423 TESTS += $$(TEST_NAME)
424 TEST_MSG := $$(MSG_TEST)
425 $$(TEST_NAME)_COMMAND := \
426 printf "$$(TEST_MSG)\n"; \
427 $$(TEST_EXECUTABLE); \
428 if [ $$$$? -gt 0 ]; \
429 then error_occured=1; \
437 TEST_NAME := $$(firstword $$(subst -, ,$$(RULE)))
438 TEST_TARGET := $$(subst $$(TEST_NAME),,$$(subst $$(TEST_NAME)-,,$$(RULE)))
439 ifeq ($$(TEST_NAME),all)
440 MATCHED_TESTS := $$(TEST_LIST)
442 MATCHED_TESTS := $$(foreach TEST,$$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME),$$(TEST)),$$(TEST),))
444 $$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
448 # Set the silent mode depending on if we are trying to compile multiple keyboards or not
449 # By default it's on in that case, but it can be overriden by specifying silent=false
450 # from the command line
451 define SET_SILENT_MODE
453 SILENT_MODE := $(SUB_IS_SILENT)
454 else ifeq ($$(words $$(COMMANDS)),1)
461 include $(ROOT_DIR)/message.mk
464 $(COMMAND_$(SILENT_MODE)_$(COMMAND))
466 # Allow specifying just the subproject, in the keyboard directory, which will compile all keymaps
467 SUBPROJECTS := $(notdir $(patsubst %/Makefile,%,$(wildcard ./*/Makefile)))
468 .PHONY: $(SUBPROJECTS)
469 $(SUBPROJECTS): %: %-allkm
471 # Let's match everything, we handle all the rule parsing ourselves
474 # Check if we have the CMP tool installed
475 cmp --version >/dev/null 2>&1; if [ $$? -gt 0 ]; then printf "$(MSG_NO_CMP)"; exit 1; fi;
476 # Check if the submodules are dirty, and display a warning if they are
477 git submodule status --recursive 2>/dev/null | \
478 while IFS= read -r x; do \
481 *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
484 $(eval $(call PARSE_RULE,$@))
485 $(eval $(call SET_SILENT_MODE))
486 # Run all the commands in the same shell, notice the + at the first line
487 # it has to be there to allow parallel execution of the submake
488 # This always tries to compile everything, even if error occurs in the middle
489 # But we return the error code at the end, to trigger travis failures
491 $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND)) \
492 if [ $$error_occured -gt 0 ]; then printf "$(MSG_ERRORS)" & exit $$error_occured; fi;\
493 $(foreach TEST,$(TESTS),$($(TEST)_COMMAND)) \
494 if [ $$error_occured -gt 0 ]; then printf "$(MSG_ERRORS)" & exit $$error_occured; fi;\
496 # All should compile everything
498 all: all-keyboards test-all
500 # Define some shortcuts, mostly for compability with the old syntax
501 .PHONY: all-keyboards
502 all-keyboards: allkb-allsp-allkm
504 .PHONY: all-keyboards-defaults
505 all-keyboards-defaults: allkb-allsp-default
511 test-clean: test-all-clean
513 # Generate the version.h file
514 GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
515 BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
516 $(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
517 $(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
519 include $(ROOT_DIR)/testlist.mk