Makefile 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. ifneq ($(VERBOSE),true)
  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. ifeq ($(shell git rev-parse --is-inside-work-tree 2>/dev/null),)
  18. export SKIP_GIT := yes
  19. export NOT_REPO := yes
  20. endif
  21. ifdef SKIP_VERSION
  22. export SKIP_GIT := yes
  23. endif
  24. ifndef SUB_IS_SILENT
  25. ifndef SKIP_GIT
  26. QMK_VERSION := $(shell git describe --abbrev=0 --tags 2>/dev/null)
  27. endif
  28. ifneq ($(QMK_VERSION),)
  29. $(info QMK Firmware $(QMK_VERSION))
  30. endif
  31. endif
  32. # Determine which qmk cli to use
  33. QMK_BIN := qmk
  34. # Try to determine userspace from qmk config, if set. Handle direct query on qmk_cli>=1.1.7
  35. # falling back to legacy method of only supporting user.overlay_dir config
  36. # sort is used to buffer 'qmk env' output and avoid BrokenPipeError errors
  37. export override QMK_USERSPACE := $(shell \
  38. $(QMK_BIN) env | sort | grep -q QMK_USERSPACE \
  39. && $(QMK_BIN) env QMK_USERSPACE \
  40. || $(QMK_BIN) config -ro user.overlay_dir | cut -d= -f2 | sed -e 's@^None$$@@g')
  41. # avoid 'Entering|Leaving directory' messages
  42. MAKEFLAGS += --no-print-directory
  43. ON_ERROR := error_occurred=1
  44. BREAK_ON_ERRORS = no
  45. ROOT_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
  46. ifeq ($(ROOT_DIR),)
  47. ROOT_DIR := .
  48. endif
  49. include paths.mk
  50. include $(BUILDDEFS_PATH)/support.mk
  51. TEST_OUTPUT_DIR := $(BUILD_DIR)/test
  52. ERROR_FILE := $(BUILD_DIR)/error_occurred
  53. .DEFAULT_GOAL := all:all
  54. # Compare the start of the RULE variable with the first argument($1)
  55. # If the rules equals $1 or starts with $1:, RULE_FOUND is set to true
  56. # and $1 is removed from the RULE variable
  57. # Otherwise the RULE_FOUND variable is set to false, and RULE left as it was
  58. # The function is a bit tricky, since there's no built in $(startswith) function
  59. define COMPARE_AND_REMOVE_FROM_RULE_HELPER
  60. ifeq ($1,$$(RULE))
  61. RULE:=
  62. RULE_FOUND := true
  63. else
  64. STARTCOLON_REMOVED=$$(subst START$1:,,START$$(RULE))
  65. ifneq ($$(STARTCOLON_REMOVED),START$$(RULE))
  66. RULE_FOUND := true
  67. RULE := $$(STARTCOLON_REMOVED)
  68. else
  69. RULE_FOUND := false
  70. endif
  71. endif
  72. endef
  73. # This makes it easier to call COMPARE_AND_REMOVE_FROM_RULE, since it makes it behave like
  74. # a function that returns the value
  75. COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND)
  76. # Try to find a match for the start of the rule to be checked
  77. # $1 The list to be checked
  78. # If a match is found, then RULE_FOUND is set to true
  79. # and MATCHED_ITEM to the item that was matched
  80. define TRY_TO_MATCH_RULE_FROM_LIST_HELPER
  81. # Split on ":", padding with empty strings to avoid indexing issues
  82. TOKEN1:=$$(shell python3 -c "import sys; print((sys.argv[1].split(':',1)+[''])[0])" $$(RULE))
  83. TOKENr:=$$(shell python3 -c "import sys; print((sys.argv[1].split(':',1)+[''])[1])" $$(RULE))
  84. FOUNDx:=$$(shell echo $1 | tr " " "\n" | grep -Fx $$(TOKEN1))
  85. ifneq ($$(FOUNDx),)
  86. RULE := $$(TOKENr)
  87. RULE_FOUND := true
  88. MATCHED_ITEM := $$(TOKEN1)
  89. else
  90. RULE_FOUND := false
  91. MATCHED_ITEM :=
  92. endif
  93. endef
  94. # Make it easier to call TRY_TO_MATCH_RULE_FROM_LIST
  95. TRY_TO_MATCH_RULE_FROM_LIST = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$1))$(RULE_FOUND)
  96. # As TRY_TO_MATCH_RULE_FROM_LIST_HELPER, but with additional
  97. # resolution of keyboard_aliases.hjson for provided rule
  98. define TRY_TO_MATCH_RULE_FROM_LIST_HELPER_KB
  99. # Split on ":", padding with empty strings to avoid indexing issues
  100. TOKEN1:=$$(shell python3 -c "import sys; print((sys.argv[1].split(':',1)+[''])[0])" $$(RULE))
  101. TOKENr:=$$(shell python3 -c "import sys; print((sys.argv[1].split(':',1)+[''])[1])" $$(RULE))
  102. TOKEN1:=$$(shell $(QMK_BIN) resolve-alias --allow-unknown $$(TOKEN1))
  103. FOUNDx:=$$(shell echo $1 | tr " " "\n" | grep -Fx $$(TOKEN1))
  104. ifneq ($$(FOUNDx),)
  105. RULE := $$(TOKENr)
  106. RULE_FOUND := true
  107. MATCHED_ITEM := $$(TOKEN1)
  108. else
  109. RULE_FOUND := false
  110. MATCHED_ITEM :=
  111. endif
  112. endef
  113. # Make it easier to call TRY_TO_MATCH_RULE_FROM_LIST_KB
  114. TRY_TO_MATCH_RULE_FROM_LIST_KB = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER_KB,$1))$(RULE_FOUND)
  115. define ALL_IN_LIST_LOOP
  116. OLD_RULE$1 := $$(RULE)
  117. $$(eval $$(call $1,$$(ITEM$1)))
  118. RULE := $$(OLD_RULE$1)
  119. endef
  120. define PARSE_ALL_IN_LIST
  121. $$(foreach ITEM$1,$2,$$(eval $$(call ALL_IN_LIST_LOOP,$1)))
  122. endef
  123. # The entry point for rule parsing
  124. # parses a rule in the format <keyboard>:<keymap>:<target>
  125. # but this particular function only deals with the first <keyboard> part
  126. define PARSE_RULE
  127. RULE := $1
  128. COMMANDS :=
  129. # If the rule starts with all, then continue the parsing from
  130. # PARSE_ALL_KEYBOARDS
  131. ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,all),true)
  132. KEYBOARD_RULE=all
  133. $$(eval $$(call PARSE_ALL_KEYBOARDS))
  134. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,test),true)
  135. $$(eval $$(call PARSE_TEST))
  136. # If the rule starts with the name of a known keyboard, then continue
  137. # the parsing from PARSE_KEYBOARD
  138. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST_KB,$$(shell $(QMK_BIN) list-keyboards)),true)
  139. KEYBOARD_RULE=$$(MATCHED_ITEM)
  140. $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM)))
  141. else
  142. $$(info make: *** No rule to make target '$1'. Stop.)
  143. $$(info |)
  144. $$(info | QMK's make format is:)
  145. $$(info | make keyboard_folder:keymap_folder[:target])
  146. $$(info |)
  147. $$(info | Where `keyboard_folder` is the path to the keyboard relative to)
  148. $$(info | `qmk_firmware/keyboards/`, and `keymap_folder` is the name of the)
  149. $$(info | keymap folder under that board's `keymaps/` directory.)
  150. $$(info |)
  151. $$(info | Examples:)
  152. $$(info | keyboards/dz60, keyboards/dz60/keymaps/default)
  153. $$(info | -> make dz60:default)
  154. $$(info | -> qmk compile -kb dz60 -km default)
  155. $$(info | keyboards/planck/rev6, keyboards/planck/keymaps/default)
  156. $$(info | -> make planck/rev6:default:flash)
  157. $$(info | -> qmk flash -kb planck/rev6 -km default)
  158. $$(info |)
  159. endif
  160. endef
  161. # $1 = Keyboard
  162. # Parses a rule in the format <keymap>:<target>
  163. # the keyboard is already known when entering this function
  164. define PARSE_KEYBOARD
  165. CURRENT_KB := $1
  166. KEYMAPS := $(shell $(QMK_BIN) list-keymaps --keyboard $1)
  167. # if the rule after removing the start of it is empty (we haven't specified a kemap or target)
  168. # compile all the keymaps
  169. ifeq ($$(RULE),)
  170. $$(eval $$(call PARSE_ALL_KEYMAPS))
  171. # The same if all was specified
  172. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,all),true)
  173. $$(eval $$(call PARSE_ALL_KEYMAPS))
  174. # List all keymaps for the given keyboard
  175. else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,list-keymaps),true)
  176. $$(eval $$(call LIST_ALL_KEYMAPS))
  177. # Try to match the specified keyamp with the list of known keymaps
  178. else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYMAPS)),true)
  179. $$(eval $$(call PARSE_KEYMAP,$$(MATCHED_ITEM)))
  180. # Otherwise try to match the keymap from the current folder, or arguments to the make command
  181. else ifneq ($$(KEYMAP),)
  182. $$(eval $$(call PARSE_KEYMAP,$$(KEYMAP)))
  183. # Otherwise if we are running make all:<user> just skip
  184. else ifeq ($$(KEYBOARD_RULE),all)
  185. # $$(info Skipping: No user keymap for $$(CURRENT_KB))
  186. # Otherwise, make all keymaps, again this is consistent with how it works without
  187. # any arguments
  188. else
  189. $$(eval $$(call PARSE_ALL_KEYMAPS))
  190. endif
  191. endef
  192. # if we are going to compile all keyboards, match the rest of the rule
  193. # for each of them
  194. define PARSE_ALL_KEYBOARDS
  195. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(shell $(QMK_BIN) list-keyboards)))
  196. endef
  197. # Prints a list of all known keymaps for the given keyboard
  198. define LIST_ALL_KEYMAPS
  199. COMMAND_true_LIST_KEYMAPS := \
  200. printf "$$(KEYMAPS)\n";
  201. COMMAND_false_LIST_KEYMAPS := \
  202. printf "$$(MSG_AVAILABLE_KEYMAPS)\n"; \
  203. printf "$$(KEYMAPS)\n";
  204. COMMANDS += LIST_KEYMAPS
  205. endef
  206. # $1 Keymap
  207. # This is the meat of compiling a keyboard, when entering this, everything is known
  208. # keyboard, subproject, and keymap
  209. # Note that we are not directly calling the command here, but instead building a list,
  210. # which will later be processed
  211. define PARSE_KEYMAP
  212. CURRENT_KM = $1
  213. # The rest of the rule is the target
  214. # Remove the leading ":" from the target, as it acts as a separator
  215. MAKE_TARGET := $$(patsubst :%,%,$$(RULE))
  216. # We need to generate an unique identifier to append to the COMMANDS list
  217. CURRENT_KB_UNDER := $$(subst /,_,$$(CURRENT_KB))
  218. COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB_UNDER)_KEYMAP_$$(CURRENT_KM)
  219. # If we are compiling a keyboard without a subproject, we want to display just the name
  220. # of the keyboard, otherwise keyboard/subproject
  221. KB_SP := $$(CURRENT_KB)
  222. # Format it in bold
  223. KB_SP := $(BOLD)$$(KB_SP)$(NO_COLOR)
  224. # Specify the variables that we are passing forward to submake
  225. MAKE_VARS := KEYBOARD=$$(CURRENT_KB) KEYMAP=$$(CURRENT_KM) QMK_BIN=$$(QMK_BIN)
  226. # And the first part of the make command
  227. MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f $(BUILDDEFS_PATH)/build_keyboard.mk $$(MAKE_TARGET)
  228. # The message to display
  229. MAKE_MSG := $$(MSG_MAKE_KB)
  230. # We run the command differently, depending on if we want more output or not
  231. # The true version for silent output and the false version otherwise
  232. $$(eval $$(call BUILD))
  233. endef
  234. define BUILD
  235. MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
  236. COMMANDS += $$(COMMAND)
  237. COMMAND_true_$$(COMMAND) := \
  238. printf "$$(MAKE_MSG)" | \
  239. $$(MAKE_MSG_FORMAT); \
  240. LOG=$$$$($$(MAKE_CMD) $$(MAKE_VARS) SILENT=true 2>&1) ; \
  241. if [ $$$$? -gt 0 ]; \
  242. then $$(PRINT_ERROR_PLAIN); \
  243. elif [ "$$$$LOG" = "skipped" ] ; \
  244. then $$(PRINT_SKIPPED_PLAIN); \
  245. elif [ "$$$$LOG" != "" ] ; \
  246. then $$(PRINT_WARNING_PLAIN); \
  247. else \
  248. $$(PRINT_OK); \
  249. fi;
  250. COMMAND_false_$$(COMMAND) := \
  251. printf "$$(MAKE_MSG)\n\n"; \
  252. $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false; \
  253. if [ $$$$? -gt 0 ]; \
  254. then error_occurred=1; \
  255. fi;
  256. endef
  257. # Just parse all the keymaps for a specific keyboard
  258. define PARSE_ALL_KEYMAPS
  259. $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
  260. endef
  261. define BUILD_TEST
  262. TEST_PATH := $1
  263. TEST_NAME := $$(notdir $$(TEST_PATH))
  264. TEST_ID := $$(patsubst ./tests/%,%,$$(TEST_PATH))
  265. TEST_FULL_NAME := $$(subst /,_,$$(patsubst $$(ROOT_DIR)tests/%,%,$$(TEST_PATH)))
  266. MAKE_TARGET := $2
  267. COMMAND := $1
  268. MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f $(BUILDDEFS_PATH)/build_test.mk $$(MAKE_TARGET)
  269. MAKE_VARS := TEST=$$(TEST_NAME) TEST_OUTPUT=$$(TEST_FULL_NAME) TEST_PATH=$$(TEST_PATH) FULL_TESTS="$$(FULL_TESTS)"
  270. MAKE_MSG := $$(MSG_MAKE_TEST)
  271. $$(eval $$(call BUILD))
  272. ifneq ($$(MAKE_TARGET),clean)
  273. TEST_EXECUTABLE := $$(TEST_OUTPUT_DIR)/$$(TEST_FULL_NAME).elf
  274. TESTS += $$(TEST_FULL_NAME)
  275. TEST_MSG := $$(MSG_TEST)
  276. $$(TEST_FULL_NAME)_COMMAND := \
  277. printf "$$(TEST_MSG)\n"; \
  278. $$(TEST_EXECUTABLE) --gtest_color=$$(COLOR); \
  279. if [ $$$$? -gt 0 ]; \
  280. then error_occurred=1; \
  281. fi; \
  282. printf "\n";
  283. endif
  284. endef
  285. define LIST_TEST
  286. include $(BUILDDEFS_PATH)/testlist.mk
  287. FOUND_TESTS := $$(patsubst ./tests/%,%,$$(TEST_LIST))
  288. $$(info $$(FOUND_TESTS))
  289. endef
  290. define PARSE_TEST
  291. TESTS :=
  292. TEST_NAME := $$(firstword $$(subst :, ,$$(RULE)))
  293. TEST_TARGET := $$(subst $$(TEST_NAME),,$$(subst $$(TEST_NAME):,,$$(RULE)))
  294. include $(BUILDDEFS_PATH)/testlist.mk
  295. ifeq ($$(TEST_NAME),all)
  296. MATCHED_TESTS := $$(TEST_LIST)
  297. else
  298. MATCHED_TESTS := $$(foreach TEST, $$(TEST_LIST),$$(if $$(findstring x$$(TEST_NAME)x, x$$(patsubst ./tests/%,%,$$(TEST)x)), $$(TEST),))
  299. endif
  300. $$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
  301. endef
  302. # Set the silent mode depending on if we are trying to compile multiple keyboards or not
  303. # By default it's on in that case, but it can be overridden by specifying silent=false
  304. # from the command line
  305. define SET_SILENT_MODE
  306. ifdef SUB_IS_SILENT
  307. SILENT_MODE := $(SUB_IS_SILENT)
  308. else ifeq ($$(words $$(COMMANDS)),1)
  309. SILENT_MODE := false
  310. else
  311. SILENT_MODE := true
  312. endif
  313. endef
  314. include $(BUILDDEFS_PATH)/message.mk
  315. ifeq ($(strip $(BREAK_ON_ERRORS)), yes)
  316. HANDLE_ERROR = exit 1
  317. else
  318. HANDLE_ERROR = echo $$error_occurred > $(ERROR_FILE)
  319. endif
  320. # The empty line is important here, as it will force a new shell to be created for each command
  321. # Otherwise the command line will become too long with a lot of keyboards and keymaps
  322. define RUN_COMMAND
  323. +error_occurred=0;\
  324. $(COMMAND_$(SILENT_MODE)_$(COMMAND))\
  325. if [ $$error_occurred -gt 0 ]; then $(HANDLE_ERROR); fi;
  326. endef
  327. define RUN_TEST
  328. +error_occurred=0;\
  329. $($(TEST)_COMMAND)\
  330. if [ $$error_occurred -gt 0 ]; then $(HANDLE_ERROR); fi;
  331. endef
  332. # Catch everything and parse the command line ourselves.
  333. .PHONY: %
  334. %:
  335. # Ensure that $(QMK_BIN) works.
  336. if ! $(QMK_BIN) hello 1> /dev/null 2>&1; then printf "$(MSG_PYTHON_MISSING)"; exit 1; fi
  337. ifdef NOT_REPO
  338. printf "$(MSG_NOT_REPO)"
  339. endif
  340. ifndef SKIP_GIT
  341. $(QMK_BIN) git-submodule --sync
  342. # Check if the submodules are dirty, and display a warning if they are
  343. if ! $(QMK_BIN) git-submodule --check 1> /dev/null 2>&1; then printf "$(MSG_SUBMODULE_DIRTY)"; fi
  344. endif
  345. rm -f $(ERROR_FILE) > /dev/null 2>&1
  346. $(eval $(call PARSE_RULE,$@))
  347. $(eval $(call SET_SILENT_MODE))
  348. # Run all the commands in the same shell, notice the + at the first line
  349. # it has to be there to allow parallel execution of the submake
  350. # This always tries to compile everything, even if error occurs in the middle
  351. # But we return the error code at the end, to trigger travis failures
  352. # The sort at this point is to remove duplicates
  353. $(foreach COMMAND,$(sort $(COMMANDS)),$(RUN_COMMAND))
  354. if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
  355. $(foreach TEST,$(sort $(TESTS)),$(RUN_TEST))
  356. if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
  357. lib/%:
  358. git submodule sync $?
  359. git submodule update --init $?
  360. .PHONY: git-submodule
  361. git-submodule:
  362. $(QMK_BIN) git-submodule
  363. .PHONY: git-submodules
  364. git-submodules: git-submodule
  365. .PHONY: list-keyboards
  366. list-keyboards:
  367. $(QMK_BIN) list-keyboards | tr '\n' ' '
  368. .PHONY: list-tests
  369. list-tests:
  370. $(eval $(call LIST_TEST))
  371. .PHONY: generate-keyboards-file
  372. generate-keyboards-file:
  373. $(QMK_BIN) list-keyboards
  374. .PHONY: clean
  375. clean:
  376. echo -n 'Deleting .build/ ... '
  377. rm -rf $(BUILD_DIR)
  378. echo 'done.'
  379. .PHONY: distclean distclean_qmk
  380. distclean: distclean_qmk
  381. distclean_qmk: clean
  382. echo -n 'Deleting *.bin, *.hex, and *.uf2 ... '
  383. rm -f *.bin *.hex *.uf2
  384. echo 'done.'
  385. ifneq ($(QMK_USERSPACE),)
  386. .PHONY: distclean_userspace
  387. distclean: distclean_userspace
  388. distclean_userspace: clean
  389. echo -n 'Deleting userspace *.bin, *.hex, and *.uf2 ... '
  390. rm -f $(QMK_USERSPACE)/*.bin $(QMK_USERSPACE)/*.hex $(QMK_USERSPACE)/*.uf2
  391. echo 'done.'
  392. endif
  393. # Extra targets for formatting and/or pytest, running within the qmk/qmk_cli container to match GHA.
  394. .PHONY: format-core
  395. format-core:
  396. RUNTIME=docker ./util/docker_cmd.sh qmk format-c --core-only -a
  397. RUNTIME=docker ./util/docker_cmd.sh qmk format-python -a
  398. .PHONY: pytest
  399. pytest:
  400. RUNTIME=docker ./util/docker_cmd.sh qmk pytest
  401. .PHONY: format-and-pytest
  402. format-and-pytest:
  403. RUNTIME=docker ./util/docker_cmd.sh qmk format-c --core-only -a
  404. RUNTIME=docker ./util/docker_cmd.sh qmk format-python -a
  405. RUNTIME=docker ./util/docker_cmd.sh qmk pytest