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.

585 lines
21 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
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 v$(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. ifeq ($(CURRENT_PATH_ELEMENT),keyboards)
  57. $(eval $(call NEXT_PATH_ELEMENT))
  58. KEYBOARD := $(CURRENT_PATH_ELEMENT)
  59. $(eval $(call NEXT_PATH_ELEMENT))
  60. # If we are in a subfolder of keymaps, or in other words in a keymap
  61. # folder
  62. ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
  63. $(eval $(call NEXT_PATH_ELEMENT))
  64. KEYMAP := $(CURRENT_PATH_ELEMENT)
  65. # else if we are not in the keyboard folder itself
  66. else ifneq ($(CURRENT_PATH_ELEMENT),)
  67. # the we can assume it's a subproject, as no other folders
  68. # should have make files in them
  69. SUBPROJECT := $(CURRENT_PATH_ELEMENT)
  70. $(eval $(call NEXT_PATH_ELEMENT))
  71. # if we are inside a keymap folder of a subproject
  72. ifeq ($(CURRENT_PATH_ELEMENT),keymaps)
  73. $(eval $(call NEXT_PATH_ELEMENT))
  74. KEYMAP := $(CURRENT_PATH_ELEMENT)
  75. endif
  76. endif
  77. endif
  78. # Only consider folders with makefiles, to prevent errors in case there are extra folders
  79. KEYBOARDS := $(notdir $(patsubst %/rules.mk,%,$(wildcard $(ROOT_DIR)/keyboards/*/rules.mk)))
  80. #Compatibility with the old make variables, anything you specify directly on the command line
  81. # always overrides the detected folders
  82. ifdef keyboard
  83. KEYBOARD := $(keyboard)
  84. endif
  85. ifdef sub
  86. SUBPROJECT := $(sub)
  87. endif
  88. ifdef subproject
  89. SUBPROJECT := $(subproject)
  90. endif
  91. ifdef keymap
  92. KEYMAP := $(keymap)
  93. endif
  94. # Uncomment these for debugging
  95. #$(info Keyboard: $(KEYBOARD))
  96. #$(info Keymap: $(KEYMAP))
  97. #$(info Subproject: $(SUBPROJECT))
  98. #$(info Keyboards: $(KEYBOARDS))
  99. # Set the default goal depending on where we are running make from
  100. # this handles the case where you run make without any arguments
  101. .DEFAULT_GOAL := all
  102. ifneq ($(KEYMAP),)
  103. ifeq ($(SUBPROJECT),)
  104. # Inside a keymap folder, just build the keymap, with the
  105. # default subproject
  106. .DEFAULT_GOAL := $(KEYBOARD)-$(KEYMAP)
  107. else
  108. # Inside a subproject keyamp folder, build the keymap
  109. # for that subproject
  110. .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-$(KEYMAP)
  111. endif
  112. else ifneq ($(SUBPROJECT),)
  113. # Inside a subproject folder, build all keymaps for that subproject
  114. .DEFAULT_GOAL := $(KEYBOARD)-$(SUBPROJECT)-allkm
  115. else ifneq ($(KEYBOARD),)
  116. # Inside a keyboard folder, build all keymaps for all subprojects
  117. # Note that this is different from the old behaviour, which would
  118. # build only the default keymap of the default keyboard
  119. .DEFAULT_GOAL := $(KEYBOARD)-allsp-allkm
  120. endif
  121. # Compare the start of the RULE variable with the first argument($1)
  122. # If the rules equals $1 or starts with $1-, RULE_FOUND is set to true
  123. # and $1 is removed from the RULE variable
  124. # Otherwise the RULE_FOUND variable is set to false, and RULE left as it was
  125. # The function is a bit tricky, since there's no built in $(startswith) function
  126. define COMPARE_AND_REMOVE_FROM_RULE_HELPER
  127. ifeq ($1,$$(RULE))
  128. RULE:=
  129. RULE_FOUND := true
  130. else
  131. STARTDASH_REMOVED=$$(subst START$1-,,START$$(RULE))
  132. ifneq ($$(STARTDASH_REMOVED),START$$(RULE))
  133. RULE_FOUND := true
  134. RULE := $$(STARTDASH_REMOVED)
  135. else
  136. RULE_FOUND := false
  137. endif
  138. endif
  139. endef
  140. # This makes it easier to call COMPARE_AND_REMOVE_FROM_RULE, since it makes it behave like
  141. # a function that returns the value
  142. COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND)
  143. # Recursively try to find a match for the start of the rule to be checked
  144. # $1 The list to be checked
  145. # If a match is found, then RULE_FOUND is set to true
  146. # and MATCHED_ITEM to the item that was matched
  147. define TRY_TO_MATCH_RULE_FROM_LIST_HELPER3
  148. ifneq ($1,)
  149. ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,$$(firstword $1)),true)
  150. MATCHED_ITEM := $$(firstword $1)
  151. else
  152. $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$$(wordlist 2,9999,$1)))
  153. endif
  154. endif
  155. endef
  156. # A recursive helper function for finding the longest match
  157. # $1 The list to be checked
  158. # It works by always removing the currently matched item from the list
  159. # and call itself recursively, until a match is found
  160. define TRY_TO_MATCH_RULE_FROM_LIST_HELPER2
  161. # Stop the recursion when the list is empty
  162. ifneq ($1,)
  163. RULE_BEFORE := $$(RULE)
  164. $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$1))
  165. # If a match is found in the current list, otherwise just return what we had before
  166. ifeq ($$(RULE_FOUND),true)
  167. # Save the best match so far and call itself recursively
  168. BEST_MATCH := $$(MATCHED_ITEM)
  169. BEST_MATCH_RULE := $$(RULE)
  170. RULE_FOUND := false
  171. RULE := $$(RULE_BEFORE)
  172. $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$$(filter-out $$(MATCHED_ITEM),$1)))
  173. endif
  174. endif
  175. endef
  176. # Recursively try to find the longest match for the start of the rule to be checked
  177. # $1 The list to be checked
  178. # If a match is found, then RULE_FOUND is set to true
  179. # and MATCHED_ITEM to the item that was matched
  180. define TRY_TO_MATCH_RULE_FROM_LIST_HELPER
  181. BEST_MATCH :=
  182. $$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$1))
  183. ifneq ($$(BEST_MATCH),)
  184. RULE_FOUND := true
  185. RULE := $$(BEST_MATCH_RULE)
  186. MATCHED_ITEM := $$(BEST_MATCH)
  187. else
  188. RULE_FOUND := false
  189. MATCHED_ITEM :=
  190. endif
  191. endef
  192. # Make it easier to call TRY_TO_MATCH_RULE_FROM_LIST
  193. TRY_TO_MATCH_RULE_FROM_LIST = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$1))$(RULE_FOUND)
  194. define ALL_IN_LIST_LOOP
  195. OLD_RULE$1 := $$(RULE)
  196. $$(eval $$(call $1,$$(ITEM$1)))
  197. RULE := $$(OLD_RULE$1)
  198. endef
  199. define PARSE_ALL_IN_LIST
  200. $$(foreach ITEM$1,$2,$$(eval $$(call ALL_IN_LIST_LOOP,$1)))
  201. endef
  202. # The entry point for rule parsing
  203. # parses a rule in the format <keyboard>-<subproject>-<keymap>-<target>
  204. # but this particular function only deals with the first <keyboard> part
  205. define PARSE_RULE
  206. RULE := $1
  207. COMMANDS :=
  208. # If the rule starts with allkb, then continue the parsing from
  209. # PARSE_ALL_KEYBOARDS
  210. ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkb),true)
  211. $$(eval $$(call PARSE_ALL_KEYBOARDS))
  212. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,test),true)
  213. $$(eval $$(call PARSE_TEST))
  214. # If the rule starts with the name of a known keyboard, then continue
  215. # the parsing from PARSE_KEYBOARD
  216. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYBOARDS)),true)
  217. $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM)))
  218. # Otherwise use the KEYBOARD variable, which is determined either by
  219. # the current directory you run make from, or passed in as an argument
  220. else ifneq ($$(KEYBOARD),)
  221. $$(eval $$(call PARSE_KEYBOARD,$$(KEYBOARD)))
  222. else
  223. $$(info make: *** No rule to make target '$1'. Stop.)
  224. # Notice the tab instead of spaces below!
  225. exit 1
  226. endif
  227. endef
  228. # $1 = Keyboard
  229. # Parses a rule in the format <subproject>-<keymap>-<target>
  230. # the keyboard is already known when entering this function
  231. define PARSE_KEYBOARD
  232. CURRENT_KB := $1
  233. # A subproject is any keyboard subfolder with a makefile
  234. SUBPROJECTS := $$(notdir $$(patsubst %/rules.mk,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/*/rules.mk)))
  235. # if the rule starts with allsp, then continue with looping over all subprojects
  236. ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allsp),true)
  237. $$(eval $$(call PARSE_ALL_SUBPROJECTS))
  238. # A special case for matching the defaultsp (default subproject)
  239. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,defaultsp),true)
  240. $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
  241. # If the rule starts with the name of a known subproject
  242. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(SUBPROJECTS)),true)
  243. $$(eval $$(call PARSE_SUBPROJECT,$$(MATCHED_ITEM)))
  244. # Try to use the SUBPROJECT variable, which is either determined by the
  245. # directory which invoked make, or passed as an argument to make
  246. else ifneq ($$(SUBPROJECT),)
  247. $$(eval $$(call PARSE_SUBPROJECT,$$(SUBPROJECT)))
  248. # If there's no matching subproject, we assume it's the default
  249. # This will allow you to leave the subproject part of the target out
  250. else
  251. $$(eval $$(call PARSE_SUBPROJECT,))
  252. endif
  253. endef
  254. # if we are going to compile all keyboards, match the rest of the rule
  255. # for each of them
  256. define PARSE_ALL_KEYBOARDS
  257. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(KEYBOARDS)))
  258. endef
  259. # $1 Subproject
  260. # When entering this, the keyboard and subproject are known, so now we need
  261. # to determine which keymaps are going to get compiled
  262. define PARSE_SUBPROJECT
  263. # If we want to compile the default subproject, then we need to
  264. # include the correct makefile to determine the actual name of it
  265. CURRENT_SP := $1
  266. ifeq ($$(CURRENT_SP),)
  267. CURRENT_SP := defaultsp
  268. endif
  269. ifeq ($$(CURRENT_SP),defaultsp)
  270. SUBPROJECT_DEFAULT=
  271. ifneq ("$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/subproject.mk)","")
  272. $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/subproject.mk)
  273. endif
  274. CURRENT_SP := $$(SUBPROJECT_DEFAULT)
  275. endif
  276. # If current subproject is empty (the default was not defined), and we have a list of subproject
  277. # then make all of them
  278. ifeq ($$(CURRENT_SP),)
  279. ifneq ($$(SUBPROJECTS),)
  280. CURRENT_SP := allsp
  281. endif
  282. endif
  283. # The special allsp is handled later
  284. ifneq ($$(CURRENT_SP),allsp)
  285. # get a list of all keymaps
  286. KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/keymaps/*/.)))
  287. LAYOUTS :=
  288. $$(eval -include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/rules.mk)
  289. KEYBOARD_LAYOUTS := $$(LAYOUTS)
  290. ifneq ($$(CURRENT_SP),)
  291. # if the subproject is defined, then also look for keymaps inside the subproject folder
  292. SP_KEYMAPS := $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/$$(CURRENT_SP)/keymaps/*/.)))
  293. KEYMAPS := $$(sort $$(KEYMAPS) $$(SP_KEYMAPS))
  294. # $$(eval -include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/$$(CURRENT_SP)/rules.mk)
  295. # KEYBOARD_LAYOUTS := $$(sort $$(KEYBOARD_LAYOUTS) $$(LAYOUTS))
  296. endif
  297. LAYOUT_KEYMAPS :=
  298. $$(foreach LAYOUT,$$(KEYBOARD_LAYOUTS),$$(eval LAYOUT_KEYMAPS += $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/layouts/*/$$(LAYOUT)/*/.)))))
  299. KEYMAPS := $$(sort $$(KEYMAPS) $$(LAYOUT_KEYMAPS))
  300. # if the rule after removing the start of it is empty (we haven't specified a kemap or target)
  301. # compile all the keymaps
  302. ifeq ($$(RULE),)
  303. $$(eval $$(call PARSE_ALL_KEYMAPS))
  304. # The same if allkm was specified
  305. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkm),true)
  306. $$(eval $$(call PARSE_ALL_KEYMAPS))
  307. # Try to match the specified keyamp with the list of known keymaps
  308. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYMAPS)),true)
  309. $$(eval $$(call PARSE_KEYMAP,$$(MATCHED_ITEM)))
  310. # Otherwise try to match the keymap from the current folder, or arguments to the make command
  311. else ifneq ($$(KEYMAP),)
  312. $$(eval $$(call PARSE_KEYMAP,$$(KEYMAP)))
  313. # No matching keymap found, so we assume that the rest of the rule is the target
  314. # If we haven't been able to parse out a subproject, then make all of them
  315. # This is consistent with running make without any arguments from the keyboard
  316. # folder
  317. else ifeq ($1,)
  318. $$(eval $$(call PARSE_ALL_SUBPROJECTS))
  319. # Otherwise, make all keymaps, again this is consistent with how it works without
  320. # any arguments
  321. else
  322. $$(eval $$(call PARSE_ALL_KEYMAPS))
  323. endif
  324. else
  325. # As earlier mentioned when allsb is specified, we call our self recursively
  326. # for all of the subprojects
  327. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$(SUBPROJECTS)))
  328. endif
  329. endef
  330. # If we want to parse all subprojects, but the keyboard doesn't have any,
  331. # then use defaultsp instead
  332. define PARSE_ALL_SUBPROJECTS
  333. ifeq ($$(SUBPROJECTS),)
  334. $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
  335. else
  336. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$$(SUBPROJECTS)))
  337. endif
  338. endef
  339. # $1 Keymap
  340. # This is the meat of compiling a keyboard, when entering this, everything is known
  341. # keyboard, subproject, and keymap
  342. # Note that we are not directly calling the command here, but instead building a list,
  343. # which will later be processed
  344. define PARSE_KEYMAP
  345. CURRENT_KM = $1
  346. # The rest of the rule is the target
  347. # Remove the leading "-" from the target, as it acts as a separator
  348. MAKE_TARGET := $$(patsubst -%,%,$$(RULE))
  349. # We need to generate an unique indentifer to append to the COMMANDS list
  350. COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM)
  351. # If we are compiling a keyboard without a subproject, we want to display just the name
  352. # of the keyboard, otherwise keyboard/subproject
  353. ifeq ($$(CURRENT_SP),)
  354. KB_SP := $(CURRENT_KB)
  355. else
  356. KB_SP := $(CURRENT_KB)/$$(CURRENT_SP)
  357. endif
  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) SUBPROJECT=$$(CURRENT_SP) 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. # Allow specifying just the subproject, in the keyboard directory, which will compile all keymaps
  458. SUBPROJECTS := $(notdir $(patsubst %/rules.mk,%,$(wildcard ./*/rules.mk)))
  459. .PHONY: $(SUBPROJECTS)
  460. $(SUBPROJECTS): %: %-allkm
  461. # Let's match everything, we handle all the rule parsing ourselves
  462. .PHONY: %
  463. %:
  464. # Check if we have the CMP tool installed
  465. cmp $(ROOT_DIR)/Makefile $(ROOT_DIR)/Makefile >/dev/null 2>&1; if [ $$? -gt 0 ]; then printf "$(MSG_NO_CMP)"; exit 1; fi;
  466. # Check if the submodules are dirty, and display a warning if they are
  467. ifndef SKIP_GIT
  468. if [ ! -e lib/chibios ]; then git submodule sync lib/chibios && git submodule update --init lib/chibios; fi
  469. if [ ! -e lib/chibios-contrib ]; then git submodule sync lib/chibios-contrib && git submodule update --init lib/chibios-contrib; fi
  470. if [ ! -e lib/ugfx ]; then git submodule sync lib/ugfx && git submodule update --init lib/ugfx; fi
  471. git submodule status --recursive 2>/dev/null | \
  472. while IFS= read -r x; do \
  473. case "$$x" in \
  474. \ *) ;; \
  475. *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
  476. esac \
  477. done
  478. endif
  479. rm -f $(ERROR_FILE) > /dev/null 2>&1
  480. $(eval $(call PARSE_RULE,$@))
  481. $(eval $(call SET_SILENT_MODE))
  482. # Run all the commands in the same shell, notice the + at the first line
  483. # it has to be there to allow parallel execution of the submake
  484. # This always tries to compile everything, even if error occurs in the middle
  485. # But we return the error code at the end, to trigger travis failures
  486. $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND))
  487. if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
  488. $(foreach TEST,$(TESTS),$(RUN_TEST))
  489. if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
  490. # All should compile everything
  491. .PHONY: all
  492. all: all-keyboards test-all
  493. # Define some shortcuts, mostly for compatibility with the old syntax
  494. .PHONY: all-keyboards
  495. all-keyboards: allkb-allsp-allkm
  496. .PHONY: all-keyboards-defaults
  497. all-keyboards-defaults: allkb-allsp-default
  498. .PHONY: test
  499. test: test-all
  500. .PHONY: test-clean
  501. test-clean: test-all-clean
  502. lib/%:
  503. git submodule sync $?
  504. git submodule update --init $?
  505. git-submodule:
  506. git submodule sync --recursive
  507. git submodule update --init --recursive
  508. ifdef SKIP_VERSION
  509. SKIP_GIT := yes
  510. endif
  511. # Generate the version.h file
  512. ifndef SKIP_GIT
  513. GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
  514. else
  515. GIT_VERSION := NA
  516. endif
  517. ifndef SKIP_VERSION
  518. BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
  519. $(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
  520. $(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
  521. else
  522. BUILD_DATE := NA
  523. endif
  524. include $(ROOT_DIR)/testlist.mk