compile.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """Compile a QMK Firmware.
  2. You can compile a keymap already in the repo or using a QMK Configurator export.
  3. """
  4. import subprocess
  5. import os
  6. from argparse import FileType
  7. from milc import cli
  8. from qmk.commands import create_make_command
  9. from qmk.commands import parse_configurator_json
  10. from qmk.commands import compile_configurator_json
  11. import qmk.keymap
  12. import qmk.path
  13. @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile')
  14. @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
  15. @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
  16. @cli.subcommand('Compile a QMK Firmware.')
  17. def compile(cli):
  18. """Compile a QMK Firmware.
  19. If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists.
  20. FIXME(skullydazed): add code to check and warn if the keymap already exists
  21. If --keyboard and --keymap are provided this command will build a firmware based on that.
  22. """
  23. # Set CWD as directory command was issued from
  24. cwd = os.environ['ORIG_CWD']
  25. qmk_path = os.getcwd()
  26. current_folder = os.path.basename(cwd)
  27. # Initialize boolean to check for being in a keyboard directory and initialize keyboard string
  28. in_keyboard = False
  29. in_layout = False
  30. keyboard = ""
  31. keymap = ""
  32. user_keymap = ""
  33. user_keyboard = ""
  34. # Set path for '/keyboards/' directory
  35. keyboards_path = os.path.join(qmk_path, "keyboards")
  36. layouts_path = os.path.join(qmk_path, "layouts")
  37. # If below 'keyboards' and not in 'keyboards' or 'keymaps', get current keyboard name
  38. if cwd.startswith(keyboards_path):
  39. if current_folder != "keyboards" and current_folder != "keymaps":
  40. if os.path.basename(os.path.abspath(os.path.join(cwd, ".."))) == "keymaps":
  41. # If in a keymap folder, set relative path, get everything before /keymaps, and the keymap name
  42. relative_path = cwd[len(keyboards_path):][1:]
  43. keyboard = str(relative_path).split("/keymaps", 1)[0]
  44. keymap = str(relative_path.rsplit("/", 1)[-1])
  45. else:
  46. keyboard = str(cwd[len(keyboards_path):])[1:]
  47. in_keyboard = True
  48. # If in layouts dir
  49. if cwd.startswith(layouts_path):
  50. if current_folder != "layouts":
  51. in_layout = True
  52. # If user keyboard/keymap or compile keyboard/keymap are supplied, assign those
  53. if cli.config.compile.keyboard:
  54. user_keyboard = cli.config.compile.keyboard
  55. if cli.config.compile.keymap and not in_layout:
  56. user_keymap = cli.config.compile.keymap
  57. if cli.args.filename:
  58. # Parse the configurator json
  59. user_keymap = parse_configurator_json(cli.args.filename)
  60. # Generate the keymap
  61. keymap_path = qmk.path.keymap(user_keymap['keyboard'])
  62. cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path)
  63. # Compile the keymap
  64. command = compile_configurator_json(user_keymap)
  65. cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
  66. elif user_keyboard and user_keymap:
  67. # Generate the make command for a specific keyboard/keymap.
  68. command = create_make_command(user_keyboard, user_keymap)
  69. elif in_keyboard:
  70. keyboard = user_keyboard if user_keyboard else keyboard
  71. keymap = user_keymap if user_keymap else keymap
  72. if not os.path.exists(os.path.join(keyboards_path, keyboard, "rules.mk")):
  73. cli.log.error('This directory does not contain a rules.mk file. Change directory or supply --keyboard with optional --keymap')
  74. return False
  75. # Get path for keyboard directory
  76. keymap_path = qmk.path.keymap(keyboard)
  77. # Check for global keymap config first
  78. if keymap:
  79. command = create_make_command(keyboard, keymap)
  80. else:
  81. # If no default keymap exists and none provided
  82. cli.log.error('This directory does not contain a keymap. Set one with `qmk config` or supply `--keymap` ')
  83. return False
  84. elif in_layout:
  85. if user_keyboard:
  86. keymap = current_folder
  87. command = create_make_command(user_keyboard, keymap)
  88. else:
  89. cli.log.error('You must supply a keyboard to compile a layout keymap. Set one with `qmk config` or supply `--keyboard` ')
  90. return False
  91. else:
  92. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  93. return False
  94. cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command))
  95. subprocess.run(command)