compile.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Compile a QMK Firmware.
  2. You can compile a keymap already in the repo or using a QMK Configurator export.
  3. """
  4. from argcomplete.completers import FilesCompleter
  5. from milc import cli
  6. import qmk.path
  7. from qmk.constants import QMK_FIRMWARE
  8. from qmk.decorators import automagic_keyboard, automagic_keymap
  9. from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json, build_environment
  10. from qmk.keyboard import keyboard_completer, keyboard_folder_or_all, is_all_keyboards
  11. from qmk.keymap import keymap_completer, locate_keymap
  12. from qmk.cli.generate.compilation_database import write_compilation_database
  13. def _is_keymap_target(keyboard, keymap):
  14. if keymap == 'all':
  15. return True
  16. if locate_keymap(keyboard, keymap):
  17. return True
  18. return False
  19. @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='The configurator export to compile')
  20. @cli.argument('-kb', '--keyboard', type=keyboard_folder_or_all, completer=keyboard_completer, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
  21. @cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
  22. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
  23. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
  24. @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
  25. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  26. @cli.argument('--compiledb', arg_only=True, action='store_true', help="Generates the clang compile_commands.json file during build. Implies --clean.")
  27. @cli.subcommand('Compile a QMK Firmware.')
  28. @automagic_keyboard
  29. @automagic_keymap
  30. def compile(cli):
  31. """Compile a QMK Firmware.
  32. If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists.
  33. If a keyboard and keymap are provided this command will build a firmware based on that.
  34. """
  35. if is_all_keyboards(cli.args.keyboard):
  36. from .mass_compile import mass_compile
  37. cli.args.builds = []
  38. cli.args.filter = []
  39. cli.args.no_temp = False
  40. return mass_compile(cli)
  41. # Build the environment vars
  42. envs = build_environment(cli.args.env)
  43. # Determine the compile command
  44. commands = []
  45. current_keyboard = None
  46. current_keymap = None
  47. if cli.args.filename:
  48. # If a configurator JSON was provided generate a keymap and compile it
  49. user_keymap = parse_configurator_json(cli.args.filename)
  50. commands = [compile_configurator_json(user_keymap, parallel=cli.config.compile.parallel, clean=cli.args.clean, **envs)]
  51. elif cli.config.compile.keyboard and cli.config.compile.keymap:
  52. # Generate the make command for a specific keyboard/keymap.
  53. if not _is_keymap_target(cli.config.compile.keyboard, cli.config.compile.keymap):
  54. cli.log.error('Invalid keymap argument.')
  55. cli.print_help()
  56. return False
  57. if cli.args.clean:
  58. commands.append(create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, 'clean', **envs))
  59. commands.append(create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, parallel=cli.config.compile.parallel, **envs))
  60. current_keyboard = cli.config.compile.keyboard
  61. current_keymap = cli.config.compile.keymap
  62. if not commands:
  63. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  64. cli.print_help()
  65. return False
  66. if cli.args.compiledb:
  67. if current_keyboard is None or current_keymap is None:
  68. cli.log.error('You must supply both `--keyboard` and `--keymap` or be in a directory with a keymap to generate a compile_commands.json file.')
  69. cli.print_help()
  70. return False
  71. write_compilation_database(current_keyboard, current_keymap, QMK_FIRMWARE / 'compile_commands.json')
  72. cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(commands[-1]))
  73. if not cli.args.dry_run:
  74. cli.echo('\n')
  75. for command in commands:
  76. ret = cli.run(command, capture_output=False)
  77. if ret.returncode:
  78. return ret.returncode