2019-08-25 20:58:24 +02:00
""" Compile a QMK Firmware.
You can compile a keymap already in the repo or using a QMK Configurator export .
"""
2021-04-15 04:00:22 +02:00
from argcomplete . completers import FilesCompleter
2022-10-20 15:35:27 +02:00
2019-08-25 20:58:24 +02:00
from milc import cli
2021-02-07 22:02:51 +01:00
import qmk . path
2020-03-13 23:47:04 +01:00
from qmk . decorators import automagic_keyboard , automagic_keymap
2023-11-15 06:24:54 +01:00
from qmk . commands import build_environment
2023-09-22 04:12:20 +02:00
from qmk . keyboard import keyboard_completer , keyboard_folder_or_all , is_all_keyboards
2023-01-19 01:24:13 +01:00
from qmk . keymap import keymap_completer , locate_keymap
2023-11-15 06:24:54 +01:00
from qmk . build_targets import KeyboardKeymapBuildTarget , JsonKeymapBuildTarget
2019-08-25 20:58:24 +02:00
2021-04-15 04:00:22 +02:00
@cli.argument ( ' filename ' , nargs = ' ? ' , arg_only = True , type = qmk . path . FileType ( ' r ' ) , completer = FilesCompleter ( ' .json ' ) , help = ' The configurator export to compile ' )
2023-09-22 04:12:20 +02:00
@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. ' )
2021-04-15 04:00:22 +02:00
@cli.argument ( ' -km ' , ' --keymap ' , completer = keymap_completer , help = ' The keymap to build a firmware for. Ignored when a configurator export is supplied. ' )
2020-03-13 23:47:04 +01:00
@cli.argument ( ' -n ' , ' --dry-run ' , arg_only = True , action = ' store_true ' , help = " Don ' t actually build, just show the make command to be run. " )
2021-08-18 00:46:59 +02:00
@cli.argument ( ' -j ' , ' --parallel ' , type = int , default = 1 , help = " Set the number of parallel make jobs; 0 means unlimited. " )
2021-01-17 00:13:04 +01:00
@cli.argument ( ' -e ' , ' --env ' , arg_only = True , action = ' append ' , default = [ ] , help = " Set a variable to be passed to make. May be passed multiple times. " )
@cli.argument ( ' -c ' , ' --clean ' , arg_only = True , action = ' store_true ' , help = " Remove object files before compiling. " )
2023-11-15 06:24:54 +01:00
@cli.argument ( ' -t ' , ' --target ' , type = str , default = None , help = " Intended alternative build target, such as `production` in `make planck/rev4:default:production`. " )
2023-09-28 12:48:58 +02:00
@cli.argument ( ' --compiledb ' , arg_only = True , action = ' store_true ' , help = " Generates the clang compile_commands.json file during build. Implies --clean. " )
2019-09-22 22:25:33 +02:00
@cli.subcommand ( ' Compile a QMK Firmware. ' )
2020-03-13 23:47:04 +01:00
@automagic_keyboard
@automagic_keymap
2019-09-22 22:25:33 +02:00
def compile ( cli ) :
2019-08-25 20:58:24 +02:00
""" Compile a QMK Firmware.
If a Configurator export is supplied this command will create a new keymap , overwriting an existing keymap if one exists .
2020-02-17 20:42:11 +01:00
If a keyboard and keymap are provided this command will build a firmware based on that .
2019-08-25 20:58:24 +02:00
"""
2023-11-29 03:46:10 +01:00
# If we've received `-kb all`, reroute it to mass-compile.
2023-09-22 04:12:20 +02:00
if is_all_keyboards ( cli . args . keyboard ) :
from . mass_compile import mass_compile
cli . args . builds = [ ]
cli . args . filter = [ ]
2023-11-27 21:53:43 +01:00
cli . config . mass_compile . keymap = cli . config . compile . keymap
cli . config . mass_compile . parallel = cli . config . compile . parallel
2024-03-31 13:23:40 +02:00
cli . args . no_temp = False
2023-09-22 04:12:20 +02:00
return mass_compile ( cli )
2023-11-29 03:46:10 +01:00
# If we've received `-km all`, reroute it to mass-compile.
if cli . args . keymap == ' all ' :
from . mass_compile import mass_compile
2024-03-31 13:23:40 +02:00
cli . args . builds = [ f ' { cli . config . compile . keyboard } :all ' ]
2023-11-29 03:46:10 +01:00
cli . args . filter = [ ]
cli . config . mass_compile . keymap = None
cli . config . mass_compile . parallel = cli . config . compile . parallel
2024-03-31 13:23:40 +02:00
cli . args . no_temp = False
2023-11-29 03:46:10 +01:00
return mass_compile ( cli )
2021-01-17 00:13:04 +01:00
# Build the environment vars
2022-10-20 15:35:27 +02:00
envs = build_environment ( cli . args . env )
2021-01-17 00:13:04 +01:00
2023-11-15 06:24:54 +01:00
# Handler for the build target
target = None
2023-09-28 12:48:58 +02:00
2019-08-25 20:58:24 +02:00
if cli . args . filename :
2023-11-15 06:24:54 +01:00
# if we were given a filename, assume we have a json build target
target = JsonKeymapBuildTarget ( cli . args . filename )
2019-10-05 08:38:34 +02:00
2022-10-20 15:35:27 +02:00
elif cli . config . compile . keyboard and cli . config . compile . keymap :
2023-11-15 06:24:54 +01:00
# if we got a keyboard and keymap, attempt to find it
if not locate_keymap ( cli . config . compile . keyboard , cli . config . compile . keymap ) :
2023-01-19 01:24:13 +01:00
cli . log . error ( ' Invalid keymap argument. ' )
cli . print_help ( )
return False
2023-11-15 06:24:54 +01:00
# If we got here, then we have a valid keyboard and keymap for a build target
target = KeyboardKeymapBuildTarget ( cli . config . compile . keyboard , cli . config . compile . keymap )
2020-03-13 23:47:04 +01:00
2023-11-15 06:24:54 +01:00
if not target :
2020-03-13 23:47:04 +01:00
cli . log . error ( ' You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap. ' )
2022-10-20 15:35:27 +02:00
cli . print_help ( )
2020-03-13 23:47:04 +01:00
return False
2022-10-20 15:35:27 +02:00
2023-11-15 06:24:54 +01:00
target . configure ( parallel = cli . config . compile . parallel , clean = cli . args . clean , compiledb = cli . args . compiledb )
2023-12-13 23:19:49 +01:00
return target . compile ( cli . args . target , dry_run = cli . args . dry_run , * * envs )