mass_compile.py 5.0 KB

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