multibuild.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Compile all keyboards.
  2. This will compile everything in parallel, for testing purposes.
  3. """
  4. import re
  5. from pathlib import Path
  6. from subprocess import DEVNULL
  7. from milc import cli
  8. from qmk.constants import QMK_FIRMWARE
  9. from qmk.commands import _find_make
  10. import qmk.keyboard
  11. def _make_rules_mk_filter(key, value):
  12. def _rules_mk_filter(keyboard_name):
  13. rules_mk = qmk.keyboard.rules_mk(keyboard_name)
  14. return True if key in rules_mk and rules_mk[key].lower() == str(value).lower() else False
  15. return _rules_mk_filter
  16. def _is_split(keyboard_name):
  17. rules_mk = qmk.keyboard.rules_mk(keyboard_name)
  18. return True if 'SPLIT_KEYBOARD' in rules_mk and rules_mk['SPLIT_KEYBOARD'].lower() == 'yes' else False
  19. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.")
  20. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  21. @cli.argument('-f', '--filter', arg_only=True, action='append', default=[], help="Filter the list of keyboards based on the supplied value in rules.mk. Supported format is 'SPLIT_KEYBOARD=yes'. May be passed multiple times.")
  22. @cli.subcommand('Compile QMK Firmware for all keyboards.', hidden=False if cli.config.user.developer else True)
  23. def multibuild(cli):
  24. """Compile QMK Firmware against all keyboards.
  25. """
  26. make_cmd = _find_make()
  27. if cli.args.clean:
  28. cli.run([make_cmd, 'clean'], capture_output=False, stdin=DEVNULL)
  29. builddir = Path(QMK_FIRMWARE) / '.build'
  30. makefile = builddir / 'parallel_kb_builds.mk'
  31. keyboard_list = qmk.keyboard.list_keyboards()
  32. filter_re = re.compile(r'^(?P<key>[A-Z0-9_]+)\s*=\s*(?P<value>[^#]+)$')
  33. for filter_txt in cli.args.filter:
  34. f = filter_re.match(filter_txt)
  35. if f is not None:
  36. keyboard_list = filter(_make_rules_mk_filter(f.group('key'), f.group('value')), keyboard_list)
  37. keyboard_list = list(sorted(keyboard_list))
  38. if len(keyboard_list) == 0:
  39. return
  40. builddir.mkdir(parents=True, exist_ok=True)
  41. with open(makefile, "w") as f:
  42. for keyboard_name in keyboard_list:
  43. keyboard_safe = keyboard_name.replace('/', '_')
  44. # yapf: disable
  45. f.write(
  46. f"""\
  47. all: {keyboard_safe}_binary
  48. {keyboard_safe}_binary:
  49. @rm -f "{QMK_FIRMWARE}/.build/failed.log.{keyboard_safe}" || true
  50. +@$(MAKE) -C "{QMK_FIRMWARE}" -f "{QMK_FIRMWARE}/build_keyboard.mk" KEYBOARD="{keyboard_name}" KEYMAP="default" REQUIRE_PLATFORM_KEY= COLOR=true SILENT=false \\
  51. >>"{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" 2>&1 \\
  52. || cp "{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" "{QMK_FIRMWARE}/.build/failed.log.{keyboard_safe}"
  53. @{{ grep '\[ERRORS\]' "{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" >/dev/null 2>&1 && printf "Build %-64s \e[1;31m[ERRORS]\e[0m\\n" "{keyboard_name}:default" ; }} \\
  54. || {{ grep '\[WARNINGS\]' "{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" >/dev/null 2>&1 && printf "Build %-64s \e[1;33m[WARNINGS]\e[0m\\n" "{keyboard_name}:default" ; }} \\
  55. || printf "Build %-64s \e[1;32m[OK]\e[0m\\n" "{keyboard_name}:default"
  56. @rm -f "{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" || true
  57. """# noqa
  58. )
  59. # yapf: enable
  60. cli.run([make_cmd, '-j', str(cli.args.parallel), '-f', makefile, 'all'], capture_output=False, stdin=DEVNULL)