multibuild.py 4.1 KB

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