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