mass_compile.py 5.9 KB

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