Makefile 16 KB

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