diff options
author | Zach White <skullydazed@gmail.com> | 2021-01-16 15:21:06 -0800 |
---|---|---|
committer | Zach White <skullydazed@gmail.com> | 2021-01-16 15:21:06 -0800 |
commit | 5abe66674921094c2686b98a4c0d188088e43bb3 (patch) | |
tree | e4580fa1e8b29f138e652e2db76e1c5b76646ded /lib/python/qmk | |
parent | f35b1127fa3662285f37a12632be385bd84b79d3 (diff) | |
parent | d9785ec31339d7f80279fd3d1005f76689ed2f6a (diff) |
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'lib/python/qmk')
-rwxr-xr-x | lib/python/qmk/cli/compile.py | 27 | ||||
-rw-r--r-- | lib/python/qmk/cli/flash.py | 26 | ||||
-rw-r--r-- | lib/python/qmk/commands.py | 93 | ||||
-rw-r--r-- | lib/python/qmk/constants.py | 5 |
4 files changed, 129 insertions, 22 deletions
diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index daee597d8e..322ce6a257 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -2,7 +2,6 @@ You can compile a keymap already in the repo or using a QMK Configurator export. """ -import subprocess from argparse import FileType from milc import cli @@ -15,6 +14,9 @@ from qmk.commands import compile_configurator_json, create_make_command, parse_c @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") +@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.") +@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.") @cli.subcommand('Compile a QMK Firmware.') @automagic_keyboard @automagic_keymap @@ -25,18 +27,32 @@ def compile(cli): If a keyboard and keymap are provided this command will build a firmware based on that. """ + if cli.args.clean and not cli.args.filename and not cli.args.dry_run: + command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, 'clean') + # FIXME(skullydazed/anyone): Remove text=False once milc 1.0.11 has had enough time to be installed everywhere. + cli.run(command, capture_output=False, text=False) + + # Build the environment vars + envs = {} + for env in cli.args.env: + if '=' in env: + key, value = env.split('=', 1) + envs[key] = value + else: + cli.log.warning('Invalid environment variable: %s', env) + + # Determine the compile command command = None if cli.args.filename: # If a configurator JSON was provided generate a keymap and compile it - # FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap. user_keymap = parse_configurator_json(cli.args.filename) - command = compile_configurator_json(user_keymap) + command = compile_configurator_json(user_keymap, parallel=cli.config.compile.parallel, **envs) else: if cli.config.compile.keyboard and cli.config.compile.keymap: # Generate the make command for a specific keyboard/keymap. - command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap) + command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, parallel=cli.config.compile.parallel, **envs) elif not cli.config.compile.keyboard: cli.log.error('Could not determine keyboard!') @@ -48,7 +64,8 @@ def compile(cli): cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command)) if not cli.args.dry_run: cli.echo('\n') - compile = subprocess.run(command) + # FIXME(skullydazed/anyone): Remove text=False once milc 1.0.11 has had enough time to be installed everywhere. + compile = cli.run(command, capture_output=False, text=False) return compile.returncode else: diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py index d720d42e71..b3827e8003 100644 --- a/lib/python/qmk/cli/flash.py +++ b/lib/python/qmk/cli/flash.py @@ -3,7 +3,6 @@ You can compile a keymap already in the repo or using a QMK Configurator export. A bootloader must be specified. """ -import subprocess from argparse import FileType from milc import cli @@ -37,6 +36,9 @@ def print_bootloader_help(): @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.') @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.') @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") +@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.") +@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.") @cli.subcommand('QMK Flash.') @automagic_keyboard @automagic_keymap @@ -50,6 +52,20 @@ def flash(cli): If bootloader is omitted the make system will use the configured bootloader for that keyboard. """ + if cli.args.clean and not cli.args.filename and not cli.args.dry_run: + command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, 'clean') + cli.run(command, capture_output=False) + + # Build the environment vars + envs = {} + for env in cli.args.env: + if '=' in env: + key, value = env.split('=', 1) + envs[key] = value + else: + cli.log.warning('Invalid environment variable: %s', env) + + # Determine the compile command command = '' if cli.args.bootloaders: @@ -60,16 +76,16 @@ def flash(cli): if cli.args.filename: # Handle compiling a configurator JSON - user_keymap = parse_configurator_json(cli.args.filename) + user_keymap = parse_configurator_json(cli.args.filename, parallel=cli.config.flash.parallel) keymap_path = qmk.path.keymap(user_keymap['keyboard']) - command = compile_configurator_json(user_keymap, cli.args.bootloader) + command = compile_configurator_json(user_keymap, cli.args.bootloader, **envs) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) else: if cli.config.flash.keyboard and cli.config.flash.keymap: # Generate the make command for a specific keyboard/keymap. - command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader) + command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs) elif not cli.config.flash.keyboard: cli.log.error('Could not determine keyboard!') @@ -81,7 +97,7 @@ def flash(cli): cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command)) if not cli.args.dry_run: cli.echo('\n') - compile = subprocess.run(command) + compile = cli.run(command, capture_output=False, text=True) return compile.returncode else: diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 4ec7d2ea53..ba225bf558 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -6,11 +6,26 @@ import platform import subprocess import shlex import shutil +from pathlib import Path + +from milc import cli import qmk.keymap +from qmk.constants import KEYBOARD_OUTPUT_PREFIX + + +def _find_make(): + """Returns the correct make command for this environment. + """ + make_cmd = os.environ.get('MAKE') + + if not make_cmd: + make_cmd = 'gmake' if shutil.which('gmake') else 'make' + return make_cmd -def create_make_command(keyboard, keymap, target=None): + +def create_make_command(keyboard, keymap, target=None, parallel=1, **env_vars): """Create a make compile command Args: @@ -24,41 +39,95 @@ def create_make_command(keyboard, keymap, target=None): target Usually a bootloader. + parallel + The number of make jobs to run in parallel + + **env_vars + Environment variables to be passed to make. + Returns: A command that can be run to make the specified keyboard and keymap """ + env = [] make_args = [keyboard, keymap] - make_cmd = 'gmake' if shutil.which('gmake') else 'make' + make_cmd = _find_make() if target: make_args.append(target) - return [make_cmd, ':'.join(make_args)] + for key, value in env_vars.items(): + env.append(f'{key}={value}') + return [make_cmd, '-j', str(parallel), *env, ':'.join(make_args)] -def compile_configurator_json(user_keymap, bootloader=None): - """Convert a configurator export JSON file into a C file + +def compile_configurator_json(user_keymap, parallel=1, **env_vars): + """Convert a configurator export JSON file into a C file and then compile it. Args: - configurator_filename - The configurator JSON export file + user_keymap + A deserialized keymap export bootloader A bootloader to flash + parallel + The number of make jobs to run in parallel + Returns: A command to run to compile and flash the C file. """ - # Write the keymap C file - qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) + # Write the keymap.c file + keyboard_filesafe = user_keymap['keyboard'].replace('/', '_') + target = f'{keyboard_filesafe}_{user_keymap["keymap"]}' + keyboard_output = Path(f'{KEYBOARD_OUTPUT_PREFIX}{keyboard_filesafe}') + keymap_output = Path(f'{keyboard_output}_{user_keymap["keymap"]}') + c_text = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) + keymap_dir = keymap_output / 'src' + keymap_c = keymap_dir / 'keymap.c' + + keymap_dir.mkdir(exist_ok=True, parents=True) + keymap_c.write_text(c_text) # Return a command that can be run to make the keymap and flash if given - if bootloader is None: - return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) - return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) + verbose = 'true' if cli.config.general.verbose else 'false' + color = 'true' if cli.config.general.color else 'false' + make_command = [ + _find_make(), + '-j', + str(parallel), + '-r', + '-R', + '-f', + 'build_keyboard.mk', + ] + + for key, value in env_vars.items(): + make_command.append(f'{key}={value}') + + make_command.extend([ + f'KEYBOARD={user_keymap["keyboard"]}', + f'KEYMAP={user_keymap["keymap"]}', + f'KEYBOARD_FILESAFE={keyboard_filesafe}', + f'TARGET={target}', + f'KEYBOARD_OUTPUT={keyboard_output}', + f'KEYMAP_OUTPUT={keymap_output}', + f'MAIN_KEYMAP_PATH_1={keymap_output}', + f'MAIN_KEYMAP_PATH_2={keymap_output}', + f'MAIN_KEYMAP_PATH_3={keymap_output}', + f'MAIN_KEYMAP_PATH_4={keymap_output}', + f'MAIN_KEYMAP_PATH_5={keymap_output}', + f'KEYMAP_C={keymap_c}', + f'KEYMAP_PATH={keymap_dir}', + f'VERBOSE={verbose}', + f'COLOR={color}', + 'SILENT=false', + ]) + + return make_command def parse_configurator_json(configurator_file): diff --git a/lib/python/qmk/constants.py b/lib/python/qmk/constants.py index 6a643070fd..404a58a7e5 100644 --- a/lib/python/qmk/constants.py +++ b/lib/python/qmk/constants.py @@ -1,5 +1,6 @@ """Information that should be available to the python library. """ +from os import environ from pathlib import Path # The root of the qmk_firmware tree. @@ -28,3 +29,7 @@ LED_INDICATORS = { 'num_lock': 'LED_NUM_LOCK_PIN', 'scrol_lock': 'LED_SCROLL_LOCK_PIN', } + +# Constants that should match their counterparts in make +BUILD_DIR = environ.get('BUILD_DIR', '.build') +KEYBOARD_OUTPUT_PREFIX = f'{BUILD_DIR}/obj_' |