compile.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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
  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, 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. # Build the environment vars
  33. envs = build_environment(cli.args.env)
  34. # Determine the compile command
  35. commands = []
  36. if cli.args.filename:
  37. # If a configurator JSON was provided generate a keymap and compile it
  38. user_keymap = parse_configurator_json(cli.args.filename)
  39. commands = [compile_configurator_json(user_keymap, parallel=cli.config.compile.parallel, clean=cli.args.clean, **envs)]
  40. elif cli.config.compile.keyboard and cli.config.compile.keymap:
  41. # Generate the make command for a specific keyboard/keymap.
  42. if not _is_keymap_target(cli.config.compile.keyboard, cli.config.compile.keymap):
  43. cli.log.error('Invalid keymap argument.')
  44. cli.print_help()
  45. return False
  46. if cli.args.clean:
  47. commands.append(create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, 'clean', **envs))
  48. commands.append(create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, parallel=cli.config.compile.parallel, **envs))
  49. if not commands:
  50. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  51. cli.print_help()
  52. return False
  53. cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(commands[-1]))
  54. if not cli.args.dry_run:
  55. cli.echo('\n')
  56. for command in commands:
  57. ret = cli.run(command, capture_output=False)
  58. if ret.returncode:
  59. return ret.returncode