]> git.donarmstrong.com Git - qmk_firmware.git/blob - Makefile
d70ac9a3b8f70355ccdbc9f49497bfb6e52fa2c3
[qmk_firmware.git] / Makefile
1 ifndef VERBOSE
2 .SILENT:
3 endif
4
5 # Allow the silent with lower caps to work the same way as upper caps
6 ifdef silent
7     SILENT = $(silent)
8 endif
9
10 ifdef SILENT
11     SUB_IS_SILENT := $(SILENT)
12 endif
13
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
17
18 ON_ERROR := error_occured=1
19
20 STARTING_MAKEFILE := $(firstword $(MAKEFILE_LIST))
21 ROOT_MAKEFILE := $(lastword $(MAKEFILE_LIST))
22 ROOT_DIR := $(dir $(ROOT_MAKEFILE))
23 ifeq ($(ROOT_DIR),)
24     ROOT_DIR := .
25 endif
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
32 MAKEFILE_INCLUDED=yes
33
34 # Helper function to process the newt element of a space separated path 
35 # It works a bit like the traditional functional head tail
36 # so the CURRENT_PATH_ELEMENT will beome the new head
37 # and the PATH_ELEMENTS are the rest that are still unprocessed
38 define NEXT_PATH_ELEMENT
39     $$(eval CURRENT_PATH_ELEMENT := $$(firstword  $$(PATH_ELEMENTS)))
40     $$(eval PATH_ELEMENTS := $$(wordlist  2,9999,$$(PATH_ELEMENTS)))
41 endef
42
43 # We change the / to spaces so that we more easily can work with the elements 
44 # separately
45 PATH_ELEMENTS := $(subst /, ,$(STARTING_DIR))
46 # Initialize the path elements list for further processing
47 $(eval $(call NEXT_PATH_ELEMENT))
48
49 # This function sets the KEYBOARD; KEYMAP and SUBPROJECT to the correct 
50 # variables depending on which directory you stand in.
51 # It's really a very simple if else chain, if you squint enough, 
52 # but the makefile syntax makes it very verbose. 
53 # If we are in a subfolder of keyboards
54 ifeq ($(CURRENT_PATH_ELEMENT),keyboards)
55     $(eval $(call NEXT_PATH_ELEMENT))
56     KEYBOARD := $(CURRENT_PATH_ELEMENT)
57     $(eval $(call NEXT_PATH_ELEMENT))
58     # If we are in a subfolder of keymaps, or in other words in a keymap
59     # folder
60     ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
61         $(eval $(call NEXT_PATH_ELEMENT))
62         KEYMAP := $(CURRENT_PATH_ELEMENT)
63      # else if we are not in the keyboard folder itself
64     else ifneq ($(CURRENT_PATH_ELEMENT),)
65         # the we can assume it's a subproject, as no other folders
66         # should have make files in them
67         SUBPROJECT := $(CURRENT_PATH_ELEMENT)
68         $(eval $(call NEXT_PATH_ELEMENT))
69         # if we are inside a keymap folder of a subproject
70         ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
71             $(eval $(call NEXT_PATH_ELEMENT))
72             KEYMAP := $(CURRENT_PATH_ELEMENT)
73         endif
74     endif
75 endif
76
77 # Only consider folders with makefiles, to prevent errors in case there are extra folders
78 KEYBOARDS := $(notdir $(patsubst %/Makefile,%,$(wildcard $(ROOT_DIR)/keyboards/*/Makefile)))
79
80 #Compability with the old make variables, anything you specify directly on the command line
81 # always overrides the detected folders
82 ifdef keyboard
83     KEYBOARD := $(keyboard)
84 endif
85 ifdef sub
86     SUBPROJECT := $(sub)
87 endif
88 ifdef subproject
89     SUBPROJECT := $(subproject)
90 endif
91 ifdef keymap
92     KEYMAP := $(keymap)
93 endif
94
95 # Uncomment these for debugging
96 #$(info Keyboard: $(KEYBOARD))
97 #$(info Keymap: $(KEYMAP))
98 #$(info Subproject: $(SUBPROJECT))
99 #$(info Keyboards: $(KEYBOARDS))
100
101
102 # Set the default goal depening on where we are running make from
103 # this handles the case where you run make without any arguments
104 .DEFAULT_GOAL := all
105 ifneq ($(KEYMAP),)
106     ifeq ($(SUBPROJECT),)
107          # Inside a keymap folder, just build the keymap, with the 
108          # default subproject
109         .DEFAULT_GOAL := $(KEYBOARD)-$(KEYMAP)
110     else
111          # Inside a subproject keyamp folder, build the keymap
112          # for that subproject
113         .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-$(KEYMAP)
114     endif
115 else ifneq ($(SUBPROJECT),)
116      # Inside a subproject folder, build all keymaps for that subproject
117     .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-allkm
118 else ifneq ($(KEYBOARD),)
119      # Inside a keyboard folder, build all keymaps for all subprojects
120      # Note that this is different from the old behaviour, which would
121      # build only the default keymap of the default keyboard
122     .DEFAULT_GOAL := $(KEYBOARD)-allsp-allkm
123 endif
124
125
126 # Compare the start of the RULE variable with the first argument($1)
127 # If the rules equals $1 or starts with $1-, RULE_FOUND is set to true
128 #     and $1 is removed from the RULE variable
129 # Otherwise the RULE_FOUND variable is set to false, and RULE left as it was
130 # The function is a bit tricky, since there's no built in $(startswith) function
131 define COMPARE_AND_REMOVE_FROM_RULE_HELPER
132     ifeq ($1,$$(RULE))
133         RULE:=
134         RULE_FOUND := true
135     else
136         STARTDASH_REMOVED=$$(subst START$1-,,START$$(RULE))
137         ifneq ($$(STARTDASH_REMOVED),START$$(RULE))
138             RULE_FOUND := true
139             RULE := $$(STARTDASH_REMOVED)
140         else
141             RULE_FOUND := false
142         endif
143     endif
144 endef
145
146 # This makes it easier to call COMPARE_AND_REMOVE_FROM_RULE, since it makes it behave like
147 # a function that returns the value
148 COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND)
149
150
151 # Recursively try to find a match for the start of the rule to be checked
152 # $1 The list to be checked
153 # If a match is found, then RULE_FOUND is set to true
154 # and MATCHED_ITEM to the item that was matched
155 define TRY_TO_MATCH_RULE_FROM_LIST_HELPER
156     ifneq ($1,)
157         ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,$$(firstword $1)),true)
158             MATCHED_ITEM := $$(firstword $1)
159         else 
160             $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$$(wordlist 2,9999,$1)))
161         endif
162     endif
163 endef
164
165 # Make it easier to call TRY_TO_MATCH_RULE_FROM_LIST
166 TRY_TO_MATCH_RULE_FROM_LIST = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$1))$(RULE_FOUND)
167
168 define ALL_IN_LIST_LOOP
169     OLD_RULE$1 := $$(RULE)
170     $$(eval $$(call $1,$$(ITEM$1)))
171     RULE := $$(OLD_RULE$1)
172 endef
173
174 define PARSE_ALL_IN_LIST
175     $$(foreach ITEM$1,$2,$$(eval $$(call ALL_IN_LIST_LOOP,$1)))
176 endef
177
178 # The entry point for rule parsing
179 # parses a rule in the format <keyboard>-<subproject>-<keymap>-<target>
180 # but this particular function only deals with the first <keyboard> part
181 define PARSE_RULE
182     RULE := $1
183     COMMANDS :=
184     # If the rule starts with allkb, then continue the parsing from
185     # PARSE_ALL_KEYBOARDS
186     ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkb),true)
187         $$(eval $$(call PARSE_ALL_KEYBOARDS))
188     # If the rule starts with the name of a known keyboard, then continue
189     # the parsing from PARSE_KEYBOARD
190     else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYBOARDS)),true)
191         $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM)))
192     # Otherwise use the KEYBOARD variable, which is determined either by
193     # the current directory you run make from, or passed in as an argument
194     else ifneq ($$(KEYBOARD),)
195         $$(eval $$(call PARSE_KEYBOARD,$$(KEYBOARD)))
196     else
197         $$(info make: *** No rule to make target '$1'. Stop.)
198         # Notice the tab instead of spaces below!
199                 exit 1
200     endif
201 endef
202
203 # $1 = Keyboard
204 # Parses a rule in the format <subproject>-<keymap>-<target>
205 # the keyboard is already known when entering this function
206 define PARSE_KEYBOARD
207     CURRENT_KB := $1
208     # A subproject is any keyboard subfolder with a makefile
209     SUBPROJECTS := $$(notdir $$(patsubst %/Makefile,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/*/Makefile)))
210     # if the rule starts with allsp, then continue with looping over all subprojects
211     ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allsp),true)
212         $$(eval $$(call PARSE_ALL_SUBPROJECTS))
213     # A special case for matching the defaultsp (default subproject)
214     else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,defaultsp),true)
215         $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
216     # If the rule starts with the name of a known subproject
217     else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(SUBPROJECTS)),true)
218         $$(eval $$(call PARSE_SUBPROJECT,$$(MATCHED_ITEM)))
219     # Try to use the SUBPROJECT variable, which is either determined by the
220     # directory which invoked make, or passed as an argument to make
221     else ifneq ($$(SUBPROJECT),)
222         $$(eval $$(call PARSE_SUBPROJECT,$$(SUBPROJECT)))
223         # If there's no matching subproject, we assume it's the default
224         # This will allow you to leave the subproject part of the target out
225     else 
226         $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
227     endif
228 endef
229
230 # if we are going to compile all keyboards, match the rest of the rule
231 # for each of them
232 define PARSE_ALL_KEYBOARDS
233     $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(KEYBOARDS)))
234 endef
235
236 # $1 Subproject
237 # When entering this, the keyboard and subproject are known, so now we need
238 # to determine which keymaps are going to get compiled
239 define PARSE_SUBPROJECT
240     # If we want to compile the default subproject, then we need to 
241     # include the correct makefile to determine the actual name of it
242     ifeq ($1,defaultsp)
243         SUBPROJECT_DEFAULT=
244         $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/Makefile)
245         CURRENT_SP := $$(SUBPROJECT_DEFAULT)
246     else
247         CURRENT_SP := $1
248     endif
249     # If current subproject is empty (the default was not defined), and we have a list of subproject
250     # then make all of them
251     ifeq ($$(CURRENT_SP),)
252         ifneq ($$(SUBPROJECTS),)
253             CURRENT_SP := allsp
254          endif
255     endif
256     # The special allsp is handled later
257     ifneq ($$(CURRENT_SP),allsp) 
258         # get a list of all keymaps
259         KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/keymaps/*/.)))
260         ifneq ($$(CURRENT_SP),)
261             # if the subproject is defined, then also look for keymaps inside the subproject folder
262             SP_KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/$$(CURRENT_SP)/keymaps/*/.)))
263             KEYMAPS := $$(sort $$(KEYMAPS) $$(SP_KEYMAPS))
264         endif
265         # if the rule after removing the start of it is empty (we haven't specified a kemap or target)
266         # compile all the keymaps
267         ifeq ($$(RULE),)
268             $$(eval $$(call PARSE_ALL_KEYMAPS))
269         # The same if allkm was specified
270         else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkm),true)
271             $$(eval $$(call PARSE_ALL_KEYMAPS))
272         # Try to match the specified keyamp with the list of known keymaps
273         else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYMAPS)),true)
274             $$(eval $$(call PARSE_KEYMAP,$$(MATCHED_ITEM)))
275         # Otherwise try to match the keymap from the current folder, or arguments to the make command
276         else ifneq ($$(KEYMAP),)
277             $$(eval $$(call PARSE_KEYMAP,$$(KEYMAP)))
278         else
279             # Otherwise something is wrong with the target
280             # Try to give as much information as possible of what it it was trying to do
281             ifeq ($$(CURRENT_SP),)
282                 $$(info make: *** No rule to make target '$$(CURRENT_KB)-$$(RULE)'. Stop.)
283             else
284                 $$(info make: *** No rule to make target '$$(CURRENT_KB)-$$(CURRENT_SP)-$$(RULE)'. Stop.)
285             endif
286             # Notice the tab instead of spaces below!
287                         exit 1
288         endif
289     else
290         # As earlier mentione,d when allsb is specified, we call our self recursively
291         # for all of the subprojects
292         $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$(SUBPROJECTS)))
293     endif
294 endef
295
296 # If we want to parse all subprojects, but the keyboard doesn't have any, 
297 # then use defaultsp instead
298 define PARSE_ALL_SUBPROJECTS
299     ifeq ($$(SUBPROJECTS),)
300         $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
301     else
302         $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$$(SUBPROJECTS)))
303     endif
304 endef
305
306 # $1 Keymap
307 # This is the meat of compiling a keyboard, when entering this, everything is known
308 # keyboard, subproject, and keymap
309 # Note that we are not directly calling the command here, but instead building a list,
310 # which will later be processed
311 define PARSE_KEYMAP
312     CURRENT_KM = $1
313     # The rest of the rule is the target
314     # Remove the leading "-" from the target, as it acts as a separator
315     MAKE_TARGET := $$(patsubst -%,%,$$(RULE))
316     # We need to generate an unique indentifer to append to the COMMANDS list
317     COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM)
318     COMMANDS += $$(COMMAND)
319     # If we are compiling a keyboard without a subproject, we want to display just the name
320     # of the keyboard, otherwise keyboard/subproject
321     ifeq ($$(CURRENT_SP),)
322         KB_SP := $(CURRENT_KB)
323     else
324         KB_SP := $(CURRENT_KB)/$$(CURRENT_SP)
325     endif
326     # Format it in bold
327     KB_SP := $(BOLD)$$(KB_SP)$(NO_COLOR)
328     # Specify the variables that we are passing forward to submake
329     MAKE_VARS := KEYBOARD=$$(CURRENT_KB) SUBPROJECT=$$(CURRENT_SP) KEYMAP=$$(CURRENT_KM)
330     MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
331     # And the first part of the make command
332     MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_keyboard.mk $$(MAKE_TARGET)
333     # The message to display
334     MAKE_MSG := $$(MSG_MAKE_KB)
335     # We run the command differently, depending on if we want more output or not
336     # The true version for silent output and the false version otherwise
337     COMMAND_true_$$(COMMAND) := \
338         printf "$$(MAKE_MSG)" | \
339         $$(MAKE_MSG_FORMAT); \
340         LOG=$$$$($$(MAKE_CMD) $$(MAKE_VARS) SILENT=true 2>&1) ; \
341         if [ $$$$? -gt 0 ]; \
342             then $$(PRINT_ERROR_PLAIN); \
343         elif [ "$$$$LOG" != "" ] ; \
344             then $$(PRINT_WARNING_PLAIN); \
345         else \
346             $$(PRINT_OK); \
347         fi;
348     COMMAND_false_$$(COMMAND) := \
349         printf "$$(MAKE_MSG)\n\n"; \
350         $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false;
351 endef
352
353 # Just parse all the keymaps for a specifc keyboard
354 define PARSE_ALL_KEYMAPS
355     $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
356 endef
357
358 # Set the silent mode depending on if we are trying to compile multiple keyboards or not
359 # By default it's on in that case, but it can be overriden by specifying silent=false 
360 # from the command line
361 define SET_SILENT_MODE
362     ifdef SUB_IS_SILENT
363         SILENT_MODE := $(SUB_IS_SILENT)
364     else ifeq ($$(words $$(COMMANDS)),1)
365         SILENT_MODE := false
366     else
367         SILENT_MODE := true
368     endif
369 endef
370
371 include $(ROOT_DIR)/message.mk
372
373 RUN_COMMAND = \
374 $(COMMAND_$(SILENT_MODE)_$(COMMAND))
375
376 # Allow specifying just the subproject, in the keyboard directory, which will compile all keymaps
377 SUBPROJECTS := $(notdir $(patsubst %/Makefile,%,$(wildcard ./*/Makefile)))
378 .PHONY: $(SUBPROJECTS)
379 $(SUBPROJECTS): %: %-allkm 
380
381 # Let's match everything, we handle all the rule parsing ourselves
382 .PHONY: %
383 %: 
384         # Check if we have the CMP tool installed
385         cmp --version >/dev/null 2>&1; if [ $$? -gt 0 ]; then printf "$(MSG_NO_CMP)"; exit 1; fi;
386         # Check if the submodules are dirty, and display a warning if they are
387         git submodule status --recursive 2>/dev/null | \
388         while IFS= read -r x; do \
389                 case "$$x" in \
390                         \ *) ;; \
391                         *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
392                 esac \
393         done
394         $(eval $(call PARSE_RULE,$@))
395         $(eval $(call SET_SILENT_MODE))
396         # Run all the commands in the same shell, notice the + at the first line
397         # it has to be there to allow parallel execution of the submake
398         # This always tries to compile everything, even if error occurs in the middle
399         # But we return the error code at the end, to trigger travis failures
400         +error_occured=0; \
401         $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND)) \
402         if [ $$error_occured -gt 0 ]; then printf "$(MSG_ERRORS)" & exit $$error_occured; fi
403         
404
405 # All should compile everything
406 .PHONY: all
407 all: all-keyboards 
408
409 # Define some shortcuts, mostly for compability with the old syntax
410 .PHONY: all-keyboards
411 all-keyboards: allkb-allsp-allkm
412
413 .PHONY: all-keyboards-defaults
414 all-keyboards-defaults: allkb-allsp-default
415
416
417 # Generate the version.h file
418 GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
419 BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
420 $(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
421 $(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)