compile.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """Compile a QMK Firmware.
  2. You can compile a keymap already in the repo or using a QMK Configurator export.
  3. """
  4. import subprocess
  5. from argparse import FileType
  6. from milc import cli
  7. from qmk.decorators import automagic_keyboard, automagic_keymap
  8. from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
  9. @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile')
  10. @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
  11. @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
  12. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
  13. @cli.subcommand('Compile a QMK Firmware.')
  14. @automagic_keyboard
  15. @automagic_keymap
  16. def compile(cli):
  17. """Compile a QMK Firmware.
  18. If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists.
  19. If a keyboard and keymap are provided this command will build a firmware based on that.
  20. """
  21. command = None
  22. if cli.args.filename:
  23. # If a configurator JSON was provided generate a keymap and compile it
  24. # FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap.
  25. user_keymap = parse_configurator_json(cli.args.filename)
  26. command = compile_configurator_json(user_keymap)
  27. else:
  28. if cli.config.compile.keyboard and cli.config.compile.keymap:
  29. # Generate the make command for a specific keyboard/keymap.
  30. command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap)
  31. elif not cli.config.compile.keyboard:
  32. cli.log.error('Could not determine keyboard!')
  33. elif not cli.config.compile.keymap:
  34. cli.log.error('Could not determine keymap!')
  35. # Compile the firmware, if we're able to
  36. if command:
  37. cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
  38. if not cli.args.dry_run:
  39. cli.echo('\n')
  40. compile = subprocess.run(command)
  41. return compile.returncode
  42. else:
  43. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  44. cli.echo('usage: qmk compile [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [filename]')
  45. return False