You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
22 KiB

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