Peter's keyboard firmware (QMK) https://qmk.fm/
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.

Makefile 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. KEYMAPS := $$(sort $$(filter-out $$(KEYBOARD_FOLDER_1) $$(KEYBOARD_FOLDER_2) \
  268. $$(KEYBOARD_FOLDER_3) $$(KEYBOARD_FOLDER_4) $$(KEYBOARD_FOLDER_5), $$(KEYMAPS)))
  269. KEYBOARD_LAYOUTS :=
  270. ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_5)/rules.mk)","")
  271. LAYOUTS :=
  272. $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_5)/rules.mk)
  273. KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS))
  274. endif
  275. ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_4)/rules.mk)","")
  276. LAYOUTS :=
  277. $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_4)/rules.mk)
  278. KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS))
  279. endif
  280. ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_3)/rules.mk)","")
  281. LAYOUTS :=
  282. $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_3)/rules.mk)
  283. KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS))
  284. endif
  285. ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_2)/rules.mk)","")
  286. LAYOUTS :=
  287. $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_2)/rules.mk)
  288. KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS))
  289. endif
  290. ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_1)/rules.mk)","")
  291. LAYOUTS :=
  292. $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_1)/rules.mk)
  293. KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS))
  294. endif
  295. LAYOUT_KEYMAPS :=
  296. $$(foreach LAYOUT,$$(KEYBOARD_LAYOUTS),$$(eval LAYOUT_KEYMAPS += $$(notdir $$(patsubst %/.,%,$$(wildcard $(ROOT_DIR)/layouts/*/$$(LAYOUT)/*/.)))))
  297. KEYMAPS := $$(sort $$(KEYMAPS) $$(LAYOUT_KEYMAPS))
  298. # if the rule after removing the start of it is empty (we haven't specified a kemap or target)
  299. # compile all the keymaps
  300. ifeq ($$(RULE),)
  301. $$(eval $$(call PARSE_ALL_KEYMAPS))
  302. # The same if all was specified
  303. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,all),true)
  304. $$(eval $$(call PARSE_ALL_KEYMAPS))
  305. # Try to match the specified keyamp with the list of known keymaps
  306. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYMAPS)),true)
  307. $$(eval $$(call PARSE_KEYMAP,$$(MATCHED_ITEM)))
  308. # Otherwise try to match the keymap from the current folder, or arguments to the make command
  309. else ifneq ($$(KEYMAP),)
  310. $$(eval $$(call PARSE_KEYMAP,$$(KEYMAP)))
  311. # Otherwise, make all keymaps, again this is consistent with how it works without
  312. # any arguments
  313. else
  314. $$(eval $$(call PARSE_ALL_KEYMAPS))
  315. endif
  316. endef
  317. # if we are going to compile all keyboards, match the rest of the rule
  318. # for each of them
  319. define PARSE_ALL_KEYBOARDS
  320. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(KEYBOARDS)))
  321. endef
  322. # $1 Subproject
  323. # When entering this, the keyboard and subproject are known, so now we need
  324. # to determine which keymaps are going to get compiled
  325. # define PARSE_SUBPROJECT
  326. # endef
  327. # If we want to parse all subprojects, but the keyboard doesn't have any,
  328. # then use defaultsp instead
  329. # define PARSE_ALL_SUBPROJECTS
  330. # ifeq ($$(SUBPROJECTS),)
  331. # $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
  332. # else
  333. # $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_SUBPROJECT,$$(SUBPROJECTS)))
  334. # endif
  335. # endef
  336. # $1 Keymap
  337. # This is the meat of compiling a keyboard, when entering this, everything is known
  338. # keyboard, subproject, and keymap
  339. # Note that we are not directly calling the command here, but instead building a list,
  340. # which will later be processed
  341. define PARSE_KEYMAP
  342. CURRENT_KM = $1
  343. # The rest of the rule is the target
  344. # Remove the leading ":" from the target, as it acts as a separator
  345. MAKE_TARGET := $$(patsubst :%,%,$$(RULE))
  346. # We need to generate an unique indentifer to append to the COMMANDS list
  347. CURRENT_KB_UNDER := $$(subst /,_,$$(CURRENT_KB))
  348. COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB_UNDER)_KEYMAP_$$(CURRENT_KM)
  349. # If we are compiling a keyboard without a subproject, we want to display just the name
  350. # of the keyboard, otherwise keyboard/subproject
  351. KB_SP := $$(CURRENT_KB)
  352. # Format it in bold
  353. KB_SP := $(BOLD)$$(KB_SP)$(NO_COLOR)
  354. # Specify the variables that we are passing forward to submake
  355. MAKE_VARS := KEYBOARD=$$(CURRENT_KB) KEYMAP=$$(CURRENT_KM)
  356. # And the first part of the make command
  357. MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_keyboard.mk $$(MAKE_TARGET)
  358. # The message to display
  359. MAKE_MSG := $$(MSG_MAKE_KB)
  360. # We run the command differently, depending on if we want more output or not
  361. # The true version for silent output and the false version otherwise
  362. $$(eval $$(call BUILD))
  363. endef
  364. define BUILD
  365. MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
  366. COMMANDS += $$(COMMAND)
  367. COMMAND_true_$$(COMMAND) := \
  368. printf "$$(MAKE_MSG)" | \
  369. $$(MAKE_MSG_FORMAT); \
  370. LOG=$$$$($$(MAKE_CMD) $$(MAKE_VARS) SILENT=true 2>&1) ; \
  371. if [ $$$$? -gt 0 ]; \
  372. then $$(PRINT_ERROR_PLAIN); \
  373. elif [ "$$$$LOG" != "" ] ; \
  374. then $$(PRINT_WARNING_PLAIN); \
  375. else \
  376. $$(PRINT_OK); \
  377. fi;
  378. COMMAND_false_$$(COMMAND) := \
  379. printf "$$(MAKE_MSG)\n\n"; \
  380. $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false; \
  381. if [ $$$$? -gt 0 ]; \
  382. then error_occurred=1; \
  383. fi;
  384. endef
  385. # Just parse all the keymaps for a specific keyboard
  386. define PARSE_ALL_KEYMAPS
  387. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
  388. endef
  389. define BUILD_TEST
  390. TEST_NAME := $1
  391. MAKE_TARGET := $2
  392. COMMAND := $1
  393. MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_test.mk $$(MAKE_TARGET)
  394. MAKE_VARS := TEST=$$(TEST_NAME) FULL_TESTS="$$(FULL_TESTS)"
  395. MAKE_MSG := $$(MSG_MAKE_TEST)
  396. $$(eval $$(call BUILD))
  397. ifneq ($$(MAKE_TARGET),clean)
  398. TEST_EXECUTABLE := $$(TEST_DIR)/$$(TEST_NAME).elf
  399. TESTS += $$(TEST_NAME)
  400. TEST_MSG := $$(MSG_TEST)
  401. $$(TEST_NAME)_COMMAND := \
  402. printf "$$(TEST_MSG)\n"; \
  403. $$(TEST_EXECUTABLE); \
  404. if [ $$$$? -gt 0 ]; \
  405. then error_occurred=1; \
  406. fi; \
  407. printf "\n";
  408. endif
  409. endef
  410. define PARSE_TEST
  411. TESTS :=
  412. TEST_NAME := $$(firstword $$(subst :, ,$$(RULE)))
  413. TEST_TARGET := $$(subst $$(TEST_NAME),,$$(subst $$(TEST_NAME):,,$$(RULE)))
  414. ifeq ($$(TEST_NAME),all)
  415. MATCHED_TESTS := $$(TEST_LIST)
  416. else
  417. MATCHED_TESTS := $$(foreach TEST,$$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME),$$(TEST)),$$(TEST),))
  418. endif
  419. $$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
  420. endef
  421. # Set the silent mode depending on if we are trying to compile multiple keyboards or not
  422. # By default it's on in that case, but it can be overridden by specifying silent=false
  423. # from the command line
  424. define SET_SILENT_MODE
  425. ifdef SUB_IS_SILENT
  426. SILENT_MODE := $(SUB_IS_SILENT)
  427. else ifeq ($$(words $$(COMMANDS)),1)
  428. SILENT_MODE := false
  429. else
  430. SILENT_MODE := true
  431. endif
  432. endef
  433. include $(ROOT_DIR)/message.mk
  434. ifeq ($(strip $(BREAK_ON_ERRORS)), yes)
  435. HANDLE_ERROR = exit 1
  436. else
  437. HANDLE_ERROR = echo $$error_occurred > $(ERROR_FILE)
  438. endif
  439. # The empty line is important here, as it will force a new shell to be created for each command
  440. # Otherwise the command line will become too long with a lot of keyboards and keymaps
  441. define RUN_COMMAND
  442. +error_occurred=0;\
  443. $(COMMAND_$(SILENT_MODE)_$(COMMAND))\
  444. if [ $$error_occurred -gt 0 ]; then $(HANDLE_ERROR); fi;
  445. endef
  446. define RUN_TEST
  447. +error_occurred=0;\
  448. $($(TEST)_COMMAND)\
  449. if [ $$error_occurred -gt 0 ]; then $(HANDLE_ERROR); fi;
  450. endef
  451. # Let's match everything, we handle all the rule parsing ourselves
  452. .PHONY: %
  453. %:
  454. # Check if we have the CMP tool installed
  455. cmp $(ROOT_DIR)/Makefile $(ROOT_DIR)/Makefile >/dev/null 2>&1; if [ $$? -gt 0 ]; then printf "$(MSG_NO_CMP)"; exit 1; fi;
  456. # Check if the submodules are dirty, and display a warning if they are
  457. ifndef SKIP_GIT
  458. if [ ! -e lib/chibios ]; then git submodule sync lib/chibios && git submodule update --init lib/chibios; fi
  459. if [ ! -e lib/chibios-contrib ]; then git submodule sync lib/chibios-contrib && git submodule update --init lib/chibios-contrib; fi
  460. if [ ! -e lib/ugfx ]; then git submodule sync lib/ugfx && git submodule update --init lib/ugfx; fi
  461. git submodule status --recursive 2>/dev/null | \
  462. while IFS= read -r x; do \
  463. case "$$x" in \
  464. \ *) ;; \
  465. *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
  466. esac \
  467. done
  468. endif
  469. rm -f $(ERROR_FILE) > /dev/null 2>&1
  470. $(eval $(call PARSE_RULE,$@))
  471. $(eval $(call SET_SILENT_MODE))
  472. # Run all the commands in the same shell, notice the + at the first line
  473. # it has to be there to allow parallel execution of the submake
  474. # This always tries to compile everything, even if error occurs in the middle
  475. # But we return the error code at the end, to trigger travis failures
  476. $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND))
  477. if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
  478. $(foreach TEST,$(TESTS),$(RUN_TEST))
  479. if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
  480. # These no longer work because of the colon system
  481. # All should compile everything
  482. # .PHONY: all
  483. # all: all-keyboards test-all
  484. # Define some shortcuts, mostly for compatibility with the old syntax
  485. # .PHONY: all-keyboards
  486. # all-keyboards: all\:all\:all
  487. # .PHONY: all-keyboards-defaults
  488. # all-keyboards-defaults: all\:default
  489. # .PHONY: test
  490. # test: test-all
  491. # .PHONY: test-clean
  492. # test-clean: test-all-clean
  493. lib/%:
  494. git submodule sync $?
  495. git submodule update --init $?
  496. git-submodule:
  497. git submodule sync --recursive
  498. git submodule update --init --recursive
  499. ifdef SKIP_VERSION
  500. SKIP_GIT := yes
  501. endif
  502. # Generate the version.h file
  503. ifndef SKIP_GIT
  504. GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
  505. else
  506. GIT_VERSION := NA
  507. endif
  508. ifndef SKIP_VERSION
  509. BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
  510. $(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
  511. $(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
  512. else
  513. BUILD_DATE := NA
  514. endif
  515. include $(ROOT_DIR)/testlist.mk