compile.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Compile a QMK Firmware.
  2. You can compile a keymap already in the repo or using a QMK Configurator export.
  3. """
  4. from argparse import FileType
  5. from milc import cli
  6. from qmk.decorators import automagic_keyboard, automagic_keymap
  7. from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
  8. @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile')
  9. @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
  10. @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
  11. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
  12. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.")
  13. @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
  14. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  15. @cli.subcommand('Compile a QMK Firmware.')
  16. @automagic_keyboard
  17. @automagic_keymap
  18. def compile(cli):
  19. """Compile a QMK Firmware.
  20. If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists.
  21. If a keyboard and keymap are provided this command will build a firmware based on that.
  22. """
  23. if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
  24. command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, 'clean')
  25. # FIXME(skullydazed/anyone): Remove text=False once milc 1.0.11 has had enough time to be installed everywhere.
  26. cli.run(command, capture_output=False, text=False)
  27. # Build the environment vars
  28. envs = {}
  29. for env in cli.args.env:
  30. if '=' in env:
  31. key, value = env.split('=', 1)
  32. envs[key] = value
  33. else:
  34. cli.log.warning('Invalid environment variable: %s', env)
  35. # Determine the compile command
  36. command = None
  37. if cli.args.filename:
  38. # If a configurator JSON was provided generate a keymap and compile it
  39. user_keymap = parse_configurator_json(cli.args.filename)
  40. command = compile_configurator_json(user_keymap, parallel=cli.config.compile.parallel, **envs)
  41. else:
  42. if cli.config.compile.keyboard and cli.config.compile.keymap:
  43. # Generate the make command for a specific keyboard/keymap.
  44. command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, parallel=cli.config.compile.parallel, **envs)
  45. elif not cli.config.compile.keyboard:
  46. cli.log.error('Could not determine keyboard!')
  47. elif not cli.config.compile.keymap:
  48. cli.log.error('Could not determine keymap!')
  49. # Compile the firmware, if we're able to
  50. if command:
  51. cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
  52. if not cli.args.dry_run:
  53. cli.echo('\n')
  54. # FIXME(skullydazed/anyone): Remove text=False once milc 1.0.11 has had enough time to be installed everywhere.
  55. compile = cli.run(command, capture_output=False, text=False)
  56. return compile.returncode
  57. else:
  58. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  59. cli.echo('usage: qmk compile [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [filename]')
  60. return False