compile.py 3.9 KB

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