diff options
author | Cody Bender <50554676+cfbender@users.noreply.github.com> | 2020-02-07 13:48:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-07 21:48:37 +0100 |
commit | e77188458f136da3e25ce066e80b21d38f6e8cdc (patch) | |
tree | 1066504f2e5a1a27b068ce6383f75ede0e9c21de | |
parent | 774cbbf879eaec2149413917cb970054e4c1bf59 (diff) | |
download | qmk_firmware-e77188458f136da3e25ce066e80b21d38f6e8cdc.tar.gz qmk_firmware-e77188458f136da3e25ce066e80b21d38f6e8cdc.zip |
Add QMK Compile Context Sensitivity (#6884)
* Add context sensitive compile, without config check
* Initial full working state. Plan to refactor
* Refactor loop for simplicity, add comments
* Update docs/cli.md with qmk compile examples
* Simplify path for keyboard derivation
* Update path to use path.join instead of concat
* Refactor keyboard path, the skully way
* Add in keymap folder support
* Add /layouts compile support
* Update docs/cli.md with empty compile in layouts
* Add comments to compile.py
* Update docs for clarity, and fix compile error typo
* Fix config option compile
* Fix layout compile and failure mode
* Add rules.mk check
* Fix variable names for global config
* Add in_layout priority
* Remove default fallback in favor of throw, update docs
* Add keymap folder context
* Fix formatting
* Add os import
* Convert to create_make_command
* Fix Travis lint errors
* Remove blank line with whitespace
* Add blank lines for readability
* Remove unnecessary config logic
* Update Docs to add flash
Co-Authored-By: skullydazed <skullydazed@users.noreply.github.com>
* Shift config precedence to MILC
Co-authored-by: skullydazed <skullydazed@users.noreply.github.com>
-rw-r--r-- | docs/cli.md | 49 | ||||
-rwxr-xr-x | lib/python/qmk/cli/compile.py | 75 |
2 files changed, 120 insertions, 4 deletions
diff --git a/docs/cli.md b/docs/cli.md index 4f328a75a..f1c158af4 100644 --- a/docs/cli.md +++ b/docs/cli.md | |||
@@ -81,7 +81,7 @@ qmk cformat [file1] [file2] [...] [fileN] | |||
81 | 81 | ||
82 | ## `qmk compile` | 82 | ## `qmk compile` |
83 | 83 | ||
84 | This command allows you to compile firmware from any directory. You can compile JSON exports from <https://config.qmk.fm> or compile keymaps in the repo. | 84 | This command allows you to compile firmware from any directory. You can compile JSON exports from <https://config.qmk.fm>, compile keymaps in the repo, or compile the keyboard in the current working directory. |
85 | 85 | ||
86 | **Usage for Configurator Exports**: | 86 | **Usage for Configurator Exports**: |
87 | 87 | ||
@@ -95,6 +95,53 @@ qmk compile <configuratorExport.json> | |||
95 | qmk compile -kb <keyboard_name> -km <keymap_name> | 95 | qmk compile -kb <keyboard_name> -km <keymap_name> |
96 | ``` | 96 | ``` |
97 | 97 | ||
98 | **Usage in Keyboard Directory**: | ||
99 | |||
100 | Must be in keyboard directory with a default keymap, or in keymap directory for keyboard, or supply one with `--keymap <keymap_name>` | ||
101 | ``` | ||
102 | qmk compile | ||
103 | ``` | ||
104 | |||
105 | **Example**: | ||
106 | ``` | ||
107 | $ qmk config compile.keymap=default | ||
108 | $ cd ~/qmk_firmware/keyboards/planck/rev6 | ||
109 | $ qmk compile | ||
110 | Ψ Compiling keymap with make planck/rev6:default | ||
111 | ... | ||
112 | ``` | ||
113 | or with optional keymap argument | ||
114 | |||
115 | ``` | ||
116 | $ cd ~/qmk_firmware/keyboards/clueboard/66/rev4 | ||
117 | $ qmk compile -km 66_iso | ||
118 | Ψ Compiling keymap with make clueboard/66/rev4:66_iso | ||
119 | ... | ||
120 | ``` | ||
121 | or in keymap directory | ||
122 | |||
123 | ``` | ||
124 | $ cd ~/qmk_firmware/keyboards/gh60/satan/keymaps/colemak | ||
125 | $ qmk compile | ||
126 | Ψ Compiling keymap with make make gh60/satan:colemak | ||
127 | ... | ||
128 | ``` | ||
129 | |||
130 | **Usage in Layout Directory**: | ||
131 | |||
132 | Must be under `qmk_firmware/layouts/`, and in a keymap folder. | ||
133 | ``` | ||
134 | qmk compile -kb <keyboard_name> | ||
135 | ``` | ||
136 | |||
137 | **Example**: | ||
138 | ``` | ||
139 | $ cd ~/qmk_firmware/layouts/community/60_ansi/mechmerlin-ansi | ||
140 | $ qmk compile -kb dz60 | ||
141 | Ψ Compiling keymap with make dz60:mechmerlin-ansi | ||
142 | ... | ||
143 | ``` | ||
144 | |||
98 | ## `qmk flash` | 145 | ## `qmk flash` |
99 | 146 | ||
100 | This command is similar to `qmk compile`, but can also target a bootloader. The bootloader is optional, and is set to `:flash` by default. | 147 | This command is similar to `qmk compile`, but can also target a bootloader. The bootloader is optional, and is set to `:flash` by default. |
diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 8e2d0cdbf..826b969ef 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py | |||
@@ -3,6 +3,7 @@ | |||
3 | You can compile a keymap already in the repo or using a QMK Configurator export. | 3 | You can compile a keymap already in the repo or using a QMK Configurator export. |
4 | """ | 4 | """ |
5 | import subprocess | 5 | import subprocess |
6 | import os | ||
6 | from argparse import FileType | 7 | from argparse import FileType |
7 | 8 | ||
8 | from milc import cli | 9 | from milc import cli |
@@ -28,6 +29,46 @@ def compile(cli): | |||
28 | If --keyboard and --keymap are provided this command will build a firmware based on that. | 29 | If --keyboard and --keymap are provided this command will build a firmware based on that. |
29 | 30 | ||
30 | """ | 31 | """ |
32 | # Set CWD as directory command was issued from | ||
33 | cwd = os.environ['ORIG_CWD'] | ||
34 | qmk_path = os.getcwd() | ||
35 | current_folder = os.path.basename(cwd) | ||
36 | # Initialize boolean to check for being in a keyboard directory and initialize keyboard string | ||
37 | in_keyboard = False | ||
38 | in_layout = False | ||
39 | keyboard = "" | ||
40 | keymap = "" | ||
41 | user_keymap = "" | ||
42 | user_keyboard = "" | ||
43 | |||
44 | # Set path for '/keyboards/' directory | ||
45 | keyboards_path = os.path.join(qmk_path, "keyboards") | ||
46 | layouts_path = os.path.join(qmk_path, "layouts") | ||
47 | |||
48 | # If below 'keyboards' and not in 'keyboards' or 'keymaps', get current keyboard name | ||
49 | if cwd.startswith(keyboards_path): | ||
50 | if current_folder != "keyboards" and current_folder != "keymaps": | ||
51 | if os.path.basename(os.path.abspath(os.path.join(cwd, ".."))) == "keymaps": | ||
52 | # If in a keymap folder, set relative path, get everything before /keymaps, and the keymap name | ||
53 | relative_path = cwd[len(keyboards_path):][1:] | ||
54 | keyboard = str(relative_path).split("/keymaps", 1)[0] | ||
55 | keymap = str(relative_path.rsplit("/", 1)[-1]) | ||
56 | else: | ||
57 | keyboard = str(cwd[len(keyboards_path):])[1:] | ||
58 | |||
59 | in_keyboard = True | ||
60 | |||
61 | # If in layouts dir | ||
62 | if cwd.startswith(layouts_path): | ||
63 | if current_folder != "layouts": | ||
64 | in_layout = True | ||
65 | |||
66 | # If user keyboard/keymap or compile keyboard/keymap are supplied, assign those | ||
67 | if cli.config.compile.keyboard: | ||
68 | user_keyboard = cli.config.compile.keyboard | ||
69 | if cli.config.compile.keymap and not in_layout: | ||
70 | user_keymap = cli.config.compile.keymap | ||
71 | |||
31 | if cli.args.filename: | 72 | if cli.args.filename: |
32 | # Parse the configurator json | 73 | # Parse the configurator json |
33 | user_keymap = parse_configurator_json(cli.args.filename) | 74 | user_keymap = parse_configurator_json(cli.args.filename) |
@@ -41,12 +82,40 @@ def compile(cli): | |||
41 | 82 | ||
42 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) | 83 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) |
43 | 84 | ||
44 | elif cli.config.compile.keyboard and cli.config.compile.keymap: | 85 | elif user_keyboard and user_keymap: |
45 | # Generate the make command for a specific keyboard/keymap. | 86 | # Generate the make command for a specific keyboard/keymap. |
46 | command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap) | 87 | command = create_make_command(user_keyboard, user_keymap) |
88 | |||
89 | elif in_keyboard: | ||
90 | keyboard = user_keyboard if user_keyboard else keyboard | ||
91 | keymap = user_keymap if user_keymap else keymap | ||
92 | |||
93 | if not os.path.exists(os.path.join(keyboards_path, keyboard, "rules.mk")): | ||
94 | cli.log.error('This directory does not contain a rules.mk file. Change directory or supply --keyboard with optional --keymap') | ||
95 | return False | ||
96 | |||
97 | # Get path for keyboard directory | ||
98 | keymap_path = qmk.path.keymap(keyboard) | ||
99 | |||
100 | # Check for global keymap config first | ||
101 | if keymap: | ||
102 | command = create_make_command(keyboard, keymap) | ||
103 | |||
104 | else: | ||
105 | # If no default keymap exists and none provided | ||
106 | cli.log.error('This directory does not contain a keymap. Set one with `qmk config` or supply `--keymap` ') | ||
107 | return False | ||
108 | |||
109 | elif in_layout: | ||
110 | if user_keyboard: | ||
111 | keymap = current_folder | ||
112 | command = create_make_command(user_keyboard, keymap) | ||
113 | else: | ||
114 | cli.log.error('You must supply a keyboard to compile a layout keymap. Set one with `qmk config` or supply `--keyboard` ') | ||
115 | return False | ||
47 | 116 | ||
48 | else: | 117 | else: |
49 | cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`.') | 118 | cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.') |
50 | return False | 119 | return False |
51 | 120 | ||
52 | cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command)) | 121 | cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command)) |