mass_compile.py 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """Compile all keyboards.
  2. This will compile everything in parallel, for testing purposes.
  3. """
  4. import os
  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, get_make_parallel_args
  10. from qmk.keyboard import resolve_keyboard
  11. from qmk.search import search_keymap_targets
  12. @cli.argument('builds', nargs='*', arg_only=True, help="List of builds in form <keyboard>:<keymap> to compile in parallel. Specifying this overrides all other target search options.")
  13. @cli.argument('-t', '--no-temp', arg_only=True, action='store_true', help="Remove temporary files during build.")
  14. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
  15. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  16. @cli.argument(
  17. '-f',
  18. '--filter',
  19. arg_only=True,
  20. action='append',
  21. default=[],
  22. help= # noqa: `format-python` and `pytest` don't agree here.
  23. "Filter the list of keyboards based on the supplied value in rules.mk. Matches info.json structure, and accepts the formats 'features.rgblight=true' or 'exists(matrix_pins.direct)'. May be passed multiple times, all filters need to match. Value may include wildcards such as '*' and '?'." # noqa: `format-python` and `pytest` don't agree here.
  24. )
  25. @cli.argument('-km', '--keymap', type=str, default='default', help="The keymap name to build. Default is 'default'.")
  26. @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
  27. @cli.subcommand('Compile QMK Firmware for all keyboards.', hidden=False if cli.config.user.developer else True)
  28. def mass_compile(cli):
  29. """Compile QMK Firmware against all keyboards.
  30. """
  31. make_cmd = _find_make()
  32. if cli.args.clean:
  33. cli.run([make_cmd, 'clean'], capture_output=False, stdin=DEVNULL)
  34. builddir = Path(QMK_FIRMWARE) / '.build'
  35. makefile = builddir / 'parallel_kb_builds.mk'
  36. if len(cli.args.builds) > 0:
  37. targets = list(sorted(set([(resolve_keyboard(e[0]), e[1]) for e in [b.split(':') for b in cli.args.builds]])))
  38. else:
  39. targets = search_keymap_targets(cli.args.keymap, cli.args.filter)
  40. if len(targets) == 0:
  41. return
  42. builddir.mkdir(parents=True, exist_ok=True)
  43. with open(makefile, "w") as f:
  44. for target in sorted(targets):
  45. keyboard_name = target[0]
  46. keymap_name = target[1]
  47. keyboard_safe = keyboard_name.replace('/', '_')
  48. # yapf: disable
  49. f.write(
  50. f"""\
  51. all: {keyboard_safe}_{keymap_name}_binary
  52. {keyboard_safe}_{keymap_name}_binary:
  53. @rm -f "{QMK_FIRMWARE}/.build/failed.log.{keyboard_safe}.{keymap_name}" || true
  54. @echo "Compiling QMK Firmware for target: '{keyboard_name}:{keymap_name}'..." >>"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}"
  55. +@$(MAKE) -C "{QMK_FIRMWARE}" -f "{QMK_FIRMWARE}/builddefs/build_keyboard.mk" KEYBOARD="{keyboard_name}" KEYMAP="{keymap_name}" COLOR=true SILENT=false {' '.join(cli.args.env)} \\
  56. >>"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" 2>&1 \\
  57. || cp "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" "{QMK_FIRMWARE}/.build/failed.log.{os.getpid()}.{keyboard_safe}.{keymap_name}"
  58. @{{ grep '\[ERRORS\]' "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" >/dev/null 2>&1 && printf "Build %-64s \e[1;31m[ERRORS]\e[0m\\n" "{keyboard_name}:{keymap_name}" ; }} \\
  59. || {{ grep '\[WARNINGS\]' "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" >/dev/null 2>&1 && printf "Build %-64s \e[1;33m[WARNINGS]\e[0m\\n" "{keyboard_name}:{keymap_name}" ; }} \\
  60. || printf "Build %-64s \e[1;32m[OK]\e[0m\\n" "{keyboard_name}:{keymap_name}"
  61. @rm -f "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" || true
  62. """# noqa
  63. )
  64. # yapf: enable
  65. if cli.args.no_temp:
  66. # yapf: disable
  67. f.write(
  68. f"""\
  69. @rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.elf" 2>/dev/null || true
  70. @rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.map" 2>/dev/null || true
  71. @rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}" || true
  72. @rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}_{keymap_name}" || true
  73. """# noqa
  74. )
  75. # yapf: enable
  76. f.write('\n')
  77. cli.run([make_cmd, *get_make_parallel_args(cli.args.parallel), '-f', makefile.as_posix(), 'all'], capture_output=False, stdin=DEVNULL)
  78. # Check for failures
  79. failures = [f for f in builddir.glob(f'failed.log.{os.getpid()}.*')]
  80. if len(failures) > 0:
  81. return False