diff options
Diffstat (limited to 'lib')
46 files changed, 1236 insertions, 433 deletions
diff --git a/lib/chibios b/lib/chibios -Subproject be44b3305f9a9fe5f2f49a4e7b978db322dc463 +Subproject 8bd61b804303f1614d574546c2dd735eeabb09f diff --git a/lib/chibios-contrib b/lib/chibios-contrib -Subproject 77cb0a4f7589f89e724f5e6ecb1d76d514dd121 +Subproject 3ac181e4ca5cafddaf8b472baa1d09c2b24c77b diff --git a/lib/fnv/qmk_fnv_type_validation.c b/lib/fnv/qmk_fnv_type_validation.c index e8576617ba..5e8ef5c54c 100644 --- a/lib/fnv/qmk_fnv_type_validation.c +++ b/lib/fnv/qmk_fnv_type_validation.c @@ -1,14 +1,15 @@ // Copyright 2022 Nick Brassel (@tzarc) // SPDX-License-Identifier: GPL-2.0-or-later #include "fnv.h" +#include "compiler_support.h" // This library was originally sourced from: // http://www.isthe.com/chongo/tech/comp/fnv/index.html // // Version at the time of retrieval on 2022-06-26: v5.0.3 -_Static_assert(sizeof(long long) == 8, "long long should be 64 bits"); -_Static_assert(sizeof(unsigned long long) == 8, "unsigned long long should be 64 bits"); +STATIC_ASSERT(sizeof(long long) == 8, "long long should be 64 bits"); +STATIC_ASSERT(sizeof(unsigned long long) == 8, "unsigned long long should be 64 bits"); -_Static_assert(sizeof(Fnv32_t) == 4, "Fnv32_t should be 32 bits"); -_Static_assert(sizeof(Fnv64_t) == 8, "Fnv64_t should be 64 bits"); +STATIC_ASSERT(sizeof(Fnv32_t) == 4, "Fnv32_t should be 32 bits"); +STATIC_ASSERT(sizeof(Fnv64_t) == 8, "Fnv64_t should be 64 bits"); diff --git a/lib/pico-sdk b/lib/pico-sdk -Subproject a3398d8d3a772f37fef44a74743a1de69770e9c +Subproject d0c5cac430cc955b65efa0e899748853d9a8092 diff --git a/lib/python/qmk/build_targets.py b/lib/python/qmk/build_targets.py index e2df029490..35a5f89f91 100644 --- a/lib/python/qmk/build_targets.py +++ b/lib/python/qmk/build_targets.py @@ -11,7 +11,8 @@ from qmk.commands import find_make, get_make_parallel_args, parse_configurator_j from qmk.keyboard import keyboard_folder from qmk.info import keymap_json from qmk.keymap import locate_keymap -from qmk.path import is_under_qmk_firmware, is_under_qmk_userspace +from qmk.path import is_under_qmk_firmware, is_under_qmk_userspace, unix_style_path +from qmk.compilation_database import write_compilation_database # These must be kept in the order in which they're applied to $(TARGET) in the makefiles in order to ensure consistency. TARGET_FILENAME_MODIFIERS = ['FORCE_LAYOUT', 'CONVERT_TO'] @@ -150,7 +151,6 @@ class BuildTarget: def generate_compilation_database(self, build_target: str = None, skip_clean: bool = False, **env_vars) -> None: self.prepare_build(build_target=build_target, **env_vars) command = self.compile_command(build_target=build_target, dry_run=True, **env_vars) - from qmk.cli.generate.compilation_database import write_compilation_database # Lazy load due to circular references output_path = QMK_FIRMWARE / 'compile_commands.json' ret = write_compilation_database(command=command, output_path=output_path, skip_clean=skip_clean, **env_vars) if ret and output_path.exists() and HAS_QMK_USERSPACE: @@ -204,11 +204,11 @@ class KeyboardKeymapBuildTarget(BuildTarget): if is_under_qmk_userspace(keymap_location) and not is_under_qmk_firmware(keymap_location): keymap_directory = keymap_location.parent compile_args.extend([ - f'MAIN_KEYMAP_PATH_1={keymap_directory}', - f'MAIN_KEYMAP_PATH_2={keymap_directory}', - f'MAIN_KEYMAP_PATH_3={keymap_directory}', - f'MAIN_KEYMAP_PATH_4={keymap_directory}', - f'MAIN_KEYMAP_PATH_5={keymap_directory}', + f'MAIN_KEYMAP_PATH_1={unix_style_path(keymap_directory)}', + f'MAIN_KEYMAP_PATH_2={unix_style_path(keymap_directory)}', + f'MAIN_KEYMAP_PATH_3={unix_style_path(keymap_directory)}', + f'MAIN_KEYMAP_PATH_4={unix_style_path(keymap_directory)}', + f'MAIN_KEYMAP_PATH_5={unix_style_path(keymap_directory)}', ]) return compile_args @@ -267,11 +267,11 @@ class JsonKeymapBuildTarget(BuildTarget): generated_files_path = intermediate_output / 'src' keymap_json = generated_files_path / 'keymap.json' compile_args.extend([ - f'MAIN_KEYMAP_PATH_1={intermediate_output}', - f'MAIN_KEYMAP_PATH_2={intermediate_output}', - f'MAIN_KEYMAP_PATH_3={intermediate_output}', - f'MAIN_KEYMAP_PATH_4={intermediate_output}', - f'MAIN_KEYMAP_PATH_5={intermediate_output}', + f'MAIN_KEYMAP_PATH_1={unix_style_path(intermediate_output)}', + f'MAIN_KEYMAP_PATH_2={unix_style_path(intermediate_output)}', + f'MAIN_KEYMAP_PATH_3={unix_style_path(intermediate_output)}', + f'MAIN_KEYMAP_PATH_4={unix_style_path(intermediate_output)}', + f'MAIN_KEYMAP_PATH_5={unix_style_path(intermediate_output)}', f'KEYMAP_JSON={keymap_json}', f'KEYMAP_PATH={generated_files_path}', ]) diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index 08d23cf5ba..785b940456 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py @@ -24,7 +24,7 @@ def _get_chunks(it, size): return iter(lambda: tuple(islice(it, size)), ()) -def _preprocess_c_file(file): +def preprocess_c_file(file): """Load file and strip comments """ file_contents = file.read_text(encoding='utf-8') @@ -66,7 +66,7 @@ def find_layouts(file): parsed_layouts = {} # Search the file for LAYOUT macros and aliases - file_contents = _preprocess_c_file(file) + file_contents = preprocess_c_file(file) for line in file_contents.split('\n'): if layout_macro_define_regex.match(line.lstrip()) and '(' in line and 'LAYOUT' in line: @@ -248,7 +248,7 @@ def _parse_led_config(file, matrix_cols, matrix_rows): current_row_index = 0 current_row = [] - for _type, value in lex(_preprocess_c_file(file), CLexer()): + for _type, value in lex(preprocess_c_file(file), CLexer()): if not found_g_led_config: # Check for type if value == 'led_config_t': diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 2d63dfb447..26905ec134 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -49,6 +49,7 @@ subcommands = [ 'qmk.cli.generate.api', 'qmk.cli.generate.autocorrect_data', 'qmk.cli.generate.compilation_database', + 'qmk.cli.generate.community_modules', 'qmk.cli.generate.config_h', 'qmk.cli.generate.develop_pr_list', 'qmk.cli.generate.dfu_header', @@ -57,7 +58,6 @@ subcommands = [ 'qmk.cli.generate.keyboard_c', 'qmk.cli.generate.keyboard_h', 'qmk.cli.generate.keycodes', - 'qmk.cli.generate.keycodes_tests', 'qmk.cli.generate.keymap_h', 'qmk.cli.generate.make_dependencies', 'qmk.cli.generate.rgb_breathe_table', @@ -82,6 +82,7 @@ subcommands = [ 'qmk.cli.new.keymap', 'qmk.cli.painter', 'qmk.cli.pytest', + 'qmk.cli.resolve_alias', 'qmk.cli.test.c', 'qmk.cli.userspace.add', 'qmk.cli.userspace.compile', @@ -213,7 +214,7 @@ if sys.version_info[0] != 3 or sys.version_info[1] < 9: milc_version = __VERSION__.split('.') -if int(milc_version[0]) < 2 and int(milc_version[1]) < 4: +if int(milc_version[0]) < 2 and int(milc_version[1]) < 9: requirements = Path('requirements.txt').resolve() _eprint(f'Your MILC library is too old! Please upgrade: python3 -m pip install -U -r {str(requirements)}') diff --git a/lib/python/qmk/cli/ci/validate_aliases.py b/lib/python/qmk/cli/ci/validate_aliases.py index 7f781d4397..8b062dbe56 100644 --- a/lib/python/qmk/cli/ci/validate_aliases.py +++ b/lib/python/qmk/cli/ci/validate_aliases.py @@ -2,7 +2,7 @@ """ from milc import cli -from qmk.keyboard import resolve_keyboard, keyboard_folder, keyboard_alias_definitions +from qmk.keyboard import keyboard_folder, keyboard_alias_definitions def _safe_keyboard_folder(target): @@ -17,10 +17,6 @@ def _target_keyboard_exists(target): if not target: return False - # If the target directory existed but there was no rules.mk or rules.mk was incorrectly parsed, then we can't build it. - if not resolve_keyboard(target): - return False - # If the target directory exists but it itself has an invalid alias or invalid rules.mk, then we can't build it either. if not _safe_keyboard_folder(target): return False diff --git a/lib/python/qmk/cli/docs.py b/lib/python/qmk/cli/docs.py index d28dddf194..da02ebf95e 100644 --- a/lib/python/qmk/cli/docs.py +++ b/lib/python/qmk/cli/docs.py @@ -6,6 +6,8 @@ from qmk.docs import prepare_docs_build_area, run_docs_command from milc import cli [email protected]('-p', '--port', default=8936, type=int, help='Port number to use.') [email protected]('-b', '--browser', action='store_true', help='Open the docs in the default browser.') @cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True) def docs(cli): """Spin up a local HTTP server for the QMK docs. @@ -22,6 +24,7 @@ def docs(cli): if not prepare_docs_build_area(is_production=False): return False - if not cli.config.general.verbose: - cli.log.info('Serving docs at http://localhost:5173/ (Ctrl+C to stop)') - run_docs_command('run', 'docs:dev') + cmd = ['docs:dev', '--port', f'{cli.args.port}'] + if cli.args.browser: + cmd.append('--open') + run_docs_command('run', cmd) diff --git a/lib/python/qmk/cli/doctor/check.py b/lib/python/qmk/cli/doctor/check.py index 2804a1d7df..51b0f0c80a 100644 --- a/lib/python/qmk/cli/doctor/check.py +++ b/lib/python/qmk/cli/doctor/check.py @@ -54,10 +54,13 @@ def _check_arm_gcc_installation(): """Returns OK if the arm-none-eabi-gcc is fully installed and can produce binaries. """ with TemporaryDirectory() as temp_dir: - temp_file = Path(temp_dir) / 'test.elf' + temp_in = Path(temp_dir) / 'test.c' + temp_out = Path(temp_dir) / 'test.elf' - args = ['arm-none-eabi-gcc', '-mcpu=cortex-m0', '-mthumb', '-mno-thumb-interwork', '--specs=nosys.specs', '--specs=nano.specs', '-x', 'c', '-o', str(temp_file), '-'] - result = cli.run(args, stdin=None, stdout=None, stderr=None, input='#include <newlib.h>\nint main() { return __NEWLIB__ * __NEWLIB_MINOR__ * __NEWLIB_PATCHLEVEL__; }') + temp_in.write_text('#include <newlib.h>\nint main() { return __NEWLIB__ * __NEWLIB_MINOR__ * __NEWLIB_PATCHLEVEL__; }', encoding='utf-8') + + args = ['arm-none-eabi-gcc', '-mcpu=cortex-m0', '-mthumb', '-mno-thumb-interwork', '--specs=nosys.specs', '--specs=nano.specs', '-x', 'c', '-o', str(temp_out), str(temp_in)] + result = cli.run(args, stdout=None, stderr=None) if result.returncode == 0: cli.log.info('Successfully compiled using arm-none-eabi-gcc') else: @@ -65,8 +68,8 @@ def _check_arm_gcc_installation(): cli.log.error(f'Command: {" ".join(args)}') return CheckStatus.ERROR - args = ['arm-none-eabi-size', str(temp_file)] - result = cli.run(args, stdin=None, stdout=None, stderr=None) + args = ['arm-none-eabi-size', str(temp_out)] + result = cli.run(args, stdout=None, stderr=None) if result.returncode == 0: cli.log.info('Successfully tested arm-none-eabi-binutils using arm-none-eabi-size') else: @@ -91,10 +94,13 @@ def _check_avr_gcc_installation(): """Returns OK if the avr-gcc is fully installed and can produce binaries. """ with TemporaryDirectory() as temp_dir: - temp_file = Path(temp_dir) / 'test.elf' + temp_in = Path(temp_dir) / 'test.c' + temp_out = Path(temp_dir) / 'test.elf' + + temp_in.write_text('int main() { return 0; }', encoding='utf-8') - args = ['avr-gcc', '-mmcu=atmega32u4', '-x', 'c', '-o', str(temp_file), '-'] - result = cli.run(args, stdin=None, stdout=None, stderr=None, input='int main() { return 0; }') + args = ['avr-gcc', '-mmcu=atmega32u4', '-x', 'c', '-o', str(temp_out), str(temp_in)] + result = cli.run(args, stdout=None, stderr=None) if result.returncode == 0: cli.log.info('Successfully compiled using avr-gcc') else: @@ -102,8 +108,8 @@ def _check_avr_gcc_installation(): cli.log.error(f'Command: {" ".join(args)}') return CheckStatus.ERROR - args = ['avr-size', str(temp_file)] - result = cli.run(args, stdin=None, stdout=None, stderr=None) + args = ['avr-size', str(temp_out)] + result = cli.run(args, stdout=None, stderr=None) if result.returncode == 0: cli.log.info('Successfully tested avr-binutils using avr-size') else: diff --git a/lib/python/qmk/cli/format/c.py b/lib/python/qmk/cli/format/c.py index a58aef3fbc..65818155b0 100644 --- a/lib/python/qmk/cli/format/c.py +++ b/lib/python/qmk/cli/format/c.py @@ -10,7 +10,7 @@ from qmk.path import normpath from qmk.c_parse import c_source_files c_file_suffixes = ('c', 'h', 'cpp', 'hpp') -core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms') +core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms', 'modules') ignored = ('tmk_core/protocol/usb_hid', 'platforms/chibios/boards') diff --git a/lib/python/qmk/cli/format/json.py b/lib/python/qmk/cli/format/json.py index 3670294434..61f5254184 100755 --- a/lib/python/qmk/cli/format/json.py +++ b/lib/python/qmk/cli/format/json.py @@ -9,7 +9,7 @@ from milc import cli from qmk.info import info_json from qmk.json_schema import json_load, validate -from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder, UserspaceJSONEncoder +from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder, UserspaceJSONEncoder, CommunityModuleJSONEncoder from qmk.path import normpath @@ -32,6 +32,13 @@ def _detect_json_format(file, json_data): if json_encoder is None: try: + validate(json_data, 'qmk.community_module.v1') + json_encoder = CommunityModuleJSONEncoder + except ValidationError: + pass + + if json_encoder is None: + try: validate(json_data, 'qmk.keyboard.v1') json_encoder = InfoJSONEncoder except ValidationError as e: @@ -54,6 +61,8 @@ def _get_json_encoder(file, json_data): json_encoder = KeymapJSONEncoder elif cli.args.format == 'userspace': json_encoder = UserspaceJSONEncoder + elif cli.args.format == 'community_module': + json_encoder = CommunityModuleJSONEncoder else: # This should be impossible cli.log.error('Unknown format: %s', cli.args.format) @@ -61,7 +70,7 @@ def _get_json_encoder(file, json_data): @cli.argument('json_file', arg_only=True, type=normpath, help='JSON file to format') [email protected]('-f', '--format', choices=['auto', 'keyboard', 'keymap', 'userspace'], default='auto', arg_only=True, help='JSON formatter to use (Default: autodetect)') [email protected]('-f', '--format', choices=['auto', 'keyboard', 'keymap', 'userspace', 'community_module'], default='auto', arg_only=True, help='JSON formatter to use (Default: autodetect)') @cli.argument('-i', '--inplace', action='store_true', arg_only=True, help='If set, will operate in-place on the input file') @cli.argument('-p', '--print', action='store_true', arg_only=True, help='If set, will print the formatted json to stdout ') @cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) diff --git a/lib/python/qmk/cli/format/text.py b/lib/python/qmk/cli/format/text.py index 6dd4511896..344631081b 100644 --- a/lib/python/qmk/cli/format/text.py +++ b/lib/python/qmk/cli/format/text.py @@ -18,7 +18,7 @@ def _get_chunks(it, size): def dos2unix_run(files): """Spawn multiple dos2unix subprocess avoiding too long commands on formatting everything """ - for chunk in _get_chunks(files, 10): + for chunk in _get_chunks([normpath(file).as_posix() for file in files], 10): dos2unix = cli.run(['dos2unix', *chunk]) if dos2unix.returncode: diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 8311818476..7f7c05f6e2 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py @@ -131,6 +131,11 @@ def generate_api(cli): if keymap_rel is None: cli.log.debug('Skipping keymap %s (not in qmk_firmware)', keymap) continue + + if (keymap_rel / 'keymap.c').exists(): + cli.log.debug('Skipping keymap %s (not pure dd keymap)', keymap) + continue + kb_json['keymaps'][keymap.name] = { # TODO: deprecate 'url' as consumer needs to know its potentially hjson 'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap_rel}/keymap.json', diff --git a/lib/python/qmk/cli/generate/community_modules.py b/lib/python/qmk/cli/generate/community_modules.py new file mode 100644 index 0000000000..a5ab61f9bd --- /dev/null +++ b/lib/python/qmk/cli/generate/community_modules.py @@ -0,0 +1,341 @@ +import contextlib +from argcomplete.completers import FilesCompleter +from pathlib import Path + +from milc import cli + +import qmk.path +from qmk.info import get_modules +from qmk.keyboard import keyboard_completer, keyboard_folder +from qmk.commands import dump_lines +from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE +from qmk.community_modules import module_api_list, load_module_jsons, find_module_path + + +def _render_api_guard(lines, api): + if api.guard: + lines.append(f'#if {api.guard}') + yield + if api.guard: + lines.append(f'#endif // {api.guard}') + + +def _render_api_header(api): + lines = [] + if api.header: + lines.append('') + with _render_api_guard(lines, api): + lines.append(f'#include <{api.header}>') + return lines + + +def _render_keycodes(module_jsons): + lines = [] + lines.append('') + lines.append('enum {') + first = True + for module_json in module_jsons: + module_name = Path(module_json['module']).name + keycodes = module_json.get('keycodes', []) + if len(keycodes) > 0: + lines.append(f' // From module: {module_name}') + for keycode in keycodes: + key = keycode.get('key', None) + if first: + lines.append(f' {key} = QK_COMMUNITY_MODULE,') + first = False + else: + lines.append(f' {key},') + for alias in keycode.get('aliases', []): + lines.append(f' {alias} = {key},') + lines.append('') + lines.append(' LAST_COMMUNITY_MODULE_KEY') + lines.append('};') + lines.append('STATIC_ASSERT((int)LAST_COMMUNITY_MODULE_KEY <= (int)(QK_COMMUNITY_MODULE_MAX+1), "Too many community module keycodes");') + return lines + + +def _render_api_declarations(api, module, user_kb=True): + lines = [] + lines.append('') + with _render_api_guard(lines, api): + if user_kb: + lines.append(f'{api.ret_type} {api.name}_{module}_user({api.args});') + lines.append(f'{api.ret_type} {api.name}_{module}_kb({api.args});') + lines.append(f'{api.ret_type} {api.name}_{module}({api.args});') + return lines + + +def _render_api_implementations(api, module): + module_name = Path(module).name + lines = [] + lines.append('') + with _render_api_guard(lines, api): + # _user + lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_user({api.args}) {{') + if api.ret_type == 'bool': + lines.append(' return true;') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' return {api.call_params};') + else: + pass + lines.append('}') + lines.append('') + + # _kb + lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_kb({api.args}) {{') + if api.ret_type == 'bool': + lines.append(f' if(!{api.name}_{module_name}_user({api.call_params})) {{ return false; }}') + lines.append(' return true;') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' return {api.name}_{module_name}_user({api.call_params});') + else: + lines.append(f' {api.name}_{module_name}_user({api.call_params});') + lines.append('}') + lines.append('') + + # module (non-suffixed) + lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}({api.args}) {{') + if api.ret_type == 'bool': + lines.append(f' if(!{api.name}_{module_name}_kb({api.call_params})) {{ return false; }}') + lines.append(' return true;') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' return {api.name}_{module_name}_kb({api.call_params});') + else: + lines.append(f' {api.name}_{module_name}_kb({api.call_params});') + lines.append('}') + return lines + + +def _render_core_implementation(api, modules): + lines = [] + lines.append('') + with _render_api_guard(lines, api): + lines.append(f'{api.ret_type} {api.name}_modules({api.args}) {{') + if api.ret_type == 'bool': + lines.append(' return true') + for module in modules: + module_name = Path(module).name + if api.ret_type == 'bool': + lines.append(f' && {api.name}_{module_name}({api.call_params})') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' {api.call_params} = {api.name}_{module_name}({api.call_params});') + else: + lines.append(f' {api.name}_{module_name}({api.call_params});') + if api.ret_type == 'bool': + lines.append(' ;') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' return {api.call_params};') + lines.append('}') + return lines + + +def _generate_features_rules(features_dict): + lines = [] + for feature, enabled in features_dict.items(): + feature = feature.upper() + enabled = 'yes' if enabled else 'no' + lines.append(f'{feature}_ENABLE={enabled}') + return lines + + +def _generate_modules_rules(keyboard, filename): + lines = [] + modules = get_modules(keyboard, filename) + if len(modules) > 0: + lines.append('') + lines.append('OPT_DEFS += -DCOMMUNITY_MODULES_ENABLE=TRUE') + for module in modules: + module_path = qmk.path.unix_style_path(find_module_path(module)) + if not module_path: + raise FileNotFoundError(f"Module '{module}' not found.") + lines.append('') + lines.append(f'COMMUNITY_MODULES += {module_path.name}') # use module_path here instead of module as it may be a subdirectory + lines.append(f'OPT_DEFS += -DCOMMUNITY_MODULE_{module_path.name.upper()}_ENABLE=TRUE') + lines.append(f'COMMUNITY_MODULE_PATHS += {module_path}') + lines.append(f'VPATH += {module_path}') + lines.append(f'SRC += $(wildcard {module_path}/{module_path.name}.c)') + lines.append(f'MODULE_NAME_{module_path.name.upper()} := {module_path.name}') + lines.append(f'MODULE_PATH_{module_path.name.upper()} := {module_path}') + lines.append(f'-include {module_path}/rules.mk') + + module_jsons = load_module_jsons(modules) + for module_json in module_jsons: + if 'features' in module_json: + lines.append('') + lines.append(f'# Module: {module_json["module_name"]}') + lines.extend(_generate_features_rules(module_json['features'])) + return lines + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rules.mk for.') [email protected]('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.') [email protected]('Creates a community_modules_rules_mk from a keymap.json file.') +def generate_community_modules_rules_mk(cli): + + rules_mk_lines = [GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE] + + rules_mk_lines.extend(_generate_modules_rules(cli.args.keyboard, cli.args.filename)) + + # Show the results + dump_lines(cli.args.output, rules_mk_lines) + + if cli.args.output: + if cli.args.quiet: + if cli.args.escape: + print(cli.args.output.as_posix().replace(' ', '\\ ')) + else: + print(cli.args.output) + else: + cli.log.info('Wrote rules.mk to %s.', cli.args.output) + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.h for.') [email protected]('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file') [email protected]('Creates a community_modules.h from a keymap.json file.') +def generate_community_modules_h(cli): + """Creates a community_modules.h from a keymap.json file + """ + if cli.args.output and cli.args.output.name == '-': + cli.args.output = None + + api_list, api_version, ver_major, ver_minor, ver_patch = module_api_list() + + lines = [ + GPL2_HEADER_C_LIKE, + GENERATED_HEADER_C_LIKE, + '#pragma once', + '#include <stdint.h>', + '#include <stdbool.h>', + '#include <keycodes.h>', + '', + '#include "compiler_support.h"', + '', + '#define COMMUNITY_MODULES_API_VERSION_BUILDER(ver_major,ver_minor,ver_patch) (((((uint32_t)(ver_major))&0xFF) << 24) | ((((uint32_t)(ver_minor))&0xFF) << 16) | (((uint32_t)(ver_patch))&0xFF))', + f'#define COMMUNITY_MODULES_API_VERSION COMMUNITY_MODULES_API_VERSION_BUILDER({ver_major},{ver_minor},{ver_patch})', + f'#define ASSERT_COMMUNITY_MODULES_MIN_API_VERSION(ver_major,ver_minor,ver_patch) STATIC_ASSERT(COMMUNITY_MODULES_API_VERSION_BUILDER(ver_major,ver_minor,ver_patch) <= COMMUNITY_MODULES_API_VERSION, "Community module requires a newer version of QMK modules API -- needs: " #ver_major "." #ver_minor "." #ver_patch ", current: {api_version}.")', + '', + 'typedef struct keyrecord_t keyrecord_t; // forward declaration so we don\'t need to include quantum.h', + '', + ] + + modules = get_modules(cli.args.keyboard, cli.args.filename) + module_jsons = load_module_jsons(modules) + if len(modules) > 0: + lines.extend(_render_keycodes(module_jsons)) + + for api in api_list: + lines.extend(_render_api_header(api)) + + for module in modules: + lines.append('') + lines.append(f'// From module: {module}') + for api in api_list: + lines.extend(_render_api_declarations(api, Path(module).name)) + lines.append('') + + lines.append('// Core wrapper') + for api in api_list: + lines.extend(_render_api_declarations(api, 'modules', user_kb=False)) + + dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True) + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.') [email protected]('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file') [email protected]('Creates a community_modules.c from a keymap.json file.') +def generate_community_modules_c(cli): + """Creates a community_modules.c from a keymap.json file + """ + if cli.args.output and cli.args.output.name == '-': + cli.args.output = None + + api_list, _, _, _, _ = module_api_list() + + lines = [ + GPL2_HEADER_C_LIKE, + GENERATED_HEADER_C_LIKE, + '', + '#include "community_modules.h"', + ] + + modules = get_modules(cli.args.keyboard, cli.args.filename) + if len(modules) > 0: + + for module in modules: + for api in api_list: + lines.extend(_render_api_implementations(api, Path(module).name)) + + for api in api_list: + lines.extend(_render_core_implementation(api, modules)) + + dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True) + + +def _generate_include_per_module(cli, include_file_name): + """Generates C code to include "<module_path>/include_file_name" for each module.""" + if cli.args.output and cli.args.output.name == '-': + cli.args.output = None + + lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE] + + for module in get_modules(cli.args.keyboard, cli.args.filename): + full_path = f'{find_module_path(module)}/{include_file_name}' + lines.append('') + lines.append(f'#if __has_include("{full_path}")') + lines.append(f'#include "{full_path}"') + lines.append(f'#endif // __has_include("{full_path}")') + + dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True) + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules_introspection.h for.') [email protected]('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file') [email protected]('Creates a community_modules_introspection.h from a keymap.json file.') +def generate_community_modules_introspection_h(cli): + """Creates a community_modules_introspection.h from a keymap.json file + """ + _generate_include_per_module(cli, 'introspection.h') + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.') [email protected]('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file') [email protected]('Creates a community_modules_introspection.c from a keymap.json file.') +def generate_community_modules_introspection_c(cli): + """Creates a community_modules_introspection.c from a keymap.json file + """ + _generate_include_per_module(cli, 'introspection.c') + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate led_matrix_community_modules.inc for.') [email protected]('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file') [email protected]('Creates an led_matrix_community_modules.inc from a keymap.json file.') +def generate_led_matrix_community_modules_inc(cli): + """Creates an led_matrix_community_modules.inc from a keymap.json file + """ + _generate_include_per_module(cli, 'led_matrix_module.inc') + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rgb_matrix_community_modules.inc for.') [email protected]('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file') [email protected]('Creates an rgb_matrix_community_modules.inc from a keymap.json file.') +def generate_rgb_matrix_community_modules_inc(cli): + """Creates an rgb_matrix_community_modules.inc from a keymap.json file + """ + _generate_include_per_module(cli, 'rgb_matrix_module.inc') diff --git a/lib/python/qmk/cli/generate/compilation_database.py b/lib/python/qmk/cli/generate/compilation_database.py index b9c716bf0c..339b53c2c2 100755..100644 --- a/lib/python/qmk/cli/generate/compilation_database.py +++ b/lib/python/qmk/cli/generate/compilation_database.py @@ -1,169 +1,9 @@ -"""Creates a compilation database for the given keyboard build. -""" +from milc import cli -import json -import os -import re -import shlex -import shutil -from functools import lru_cache -from pathlib import Path -from typing import Dict, Iterator, List, Union -from milc import cli, MILC - -from qmk.commands import find_make -from qmk.constants import QMK_FIRMWARE -from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.keyboard import keyboard_completer, keyboard_folder -from qmk.keymap import keymap_completer -from qmk.build_targets import KeyboardKeymapBuildTarget - - -@lru_cache(maxsize=10) -def system_libs(binary: str) -> List[Path]: - """Find the system include directory that the given build tool uses. - """ - cli.log.debug("searching for system library directory for binary: %s", binary) - - # Actually query xxxxxx-gcc to find its include paths. - if binary.endswith("gcc") or binary.endswith("g++"): - # (TODO): Remove 'stdin' once 'input' no longer causes issues under MSYS - result = cli.run([binary, '-E', '-Wp,-v', '-'], capture_output=True, check=True, stdin=None, input='\n') - paths = [] - for line in result.stderr.splitlines(): - if line.startswith(" "): - paths.append(Path(line.strip()).resolve()) - return paths - - return list(Path(binary).resolve().parent.parent.glob("*/include")) if binary else [] - - -@lru_cache(maxsize=10) -def cpu_defines(binary: str, compiler_args: str) -> List[str]: - cli.log.debug("gathering definitions for compilation: %s %s", binary, compiler_args) - if binary.endswith("gcc") or binary.endswith("g++"): - invocation = [binary, '-dM', '-E'] - if binary.endswith("gcc"): - invocation.extend(['-x', 'c']) - elif binary.endswith("g++"): - invocation.extend(['-x', 'c++']) - compiler_args = shlex.split(compiler_args) - invocation.extend(compiler_args) - invocation.append('-') - result = cli.run(invocation, capture_output=True, check=True, stdin=None, input='\n') - define_args = [] - for line in result.stdout.splitlines(): - line_args = line.split(' ', 2) - if len(line_args) == 3 and line_args[0] == '#define': - define_args.append(f'-D{line_args[1]}={line_args[2]}') - elif len(line_args) == 2 and line_args[0] == '#define': - define_args.append(f'-D{line_args[1]}') - return list(sorted(set(define_args))) - return [] - - -file_re = re.compile(r'printf "Compiling: ([^"]+)') -cmd_re = re.compile(r'LOG=\$\((.+?)&&') - - -def parse_make_n(f: Iterator[str]) -> List[Dict[str, str]]: - """parse the output of `make -n <target>` - - This function makes many assumptions about the format of your build log. - This happens to work right now for qmk. - """ - - state = 'start' - this_file = None - records = [] - for line in f: - if state == 'start': - m = file_re.search(line) - if m: - this_file = m.group(1) - state = 'cmd' - - if state == 'cmd': - assert this_file - m = cmd_re.search(line) - if m: - # we have a hit! - this_cmd = m.group(1) - args = shlex.split(this_cmd) - binary = shutil.which(args[0]) - compiler_args = set(filter(lambda x: x.startswith('-m') or x.startswith('-f'), args)) - for s in system_libs(binary): - args += ['-isystem', '%s' % s] - args.extend(cpu_defines(binary, ' '.join(shlex.quote(s) for s in compiler_args))) - new_cmd = ' '.join(shlex.quote(s) for s in args) - records.append({"directory": str(QMK_FIRMWARE.resolve()), "command": new_cmd, "file": this_file}) - state = 'start' - - return records - - -def write_compilation_database(keyboard: str = None, keymap: str = None, output_path: Path = QMK_FIRMWARE / 'compile_commands.json', skip_clean: bool = False, command: List[str] = None, **env_vars) -> bool: - # Generate the make command for a specific keyboard/keymap. - if not command: - from qmk.build_targets import KeyboardKeymapBuildTarget # Lazy load due to circular references - target = KeyboardKeymapBuildTarget(keyboard, keymap) - command = target.compile_command(dry_run=True, **env_vars) - - if not command: - cli.log.error('You must supply both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.') - cli.echo('usage: qmk generate-compilation-database [-kb KEYBOARD] [-km KEYMAP]') - return False - - # remove any environment variable overrides which could trip us up - env = os.environ.copy() - env.pop("MAKEFLAGS", None) - - # re-use same executable as the main make invocation (might be gmake) - if not skip_clean: - clean_command = [find_make(), "clean"] - cli.log.info('Making clean with {fg_cyan}%s', ' '.join(clean_command)) - cli.run(clean_command, capture_output=False, check=True, env=env) - - cli.log.info('Gathering build instructions from {fg_cyan}%s', ' '.join(command)) - - result = cli.run(command, capture_output=True, check=True, env=env) - db = parse_make_n(result.stdout.splitlines()) - if not db: - cli.log.error("Failed to parse output from make output:\n%s", result.stdout) - return False - - cli.log.info("Found %s compile commands", len(db)) - - cli.log.info(f"Writing build database to {output_path}") - output_path.write_text(json.dumps(db, indent=4)) - - return True - - [email protected]('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard\'s name') [email protected]('-km', '--keymap', completer=keymap_completer, help='The keymap\'s name') [email protected]('Create a compilation database.') -@automagic_keyboard -@automagic_keymap -def generate_compilation_database(cli: MILC) -> Union[bool, int]: - """Creates a compilation database for the given keyboard build. - - Does a make clean, then a make -n for this target and uses the dry-run output to create - a compilation database (compile_commands.json). This file can help some IDEs and - IDE-like editors work better. For more information about this: - - https://clang.llvm.org/docs/JSONCompilationDatabase.html - """ - # check both config domains: the magic decorator fills in `generate_compilation_database` but the user is - # more likely to have set `compile` in their config file. - current_keyboard = cli.config.generate_compilation_database.keyboard or cli.config.user.keyboard - current_keymap = cli.config.generate_compilation_database.keymap or cli.config.user.keymap - - if not current_keyboard: - cli.log.error('Could not determine keyboard!') - elif not current_keymap: - cli.log.error('Could not determine keymap!') - - target = KeyboardKeymapBuildTarget(current_keyboard, current_keymap) - return target.generate_compilation_database() [email protected]('-kb', '--keyboard', help='[unused] The keyboard\'s name') [email protected]('-km', '--keymap', help='[unused] The keymap\'s name') [email protected]('[deprecated] Create a compilation database.') +def generate_compilation_database(cli): + cli.log.error('This command is deprecated and has effectively been removed. Please use the `--compiledb` flag with `qmk compile` instead.') + return False diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index d613f7b92c..d6d0299291 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -72,19 +72,6 @@ def generate_matrix_size(kb_info_json, config_h_lines): config_h_lines.append(generate_define('MATRIX_ROWS', kb_info_json['matrix_size']['rows'])) -def generate_matrix_masked(kb_info_json, config_h_lines): - """"Enable matrix mask if required""" - mask_required = False - - if 'matrix_grid' in kb_info_json.get('dip_switch', {}): - mask_required = True - if 'matrix_grid' in kb_info_json.get('split', {}).get('handedness', {}): - mask_required = True - - if mask_required: - config_h_lines.append(generate_define('MATRIX_MASKED')) - - def generate_config_items(kb_info_json, config_h_lines): """Iterate through the info_config map to generate basic config values. """ @@ -202,8 +189,6 @@ def generate_config_h(cli): generate_matrix_size(kb_info_json, config_h_lines) - generate_matrix_masked(kb_info_json, config_h_lines) - if 'matrix_pins' in kb_info_json: config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) diff --git a/lib/python/qmk/cli/generate/docs.py b/lib/python/qmk/cli/generate/docs.py index 5821d43b86..7abeca9d2a 100644 --- a/lib/python/qmk/cli/generate/docs.py +++ b/lib/python/qmk/cli/generate/docs.py @@ -27,10 +27,8 @@ def generate_docs(cli): return False cli.log.info('Building vitepress docs') - run_docs_command('run', 'docs:build') + run_docs_command('run', ['docs:build']) cli.log.info('Successfully generated docs to %s.', BUILD_DOCS_PATH) if cli.args.serve: - if not cli.config.general.verbose: - cli.log.info('Serving docs at http://localhost:4173/ (Ctrl+C to stop)') - run_docs_command('run', 'docs:preview') + run_docs_command('run', ['docs:preview']) diff --git a/lib/python/qmk/cli/generate/keyboard_c.py b/lib/python/qmk/cli/generate/keyboard_c.py index 228b320942..1978de4a22 100755 --- a/lib/python/qmk/cli/generate/keyboard_c.py +++ b/lib/python/qmk/cli/generate/keyboard_c.py @@ -1,5 +1,9 @@ """Used by the make system to generate keyboard.c from info.json. """ +import bisect +import dataclasses +from typing import Optional + from milc import cli from qmk.info import info_json @@ -87,6 +91,7 @@ def _gen_matrix_mask(info_data): lines.append(f' 0b{"".join(reversed(mask[i]))},') lines.append('};') lines.append('#endif') + lines.append('') return lines @@ -122,6 +127,128 @@ def _gen_joystick_axes(info_data): lines.append('};') lines.append('#endif') + lines.append('') + + return lines + + +class LayoutKey: + """Geometric info for one key in a layout.""" + row: int + col: int + x: float + y: float + w: float = 1.0 + h: float = 1.0 + hand: Optional[str] = None + + @staticmethod + def from_json(key_json): + row, col = key_json['matrix'] + return LayoutKey( + row=row, + col=col, + x=key_json['x'], + y=key_json['y'], + w=key_json.get('w', 1.0), + h=key_json.get('h', 1.0), + hand=key_json.get('hand', None), + ) + + @property + def cx(self): + """Center x coordinate of the key.""" + return self.x + self.w / 2.0 + + @property + def cy(self): + """Center y coordinate of the key.""" + return self.y + self.h / 2.0 + + +class Layout: + """Geometric info of a layout.""" + def __init__(self, layout_json): + self.keys = [LayoutKey.from_json(key_json) for key_json in layout_json['layout']] + self.x_min = min(key.cx for key in self.keys) + self.x_max = max(key.cx for key in self.keys) + self.x_mid = (self.x_min + self.x_max) / 2 + # If there is one key with width >= 6u, it is probably the spacebar. + i = [i for i, key in enumerate(self.keys) if key.w >= 6.0] + self.spacebar = self.keys[i[0]] if len(i) == 1 else None + + def is_symmetric(self, tol: float = 0.02): + """Whether the key positions are symmetric about x_mid.""" + x = sorted([key.cx for key in self.keys]) + for i in range(len(x)): + x_i_mirrored = 2.0 * self.x_mid - x[i] + # Find leftmost x element greater than or equal to (x_i_mirrored - tol). + j = bisect.bisect_left(x, x_i_mirrored - tol) + if j == len(x) or abs(x[j] - x_i_mirrored) > tol: + return False + + return True + + def widest_horizontal_gap(self): + """Finds the x midpoint of the widest horizontal gap between keys.""" + x = sorted([key.cx for key in self.keys]) + x_mid = self.x_mid + max_sep = 0 + for i in range(len(x) - 1): + sep = x[i + 1] - x[i] + if sep > max_sep: + max_sep = sep + x_mid = (x[i + 1] + x[i]) / 2 + + return x_mid + + +def _gen_chordal_hold_layout(info_data): + """Convert info.json content to chordal_hold_layout + """ + # NOTE: If there are multiple layouts, only the first is read. + for layout_name, layout_json in info_data['layouts'].items(): + layout = Layout(layout_json) + break + + if layout.is_symmetric(): + # If the layout is symmetric (e.g. most split keyboards), guess the + # handedness based on the sign of (x - layout.x_mid). + hand_signs = [key.x - layout.x_mid for key in layout.keys] + elif layout.spacebar is not None: + # If the layout has a spacebar, form a dividing line through the spacebar, + # nearly vertical but with a slight angle to follow typical row stagger. + x0 = layout.spacebar.cx - 0.05 + y0 = layout.spacebar.cy - 1.0 + hand_signs = [(key.x - x0) - (key.y - y0) / 3.0 for key in layout.keys] + else: + # Fallback: assume handedness based on the widest horizontal separation. + x_mid = layout.widest_horizontal_gap() + hand_signs = [key.x - x_mid for key in layout.keys] + + for key, hand_sign in zip(layout.keys, hand_signs): + if key.hand is None: + if key == layout.spacebar or abs(hand_sign) <= 0.02: + key.hand = '*' + else: + key.hand = 'L' if hand_sign < 0.0 else 'R' + + lines = [] + lines.append('#ifdef CHORDAL_HOLD') + line = ('__attribute__((weak)) const char chordal_hold_layout[MATRIX_ROWS][MATRIX_COLS] PROGMEM = ' + layout_name + '(') + + x_prev = None + for key in layout.keys: + if x_prev is None or key.x < x_prev: + lines.append(line) + line = ' ' + line += f"'{key.hand}', " + x_prev = key.x + + lines.append(line[:-2]) + lines.append(');') + lines.append('#endif') return lines @@ -136,11 +263,12 @@ def generate_keyboard_c(cli): kb_info_json = info_json(cli.args.keyboard) # Build the layouts.h file. - keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#include QMK_KEYBOARD_H', ''] + keyboard_c_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#include QMK_KEYBOARD_H', ''] - keyboard_h_lines.extend(_gen_led_configs(kb_info_json)) - keyboard_h_lines.extend(_gen_matrix_mask(kb_info_json)) - keyboard_h_lines.extend(_gen_joystick_axes(kb_info_json)) + keyboard_c_lines.extend(_gen_led_configs(kb_info_json)) + keyboard_c_lines.extend(_gen_matrix_mask(kb_info_json)) + keyboard_c_lines.extend(_gen_joystick_axes(kb_info_json)) + keyboard_c_lines.extend(_gen_chordal_hold_layout(kb_info_json)) # Show the results - dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet) + dump_lines(cli.args.output, keyboard_c_lines, cli.args.quiet) diff --git a/lib/python/qmk/cli/generate/keyboard_h.py b/lib/python/qmk/cli/generate/keyboard_h.py index 5863a0983a..cb9528d96b 100755 --- a/lib/python/qmk/cli/generate/keyboard_h.py +++ b/lib/python/qmk/cli/generate/keyboard_h.py @@ -22,6 +22,11 @@ def _generate_layouts(keyboard, kb_info_json): row_num = kb_info_json['matrix_size']['rows'] lines = [] + lines.append('') + lines.append('// Layout content') + lines.append('') + lines.append('#define XXX KC_NO') + for layout_name, layout_data in kb_info_json['layouts'].items(): if layout_data['c_macro']: continue @@ -31,7 +36,7 @@ def _generate_layouts(keyboard, kb_info_json): continue layout_keys = [] - layout_matrix = [['KC_NO'] * col_num for _ in range(row_num)] + layout_matrix = [['XXX'] * col_num for _ in range(row_num)] for key_data in layout_data['layout']: row, col = key_data['matrix'] @@ -46,7 +51,7 @@ def _generate_layouts(keyboard, kb_info_json): lines.append('') lines.append(f'#define {layout_name}({", ".join(layout_keys)}) {{ \\') - rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix]) + rows = ', \\\n'.join([' { ' + ', '.join(row) + ' }' for row in layout_matrix]) rows += ' \\' lines.append(rows) lines.append('}') @@ -67,6 +72,9 @@ def _generate_keycodes(kb_info_json): return [] lines = [] + lines.append('') + lines.append('// Keycode content') + lines.append('') lines.append('enum keyboard_keycodes {') for index, item in enumerate(kb_info_json.get('keycodes')): @@ -103,17 +111,14 @@ def generate_keyboard_h(cli): valid_config = dd_layouts or keyboard_h # Build the layouts.h file. - keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '#include "quantum.h"'] + keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '', '#include "quantum.h"'] - keyboard_h_lines.append('') - keyboard_h_lines.append('// Layout content') if dd_layouts: keyboard_h_lines.extend(dd_layouts) + if keyboard_h: keyboard_h_lines.append(f'#include "{Path(keyboard_h).name}"') - keyboard_h_lines.append('') - keyboard_h_lines.append('// Keycode content') if dd_keycodes: keyboard_h_lines.extend(dd_keycodes) diff --git a/lib/python/qmk/cli/generate/keycodes.py b/lib/python/qmk/cli/generate/keycodes.py index 719fced5d5..d686935fa8 100644 --- a/lib/python/qmk/cli/generate/keycodes.py +++ b/lib/python/qmk/cli/generate/keycodes.py @@ -125,6 +125,20 @@ def _generate_aliases(lines, keycodes): lines.append(f'#define {alias} {value.get("key")}') +def _generate_version(lines, keycodes, prefix=''): + version = keycodes['version'] + major, minor, patch = map(int, version.split('.')) + + bcd = f'0x{major:02d}{minor:02d}{patch:04d}' + + lines.append('') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION "{version}"') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION_BCD {bcd}') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION_MAJOR {major}') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION_MINOR {minor}') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION_PATCH {patch}') + + @cli.argument('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.') @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @@ -138,6 +152,7 @@ def generate_keycodes(cli): keycodes = load_spec(cli.args.version) + _generate_version(keycodes_h_lines, keycodes) _generate_ranges(keycodes_h_lines, keycodes) _generate_defines(keycodes_h_lines, keycodes) _generate_helpers(keycodes_h_lines, keycodes) @@ -160,6 +175,7 @@ def generate_keycode_extras(cli): keycodes = load_spec(cli.args.version, cli.args.lang) + _generate_version(keycodes_h_lines, keycodes, f'{cli.args.lang.upper()}_') _generate_aliases(keycodes_h_lines, keycodes) # Show the results diff --git a/lib/python/qmk/cli/generate/keycodes_tests.py b/lib/python/qmk/cli/generate/keycodes_tests.py deleted file mode 100644 index 453b4693a7..0000000000 --- a/lib/python/qmk/cli/generate/keycodes_tests.py +++ /dev/null @@ -1,39 +0,0 @@ -"""Used by the make system to generate a keycode lookup table from keycodes_{version}.json -""" -from milc import cli - -from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE -from qmk.commands import dump_lines -from qmk.path import normpath -from qmk.keycodes import load_spec - - -def _generate_defines(lines, keycodes): - lines.append('') - lines.append('std::map<uint16_t, std::string> KEYCODE_ID_TABLE = {') - for key, value in keycodes["keycodes"].items(): - lines.append(f' {{{value.get("key")}, "{value.get("key")}"}},') - lines.append('};') - - [email protected]('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.') [email protected]('-o', '--output', arg_only=True, type=normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('Used by the make system to generate a keycode lookup table from keycodes_{version}.json', hidden=True) -def generate_keycodes_tests(cli): - """Generates a keycode to identifier lookup table for unit test output. - """ - - # Build the keycodes.h file. - keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '// clang-format off'] - keycodes_h_lines.append('extern "C" {\n#include <keycode.h>\n}') - keycodes_h_lines.append('#include <map>') - keycodes_h_lines.append('#include <string>') - keycodes_h_lines.append('#include <cstdint>') - - keycodes = load_spec(cli.args.version) - - _generate_defines(keycodes_h_lines, keycodes) - - # Show the results - dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet) diff --git a/lib/python/qmk/cli/generate/make_dependencies.py b/lib/python/qmk/cli/generate/make_dependencies.py index 331132a20f..9548187888 100755 --- a/lib/python/qmk/cli/generate/make_dependencies.py +++ b/lib/python/qmk/cli/generate/make_dependencies.py @@ -8,7 +8,7 @@ from argcomplete.completers import FilesCompleter from qmk.commands import dump_lines from qmk.keyboard import keyboard_completer, keyboard_folder from qmk.keymap import keymap_completer, locate_keymap -from qmk.path import normpath, FileType +from qmk.path import normpath, FileType, unix_style_path @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), completer=FilesCompleter('.json'), help='A configurator export JSON.') @@ -53,4 +53,4 @@ def generate_make_dependencies(cli): for file in interesting_files: check_files.append(Path('users') / cli.args.keymap / file) - dump_lines(cli.args.output, [f'generated-files: $(wildcard {found})\n' for found in check_files]) + dump_lines(cli.args.output, [f'generated-files: $(wildcard {unix_style_path(found)})\n' for found in check_files]) diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 5291556109..358a22fd1d 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -46,6 +46,15 @@ def process_mapping_rule(kb_info_json, rules_key, info_dict): return generate_rule(rules_key, rules_value) +def generate_features_rules(features_dict): + lines = [] + for feature, enabled in features_dict.items(): + feature = feature.upper() + enabled = 'yes' if enabled else 'no' + lines.append(generate_rule(f'{feature}_ENABLE', enabled)) + return lines + + @cli.argument('filename', nargs='?', arg_only=True, type=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.') @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @@ -80,10 +89,7 @@ def generate_rules_mk(cli): # Iterate through features to enable/disable them if 'features' in kb_info_json: - for feature, enabled in kb_info_json['features'].items(): - feature = feature.upper() - enabled = 'yes' if enabled else 'no' - rules_mk_lines.append(generate_rule(f'{feature}_ENABLE', enabled)) + rules_mk_lines.extend(generate_features_rules(kb_info_json['features'])) # Set SPLIT_TRANSPORT, if needed if kb_info_json.get('split', {}).get('transport', {}).get('protocol') == 'custom': diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index e662407474..5925b57258 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py @@ -52,6 +52,11 @@ def show_keymap(kb_info_json, title_caps=True): if keymap_path and keymap_path.suffix == '.json': keymap_data = json.load(keymap_path.open(encoding='utf-8')) + + # cater for layout-less keymap.json + if 'layout' not in keymap_data: + return + layout_name = keymap_data['layout'] layout_name = kb_info_json.get('layout_aliases', {}).get(layout_name, layout_name) # Resolve alias names diff --git a/lib/python/qmk/cli/lint.py b/lib/python/qmk/cli/lint.py index efb29704ae..484ddb5bd9 100644 --- a/lib/python/qmk/cli/lint.py +++ b/lib/python/qmk/cli/lint.py @@ -1,5 +1,6 @@ """Command to look over a keyboard/keymap and check for common mistakes. """ +from dotty_dict import dotty from pathlib import Path from milc import cli @@ -10,7 +11,8 @@ from qmk.keyboard import keyboard_completer, keyboard_folder_or_all, is_all_keyb from qmk.keymap import locate_keymap, list_keymaps from qmk.path import keyboard from qmk.git import git_get_ignored_files -from qmk.c_parse import c_source_files +from qmk.c_parse import c_source_files, preprocess_c_file +from qmk.json_schema import json_load CHIBIOS_CONF_CHECKS = ['chconf.h', 'halconf.h', 'mcuconf.h', 'board.h'] INVALID_KB_FEATURES = set(['encoder_map', 'dip_switch_map', 'combo', 'tap_dance', 'via']) @@ -26,19 +28,71 @@ def _list_defaultish_keymaps(kb): defaultish.extend(INVALID_KM_NAMES) keymaps = set() - for x in list_keymaps(kb): + for x in list_keymaps(kb, include_userspace=False): if x in defaultish or x.startswith('default'): keymaps.add(x) return keymaps +def _get_readme_files(kb, km=None): + """Return potential keyboard/keymap readme files + """ + search_path = locate_keymap(kb, km).parent if km else keyboard(kb) + + readme_files = [] + + if not km: + current_path = Path(search_path.parts[0]) + for path_part in search_path.parts[1:]: + current_path = current_path / path_part + readme_files.extend(current_path.glob('*readme.md')) + + for file in search_path.glob("**/*readme.md"): + # Ignore keymaps when only globing keyboard files + if not km and 'keymaps' in file.parts: + continue + readme_files.append(file) + + return set(readme_files) + + +def _get_build_files(kb, km=None): + """Return potential keyboard/keymap build files + """ + search_path = locate_keymap(kb, km).parent if km else keyboard(kb) + + build_files = [] + + if not km: + current_path = Path() + for path_part in search_path.parts: + current_path = current_path / path_part + build_files.extend(current_path.glob('*rules.mk')) + + for file in search_path.glob("**/*rules.mk"): + # Ignore keymaps when only globing keyboard files + if not km and 'keymaps' in file.parts: + continue + build_files.append(file) + + return set(build_files) + + def _get_code_files(kb, km=None): """Return potential keyboard/keymap code files """ search_path = locate_keymap(kb, km).parent if km else keyboard(kb) code_files = [] + + if not km: + current_path = Path() + for path_part in search_path.parts: + current_path = current_path / path_part + code_files.extend(current_path.glob('*.h')) + code_files.extend(current_path.glob('*.c')) + for file in c_source_files([search_path]): # Ignore keymaps when only globing keyboard files if not km and 'keymaps' in file.parts: @@ -48,6 +102,43 @@ def _get_code_files(kb, km=None): return code_files +def _is_invalid_readme(file): + """Check if file contains any unfilled content + """ + tokens = [ + '%KEYBOARD%', + '%REAL_NAME%', + '%USER_NAME%', + 'image replace me!', + 'A short description of the keyboard/project', + 'The PCBs, controllers supported', + 'Links to where you can find this hardware', + ] + + for line in file.read_text(encoding='utf-8').split("\n"): + if any(token in line for token in tokens): + return True + return False + + +def _is_empty_rules(file): + """Check if file contains any useful content + """ + for line in file.read_text(encoding='utf-8').split("\n"): + if len(line) > 0 and not line.isspace() and not line.startswith('#'): + return False + return True + + +def _is_empty_include(file): + """Check if file contains any useful content + """ + for line in preprocess_c_file(file).split("\n"): + if len(line) > 0 and not line.isspace() and not line.startswith('#pragma once'): + return False + return True + + def _has_license(file): """Check file has a license header """ @@ -82,6 +173,14 @@ def _handle_invalid_features(kb, info): return ok +def _handle_invalid_config(kb, info): + """Check for invalid keyboard level config + """ + if info.get('url') == "": + cli.log.warning(f'{kb}: Invalid keyboard level config detected - Optional field "url" should not be empty.') + return True + + def _chibios_conf_includenext_check(target): """Check the ChibiOS conf.h for the correct inclusion of the next conf.h """ @@ -91,39 +190,56 @@ def _chibios_conf_includenext_check(target): return None -def _rules_mk_assignment_only(kb): +def _rules_mk_assignment_only(rules_mk): """Check the keyboard-level rules.mk to ensure it only has assignments. """ - keyboard_path = keyboard(kb) - current_path = Path() errors = [] + continuation = None + for i, line in enumerate(rules_mk.open()): + line = line.strip() - for path_part in keyboard_path.parts: - current_path = current_path / path_part - rules_mk = current_path / 'rules.mk' + if '#' in line: + line = line[:line.index('#')] - if rules_mk.exists(): + if continuation: + line = continuation + line continuation = None - for i, line in enumerate(rules_mk.open()): - line = line.strip() + if line: + if line[-1] == '\\': + continuation = line[:-1] + continue - if '#' in line: - line = line[:line.index('#')] + if line and '=' not in line: + errors.append(f'Non-assignment code on line +{i} {rules_mk}: {line}') - if continuation: - line = continuation + line - continuation = None + return errors - if line: - if line[-1] == '\\': - continuation = line[:-1] - continue - if line and '=' not in line: - errors.append(f'Non-assignment code on line +{i} {rules_mk}: {line}') +def _handle_duplicating_code_defaults(kb, info): + def _collect_dotted_output(kb_info_json, prefix=''): + """Print the info.json in a plain text format with dot-joined keys. + """ + for key in sorted(kb_info_json): + new_prefix = f'{prefix}.{key}' if prefix else key - return errors + if isinstance(kb_info_json[key], dict): + yield from _collect_dotted_output(kb_info_json[key], new_prefix) + elif isinstance(kb_info_json[key], list): + # TODO: handle non primitives? + yield (new_prefix, kb_info_json[key]) + else: + yield (new_prefix, kb_info_json[key]) + + defaults_map = json_load(Path('data/mappings/info_defaults.hjson')) + dotty_info = dotty(info) + + for key, v_default in _collect_dotted_output(defaults_map): + v_info = dotty_info.get(key) + if v_default == v_info: + cli.log.warning(f'{kb}: Option "{key}" duplicates default value of "{v_default}"') + + return True def keymap_check(kb, km): @@ -162,7 +278,7 @@ def keymap_check(kb, km): return ok -def keyboard_check(kb): +def keyboard_check(kb): # noqa C901 """Perform the keyboard level checks. """ ok = True @@ -175,12 +291,11 @@ def keyboard_check(kb): if not _handle_invalid_features(kb, kb_info): ok = False - rules_mk_assignment_errors = _rules_mk_assignment_only(kb) - if rules_mk_assignment_errors: + if not _handle_invalid_config(kb, kb_info): + ok = False + + if not _handle_duplicating_code_defaults(kb, kb_info): ok = False - cli.log.error('%s: Non-assignment code found in rules.mk. Move it to post_rules.mk instead.', kb) - for assignment_error in rules_mk_assignment_errors: - cli.log.error(assignment_error) invalid_files = git_get_ignored_files(f'keyboards/{kb}/') for file in invalid_files: @@ -189,11 +304,34 @@ def keyboard_check(kb): cli.log.error(f'{kb}: The file "{file}" should not exist!') ok = False + for file in _get_readme_files(kb): + if _is_invalid_readme(file): + cli.log.error(f'{kb}: The file "{file}" still contains template tokens!') + ok = False + + for file in _get_build_files(kb): + if _is_empty_rules(file): + cli.log.error(f'{kb}: The file "{file}" is effectively empty and should be removed!') + ok = False + + if file.suffix in ['rules.mk']: + rules_mk_assignment_errors = _rules_mk_assignment_only(file) + if rules_mk_assignment_errors: + ok = False + cli.log.error('%s: Non-assignment code found in rules.mk. Move it to post_rules.mk instead.', kb) + for assignment_error in rules_mk_assignment_errors: + cli.log.error(assignment_error) + for file in _get_code_files(kb): if not _has_license(file): cli.log.error(f'{kb}: The file "{file}" does not have a license header!') ok = False + if file.name in ['config.h']: + if _is_empty_include(file): + cli.log.error(f'{kb}: The file "{file}" is effectively empty and should be removed!') + ok = False + if file.name in CHIBIOS_CONF_CHECKS: check_error = _chibios_conf_includenext_check(file) if check_error is not None: @@ -221,10 +359,10 @@ def lint(cli): if isinstance(cli.config.lint.keyboard, str): # if provided via config - string not array keyboard_list = [cli.config.lint.keyboard] - elif is_all_keyboards(cli.args.keyboard[0]): + elif any(is_all_keyboards(kb) for kb in cli.args.keyboard): keyboard_list = list_keyboards() else: - keyboard_list = cli.config.lint.keyboard + keyboard_list = list(set(cli.config.lint.keyboard)) failed = [] diff --git a/lib/python/qmk/cli/list/keyboards.py b/lib/python/qmk/cli/list/keyboards.py index 405b9210e4..8b6c451673 100644 --- a/lib/python/qmk/cli/list/keyboards.py +++ b/lib/python/qmk/cli/list/keyboards.py @@ -5,10 +5,9 @@ from milc import cli import qmk.keyboard [email protected]('--no-resolve-defaults', arg_only=True, action='store_false', help='Ignore any "DEFAULT_FOLDER" within keyboards rules.mk') @cli.subcommand("List the keyboards currently defined within QMK") def list_keyboards(cli): """List the keyboards currently defined within QMK """ - for keyboard_name in qmk.keyboard.list_keyboards(cli.args.no_resolve_defaults): + for keyboard_name in qmk.keyboard.list_keyboards(): print(keyboard_name) diff --git a/lib/python/qmk/cli/migrate.py b/lib/python/qmk/cli/migrate.py index 0bab5c1949..d0f195d737 100644 --- a/lib/python/qmk/cli/migrate.py +++ b/lib/python/qmk/cli/migrate.py @@ -6,14 +6,14 @@ from dotty_dict import dotty from milc import cli -from qmk.keyboard import keyboard_completer, keyboard_folder, resolve_keyboard +from qmk.keyboard import keyboard_completer, keyboard_folder from qmk.info import info_json, find_info_json from qmk.json_encoders import InfoJSONEncoder from qmk.json_schema import json_load def _candidate_files(keyboard): - kb_dir = Path(resolve_keyboard(keyboard)) + kb_dir = Path(keyboard) cur_dir = Path('keyboards') files = [] diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index f1df05636c..20a0c0198c 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -1,10 +1,12 @@ """This script automates the copying of the default keymap into your own keymap. """ import re +import json import shutil +from pathlib import Path from milc import cli -from milc.questions import question +from milc.questions import question, choice from qmk.constants import HAS_QMK_USERSPACE, QMK_USERSPACE from qmk.path import is_keyboard, keymaps, keymap @@ -12,6 +14,34 @@ from qmk.git import git_get_username from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.keyboard import keyboard_completer, keyboard_folder from qmk.userspace import UserspaceDefs +from qmk.json_schema import json_load +from qmk.json_encoders import KeymapJSONEncoder +from qmk.info import info_json + + +def _list_available_converters(kb_name): + """Search for converters that can be applied to a given keyboard + """ + if not is_keyboard(kb_name): + return None + + info = info_json(kb_name) + pin_compatible = info.get('pin_compatible') + if not pin_compatible: + return None + + return sorted(folder.name.split('_to_')[-1] for folder in Path('platforms').glob(f'*/converters/{pin_compatible}_to_*')) + + +def _set_converter(file, converter): + """add/overwrite any existing converter specified in keymap.json + """ + json_data = json_load(file) if file.exists() else {} + + json_data['converter'] = converter + + output = json.dumps(json_data, cls=KeymapJSONEncoder, sort_keys=True) + file.write_text(output + '\n', encoding='utf-8') def validate_keymap_name(name): @@ -23,24 +53,42 @@ def validate_keymap_name(name): def prompt_keyboard(): prompt = """{fg_yellow}Select Keyboard{style_reset_all} -If you`re unsure you can view a full list of supported keyboards with {fg_yellow}qmk list-keyboards{style_reset_all}. +If you're unsure you can view a full list of supported keyboards with {fg_yellow}qmk list-keyboards{style_reset_all}. Keyboard Name? """ - return question(prompt) def prompt_user(): prompt = """ {fg_yellow}Name Your Keymap{style_reset_all} -Used for maintainer, copyright, etc -Your GitHub Username? """ +Keymap name? """ return question(prompt, default=git_get_username()) +def prompt_converter(kb_name): + prompt = """ +{fg_yellow}Configure Development Board{style_reset_all} +For more information, see: +https://docs.qmk.fm/feature_converters + +Use converter? """ + + converters = _list_available_converters(kb_name) + if not converters: + return None + + choices = ['No (default)', *converters] + + answer = choice(prompt, options=choices, default=0) + return None if choices.index(answer) == 0 else answer + + @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Specify keyboard name. Example: 1upkeyboards/1up60hse') @cli.argument('-km', '--keymap', help='Specify the name for the new keymap directory') [email protected]('--converter', help='Specify the name of a converter to configure') [email protected]('--skip-converter', arg_only=True, action='store_true', help='Skip converter') @cli.subcommand('Creates a new keymap for the keyboard of your choosing') @automagic_keyboard @automagic_keymap @@ -53,9 +101,12 @@ def new_keymap(cli): # ask for user input if keyboard or keymap was not provided in the command line kb_name = cli.config.new_keymap.keyboard if cli.config.new_keymap.keyboard else prompt_keyboard() user_name = cli.config.new_keymap.keymap if cli.config.new_keymap.keymap else prompt_user() + converter = cli.config.new_keymap.converter if cli.args.skip_converter or cli.config.new_keymap.converter else prompt_converter(kb_name) # check directories - if not is_keyboard(kb_name): + try: + kb_name = keyboard_folder(kb_name) + except ValueError: cli.log.error(f'Keyboard {{fg_cyan}}{kb_name}{{fg_reset}} does not exist! Please choose a valid name.') return False @@ -79,6 +130,9 @@ def new_keymap(cli): # create user directory with default keymap files shutil.copytree(keymap_path_default, keymap_path_new, symlinks=True) + if converter: + _set_converter(keymap_path_new / 'keymap.json', converter) + # end message to user cli.log.info(f'{{fg_green}}Created a new keymap called {{fg_cyan}}{user_name}{{fg_green}} in: {{fg_cyan}}{keymap_path_new}.{{fg_reset}}') cli.log.info(f"Compile a firmware with your new keymap by typing: {{fg_yellow}}qmk compile -kb {kb_name} -km {user_name}{{fg_reset}}.") diff --git a/lib/python/qmk/cli/resolve_alias.py b/lib/python/qmk/cli/resolve_alias.py new file mode 100644 index 0000000000..dff2242b28 --- /dev/null +++ b/lib/python/qmk/cli/resolve_alias.py @@ -0,0 +1,16 @@ +from qmk.keyboard import keyboard_folder + +from milc import cli + + [email protected]('--allow-unknown', arg_only=True, action='store_true', help="Return original if rule is not a valid keyboard.") [email protected]('keyboard', arg_only=True, help='The keyboard\'s name') [email protected]('Resolve any keyboard_aliases for provided rule') +def resolve_alias(cli): + try: + print(keyboard_folder(cli.args.keyboard)) + except ValueError: + if cli.args.allow_unknown: + print(cli.args.keyboard) + else: + raise diff --git a/lib/python/qmk/cli/via2json.py b/lib/python/qmk/cli/via2json.py index 537e102640..0997e9ca9f 100755 --- a/lib/python/qmk/cli/via2json.py +++ b/lib/python/qmk/cli/via2json.py @@ -9,19 +9,15 @@ import qmk.keyboard import qmk.path from qmk.info import info_json from qmk.json_encoders import KeymapJSONEncoder -from qmk.commands import parse_configurator_json, dump_lines -from qmk.keymap import generate_json, list_keymaps, locate_keymap, parse_keymap_c +from qmk.commands import dump_lines +from qmk.keymap import generate_json -def _find_via_layout_macro(keyboard): - keymap_layout = None - if 'via' in list_keymaps(keyboard): - keymap_path = locate_keymap(keyboard, 'via') - if keymap_path.suffix == '.json': - keymap_layout = parse_configurator_json(keymap_path)['layout'] - else: - keymap_layout = parse_keymap_c(keymap_path)['layers'][0]['layout'] - return keymap_layout +def _find_via_layout_macro(keyboard_data): + """Assume layout macro when only 1 is available + """ + layouts = list(keyboard_data['layouts'].keys()) + return layouts[0] if len(layouts) == 1 else None def _convert_macros(via_macros): @@ -130,20 +126,16 @@ def via2json(cli): This command uses the `qmk.keymap` module to generate a keymap.json from a VIA backup json. The generated keymap is written to stdout, or to a file if -o is provided. """ - # Find appropriate layout macro - keymap_layout = cli.args.layout if cli.args.layout else _find_via_layout_macro(cli.args.keyboard) - if not keymap_layout: - cli.log.error(f"Couldn't find LAYOUT macro for keyboard {cli.args.keyboard}. Please specify it with the '-l' argument.") - return False - # Load the VIA backup json with cli.args.filename.open('r') as fd: via_backup = json.load(fd) - # Generate keyboard metadata keyboard_data = info_json(cli.args.keyboard) - if not keyboard_data: - cli.log.error(f'LAYOUT macro {keymap_layout} is not a valid one for keyboard {cli.args.keyboard}!') + + # Find appropriate layout macro + keymap_layout = cli.args.layout if cli.args.layout else _find_via_layout_macro(keyboard_data) + if not keymap_layout: + cli.log.error(f"Couldn't find LAYOUT macro for keyboard {cli.args.keyboard}. Please specify it with the '-l' argument.") return False # Get keycode array diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index a05b3641b5..ac1455967d 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -12,6 +12,7 @@ from qmk.constants import QMK_USERSPACE, HAS_QMK_USERSPACE from qmk.json_schema import json_load, validate from qmk.keyboard import keyboard_alias_definitions from qmk.util import maybe_exit +from qmk.path import unix_style_path def find_make(): @@ -85,7 +86,7 @@ def build_environment(args): envs = parse_env_vars(args) if HAS_QMK_USERSPACE: - envs['QMK_USERSPACE'] = Path(QMK_USERSPACE).resolve() + envs['QMK_USERSPACE'] = unix_style_path(Path(QMK_USERSPACE).resolve()) return envs @@ -98,11 +99,14 @@ def in_virtualenv(): return active_prefix != sys.prefix -def dump_lines(output_file, lines, quiet=True): +def dump_lines(output_file, lines, quiet=True, remove_repeated_newlines=False): """Handle dumping to stdout or file Creates parent folders if required """ generated = '\n'.join(lines) + '\n' + if remove_repeated_newlines: + while '\n\n\n' in generated: + generated = generated.replace('\n\n\n', '\n\n') if output_file and output_file.name != '-': output_file.parent.mkdir(parents=True, exist_ok=True) if output_file.exists(): diff --git a/lib/python/qmk/community_modules.py b/lib/python/qmk/community_modules.py new file mode 100644 index 0000000000..f7e96a6b93 --- /dev/null +++ b/lib/python/qmk/community_modules.py @@ -0,0 +1,100 @@ +import os + +from pathlib import Path +from functools import lru_cache + +from milc.attrdict import AttrDict + +from qmk.json_schema import json_load, validate, merge_ordered_dicts +from qmk.util import truthy +from qmk.constants import QMK_FIRMWARE, QMK_USERSPACE, HAS_QMK_USERSPACE +from qmk.path import under_qmk_firmware, under_qmk_userspace + +COMMUNITY_MODULE_JSON_FILENAME = 'qmk_module.json' + + +class ModuleAPI(AttrDict): + def __init__(self, **kwargs): + super().__init__() + for key, value in kwargs.items(): + self[key] = value + + +@lru_cache(maxsize=1) +def module_api_list(): + module_definition_files = sorted(set(QMK_FIRMWARE.glob('data/constants/module_hooks/*.hjson'))) + module_definition_jsons = [json_load(f) for f in module_definition_files] + module_definitions = merge_ordered_dicts(module_definition_jsons) + latest_module_version = module_definition_files[-1].stem + latest_module_version_parts = latest_module_version.split('.') + + api_list = [] + for name, mod in module_definitions.items(): + api_list.append(ModuleAPI( + ret_type=mod['ret_type'], + name=name, + args=mod['args'], + call_params=mod.get('call_params', ''), + guard=mod.get('guard', None), + header=mod.get('header', None), + )) + + return api_list, latest_module_version, latest_module_version_parts[0], latest_module_version_parts[1], latest_module_version_parts[2] + + +def find_available_module_paths(): + """Find all available modules. + """ + search_dirs = [] + if HAS_QMK_USERSPACE: + search_dirs.append(QMK_USERSPACE / 'modules') + search_dirs.append(QMK_FIRMWARE / 'modules') + + modules = [] + for search_dir in search_dirs: + for module_json_path in search_dir.rglob(COMMUNITY_MODULE_JSON_FILENAME): + modules.append(module_json_path.parent) + return modules + + +def find_module_path(module): + """Find a module by name. + """ + for module_path in find_available_module_paths(): + # Ensure the module directory is under QMK Firmware or QMK Userspace + relative_path = under_qmk_firmware(module_path) + if not relative_path: + relative_path = under_qmk_userspace(module_path) + if not relative_path: + continue + + lhs = str(relative_path.as_posix())[len('modules/'):] + rhs = str(Path(module).as_posix()) + + if relative_path and lhs == rhs: + return module_path + return None + + +def load_module_json(module): + """Load a module JSON file. + """ + module_path = find_module_path(module) + if not module_path: + raise FileNotFoundError(f'Module not found: {module}') + + module_json = json_load(module_path / COMMUNITY_MODULE_JSON_FILENAME) + + if not truthy(os.environ.get('SKIP_SCHEMA_VALIDATION'), False): + validate(module_json, 'qmk.community_module.v1') + + module_json['module'] = module + module_json['module_path'] = module_path + + return module_json + + +def load_module_jsons(modules): + """Load the module JSON files, matching the specified order. + """ + return list(map(load_module_json, modules)) diff --git a/lib/python/qmk/compilation_database.py b/lib/python/qmk/compilation_database.py new file mode 100755 index 0000000000..851dc3f157 --- /dev/null +++ b/lib/python/qmk/compilation_database.py @@ -0,0 +1,140 @@ +"""Creates a compilation database for the given keyboard build. +""" + +import json +import os +import re +import shlex +import shutil +from functools import lru_cache +from pathlib import Path +from typing import Dict, Iterator, List + +from milc import cli + +from qmk.commands import find_make +from qmk.constants import QMK_FIRMWARE + + +@lru_cache(maxsize=10) +def system_libs(binary: str) -> List[Path]: + """Find the system include directory that the given build tool uses. + """ + cli.log.debug("searching for system library directory for binary: %s", binary) + + # Actually query xxxxxx-gcc to find its include paths. + if binary.endswith("gcc") or binary.endswith("g++"): + # (TODO): Remove 'stdin' once 'input' no longer causes issues under MSYS + result = cli.run([binary, '-E', '-Wp,-v', '-'], capture_output=True, check=True, stdin=None, input='\n') + paths = [] + for line in result.stderr.splitlines(): + if line.startswith(" "): + paths.append(Path(line.strip()).resolve()) + return paths + + return list(Path(binary).resolve().parent.parent.glob("*/include")) if binary else [] + + +@lru_cache(maxsize=10) +def cpu_defines(binary: str, compiler_args: str) -> List[str]: + cli.log.debug("gathering definitions for compilation: %s %s", binary, compiler_args) + if binary.endswith("gcc") or binary.endswith("g++"): + invocation = [binary, '-dM', '-E'] + if binary.endswith("gcc"): + invocation.extend(['-x', 'c', '-std=gnu11']) + elif binary.endswith("g++"): + invocation.extend(['-x', 'c++', '-std=gnu++14']) + invocation.extend(shlex.split(compiler_args)) + invocation.append('-') + result = cli.run(invocation, capture_output=True, check=True, stdin=None, input='\n') + define_args = [] + for line in result.stdout.splitlines(): + line_args = line.split(' ', 2) + if len(line_args) == 3 and line_args[0] == '#define': + define_args.append(f'-D{line_args[1]}={line_args[2]}') + elif len(line_args) == 2 and line_args[0] == '#define': + define_args.append(f'-D{line_args[1]}') + + type_filter = re.compile( + r'^-D__(SIZE|INT|UINT|WINT|WCHAR|BYTE|SHRT|SIG|FLOAT|LONG|CHAR|SCHAR|DBL|FLT|LDBL|PTRDIFF|QQ|DQ|DA|HA|HQ|SA|SQ|TA|TQ|UDA|UDQ|UHA|UHQ|USQ|USA|UTQ|UTA|UQQ|UQA|ACCUM|FRACT|UACCUM|UFRACT|LACCUM|LFRACT|ULACCUM|ULFRACT|LLACCUM|LLFRACT|ULLACCUM|ULLFRACT|SACCUM|SFRACT|USACCUM|USFRACT)' + ) + return list(sorted(set(filter(lambda x: not type_filter.match(x), define_args)))) + return [] + + +file_re = re.compile(r'printf "Compiling: ([^"]+)') +cmd_re = re.compile(r'LOG=\$\((.+?)&&') + + +def parse_make_n(f: Iterator[str]) -> List[Dict[str, str]]: + """parse the output of `make -n <target>` + + This function makes many assumptions about the format of your build log. + This happens to work right now for qmk. + """ + + state = 'start' + this_file = None + records = [] + for line in f: + if state == 'start': + m = file_re.search(line) + if m: + this_file = m.group(1) + state = 'cmd' + + if state == 'cmd': + assert this_file + m = cmd_re.search(line) + if m: + # we have a hit! + this_cmd = m.group(1) + args = shlex.split(this_cmd) + binary = shutil.which(args[0]) + compiler_args = set(filter(lambda x: x.startswith('-m') or x.startswith('-f'), args)) + for s in system_libs(binary): + args += ['-isystem', '%s' % s] + args.extend(cpu_defines(binary, ' '.join(shlex.quote(s) for s in compiler_args))) + args[0] = binary + records.append({"arguments": args, "directory": str(QMK_FIRMWARE.resolve()), "file": this_file}) + state = 'start' + + return records + + +def write_compilation_database(keyboard: str = None, keymap: str = None, output_path: Path = QMK_FIRMWARE / 'compile_commands.json', skip_clean: bool = False, command: List[str] = None, **env_vars) -> bool: + # Generate the make command for a specific keyboard/keymap. + if not command: + from qmk.build_targets import KeyboardKeymapBuildTarget # Lazy load due to circular references + target = KeyboardKeymapBuildTarget(keyboard, keymap) + command = target.compile_command(dry_run=True, **env_vars) + + if not command: + cli.log.error('You must supply both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.') + cli.echo('usage: qmk generate-compilation-database [-kb KEYBOARD] [-km KEYMAP]') + return False + + # remove any environment variable overrides which could trip us up + env = os.environ.copy() + env.pop("MAKEFLAGS", None) + + # re-use same executable as the main make invocation (might be gmake) + if not skip_clean: + clean_command = [find_make(), "clean"] + cli.log.info('Making clean with {fg_cyan}%s', ' '.join(clean_command)) + cli.run(clean_command, capture_output=False, check=True, env=env) + + cli.log.info('Gathering build instructions from {fg_cyan}%s', ' '.join(command)) + + result = cli.run(command, capture_output=True, check=True, env=env) + db = parse_make_n(result.stdout.splitlines()) + if not db: + cli.log.error("Failed to parse output from make output:\n%s", result.stdout) + return False + + cli.log.info("Found %s compile commands", len(db)) + + cli.log.info(f"Writing build database to {output_path}") + output_path.write_text(json.dumps(db, indent=4)) + + return True diff --git a/lib/python/qmk/constants.py b/lib/python/qmk/constants.py index e055d3fbc9..e3e47c2bd2 100644 --- a/lib/python/qmk/constants.py +++ b/lib/python/qmk/constants.py @@ -22,7 +22,7 @@ QMK_FIRMWARE_UPSTREAM = 'qmk/qmk_firmware' MAX_KEYBOARD_SUBFOLDERS = 5 # Supported processor types -CHIBIOS_PROCESSORS = 'cortex-m0', 'cortex-m0plus', 'cortex-m3', 'cortex-m4', 'MKL26Z64', 'MK20DX128', 'MK20DX256', 'MK64FX512', 'MK66FX1M0', 'RP2040', 'STM32F042', 'STM32F072', 'STM32F103', 'STM32F303', 'STM32F401', 'STM32F405', 'STM32F407', 'STM32F411', 'STM32F446', 'STM32G431', 'STM32G474', 'STM32H723', 'STM32H733', 'STM32L412', 'STM32L422', 'STM32L432', 'STM32L433', 'STM32L442', 'STM32L443', 'GD32VF103', 'WB32F3G71', 'WB32FQ95', 'AT32F415' +CHIBIOS_PROCESSORS = 'cortex-m0', 'cortex-m0plus', 'cortex-m3', 'cortex-m4', 'MKL26Z64', 'MK20DX128', 'MK20DX256', 'MK64FX512', 'MK66FX1M0', 'RP2040', 'STM32F042', 'STM32F072', 'STM32F103', 'STM32F303', 'STM32F401', 'STM32F405', 'STM32F407', 'STM32F411', 'STM32F446', 'STM32G0B1', 'STM32G431', 'STM32G474', 'STM32H723', 'STM32H733', 'STM32L412', 'STM32L422', 'STM32L432', 'STM32L433', 'STM32L442', 'STM32L443', 'GD32VF103', 'WB32F3G71', 'WB32FQ95', 'AT32F415' LUFA_PROCESSORS = 'at90usb162', 'atmega16u2', 'atmega32u2', 'atmega16u4', 'atmega32u4', 'at90usb646', 'at90usb647', 'at90usb1286', 'at90usb1287', None VUSB_PROCESSORS = 'atmega32a', 'atmega328p', 'atmega328', 'attiny85' @@ -42,6 +42,7 @@ MCU2BOOTLOADER = { "STM32F407": "stm32-dfu", "STM32F411": "stm32-dfu", "STM32F446": "stm32-dfu", + "STM32G0B1": "stm32-dfu", "STM32G431": "stm32-dfu", "STM32G474": "stm32-dfu", "STM32H723": "stm32-dfu", diff --git a/lib/python/qmk/decorators.py b/lib/python/qmk/decorators.py index 16b44d6d59..0cad55a483 100644 --- a/lib/python/qmk/decorators.py +++ b/lib/python/qmk/decorators.py @@ -5,7 +5,7 @@ from time import monotonic from milc import cli -from qmk.keyboard import find_keyboard_from_dir +from qmk.keyboard import find_keyboard_from_dir, keyboard_folder from qmk.keymap import find_keymap_from_dir @@ -27,6 +27,11 @@ def automagic_keyboard(func): def wrapper(*args, **kwargs): cmd = _get_subcommand_name() + # TODO: Workaround for if config file contains "old" keyboard name + # Potential long-term fix needs to be within global cli or milc + if cli.config_source[cmd]['keyboard'] == 'config_file': + cli.config[cmd]['keyboard'] = keyboard_folder(cli.config[cmd]['keyboard']) + # Ensure that `--keyboard` was not passed and CWD is under `qmk_firmware/keyboards` if cli.config_source[cmd]['keyboard'] != 'argument': keyboard = find_keyboard_from_dir() diff --git a/lib/python/qmk/docs.py b/lib/python/qmk/docs.py index 56694cf6ae..75d2d60bda 100644 --- a/lib/python/qmk/docs.py +++ b/lib/python/qmk/docs.py @@ -17,18 +17,18 @@ BUILD_DOCS_PATH = BUILD_PATH / 'docs' DOXYGEN_PATH = BUILD_DOCS_PATH / 'static' / 'doxygen' -def run_docs_command(verb, cmd=None): +def run_docs_command(verb, cmd_args=None): environ['PATH'] += pathsep + str(NODE_MODULES_PATH / '.bin') - args = {'capture_output': False if cli.config.general.verbose else True, 'check': True, 'stdin': DEVNULL} + args = {'capture_output': False, 'check': True} docs_env = environ.copy() if cli.config.general.verbose: docs_env['DEBUG'] = 'vitepress:*,vite:*' args['env'] = docs_env arg_list = ['yarn', verb] - if cmd: - arg_list.append(cmd) + if cmd_args: + arg_list.extend(cmd_args) chdir(BUILDDEFS_PATH) cli.run(arg_list, **args) diff --git a/lib/python/qmk/flashers.py b/lib/python/qmk/flashers.py index 7ee1bc8de7..2cca4941d3 100644 --- a/lib/python/qmk/flashers.py +++ b/lib/python/qmk/flashers.py @@ -202,6 +202,10 @@ def _flash_mdloader(file): def _flash_uf2(file): + output = cli.run(['util/uf2conv.py', '--info', file]).stdout + if 'UF2 File' not in output: + return True + cli.run(['util/uf2conv.py', '--deploy', file], capture_output=False) @@ -235,7 +239,8 @@ def flasher(mcu, file): elif bl == 'md-boot': _flash_mdloader(file) elif bl == '_uf2_compatible_': - _flash_uf2(file) + if _flash_uf2(file): + return (True, "Flashing only supports uf2 format files.") else: return (True, "Known bootloader found but flashing not currently supported!") diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index d70e7ee1b3..b4fa481b1b 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -223,12 +223,6 @@ def _validate(keyboard, info_data): def info_json(keyboard, force_layout=None): """Generate the info.json data for a specific keyboard. """ - cur_dir = Path('keyboards') - root_rules_mk = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk') - - if 'DEFAULT_FOLDER' in root_rules_mk: - keyboard = root_rules_mk['DEFAULT_FOLDER'] - info_data = { 'keyboard_name': str(keyboard), 'keyboard_folder': str(keyboard), @@ -260,6 +254,7 @@ def info_json(keyboard, force_layout=None): # Ensure that we have various calculated values info_data = _matrix_size(info_data) info_data = _joystick_axis_count(info_data) + info_data = _matrix_masked(info_data) # Merge in data from <keyboard.c> info_data = _extract_led_config(info_data, str(keyboard)) @@ -836,6 +831,25 @@ def _joystick_axis_count(info_data): return info_data +def _matrix_masked(info_data): + """"Add info_data['matrix_pins.masked'] if required""" + mask_required = False + + if 'matrix_grid' in info_data.get('dip_switch', {}): + mask_required = True + if 'matrix_grid' in info_data.get('split', {}).get('handedness', {}): + mask_required = True + + if mask_required: + if 'masked' not in info_data.get('matrix_pins', {}): + if 'matrix_pins' not in info_data: + info_data['matrix_pins'] = {} + + info_data['matrix_pins']['masked'] = True + + return info_data + + def _check_matrix(info_data): """Check the matrix to ensure that row/column count is consistent. """ @@ -1005,11 +1019,6 @@ def find_info_json(keyboard): keyboard_parent = keyboard_path.parent info_jsons = [keyboard_path / 'info.json', keyboard_path / 'keyboard.json'] - # Add DEFAULT_FOLDER before parents, if present - rules = rules_mk(keyboard) - if 'DEFAULT_FOLDER' in rules: - info_jsons.append(Path(rules['DEFAULT_FOLDER']) / 'info.json') - # Add in parent folders for least specific for _ in range(5): if keyboard_parent == base_path: @@ -1059,3 +1068,20 @@ def keymap_json(keyboard, keymap, force_layout=None): _extract_config_h(kb_info_json, parse_config_h_file(keymap_config)) return kb_info_json + + +def get_modules(keyboard, keymap_filename): + """Get the modules for a keyboard/keymap. + """ + modules = [] + + kb_info_json = info_json(keyboard) + modules.extend(kb_info_json.get('modules', [])) + + if keymap_filename: + keymap_json = parse_configurator_json(keymap_filename) + + if keymap_json: + modules.extend(keymap_json.get('modules', [])) + + return list(dict.fromkeys(modules)) # remove dupes diff --git a/lib/python/qmk/json_encoders.py b/lib/python/qmk/json_encoders.py index 0e4ad1d220..e8bcf48996 100755 --- a/lib/python/qmk/json_encoders.py +++ b/lib/python/qmk/json_encoders.py @@ -235,3 +235,33 @@ class UserspaceJSONEncoder(QMKJSONEncoder): return '01build_targets' return key + + +class CommunityModuleJSONEncoder(QMKJSONEncoder): + """Custom encoder to make qmk_module.json's a little nicer to work with. + """ + def sort_dict(self, item): + """Sorts the hashes in a nice way. + """ + key = item[0] + + if self.indentation_level == 1: + if key == 'module_name': + return '00module_name' + if key == 'maintainer': + return '01maintainer' + if key == 'license': + return '02license' + if key == 'url': + return '03url' + if key == 'features': + return '04features' + if key == 'keycodes': + return '05keycodes' + elif self.indentation_level == 3: # keycodes + if key == 'key': + return '00key' + if key == 'aliases': + return '01aliases' + + return key diff --git a/lib/python/qmk/json_schema.py b/lib/python/qmk/json_schema.py index b11a0ed7ea..e871598565 100644 --- a/lib/python/qmk/json_schema.py +++ b/lib/python/qmk/json_schema.py @@ -76,8 +76,13 @@ def compile_schema_store(): if not isinstance(schema_data, dict): cli.log.debug('Skipping schema file %s', schema_file) continue + + # `$id`-based references schema_store[schema_data['$id']] = schema_data + # Path-based references + schema_store[Path(schema_file).name] = schema_data + return schema_store diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index fcf5b5b158..254dc62309 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -99,8 +99,6 @@ def find_keyboard_from_dir(): keymap_index = len(current_path.parts) - current_path.parts.index('keymaps') - 1 current_path = current_path.parents[keymap_index] - current_path = resolve_keyboard(current_path) - if qmk.path.is_keyboard(current_path): return str(current_path) @@ -121,7 +119,7 @@ def find_readme(keyboard): def keyboard_folder(keyboard): """Returns the actual keyboard folder. - This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard. + This checks aliases to resolve the actual path for a keyboard. """ aliases = keyboard_alias_definitions() @@ -131,8 +129,6 @@ def keyboard_folder(keyboard): if keyboard == last_keyboard: break - keyboard = resolve_keyboard(keyboard) - if not qmk.path.is_keyboard(keyboard): raise ValueError(f'Invalid keyboard: {keyboard}') @@ -158,7 +154,7 @@ def keyboard_aliases(keyboard): def keyboard_folder_or_all(keyboard): """Returns the actual keyboard folder. - This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard. + This checks aliases to resolve the actual path for a keyboard. If the supplied argument is "all", it returns an AllKeyboards object. """ if keyboard == 'all': @@ -179,32 +175,18 @@ def keyboard_completer(prefix, action, parser, parsed_args): return list_keyboards() -def list_keyboards(resolve_defaults=True): - """Returns a list of all keyboards - optionally processing any DEFAULT_FOLDER. +def list_keyboards(): + """Returns a list of all keyboards """ # We avoid pathlib here because this is performance critical code. - paths = [] - for marker in ['rules.mk', 'keyboard.json']: - kb_wildcard = os.path.join(base_path, "**", marker) - paths += [path for path in glob(kb_wildcard, recursive=True) if os.path.sep + 'keymaps' + os.path.sep not in path] + kb_wildcard = os.path.join(base_path, "**", 'keyboard.json') + paths = [path for path in glob(kb_wildcard, recursive=True) if os.path.sep + 'keymaps' + os.path.sep not in path] found = map(_find_name, paths) - if resolve_defaults: - found = map(resolve_keyboard, found) return sorted(set(found)) -@lru_cache(maxsize=None) -def resolve_keyboard(keyboard): - cur_dir = Path('keyboards') - rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk') - while 'DEFAULT_FOLDER' in rules and keyboard != rules['DEFAULT_FOLDER']: - keyboard = rules['DEFAULT_FOLDER'] - rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk') - return keyboard - - def config_h(keyboard): """Parses all the config.h files for a keyboard. @@ -216,7 +198,7 @@ def config_h(keyboard): """ config = {} cur_dir = Path('keyboards') - keyboard = Path(resolve_keyboard(keyboard)) + keyboard = Path(keyboard) for dir in keyboard.parts: cur_dir = cur_dir / dir @@ -235,7 +217,7 @@ def rules_mk(keyboard): a dictionary representing the content of the entire rules.mk tree for a keyboard """ cur_dir = Path('keyboards') - keyboard = Path(resolve_keyboard(keyboard)) + keyboard = Path(keyboard) rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk') for i, dir in enumerate(keyboard.parts): diff --git a/lib/python/qmk/keycodes.py b/lib/python/qmk/keycodes.py index 966930547c..9e4664e5f1 100644 --- a/lib/python/qmk/keycodes.py +++ b/lib/python/qmk/keycodes.py @@ -89,6 +89,7 @@ def load_spec(version, lang=None): spec = _process_files(_locate_files(path, prefix, versions)) # Sort? + spec['version'] = version spec['keycodes'] = dict(sorted(spec.get('keycodes', {}).items())) spec['ranges'] = dict(sorted(spec.get('ranges', {}).items())) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 9dd043c4a8..b7138aa4a1 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -334,33 +334,6 @@ def write_json(keyboard, keymap, layout, layers, macros=None): return write_file(keymap_file, keymap_content) -def write(keymap_json): - """Generate the `keymap.c` and write it to disk. - - Returns the filename written to. - - `keymap_json` should be a dict with the following keys: - keyboard - The name of the keyboard - - keymap - The name of the keymap - - layout - The LAYOUT macro this keymap uses. - - layers - An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. - - macros - A list of macros for this keymap. - """ - keymap_content = generate_c(keymap_json) - keymap_file = qmk.path.keymaps(keymap_json['keyboard'])[0] / keymap_json['keymap'] / 'keymap.c' - - return write_file(keymap_file, keymap_content) - - def locate_keymap(keyboard, keymap, force_layout=None): """Returns the path to a keymap for a specific keyboard. """ @@ -426,7 +399,7 @@ def is_keymap_target(keyboard, keymap): return False -def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=False): +def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=False, include_userspace=True): """List the available keymaps for a keyboard. Args: @@ -445,14 +418,19 @@ def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=Fa fullpath When set to True the full path of the keymap relative to the `qmk_firmware` root will be provided. + include_userspace + When set to True, also search userspace for available keymaps + Returns: a sorted list of valid keymap names. """ names = set() + has_userspace = HAS_QMK_USERSPACE and include_userspace + # walk up the directory tree until keyboards_dir # and collect all directories' name with keymap.c file in it - for search_dir in [QMK_FIRMWARE, QMK_USERSPACE] if HAS_QMK_USERSPACE else [QMK_FIRMWARE]: + for search_dir in [QMK_FIRMWARE, QMK_USERSPACE] if has_userspace else [QMK_FIRMWARE]: keyboards_dir = search_dir / Path('keyboards') kb_path = keyboards_dir / keyboard @@ -470,7 +448,7 @@ def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=Fa info = info_json(keyboard) community_parents = list(Path('layouts').glob('*/')) - if HAS_QMK_USERSPACE and (Path(QMK_USERSPACE) / "layouts").exists(): + if has_userspace and (Path(QMK_USERSPACE) / "layouts").exists(): community_parents.append(Path(QMK_USERSPACE) / "layouts") for community_parent in community_parents: diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py index 61daad585f..1739689adf 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py @@ -3,7 +3,7 @@ import logging import os import argparse -from pathlib import Path +from pathlib import Path, PureWindowsPath, PurePosixPath from qmk.constants import MAX_KEYBOARD_SUBFOLDERS, QMK_FIRMWARE, QMK_USERSPACE, HAS_QMK_USERSPACE from qmk.errors import NoSuchKeyboardError @@ -21,11 +21,9 @@ def is_keyboard(keyboard_name): if Path(keyboard_name).is_absolute(): return False - keyboard_path = QMK_FIRMWARE / 'keyboards' / keyboard_name - rules_mk = keyboard_path / 'rules.mk' - keyboard_json = keyboard_path / 'keyboard.json' + keyboard_json = QMK_FIRMWARE / 'keyboards' / keyboard_name / 'keyboard.json' - return rules_mk.exists() or keyboard_json.exists() + return keyboard_json.exists() def under_qmk_firmware(path=Path(os.environ['ORIG_CWD'])): @@ -146,6 +144,28 @@ def normpath(path): return Path(os.environ['ORIG_CWD']) / path +def unix_style_path(path): + """Converts a Windows-style path with drive letter to a Unix path. + + Path().as_posix() normally returns the path with drive letter and forward slashes, so is inappropriate for `Makefile` paths. + + Passes through unadulterated if the path is not a Windows-style path. + + Args: + + path + The path to convert. + + Returns: + The input path converted to Unix format. + """ + if isinstance(path, PureWindowsPath): + p = list(path.parts) + p[0] = f'/{p[0][0].lower()}' # convert from `X:/` to `/x` + path = PurePosixPath(*p) + return path + + class FileType(argparse.FileType): def __init__(self, *args, **kwargs): # Use UTF8 by default for stdin diff --git a/lib/python/qmk/userspace.py b/lib/python/qmk/userspace.py index 1c2a97f9c1..881490f796 100644 --- a/lib/python/qmk/userspace.py +++ b/lib/python/qmk/userspace.py @@ -12,29 +12,30 @@ from qmk.json_encoders import UserspaceJSONEncoder def qmk_userspace_paths(): - test_dirs = set() + test_dirs = [] # If we're already in a directory with a qmk.json and a keyboards or layouts directory, interpret it as userspace if environ.get('ORIG_CWD') is not None: current_dir = Path(environ['ORIG_CWD']) while len(current_dir.parts) > 1: if (current_dir / 'qmk.json').is_file(): - test_dirs.add(current_dir) + test_dirs.append(current_dir) current_dir = current_dir.parent # If we have a QMK_USERSPACE environment variable, use that if environ.get('QMK_USERSPACE') is not None: current_dir = Path(environ['QMK_USERSPACE']).expanduser() if current_dir.is_dir(): - test_dirs.add(current_dir) + test_dirs.append(current_dir) # If someone has configured a directory, use that if cli.config.user.overlay_dir is not None: current_dir = Path(cli.config.user.overlay_dir).expanduser().resolve() if current_dir.is_dir(): - test_dirs.add(current_dir) + test_dirs.append(current_dir) - return list(test_dirs) + # remove duplicates while maintaining the current order + return list(dict.fromkeys(test_dirs)) def qmk_userspace_validate(path): |