You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
5.0 KiB

  1. """Compile and flash QMK Firmware
  2. You can compile a keymap already in the repo or using a QMK Configurator export.
  3. A bootloader must be specified.
  4. """
  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. def print_bootloader_help():
  12. """Prints the available bootloaders listed in docs.qmk.fm.
  13. """
  14. cli.log.info('Here are the available bootloaders:')
  15. cli.echo('\tdfu')
  16. cli.echo('\tdfu-ee')
  17. cli.echo('\tdfu-split-left')
  18. cli.echo('\tdfu-split-right')
  19. cli.echo('\tavrdude')
  20. cli.echo('\tBootloadHID')
  21. cli.echo('\tdfu-util')
  22. cli.echo('\tdfu-util-split-left')
  23. cli.echo('\tdfu-util-split-right')
  24. cli.echo('\tst-link-cli')
  25. cli.echo('\tst-flash')
  26. cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
  27. @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='The configurator export JSON to compile.')
  28. @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
  29. @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
  30. @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
  31. @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
  32. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
  33. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.")
  34. @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
  35. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  36. @cli.subcommand('QMK Flash.')
  37. @automagic_keyboard
  38. @automagic_keymap
  39. def flash(cli):
  40. """Compile and or flash QMK Firmware or keyboard/layout
  41. If a Configurator JSON export is supplied this command will create a new keymap. Keymap and Keyboard arguments
  42. will be ignored.
  43. If no file is supplied, keymap and keyboard are expected.
  44. If bootloader is omitted the make system will use the configured bootloader for that keyboard.
  45. """
  46. if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
  47. command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, 'clean')
  48. cli.run(command, capture_output=False)
  49. # Build the environment vars
  50. envs = {}
  51. for env in cli.args.env:
  52. if '=' in env:
  53. key, value = env.split('=', 1)
  54. envs[key] = value
  55. else:
  56. cli.log.warning('Invalid environment variable: %s', env)
  57. # Determine the compile command
  58. command = ''
  59. if cli.args.bootloaders:
  60. # Provide usage and list bootloaders
  61. cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
  62. print_bootloader_help()
  63. return False
  64. if cli.args.filename:
  65. # Handle compiling a configurator JSON
  66. user_keymap = parse_configurator_json(cli.args.filename)
  67. keymap_path = qmk.path.keymap(user_keymap['keyboard'])
  68. command = compile_configurator_json(user_keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)
  69. cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
  70. else:
  71. if cli.config.flash.keyboard and cli.config.flash.keymap:
  72. # Generate the make command for a specific keyboard/keymap.
  73. command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)
  74. elif not cli.config.flash.keyboard:
  75. cli.log.error('Could not determine keyboard!')
  76. elif not cli.config.flash.keymap:
  77. cli.log.error('Could not determine keymap!')
  78. # Compile the firmware, if we're able to
  79. if command:
  80. cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
  81. if not cli.args.dry_run:
  82. cli.echo('\n')
  83. compile = cli.run(command, capture_output=False, text=True)
  84. return compile.returncode
  85. else:
  86. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  87. cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
  88. return False