mass_compile.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """Compile all keyboards.
  2. This will compile everything in parallel, for testing purposes.
  3. """
  4. import os
  5. from typing import List
  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, build_environment
  11. from qmk.search import search_keymap_targets, search_make_targets
  12. from qmk.build_targets import BuildTarget, JsonKeymapBuildTarget
  13. from qmk.util import maybe_exit_config
  14. def mass_compile_targets(targets: List[BuildTarget], clean: bool, dry_run: bool, no_temp: bool, parallel: int, **env):
  15. if len(targets) == 0:
  16. return
  17. make_cmd = find_make()
  18. builddir = Path(QMK_FIRMWARE) / '.build'
  19. makefile = builddir / 'parallel_kb_builds.mk'
  20. if dry_run:
  21. cli.log.info('Compilation targets:')
  22. for target in sorted(targets, key=lambda t: (t.keyboard, t.keymap)):
  23. cli.log.info(f"{{fg_cyan}}qmk compile -kb {target.keyboard} -km {target.keymap}{{fg_reset}}")
  24. else:
  25. if clean:
  26. cli.run([make_cmd, 'clean'], capture_output=False, stdin=DEVNULL)
  27. builddir.mkdir(parents=True, exist_ok=True)
  28. with open(makefile, "w") as f:
  29. for target in sorted(targets, key=lambda t: (t.keyboard, t.keymap)):
  30. keyboard_name = target.keyboard
  31. keymap_name = target.keymap
  32. target.configure(parallel=1) # We ignore parallelism on a per-build basis as we defer to the parent make invocation
  33. target.prepare_build(**env) # If we've got json targets, allow them to write out any extra info to .build before we kick off `make`
  34. command = target.compile_command(**env)
  35. command[0] = '+@$(MAKE)' # Override the make so that we can use jobserver to handle parallelism
  36. keyboard_safe = keyboard_name.replace('/', '_')
  37. build_log = f"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}"
  38. failed_log = f"{QMK_FIRMWARE}/.build/failed.log.{os.getpid()}.{keyboard_safe}.{keymap_name}"
  39. # yapf: disable
  40. f.write(
  41. f"""\
  42. all: {keyboard_safe}_{keymap_name}_binary
  43. {keyboard_safe}_{keymap_name}_binary:
  44. @rm -f "{build_log}" || true
  45. @echo "Compiling QMK Firmware for target: '{keyboard_name}:{keymap_name}'..." >>"{build_log}"
  46. {' '.join(command)} \\
  47. >>"{build_log}" 2>&1 \\
  48. || cp "{build_log}" "{failed_log}"
  49. @{{ grep '\\[ERRORS\\]' "{build_log}" >/dev/null 2>&1 && printf "Build %-64s \\e[1;31m[ERRORS]\\e[0m\\n" "{keyboard_name}:{keymap_name}" ; }} \\
  50. || {{ grep '\\[WARNINGS\\]' "{build_log}" >/dev/null 2>&1 && printf "Build %-64s \\e[1;33m[WARNINGS]\\e[0m\\n" "{keyboard_name}:{keymap_name}" ; }} \\
  51. || printf "Build %-64s \\e[1;32m[OK]\\e[0m\\n" "{keyboard_name}:{keymap_name}"
  52. @rm -f "{build_log}" || true
  53. """# noqa
  54. )
  55. # yapf: enable
  56. if no_temp:
  57. # yapf: disable
  58. f.write(
  59. f"""\
  60. @rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.elf" 2>/dev/null || true
  61. @rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.map" 2>/dev/null || true
  62. @rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}_{keymap_name}" || true
  63. """# noqa
  64. )
  65. # yapf: enable
  66. f.write('\n')
  67. cli.run([find_make(), *get_make_parallel_args(parallel), '-f', makefile.as_posix(), 'all'], capture_output=False, stdin=DEVNULL)
  68. # Check for failures
  69. failures = [f for f in builddir.glob(f'failed.log.{os.getpid()}.*')]
  70. if len(failures) > 0:
  71. return False
  72. @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.")
  73. @cli.argument('-t', '--no-temp', arg_only=True, action='store_true', help="Remove temporary files during build.")
  74. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
  75. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  76. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the commands to be run.")
  77. @cli.argument(
  78. '-f',
  79. '--filter',
  80. arg_only=True,
  81. action='append',
  82. default=[],
  83. help= # noqa: `format-python` and `pytest` don't agree here.
  84. "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.
  85. )
  86. @cli.argument('-km', '--keymap', type=str, default='default', help="The keymap name to build. Default is 'default'.")
  87. @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
  88. @cli.subcommand('Compile QMK Firmware for all keyboards.', hidden=False if cli.config.user.developer else True)
  89. def mass_compile(cli):
  90. """Compile QMK Firmware against all keyboards.
  91. """
  92. maybe_exit_config(should_exit=False, should_reraise=True)
  93. if len(cli.args.builds) > 0:
  94. json_like_targets = list([Path(p) for p in filter(lambda e: Path(e).exists() and Path(e).suffix == '.json', cli.args.builds)])
  95. make_like_targets = list(filter(lambda e: Path(e) not in json_like_targets, cli.args.builds))
  96. targets = search_make_targets(make_like_targets)
  97. targets.extend([JsonKeymapBuildTarget(e) for e in json_like_targets])
  98. else:
  99. targets = search_keymap_targets([('all', cli.config.mass_compile.keymap)], cli.args.filter)
  100. return mass_compile_targets(targets, cli.args.clean, cli.args.dry_run, cli.args.no_temp, cli.config.mass_compile.parallel, **build_environment(cli.args.env))