diff options
author | Zach White <skullydazed@gmail.com> | 2021-01-16 15:13:04 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-16 15:13:04 -0800 |
commit | d9785ec31339d7f80279fd3d1005f76689ed2f6a (patch) | |
tree | 01f9e771367cfcd18d347eef7f85ce78a3b5ba50 /lib/python/qmk/commands.py | |
parent | c628408688306ed3e970505268cc4a235af8a5ff (diff) | |
download | qmk_firmware-d9785ec31339d7f80279fd3d1005f76689ed2f6a.tar.gz qmk_firmware-d9785ec31339d7f80279fd3d1005f76689ed2f6a.zip |
Improve the compile and flash subcommands (#11334)
* add support for --clean to compile and flash
* compile standalone JSON keymaps without polluting the tree
* Add support for passing environment vars to make
* make flake8 happy
* document changes to qmk compile and flash
* add -e support to json export compiling
* Fix python 3.6
* honor $MAKE
* add support for parallel builds
Diffstat (limited to 'lib/python/qmk/commands.py')
-rw-r--r-- | lib/python/qmk/commands.py | 93 |
1 files changed, 81 insertions, 12 deletions
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 4ec7d2ea5..ba225bf55 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py | |||
@@ -6,11 +6,26 @@ import platform | |||
6 | import subprocess | 6 | import subprocess |
7 | import shlex | 7 | import shlex |
8 | import shutil | 8 | import shutil |
9 | from pathlib import Path | ||
10 | |||
11 | from milc import cli | ||
9 | 12 | ||
10 | import qmk.keymap | 13 | import qmk.keymap |
14 | from qmk.constants import KEYBOARD_OUTPUT_PREFIX | ||
15 | |||
16 | |||
17 | def _find_make(): | ||
18 | """Returns the correct make command for this environment. | ||
19 | """ | ||
20 | make_cmd = os.environ.get('MAKE') | ||
21 | |||
22 | if not make_cmd: | ||
23 | make_cmd = 'gmake' if shutil.which('gmake') else 'make' | ||
11 | 24 | ||
25 | return make_cmd | ||
12 | 26 | ||
13 | def create_make_command(keyboard, keymap, target=None): | 27 | |
28 | def create_make_command(keyboard, keymap, target=None, parallel=1, **env_vars): | ||
14 | """Create a make compile command | 29 | """Create a make compile command |
15 | 30 | ||
16 | Args: | 31 | Args: |
@@ -24,41 +39,95 @@ def create_make_command(keyboard, keymap, target=None): | |||
24 | target | 39 | target |
25 | Usually a bootloader. | 40 | Usually a bootloader. |
26 | 41 | ||
42 | parallel | ||
43 | The number of make jobs to run in parallel | ||
44 | |||
45 | **env_vars | ||
46 | Environment variables to be passed to make. | ||
47 | |||
27 | Returns: | 48 | Returns: |
28 | 49 | ||
29 | A command that can be run to make the specified keyboard and keymap | 50 | A command that can be run to make the specified keyboard and keymap |
30 | """ | 51 | """ |
52 | env = [] | ||
31 | make_args = [keyboard, keymap] | 53 | make_args = [keyboard, keymap] |
32 | make_cmd = 'gmake' if shutil.which('gmake') else 'make' | 54 | make_cmd = _find_make() |
33 | 55 | ||
34 | if target: | 56 | if target: |
35 | make_args.append(target) | 57 | make_args.append(target) |
36 | 58 | ||
37 | return [make_cmd, ':'.join(make_args)] | 59 | for key, value in env_vars.items(): |
60 | env.append(f'{key}={value}') | ||
38 | 61 | ||
62 | return [make_cmd, '-j', str(parallel), *env, ':'.join(make_args)] | ||
39 | 63 | ||
40 | def compile_configurator_json(user_keymap, bootloader=None): | 64 | |
41 | """Convert a configurator export JSON file into a C file | 65 | def compile_configurator_json(user_keymap, parallel=1, **env_vars): |
66 | """Convert a configurator export JSON file into a C file and then compile it. | ||
42 | 67 | ||
43 | Args: | 68 | Args: |
44 | 69 | ||
45 | configurator_filename | 70 | user_keymap |
46 | The configurator JSON export file | 71 | A deserialized keymap export |
47 | 72 | ||
48 | bootloader | 73 | bootloader |
49 | A bootloader to flash | 74 | A bootloader to flash |
50 | 75 | ||
76 | parallel | ||
77 | The number of make jobs to run in parallel | ||
78 | |||
51 | Returns: | 79 | Returns: |
52 | 80 | ||
53 | A command to run to compile and flash the C file. | 81 | A command to run to compile and flash the C file. |
54 | """ | 82 | """ |
55 | # Write the keymap C file | 83 | # Write the keymap.c file |
56 | qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) | 84 | keyboard_filesafe = user_keymap['keyboard'].replace('/', '_') |
85 | target = f'{keyboard_filesafe}_{user_keymap["keymap"]}' | ||
86 | keyboard_output = Path(f'{KEYBOARD_OUTPUT_PREFIX}{keyboard_filesafe}') | ||
87 | keymap_output = Path(f'{keyboard_output}_{user_keymap["keymap"]}') | ||
88 | c_text = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) | ||
89 | keymap_dir = keymap_output / 'src' | ||
90 | keymap_c = keymap_dir / 'keymap.c' | ||
91 | |||
92 | keymap_dir.mkdir(exist_ok=True, parents=True) | ||
93 | keymap_c.write_text(c_text) | ||
57 | 94 | ||
58 | # Return a command that can be run to make the keymap and flash if given | 95 | # Return a command that can be run to make the keymap and flash if given |
59 | if bootloader is None: | 96 | verbose = 'true' if cli.config.general.verbose else 'false' |
60 | return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) | 97 | color = 'true' if cli.config.general.color else 'false' |
61 | return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) | 98 | make_command = [ |
99 | _find_make(), | ||
100 | '-j', | ||
101 | str(parallel), | ||
102 | '-r', | ||
103 | '-R', | ||
104 | '-f', | ||
105 | 'build_keyboard.mk', | ||
106 | ] | ||
107 | |||
108 | for key, value in env_vars.items(): | ||
109 | make_command.append(f'{key}={value}') | ||
110 | |||
111 | make_command.extend([ | ||
112 | f'KEYBOARD={user_keymap["keyboard"]}', | ||
113 | f'KEYMAP={user_keymap["keymap"]}', | ||
114 | f'KEYBOARD_FILESAFE={keyboard_filesafe}', | ||
115 | f'TARGET={target}', | ||
116 | f'KEYBOARD_OUTPUT={keyboard_output}', | ||
117 | f'KEYMAP_OUTPUT={keymap_output}', | ||
118 | f'MAIN_KEYMAP_PATH_1={keymap_output}', | ||
119 | f'MAIN_KEYMAP_PATH_2={keymap_output}', | ||
120 | f'MAIN_KEYMAP_PATH_3={keymap_output}', | ||
121 | f'MAIN_KEYMAP_PATH_4={keymap_output}', | ||
122 | f'MAIN_KEYMAP_PATH_5={keymap_output}', | ||
123 | f'KEYMAP_C={keymap_c}', | ||
124 | f'KEYMAP_PATH={keymap_dir}', | ||
125 | f'VERBOSE={verbose}', | ||
126 | f'COLOR={color}', | ||
127 | 'SILENT=false', | ||
128 | ]) | ||
129 | |||
130 | return make_command | ||
62 | 131 | ||
63 | 132 | ||
64 | def parse_configurator_json(configurator_file): | 133 | def parse_configurator_json(configurator_file): |