Makefile 16 KB

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