2
0

Makefile 22 KB

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