compile.py 2.6 KB

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