]> git.donarmstrong.com Git - qmk_firmware.git/blob - Makefile
Proper handling for running make from a subproject or keymap dir
[qmk_firmware.git] / Makefile
1 STARTING_MAKEFILE := $(firstword $(MAKEFILE_LIST))
2 ROOT_MAKEFILE := $(lastword $(MAKEFILE_LIST))
3 ROOT_DIR := $(dir $(ROOT_MAKEFILE))
4 ifeq ($(ROOT_DIR),)
5     ROOT_DIR := .
6 endif
7 ABS_STARTING_MAKEFILE := $(abspath $(STARTING_MAKEFILE))
8 ABS_ROOT_MAKEFILE := $(abspath $(ROOT_MAKEFILE))
9 ABS_STARTING_DIR := $(dir $(ABS_STARTING_MAKEFILE))
10 ABS_ROOT_DIR := $(dir $(ABS_ROOT_MAKEFILE))
11 STARTING_DIR := $(subst $(ABS_ROOT_DIR),,$(ABS_STARTING_DIR))
12
13 PATH_ELEMENTS := $(subst /, ,$(STARTING_DIR))
14
15 MAKEFILE_INCLUDED=yes
16
17 define NEXT_PATH_ELEMENT
18     $$(eval CURRENT_PATH_ELEMENT := $$(firstword  $$(PATH_ELEMENTS)))
19     $$(eval PATH_ELEMENTS := $$(wordlist  2,9999,$$(PATH_ELEMENTS)))
20 endef
21
22 $(eval $(call NEXT_PATH_ELEMENT))
23
24 ifeq ($(CURRENT_PATH_ELEMENT),keyboards)
25     $(eval $(call NEXT_PATH_ELEMENT))
26     KEYBOARD := $(CURRENT_PATH_ELEMENT)
27     $(eval $(call NEXT_PATH_ELEMENT))
28     ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
29         $(eval $(call NEXT_PATH_ELEMENT))
30         KEYMAP := $(CURRENT_PATH_ELEMENT)
31     else ifneq ($(CURRENT_PATH_ELEMENT),)
32         SUBPROJECT := $(CURRENT_PATH_ELEMENT)
33         $(eval $(call NEXT_PATH_ELEMENT))
34         ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
35             $(eval $(call NEXT_PATH_ELEMENT))
36             KEYMAP := $(CURRENT_PATH_ELEMENT)
37         endif
38     endif
39 endif
40
41 $(info $(ROOT_DIR)/keyboards)
42 # Only consider folders with makefiles, to prevent errors in case there are extra folders
43 KEYBOARDS := $(notdir $(patsubst %/Makefile,%,$(wildcard $(ROOT_DIR)/keyboards/*/Makefile)))
44
45 $(info Keyboard: $(KEYBOARD))
46 $(info Keymap: $(KEYMAP))
47 $(info Subproject: $(SUBPROJECT))
48 $(info Keyboards: $(KEYBOARDS))
49
50
51 # Compare the start of the RULE_VARIABLE with the first argument($1)
52 # If the rules equals $1 or starts with $1-, RULE_FOUND is set to true
53 #     and $1 is removed from the RULE variable
54 # Otherwise the RULE_FOUND variable is set to false
55 # The function is a bit tricky, since there's no built in $(startswith) function
56 define COMPARE_AND_REMOVE_FROM_RULE_HELPER
57     ifeq ($1,$$(RULE))
58         RULE:=
59         RULE_FOUND := true
60     else
61         STARTDASH_REMOVED=$$(subst START$1-,,START$$(RULE))
62         ifneq ($$(STARTDASH_REMOVED),START$$(RULE))
63             RULE_FOUND := true
64             RULE := $$(STARTDASH_REMOVED)
65         else
66             RULE_FOUND := false
67         endif
68     endif
69 endef
70
71 COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND)
72
73
74 # Recursively try to find a match
75 # $1 The list to be checked
76 # If a match is found, then RULE_FOUND is set to true
77 # and MATCHED_ITEM to the item that was matched
78 define TRY_TO_MATCH_RULE_FROM_LIST_HELPER
79     ifneq ($1,)
80         ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,$$(firstword $1)),true)
81             MATCHED_ITEM := $$(firstword $1)
82         else 
83             $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$$(wordlist 2,9999,$1)))
84         endif
85     endif
86 endef
87
88 TRY_TO_MATCH_RULE_FROM_LIST = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$1))$(RULE_FOUND)
89
90 define ALL_IN_LIST_LOOP
91     OLD_RULE$1 := $$(RULE)
92     $$(eval $$(call $1,$$(ITEM$1)))
93     RULE := $$(OLD_RULE$1)
94 endef
95
96 define PARSE_ALL_IN_LIST
97     $$(foreach ITEM$1,$2,$$(eval $$(call ALL_IN_LIST_LOOP,$1)))
98 endef
99
100 define PARSE_RULE
101     RULE := $1
102     COMMANDS :=
103     ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkb),true)
104         $$(eval $$(call PARSE_ALL_KEYBOARDS))
105     else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYBOARDS)),true)
106         $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM)))
107     else ifneq ($$(KEYBOARD),)
108         # If there's no match in the beginning, then use the working directory instead
109         # First add the keymap to the commandline if we are in a keymap subdirectory
110         ifneq ($$(KEYMAP),)
111             RULE := $$(KEYMAP)-$$(RULE)
112         endif
113         # If we are in a subproject subdirectory add the subproject
114         ifneq ($$(SUBPROJECT),)
115             RULE := $$(SUBPROJECT)-$$(RULE)
116         endif
117         $$(eval $$(call PARSE_KEYBOARD,$$(KEYBOARD)))
118     else
119         $$(info make: *** No rule to make target '$1'. Stop.)
120                 exit 1
121     endif
122 endef
123
124 # $1 = Keyboard
125 define PARSE_KEYBOARD
126     CURRENT_KB := $1
127     # A subproject is any keyboard subfolder with a makefile
128     SUBPROJECTS := $$(notdir $$(patsubst %/Makefile,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/*/Makefile)))
129     ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allsp),true)
130         $$(eval $$(call PARSE_ALL_SUBPROJECTS))
131     else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,defaultsp),true)
132         $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
133     else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(SUBPROJECTS)),true)
134         $$(eval $$(call PARSE_SUBPROJECT,$$(MATCHED_ITEM)))
135     else 
136         # If there's no matching subproject, we assume it's the default
137         # This will allow you to leave the subproject part of the target out
138         $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
139     endif
140 endef
141
142 define PARSE_ALL_KEYBOARDS
143     $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(KEYBOARDS)))
144 endef
145
146 # $1 Subproject
147 define PARSE_SUBPROJECT
148     ifeq ($1,defaultsp)
149         SUBPROJECT_DEFAULT=
150         $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/Makefile)
151         CURRENT_SP := $$(SUBPROJECT_DEFAULT)
152     else
153         CURRENT_SP := $1
154     endif
155     # If current subproject is empty (the default was not defined), and we have a list of subproject
156     # then make all
157     ifeq ($$(CURRENT_SP),)
158         ifneq ($$(SUBPROJECTS),)
159             CURRENT_SP := allsp
160          endif
161     endif
162     ifneq ($$(CURRENT_SP),allsp) 
163         KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/keymaps/*/.)))
164         ifneq ($$(CURRENT_SP),)
165             SP_KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/$$(CURRENT_SP)/keymaps/*/.)))
166             KEYMAPS := $$(sort $$(KEYMAPS) $$(SP_KEYMAPS))
167         endif
168         ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkm),true)
169             $$(eval $$(call PARSE_ALL_KEYMAPS))
170         else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYMAPS)),true)
171             $$(eval $$(call PARSE_KEYMAP,$$(MATCHED_ITEM)))
172         else
173             ifeq ($$(CURRENT_SP),)
174                 $$(info make: *** No rule to make target '$$(CURRENT_KB)-$$(RULE)'. Stop.)
175             else
176                 $$(info make: *** No rule to make target '$$(CURRENT_KB)-$$(CURRENT_SP)-$$(RULE)'. Stop.)
177             endif
178                         exit 1
179         endif
180     else
181         $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$(SUBPROJECTS)))
182     endif
183 endef
184
185 define PARSE_ALL_SUBPROJECTS
186     ifeq ($$(SUBPROJECTS),)
187         $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
188     else
189         $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$$(SUBPROJECTS)))
190     endif
191 endef
192
193 # $1 Keymap
194 define PARSE_KEYMAP
195     CURRENT_KM = $1
196     COMMANDS += KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM)
197     COMMAND_KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM) := Keyboard $$(CURRENT_KB), Subproject $$(CURRENT_SP), Keymap $$(CURRENT_KM)
198 endef
199
200 define PARSE_ALL_KEYMAPS
201     $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
202 endef
203
204 RUN_COMMAND = echo "Running": $(COMMAND_$(COMMAND));
205
206 .PHONY: %
207 %:
208         $(eval $(call PARSE_RULE,$@))
209         $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND))
210
211 .PHONY: all-keyboards
212 all-keyboards: allkb
213
214 .PHONY: all-keyboards-defaults
215 all-keyboards-defaults: allkb-default-default
216
217 .PHONY: all
218 all: 
219         echo "Compiling"