]> git.donarmstrong.com Git - qmk_firmware.git/blob - Makefile
Rule parsing to match kebyoards
[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 define NEXT_PATH_ELEMENT
16     $$(eval CURRENT_PATH_ELEMENT := $$(firstword  $$(PATH_ELEMENTS)))
17     $$(eval PATH_ELEMENTS := $$(wordlist  2,9999,$$(PATH_ELEMENTS)))
18 endef
19
20 $(eval $(call NEXT_PATH_ELEMENT))
21
22 ifeq ($(CURRENT_PATH_ELEMENT),keyboards)
23     $(eval $(call NEXT_PATH_ELEMENT))
24     KEYBOARD := $(CURRENT_PATH_ELEMENT)
25     $(eval $(call NEXT_PATH_ELEMENT))
26     ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
27         $(eval $(call NEXT_PATH_ELEMENT))
28         KEYMAP := $(CURRENT_PATH_ELEMENT)
29     else ifneq ($(CURRENT_PATH_ELEMENT),)
30         SUBPROJECT := $(CURRENT_PATH_ELEMENT)
31         $(eval $(call NEXT_PATH_ELEMENT))
32         ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
33             $(eval $(call NEXT_PATH_ELEMENT))
34             KEYMAP := $(CURRENT_PATH_ELEMENT)
35         endif
36     endif
37 endif
38
39 $(info $(ROOT_DIR)/keyboards)
40 KEYBOARDS := $(notdir $(patsubst %/.,%,$(wildcard $(ROOT_DIR)/keyboards/*/.)))
41
42 $(info Keyboard: $(KEYBOARD))
43 $(info Keymap: $(KEYMAP))
44 $(info Subproject: $(SUBPROJECT))
45 $(info Keyboards: $(KEYBOARDS))
46
47
48 # Compare the start of the RULE_VARIABLE with the first argument($1)
49 # If the rules equals $1 or starts with $1-, RULE_FOUND is set to true
50 #     and $1 is removed from the RULE variable
51 # Otherwise the RULE_FOUND variable is set to false
52 # The function is a bit tricky, since there's no built in $(startswith) function
53 define COMPARE_AND_REMOVE_FROM_RULE
54     ifeq ($1,$$(RULE))
55         RULE:=
56         RULE_FOUND := true
57     else
58         STARTDASH_REMOVED=$$(subst START$1-,,START$$(RULE))
59         ifneq ($$(STARTDASH_REMOVED),START$$(RULE))
60             RULE_FOUND := true
61             RULE := $$(STARTDASH_REMOVED)
62         else
63             RULE_FOUND := false
64         endif
65     endif
66 endef
67
68 define PARSE_ALL_KEYBOARDS
69     COMMANDS += allkb
70     #$$(info $$(RULE))
71     COMMAND_allkb := "All keyboards with $$(RULE)"
72 endef
73
74 define PARSE_KEYBOARD
75     COMMANDS += $1
76     #$$(info $$(RULE))
77     COMMAND_$1 := "Keyboard $1 with $$(RULE)"
78 endef
79
80
81 # Recursively try to find a matching keyboard
82 # During the first call $1 contains a list of all keyboards
83 # One keyboard is checked and removed at a time
84 define TRY_PARSE_KEYBOARD
85     CURRENT_KB := $$(firstword $1)
86     $$(eval $$(call COMPARE_AND_REMOVE_FROM_RULE,$$(CURRENT_KB)))
87     ifeq ($$(RULE_FOUND),true)
88         $$(eval $$(call PARSE_KEYBOARD,$$(CURRENT_KB)))
89     else ifneq ($1,)
90         $$(eval $$(call TRY_PARSE_KEYBOARD,$$(wordlist 2,9999,$1)))
91     endif
92 endef
93
94 define PARSE_RULE
95     RULE := $1
96     COMMANDS :=
97     $$(eval $$(call COMPARE_AND_REMOVE_FROM_RULE,allkb))
98     ifeq ($$(RULE_FOUND),true)
99         $$(eval $$(call PARSE_ALL_KEYBOARDS))
100     else
101         $$(eval $$(call TRY_PARSE_KEYBOARD,$(KEYBOARDS)))
102     endif
103 endef
104
105 RUN_COMMAND = echo "Running": $(COMMAND_$(COMMAND));
106
107 .PHONY: %
108 %:
109         $(eval $(call PARSE_RULE,$@))
110         $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND))
111
112 .PHONY: all-keyboards
113 all-keyboards: allkb
114
115 .PHONY: all-keyboards-defaults
116 all-keyboards-defaults: allkb-default-default
117
118 .PHONY: all
119 all: 
120         echo "Compiling"