mass_compile.py 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.search import search_keymap_targets
  11. @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.")
  12. @cli.argument('-t', '--no-temp', arg_only=True, action='store_true', help="Remove temporary files during build.")
  13. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
  14. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  15. @cli.argument(
  16. '-f',
  17. '--filter',
  18. arg_only=True,
  19. action='append',
  20. default=[],
  21. help= # noqa: `format-python` and `pytest` don't agree here.
  22. "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.
  23. )
  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 mass_compile(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. if len(cli.args.builds) > 0:
  36. targets = [(e[0], e[1]) for e in [b.split(':') for b in cli.args.builds]]
  37. else:
  38. targets = search_keymap_targets(cli.args.keymap, cli.args.filter)
  39. if len(targets) == 0:
  40. return
  41. builddir.mkdir(parents=True, exist_ok=True)
  42. with open(makefile, "w") as f:
  43. for target in sorted(targets):
  44. keyboard_name = target[0]
  45. keymap_name = target[1]
  46. keyboard_safe = keyboard_name.replace('/', '_')
  47. # yapf: disable
  48. f.write(
  49. f"""\
  50. all: {keyboard_safe}_{keymap_name}_binary
  51. {keyboard_safe}_{keymap_name}_binary:
  52. @rm -f "{QMK_FIRMWARE}/.build/failed.log.{keyboard_safe}.{keymap_name}" || true
  53. @echo "Compiling QMK Firmware for target: '{keyboard_name}:{keymap_name}'..." >>"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}"
  54. +@$(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)} \\
  55. >>"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" 2>&1 \\
  56. || cp "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" "{QMK_FIRMWARE}/.build/failed.log.{os.getpid()}.{keyboard_safe}.{keymap_name}"
  57. @{{ 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}" ; }} \\
  58. || {{ 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}" ; }} \\
  59. || printf "Build %-64s \e[1;32m[OK]\e[0m\\n" "{keyboard_name}:{keymap_name}"
  60. @rm -f "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" || true
  61. """# noqa
  62. )
  63. # yapf: enable
  64. if cli.args.no_temp:
  65. # yapf: disable
  66. f.write(
  67. f"""\
  68. @rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.elf" 2>/dev/null || true
  69. @rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.map" 2>/dev/null || true
  70. @rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}" || true
  71. @rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}_{keymap_name}" || true
  72. """# noqa
  73. )
  74. # yapf: enable
  75. f.write('\n')
  76. cli.run([make_cmd, *get_make_parallel_args(cli.args.parallel), '-f', makefile.as_posix(), 'all'], capture_output=False, stdin=DEVNULL)
  77. # Check for failures
  78. failures = [f for f in builddir.glob(f'failed.log.{os.getpid()}.*')]
  79. if len(failures) > 0:
  80. return False