mass_compile.py 5.0 KB

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