Makefile 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. ifndef VERBOSE
  2. .SILENT:
  3. endif
  4. # Never run this makefile in parallel, as it could screw things up
  5. # It won't affect the submakes, so you still get the speedup from specifying -jx
  6. .NOTPARALLEL:
  7. # Allow the silent with lower caps to work the same way as upper caps
  8. ifdef silent
  9. SILENT = $(silent)
  10. endif
  11. ifdef SILENT
  12. SUB_IS_SILENT := $(SILENT)
  13. endif
  14. # We need to make sure that silent is always turned off at the top level
  15. # Otherwise the [OK], [ERROR] and [WARN] messages won't be displayed correctly
  16. override SILENT := false
  17. QMK_VERSION := $(shell git describe --abbrev=0 --tags 2>/dev/null)
  18. ifneq ($(QMK_VERSION),)
  19. ifeq ($(NO_VERSION),)
  20. $(info QMK Firmware v$(QMK_VERSION))
  21. endif
  22. endif
  23. ON_ERROR := error_occurred=1
  24. BREAK_ON_ERRORS = no
  25. STARTING_MAKEFILE := $(firstword $(MAKEFILE_LIST))
  26. ROOT_MAKEFILE := $(lastword $(MAKEFILE_LIST))
  27. ROOT_DIR := $(dir $(ROOT_MAKEFILE))
  28. ifeq ($(ROOT_DIR),)
  29. ROOT_DIR := .
  30. endif
  31. ABS_STARTING_MAKEFILE := $(abspath $(STARTING_MAKEFILE))
  32. ABS_ROOT_MAKEFILE := $(abspath $(ROOT_MAKEFILE))
  33. ABS_STARTING_DIR := $(dir $(ABS_STARTING_MAKEFILE))
  34. ABS_ROOT_DIR := $(dir $(ABS_ROOT_MAKEFILE))
  35. STARTING_DIR := $(subst $(ABS_ROOT_DIR),,$(ABS_STARTING_DIR))
  36. BUILD_DIR := $(ROOT_DIR)/.build
  37. TEST_DIR := $(BUILD_DIR)/test
  38. ERROR_FILE := $(BUILD_DIR)/error_occurred
  39. MAKEFILE_INCLUDED=yes
  40. # Helper function to process the newt element of a space separated path
  41. # It works a bit like the traditional functional head tail
  42. # so the CURRENT_PATH_ELEMENT will become the new head
  43. # and the PATH_ELEMENTS are the rest that are still unprocessed
  44. define NEXT_PATH_ELEMENT
  45. $$(eval CURRENT_PATH_ELEMENT := $$(firstword $$(PATH_ELEMENTS)))
  46. $$(eval PATH_ELEMENTS := $$(wordlist 2,9999,$$(PATH_ELEMENTS)))
  47. endef
  48. # We change the / to spaces so that we more easily can work with the elements
  49. # separately
  50. PATH_ELEMENTS := $(subst /, ,$(STARTING_DIR))
  51. # Initialize the path elements list for further processing
  52. $(eval $(call NEXT_PATH_ELEMENT))
  53. # This function sets the KEYBOARD; KEYMAP and SUBPROJECT to the correct
  54. # variables depending on which directory you stand in.
  55. # It's really a very simple if else chain, if you squint enough,
  56. # but the makefile syntax makes it very verbose.
  57. # If we are in a subfolder of keyboards
  58. ifeq ($(CURRENT_PATH_ELEMENT),keyboards)
  59. $(eval $(call NEXT_PATH_ELEMENT))
  60. KEYBOARD := $(CURRENT_PATH_ELEMENT)
  61. $(eval $(call NEXT_PATH_ELEMENT))
  62. # If we are in a subfolder of keymaps, or in other words in a keymap
  63. # folder
  64. ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
  65. $(eval $(call NEXT_PATH_ELEMENT))
  66. KEYMAP := $(CURRENT_PATH_ELEMENT)
  67. # else if we are not in the keyboard folder itself
  68. else ifneq ($(CURRENT_PATH_ELEMENT),)
  69. # the we can assume it's a subproject, as no other folders
  70. # should have make files in them
  71. SUBPROJECT := $(CURRENT_PATH_ELEMENT)
  72. $(eval $(call NEXT_PATH_ELEMENT))
  73. # if we are inside a keymap folder of a subproject
  74. ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
  75. $(eval $(call NEXT_PATH_ELEMENT))
  76. KEYMAP := $(CURRENT_PATH_ELEMENT)
  77. endif
  78. endif
  79. endif
  80. # Only consider folders with makefiles, to prevent errors in case there are extra folders
  81. KEYBOARDS := $(notdir $(patsubst %/rules.mk,%,$(wildcard $(ROOT_DIR)/keyboards/*/rules.mk)))
  82. #Compatibility with the old make variables, anything you specify directly on the command line
  83. # always overrides the detected folders
  84. ifdef keyboard
  85. KEYBOARD := $(keyboard)
  86. endif
  87. ifdef sub
  88. SUBPROJECT := $(sub)
  89. endif
  90. ifdef subproject
  91. SUBPROJECT := $(subproject)
  92. endif
  93. ifdef keymap
  94. KEYMAP := $(keymap)
  95. endif
  96. # Uncomment these for debugging
  97. #$(info Keyboard: $(KEYBOARD))
  98. #$(info Keymap: $(KEYMAP))
  99. #$(info Subproject: $(SUBPROJECT))
  100. #$(info Keyboards: $(KEYBOARDS))
  101. # Set the default goal depending on where we are running make from
  102. # this handles the case where you run make without any arguments
  103. .DEFAULT_GOAL := all
  104. ifneq ($(KEYMAP),)
  105. ifeq ($(SUBPROJECT),)
  106. # Inside a keymap folder, just build the keymap, with the
  107. # default subproject
  108. .DEFAULT_GOAL := $(KEYBOARD)-$(KEYMAP)
  109. else
  110. # Inside a subproject keyamp folder, build the keymap
  111. # for that subproject
  112. .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-$(KEYMAP)
  113. endif
  114. else ifneq ($(SUBPROJECT),)
  115. # Inside a subproject folder, build all keymaps for that subproject
  116. .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-allkm
  117. else ifneq ($(KEYBOARD),)
  118. # Inside a keyboard folder, build all keymaps for all subprojects
  119. # Note that this is different from the old behaviour, which would
  120. # build only the default keymap of the default keyboard
  121. .DEFAULT_GOAL := $(KEYBOARD)-allsp-allkm
  122. endif
  123. # Compare the start of the RULE variable with the first argument($1)
  124. # If the rules equals $1 or starts with $1-, RULE_FOUND is set to true
  125. # and $1 is removed from the RULE variable
  126. # Otherwise the RULE_FOUND variable is set to false, and RULE left as it was
  127. # The function is a bit tricky, since there's no built in $(startswith) function
  128. define COMPARE_AND_REMOVE_FROM_RULE_HELPER
  129. ifeq ($1,$$(RULE))
  130. RULE:=
  131. RULE_FOUND := true
  132. else
  133. STARTDASH_REMOVED=$$(subst START$1-,,START$$(RULE))
  134. ifneq ($$(STARTDASH_REMOVED),START$$(RULE))
  135. RULE_FOUND := true
  136. RULE := $$(STARTDASH_REMOVED)
  137. else
  138. RULE_FOUND := false
  139. endif
  140. endif
  141. endef
  142. # This makes it easier to call COMPARE_AND_REMOVE_FROM_RULE, since it makes it behave like
  143. # a function that returns the value
  144. COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND)
  145. # Recursively try to find a match for the start of the rule to be checked
  146. # $1 The list to be checked
  147. # If a match is found, then RULE_FOUND is set to true
  148. # and MATCHED_ITEM to the item that was matched
  149. define TRY_TO_MATCH_RULE_FROM_LIST_HELPER3
  150. ifneq ($1,)
  151. ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,$$(firstword $1)),true)
  152. MATCHED_ITEM := $$(firstword $1)
  153. else
  154. $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$$(wordlist 2,9999,$1)))
  155. endif
  156. endif
  157. endef
  158. # A recursive helper function for finding the longest match
  159. # $1 The list to be checked
  160. # It works by always removing the currently matched item from the list
  161. # and call itself recursively, until a match is found
  162. define TRY_TO_MATCH_RULE_FROM_LIST_HELPER2
  163. # Stop the recursion when the list is empty
  164. ifneq ($1,)
  165. RULE_BEFORE := $$(RULE)
  166. $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$1))
  167. # If a match is found in the current list, otherwise just return what we had before
  168. ifeq ($$(RULE_FOUND),true)
  169. # Save the best match so far and call itself recursively
  170. BEST_MATCH := $$(MATCHED_ITEM)
  171. BEST_MATCH_RULE := $$(RULE)
  172. RULE_FOUND := false
  173. RULE := $$(RULE_BEFORE)
  174. $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$$(filter-out $$(MATCHED_ITEM),$1)))
  175. endif
  176. endif
  177. endef
  178. # Recursively try to find the longest match for the start of the rule to be checked
  179. # $1 The list to be checked
  180. # If a match is found, then RULE_FOUND is set to true
  181. # and MATCHED_ITEM to the item that was matched
  182. define TRY_TO_MATCH_RULE_FROM_LIST_HELPER
  183. BEST_MATCH :=
  184. $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$1))
  185. ifneq ($$(BEST_MATCH),)
  186. RULE_FOUND := true
  187. RULE := $$(BEST_MATCH_RULE)
  188. MATCHED_ITEM := $$(BEST_MATCH)
  189. else
  190. RULE_FOUND := false
  191. MATCHED_ITEM :=
  192. endif
  193. endef
  194. # Make it easier to call TRY_TO_MATCH_RULE_FROM_LIST
  195. TRY_TO_MATCH_RULE_FROM_LIST = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$1))$(RULE_FOUND)
  196. define ALL_IN_LIST_LOOP
  197. OLD_RULE$1 := $$(RULE)
  198. $$(eval $$(call $1,$$(ITEM$1)))
  199. RULE := $$(OLD_RULE$1)
  200. endef
  201. define PARSE_ALL_IN_LIST
  202. $$(foreach ITEM$1,$2,$$(eval $$(call ALL_IN_LIST_LOOP,$1)))
  203. endef
  204. # The entry point for rule parsing
  205. # parses a rule in the format <keyboard>-<subproject>-<keymap>-<target>
  206. # but this particular function only deals with the first <keyboard> part
  207. define PARSE_RULE
  208. RULE := $1
  209. COMMANDS :=
  210. # If the rule starts with allkb, then continue the parsing from
  211. # PARSE_ALL_KEYBOARDS
  212. ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkb),true)
  213. $$(eval $$(call PARSE_ALL_KEYBOARDS))
  214. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,test),true)
  215. $$(eval $$(call PARSE_TEST))
  216. # If the rule starts with the name of a known keyboard, then continue
  217. # the parsing from PARSE_KEYBOARD
  218. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYBOARDS)),true)
  219. $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM)))
  220. # Otherwise use the KEYBOARD variable, which is determined either by
  221. # the current directory you run make from, or passed in as an argument
  222. else ifneq ($$(KEYBOARD),)
  223. $$(eval $$(call PARSE_KEYBOARD,$$(KEYBOARD)))
  224. else
  225. $$(info make: *** No rule to make target '$1'. Stop.)
  226. # Notice the tab instead of spaces below!
  227. exit 1
  228. endif
  229. endef
  230. # $1 = Keyboard
  231. # Parses a rule in the format <subproject>-<keymap>-<target>
  232. # the keyboard is already known when entering this function
  233. define PARSE_KEYBOARD
  234. CURRENT_KB := $1
  235. # A subproject is any keyboard subfolder with a makefile
  236. SUBPROJECTS := $$(notdir $$(patsubst %/rules.mk,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/*/rules.mk)))
  237. # if the rule starts with allsp, then continue with looping over all subprojects
  238. ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allsp),true)
  239. $$(eval $$(call PARSE_ALL_SUBPROJECTS))
  240. # A special case for matching the defaultsp (default subproject)
  241. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,defaultsp),true)
  242. $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
  243. # If the rule starts with the name of a known subproject
  244. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(SUBPROJECTS)),true)
  245. $$(eval $$(call PARSE_SUBPROJECT,$$(MATCHED_ITEM)))
  246. # Try to use the SUBPROJECT variable, which is either determined by the
  247. # directory which invoked make, or passed as an argument to make
  248. else ifneq ($$(SUBPROJECT),)
  249. $$(eval $$(call PARSE_SUBPROJECT,$$(SUBPROJECT)))
  250. # If there's no matching subproject, we assume it's the default
  251. # This will allow you to leave the subproject part of the target out
  252. else
  253. $$(eval $$(call PARSE_SUBPROJECT,))
  254. endif
  255. endef
  256. # if we are going to compile all keyboards, match the rest of the rule
  257. # for each of them
  258. define PARSE_ALL_KEYBOARDS
  259. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(KEYBOARDS)))
  260. endef
  261. # $1 Subproject
  262. # When entering this, the keyboard and subproject are known, so now we need
  263. # to determine which keymaps are going to get compiled
  264. define PARSE_SUBPROJECT
  265. # If we want to compile the default subproject, then we need to
  266. # include the correct makefile to determine the actual name of it
  267. CURRENT_SP := $1
  268. ifeq ($$(CURRENT_SP),)
  269. CURRENT_SP := defaultsp
  270. endif
  271. ifeq ($$(CURRENT_SP),defaultsp)
  272. SUBPROJECT_DEFAULT=
  273. ifneq ("$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/subproject.mk)","")
  274. $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/subproject.mk)
  275. endif
  276. CURRENT_SP := $$(SUBPROJECT_DEFAULT)
  277. endif
  278. # If current subproject is empty (the default was not defined), and we have a list of subproject
  279. # then make all of them
  280. ifeq ($$(CURRENT_SP),)
  281. ifneq ($$(SUBPROJECTS),)
  282. CURRENT_SP := allsp
  283. endif
  284. endif
  285. # The special allsp is handled later
  286. ifneq ($$(CURRENT_SP),allsp)
  287. # get a list of all keymaps
  288. KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/keymaps/*/.)))
  289. LAYOUTS :=
  290. $$(eval -include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/rules.mk)
  291. KEYBOARD_LAYOUTS := $$(LAYOUTS)
  292. ifneq ($$(CURRENT_SP),)
  293. # if the subproject is defined, then also look for keymaps inside the subproject folder
  294. SP_KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/$$(CURRENT_SP)/keymaps/*/.)))
  295. KEYMAPS := $$(sort $$(KEYMAPS) $$(SP_KEYMAPS))
  296. # $$(eval -include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/$$(CURRENT_SP)/rules.mk)
  297. # KEYBOARD_LAYOUTS := $$(sort $$(KEYBOARD_LAYOUTS) $$(LAYOUTS))
  298. endif
  299. LAYOUT_KEYMAPS :=
  300. $$(foreach LAYOUT,$$(KEYBOARD_LAYOUTS),$$(eval LAYOUT_KEYMAPS += $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/layouts/*/$$(LAYOUT)/*/.)))))
  301. KEYMAPS := $$(sort $$(KEYMAPS) $$(LAYOUT_KEYMAPS))
  302. # if the rule after removing the start of it is empty (we haven't specified a kemap or target)
  303. # compile all the keymaps
  304. ifeq ($$(RULE),)
  305. $$(eval $$(call PARSE_ALL_KEYMAPS))
  306. # The same if allkm was specified
  307. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkm),true)
  308. $$(eval $$(call PARSE_ALL_KEYMAPS))
  309. # Try to match the specified keyamp with the list of known keymaps
  310. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYMAPS)),true)
  311. $$(eval $$(call PARSE_KEYMAP,$$(MATCHED_ITEM)))
  312. # Otherwise try to match the keymap from the current folder, or arguments to the make command
  313. else ifneq ($$(KEYMAP),)
  314. $$(eval $$(call PARSE_KEYMAP,$$(KEYMAP)))
  315. # No matching keymap found, so we assume that the rest of the rule is the target
  316. # If we haven't been able to parse out a subproject, then make all of them
  317. # This is consistent with running make without any arguments from the keyboard
  318. # folder
  319. else ifeq ($1,)
  320. $$(eval $$(call PARSE_ALL_SUBPROJECTS))
  321. # Otherwise, make all keymaps, again this is consistent with how it works without
  322. # any arguments
  323. else
  324. $$(eval $$(call PARSE_ALL_KEYMAPS))
  325. endif
  326. else
  327. # As earlier mentioned when allsb is specified, we call our self recursively
  328. # for all of the subprojects
  329. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$(SUBPROJECTS)))
  330. endif
  331. endef
  332. # If we want to parse all subprojects, but the keyboard doesn't have any,
  333. # then use defaultsp instead
  334. define PARSE_ALL_SUBPROJECTS
  335. ifeq ($$(SUBPROJECTS),)
  336. $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
  337. else
  338. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$$(SUBPROJECTS)))
  339. endif
  340. endef
  341. # $1 Keymap
  342. # This is the meat of compiling a keyboard, when entering this, everything is known
  343. # keyboard, subproject, and keymap
  344. # Note that we are not directly calling the command here, but instead building a list,
  345. # which will later be processed
  346. define PARSE_KEYMAP
  347. CURRENT_KM = $1
  348. # The rest of the rule is the target
  349. # Remove the leading "-" from the target, as it acts as a separator
  350. MAKE_TARGET := $$(patsubst -%,%,$$(RULE))
  351. # We need to generate an unique indentifer to append to the COMMANDS list
  352. COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM)
  353. # If we are compiling a keyboard without a subproject, we want to display just the name
  354. # of the keyboard, otherwise keyboard/subproject
  355. ifeq ($$(CURRENT_SP),)
  356. KB_SP := $(CURRENT_KB)
  357. else
  358. KB_SP := $(CURRENT_KB)/$$(CURRENT_SP)
  359. endif
  360. # Format it in bold
  361. KB_SP := $(BOLD)$$(KB_SP)$(NO_COLOR)
  362. # Specify the variables that we are passing forward to submake
  363. MAKE_VARS := KEYBOARD=$$(CURRENT_KB) SUBPROJECT=$$(CURRENT_SP) KEYMAP=$$(CURRENT_KM)
  364. # And the first part of the make command
  365. MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_keyboard.mk $$(MAKE_TARGET)
  366. # The message to display
  367. MAKE_MSG := $$(MSG_MAKE_KB)
  368. # We run the command differently, depending on if we want more output or not
  369. # The true version for silent output and the false version otherwise
  370. $$(eval $$(call BUILD))
  371. endef
  372. define BUILD
  373. MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
  374. COMMANDS += $$(COMMAND)
  375. COMMAND_true_$$(COMMAND) := \
  376. printf "$$(MAKE_MSG)" | \
  377. $$(MAKE_MSG_FORMAT); \
  378. LOG=$$$$($$(MAKE_CMD) $$(MAKE_VARS) SILENT=true 2>&1) ; \
  379. if [ $$$$? -gt 0 ]; \
  380. then $$(PRINT_ERROR_PLAIN); \
  381. elif [ "$$$$LOG" != "" ] ; \
  382. then $$(PRINT_WARNING_PLAIN); \
  383. else \
  384. $$(PRINT_OK); \
  385. fi;
  386. COMMAND_false_$$(COMMAND) := \
  387. printf "$$(MAKE_MSG)\n\n"; \
  388. $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false; \
  389. if [ $$$$? -gt 0 ]; \
  390. then error_occurred=1; \
  391. fi;
  392. endef
  393. # Just parse all the keymaps for a specific keyboard
  394. define PARSE_ALL_KEYMAPS
  395. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
  396. endef
  397. define BUILD_TEST
  398. TEST_NAME := $1
  399. MAKE_TARGET := $2
  400. COMMAND := $1
  401. MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_test.mk $$(MAKE_TARGET)
  402. MAKE_VARS := TEST=$$(TEST_NAME) FULL_TESTS="$$(FULL_TESTS)"
  403. MAKE_MSG := $$(MSG_MAKE_TEST)
  404. $$(eval $$(call BUILD))
  405. ifneq ($$(MAKE_TARGET),clean)
  406. TEST_EXECUTABLE := $$(TEST_DIR)/$$(TEST_NAME).elf
  407. TESTS += $$(TEST_NAME)
  408. TEST_MSG := $$(MSG_TEST)
  409. $$(TEST_NAME)_COMMAND := \
  410. printf "$$(TEST_MSG)\n"; \
  411. $$(TEST_EXECUTABLE); \
  412. if [ $$$$? -gt 0 ]; \
  413. then error_occurred=1; \
  414. fi; \
  415. printf "\n";
  416. endif
  417. endef
  418. define PARSE_TEST
  419. TESTS :=
  420. TEST_NAME := $$(firstword $$(subst -, ,$$(RULE)))
  421. TEST_TARGET := $$(subst $$(TEST_NAME),,$$(subst $$(TEST_NAME)-,,$$(RULE)))
  422. ifeq ($$(TEST_NAME),all)
  423. MATCHED_TESTS := $$(TEST_LIST)
  424. else
  425. MATCHED_TESTS := $$(foreach TEST,$$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME),$$(TEST)),$$(TEST),))
  426. endif
  427. $$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
  428. endef
  429. # Set the silent mode depending on if we are trying to compile multiple keyboards or not
  430. # By default it's on in that case, but it can be overridden by specifying silent=false
  431. # from the command line
  432. define SET_SILENT_MODE
  433. ifdef SUB_IS_SILENT
  434. SILENT_MODE := $(SUB_IS_SILENT)
  435. else ifeq ($$(words $$(COMMANDS)),1)
  436. SILENT_MODE := false
  437. else
  438. SILENT_MODE := true
  439. endif
  440. endef
  441. include $(ROOT_DIR)/message.mk
  442. ifeq ($(strip $(BREAK_ON_ERRORS)), yes)
  443. HANDLE_ERROR = exit 1
  444. else
  445. HANDLE_ERROR = echo $$error_occurred > $(ERROR_FILE)
  446. endif
  447. # The empty line is important here, as it will force a new shell to be created for each command
  448. # Otherwise the command line will become too long with a lot of keyboards and keymaps
  449. define RUN_COMMAND
  450. +error_occurred=0;\
  451. $(COMMAND_$(SILENT_MODE)_$(COMMAND))\
  452. if [ $$error_occurred -gt 0 ]; then $(HANDLE_ERROR); fi;
  453. endef
  454. define RUN_TEST
  455. +error_occurred=0;\
  456. $($(TEST)_COMMAND)\
  457. if [ $$error_occurred -gt 0 ]; then $(HANDLE_ERROR); fi;
  458. endef
  459. # Allow specifying just the subproject, in the keyboard directory, which will compile all keymaps
  460. SUBPROJECTS := $(notdir $(patsubst %/rules.mk,%,$(wildcard ./*/rules.mk)))
  461. .PHONY: $(SUBPROJECTS)
  462. $(SUBPROJECTS): %: %-allkm
  463. # Let's match everything, we handle all the rule parsing ourselves
  464. .PHONY: %
  465. %:
  466. # Check if we have the CMP tool installed
  467. cmp $(ROOT_DIR)/Makefile $(ROOT_DIR)/Makefile >/dev/null 2>&1; if [ $$? -gt 0 ]; then printf "$(MSG_NO_CMP)"; exit 1; fi;
  468. # Check if the submodules are dirty, and display a warning if they are
  469. ifndef SKIP_GIT
  470. if [ ! -e lib/chibios ]; then git submodule sync lib/chibios && git submodule update --init lib/chibios; fi
  471. if [ ! -e lib/chibios-contrib ]; then git submodule sync lib/chibios-contrib && git submodule update --init lib/chibios-contrib; fi
  472. if [ ! -e lib/ugfx ]; then git submodule sync lib/ugfx && git submodule update --init lib/ugfx; fi
  473. git submodule status --recursive 2>/dev/null | \
  474. while IFS= read -r x; do \
  475. case "$$x" in \
  476. \ *) ;; \
  477. *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
  478. esac \
  479. done
  480. endif
  481. rm -f $(ERROR_FILE) > /dev/null 2>&1
  482. $(eval $(call PARSE_RULE,$@))
  483. $(eval $(call SET_SILENT_MODE))
  484. # Run all the commands in the same shell, notice the + at the first line
  485. # it has to be there to allow parallel execution of the submake
  486. # This always tries to compile everything, even if error occurs in the middle
  487. # But we return the error code at the end, to trigger travis failures
  488. $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND))
  489. if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
  490. $(foreach TEST,$(TESTS),$(RUN_TEST))
  491. if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
  492. # All should compile everything
  493. .PHONY: all
  494. all: all-keyboards test-all
  495. # Define some shortcuts, mostly for compatibility with the old syntax
  496. .PHONY: all-keyboards
  497. all-keyboards: allkb-allsp-allkm
  498. .PHONY: all-keyboards-defaults
  499. all-keyboards-defaults: allkb-allsp-default
  500. .PHONY: test
  501. test: test-all
  502. .PHONY: test-clean
  503. test-clean: test-all-clean
  504. lib/%:
  505. git submodule sync $?
  506. git submodule update --init $?
  507. git-submodule:
  508. git submodule sync --recursive
  509. git submodule update --init --recursive
  510. define PRINT_OPTION
  511. ifeq ($$(SUBPROJECT_OPTION),)
  512. ALL_OPTIONS += $$(KEYBOARD_OPTION)-$$(KEYMAP_OPTION)
  513. else
  514. ALL_OPTIONS += $$(KEYBOARD_OPTION)-$$(SUBPROJECT_OPTION)-$$(KEYMAP_OPTION)
  515. endif
  516. endef
  517. define ALL_KEYMAPS
  518. KEYMAPS_OPTION := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_OPTION)/keymaps/*/.)))
  519. LAYOUTS :=
  520. $$(eval -include $(ROOT_DIR)/keyboards/$$(KEYBOARD_OPTION)/rules.mk)
  521. KEYBOARD_LAYOUTS := $$(LAYOUTS)
  522. ifneq ($$(SUBPROJECT_OPTION),)
  523. SP_KEYMAPS_OPTION := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_OPTION)/$$(SUBPROJECT_OPTION)/keymaps/*/.)))
  524. KEYMAPS_OPTION := $$(sort $$(KEYMAPS_OPTION) $$(SP_KEYMAPS_OPTION))
  525. endif
  526. LAYOUT_KEYMAPS_OPTION :=
  527. $$(foreach LAYOUT_OPTION,$$(KEYBOARD_LAYOUTS),$$(eval LAYOUT_KEYMAPS_OPTION += $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/layouts/*/$$(LAYOUT_OPTION)/*/.)))))
  528. KEYMAPS_OPTION := $$(sort $$(KEYMAPS_OPTION) $$(LAYOUT_KEYMAPS_OPTION))
  529. $$(foreach KEYMAP_OPTION, $$(KEYMAPS_OPTION), $$(eval $$(call PRINT_OPTION)))
  530. endef
  531. define ALL_SUBPROJECTS
  532. SUBPROJECTS_OPTION := $$(notdir $$(patsubst %/rules.mk,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_OPTION)/*/rules.mk)))
  533. $$(eval $$(call ALL_KEYMAPS))
  534. ifneq ($$(SUBPROJECTS_OPTION),)
  535. $$(foreach SUBPROJECT_OPTION, $$(SUBPROJECTS_OPTION), $$(eval $$(call ALL_KEYMAPS)))
  536. endif
  537. endef
  538. define ALL_KEYBOARDS
  539. $$(foreach KEYBOARD_OPTION,$$(KEYBOARDS), $$(eval $$(call ALL_SUBPROJECTS)))
  540. endef
  541. ALL_OPTIONS :=
  542. $(eval $(call ALL_KEYBOARDS))
  543. autocomplete:
  544. echo $(ALL_OPTIONS)
  545. ifdef SKIP_VERSION
  546. SKIP_GIT := yes
  547. endif
  548. # Generate the version.h file
  549. ifndef SKIP_GIT
  550. GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
  551. else
  552. GIT_VERSION := NA
  553. endif
  554. ifndef SKIP_VERSION
  555. BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
  556. $(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
  557. $(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
  558. else
  559. BUILD_DATE := NA
  560. endif
  561. include $(ROOT_DIR)/testlist.mk