flash.py 4.8 KB

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