compile.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """Compile a QMK Firmware.
  2. You can compile a keymap already in the repo or using a QMK Configurator export.
  3. """
  4. from argcomplete.completers import FilesCompleter
  5. from milc import cli
  6. import qmk.path
  7. from qmk.decorators import automagic_keyboard, automagic_keymap
  8. from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json, build_environment
  9. from qmk.keyboard import keyboard_completer, keyboard_folder_or_all, is_all_keyboards
  10. from qmk.keymap import keymap_completer, locate_keymap
  11. def _is_keymap_target(keyboard, keymap):
  12. if keymap == 'all':
  13. return True
  14. if locate_keymap(keyboard, keymap):
  15. return True
  16. return False
  17. @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='The configurator export to compile')
  18. @cli.argument('-kb', '--keyboard', type=keyboard_folder_or_all, completer=keyboard_completer, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
  19. @cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
  20. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
  21. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
  22. @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
  23. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  24. @cli.subcommand('Compile a QMK Firmware.')
  25. @automagic_keyboard
  26. @automagic_keymap
  27. def compile(cli):
  28. """Compile a QMK Firmware.
  29. If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists.
  30. If a keyboard and keymap are provided this command will build a firmware based on that.
  31. """
  32. if is_all_keyboards(cli.args.keyboard):
  33. from .mass_compile import mass_compile
  34. cli.args.builds = []
  35. cli.args.filter = []
  36. cli.args.no_temp = False
  37. return mass_compile(cli)
  38. # Build the environment vars
  39. envs = build_environment(cli.args.env)
  40. # Determine the compile command
  41. commands = []
  42. if cli.args.filename:
  43. # If a configurator JSON was provided generate a keymap and compile it
  44. user_keymap = parse_configurator_json(cli.args.filename)
  45. commands = [compile_configurator_json(user_keymap, parallel=cli.config.compile.parallel, clean=cli.args.clean, **envs)]
  46. elif cli.config.compile.keyboard and cli.config.compile.keymap:
  47. # Generate the make command for a specific keyboard/keymap.
  48. if not _is_keymap_target(cli.config.compile.keyboard, cli.config.compile.keymap):
  49. cli.log.error('Invalid keymap argument.')
  50. cli.print_help()
  51. return False
  52. if cli.args.clean:
  53. commands.append(create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, 'clean', **envs))
  54. commands.append(create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, parallel=cli.config.compile.parallel, **envs))
  55. if not commands:
  56. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  57. cli.print_help()
  58. return False
  59. cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(commands[-1]))
  60. if not cli.args.dry_run:
  61. cli.echo('\n')
  62. for command in commands:
  63. ret = cli.run(command, capture_output=False)
  64. if ret.returncode:
  65. return ret.returncode