Makefile 7.5 KB

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