diff options
Diffstat (limited to 'lib/python/qmk/cli/generate/community_modules.py')
| -rw-r--r-- | lib/python/qmk/cli/generate/community_modules.py | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/lib/python/qmk/cli/generate/community_modules.py b/lib/python/qmk/cli/generate/community_modules.py index 23678a2fb5..e12daccf1c 100644 --- a/lib/python/qmk/cli/generate/community_modules.py +++ b/lib/python/qmk/cli/generate/community_modules.py | |||
| @@ -8,7 +8,7 @@ import qmk.path | |||
| 8 | from qmk.info import get_modules | 8 | from qmk.info import get_modules |
| 9 | from qmk.keyboard import keyboard_completer, keyboard_folder | 9 | from qmk.keyboard import keyboard_completer, keyboard_folder |
| 10 | from qmk.commands import dump_lines | 10 | from qmk.commands import dump_lines |
| 11 | from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE | 11 | from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE |
| 12 | from qmk.community_modules import module_api_list, load_module_jsons, find_module_path | 12 | from qmk.community_modules import module_api_list, load_module_jsons, find_module_path |
| 13 | 13 | ||
| 14 | 14 | ||
| @@ -121,6 +121,69 @@ def _render_core_implementation(api, modules): | |||
| 121 | return lines | 121 | return lines |
| 122 | 122 | ||
| 123 | 123 | ||
| 124 | def _generate_features_rules(features_dict): | ||
| 125 | lines = [] | ||
| 126 | for feature, enabled in features_dict.items(): | ||
| 127 | feature = feature.upper() | ||
| 128 | enabled = 'yes' if enabled else 'no' | ||
| 129 | lines.append(f'{feature}_ENABLE={enabled}') | ||
| 130 | return lines | ||
| 131 | |||
| 132 | |||
| 133 | def _generate_modules_rules(keyboard, filename): | ||
| 134 | lines = [] | ||
| 135 | modules = get_modules(keyboard, filename) | ||
| 136 | if len(modules) > 0: | ||
| 137 | lines.append('') | ||
| 138 | lines.append('OPT_DEFS += -DCOMMUNITY_MODULES_ENABLE=TRUE') | ||
| 139 | for module in modules: | ||
| 140 | module_path = qmk.path.unix_style_path(find_module_path(module)) | ||
| 141 | if not module_path: | ||
| 142 | raise FileNotFoundError(f"Module '{module}' not found.") | ||
| 143 | lines.append('') | ||
| 144 | lines.append(f'COMMUNITY_MODULES += {module_path.name}') # use module_path here instead of module as it may be a subdirectory | ||
| 145 | lines.append(f'OPT_DEFS += -DCOMMUNITY_MODULE_{module_path.name.upper()}_ENABLE=TRUE') | ||
| 146 | lines.append(f'COMMUNITY_MODULE_PATHS += {module_path}') | ||
| 147 | lines.append(f'VPATH += {module_path}') | ||
| 148 | lines.append(f'SRC += $(wildcard {module_path}/{module_path.name}.c)') | ||
| 149 | lines.append(f'MODULE_NAME_{module_path.name.upper()} := {module_path.name}') | ||
| 150 | lines.append(f'MODULE_PATH_{module_path.name.upper()} := {module_path}') | ||
| 151 | lines.append(f'-include {module_path}/rules.mk') | ||
| 152 | |||
| 153 | module_jsons = load_module_jsons(modules) | ||
| 154 | for module_json in module_jsons: | ||
| 155 | if 'features' in module_json: | ||
| 156 | lines.append('') | ||
| 157 | lines.append(f'# Module: {module_json["module_name"]}') | ||
| 158 | lines.extend(_generate_features_rules(module_json['features'])) | ||
| 159 | return lines | ||
| 160 | |||
| 161 | |||
| 162 | @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') | ||
| 163 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 164 | @cli.argument('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode") | ||
| 165 | @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rules.mk for.') | ||
| 166 | @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='A configurator export JSON to be compiled and flashed or a pre-compiled binary firmware file (bin/hex) to be flashed.') | ||
| 167 | @cli.subcommand('Creates a community_modules_rules_mk from a keymap.json file.') | ||
| 168 | def generate_community_modules_rules_mk(cli): | ||
| 169 | |||
| 170 | rules_mk_lines = [GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE] | ||
| 171 | |||
| 172 | rules_mk_lines.extend(_generate_modules_rules(cli.args.keyboard, cli.args.filename)) | ||
| 173 | |||
| 174 | # Show the results | ||
| 175 | dump_lines(cli.args.output, rules_mk_lines) | ||
| 176 | |||
| 177 | if cli.args.output: | ||
| 178 | if cli.args.quiet: | ||
| 179 | if cli.args.escape: | ||
| 180 | print(cli.args.output.as_posix().replace(' ', '\\ ')) | ||
| 181 | else: | ||
| 182 | print(cli.args.output) | ||
| 183 | else: | ||
| 184 | cli.log.info('Wrote rules.mk to %s.', cli.args.output) | ||
| 185 | |||
| 186 | |||
| 124 | @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') | 187 | @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') |
| 125 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | 188 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") |
| 126 | @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.h for.') | 189 | @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.h for.') |
