diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
| -rw-r--r-- | lib/python/qmk/cli/generate/version_h.py | 28 | ||||
| -rw-r--r-- | lib/python/qmk/commands.py | 62 | ||||
| -rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 6 |
4 files changed, 73 insertions, 24 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 7f5e0a1fa..2c3c9c421 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py | |||
| @@ -49,6 +49,7 @@ subcommands = [ | |||
| 49 | 'qmk.cli.generate.layouts', | 49 | 'qmk.cli.generate.layouts', |
| 50 | 'qmk.cli.generate.rgb_breathe_table', | 50 | 'qmk.cli.generate.rgb_breathe_table', |
| 51 | 'qmk.cli.generate.rules_mk', | 51 | 'qmk.cli.generate.rules_mk', |
| 52 | 'qmk.cli.generate.version_h', | ||
| 52 | 'qmk.cli.hello', | 53 | 'qmk.cli.hello', |
| 53 | 'qmk.cli.info', | 54 | 'qmk.cli.info', |
| 54 | 'qmk.cli.json2c', | 55 | 'qmk.cli.json2c', |
diff --git a/lib/python/qmk/cli/generate/version_h.py b/lib/python/qmk/cli/generate/version_h.py new file mode 100644 index 000000000..b8e52588c --- /dev/null +++ b/lib/python/qmk/cli/generate/version_h.py | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | """Used by the make system to generate version.h for use in code. | ||
| 2 | """ | ||
| 3 | from milc import cli | ||
| 4 | |||
| 5 | from qmk.commands import create_version_h | ||
| 6 | from qmk.path import normpath | ||
| 7 | |||
| 8 | |||
| 9 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 10 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 11 | @cli.argument('--skip-git', arg_only=True, action='store_true', help='Skip Git operations') | ||
| 12 | @cli.argument('--skip-all', arg_only=True, action='store_true', help='Use placeholder values for all defines (implies --skip-git)') | ||
| 13 | @cli.subcommand('Used by the make system to generate version.h for use in code', hidden=True) | ||
| 14 | def generate_version_h(cli): | ||
| 15 | """Generates the version.h file. | ||
| 16 | """ | ||
| 17 | if cli.args.skip_all: | ||
| 18 | cli.args.skip_git = True | ||
| 19 | |||
| 20 | version_h = create_version_h(cli.args.skip_git, cli.args.skip_all) | ||
| 21 | |||
| 22 | if cli.args.output: | ||
| 23 | cli.args.output.write_text(version_h) | ||
| 24 | |||
| 25 | if not cli.args.quiet: | ||
| 26 | cli.log.info('Wrote version.h to %s.', cli.args.output) | ||
| 27 | else: | ||
| 28 | print(version_h) | ||
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 3a35c1103..104c87545 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py | |||
| @@ -86,11 +86,17 @@ def create_make_command(keyboard, keymap, target=None, parallel=1, **env_vars): | |||
| 86 | return create_make_target(':'.join(make_args), parallel, **env_vars) | 86 | return create_make_target(':'.join(make_args), parallel, **env_vars) |
| 87 | 87 | ||
| 88 | 88 | ||
| 89 | def get_git_version(repo_dir='.', check_dir='.'): | 89 | def get_git_version(current_time, repo_dir='.', check_dir='.'): |
| 90 | """Returns the current git version for a repo, or the current time. | 90 | """Returns the current git version for a repo, or the current time. |
| 91 | """ | 91 | """ |
| 92 | git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] | 92 | git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] |
| 93 | 93 | ||
| 94 | if repo_dir != '.': | ||
| 95 | repo_dir = Path('lib') / repo_dir | ||
| 96 | |||
| 97 | if check_dir != '.': | ||
| 98 | check_dir = repo_dir / check_dir | ||
| 99 | |||
| 94 | if Path(check_dir).exists(): | 100 | if Path(check_dir).exists(): |
| 95 | git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir) | 101 | git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir) |
| 96 | 102 | ||
| @@ -100,23 +106,40 @@ def get_git_version(repo_dir='.', check_dir='.'): | |||
| 100 | else: | 106 | else: |
| 101 | cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}') | 107 | cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}') |
| 102 | print(git_describe.stderr) | 108 | print(git_describe.stderr) |
| 103 | return strftime(time_fmt) | 109 | return current_time |
| 104 | 110 | ||
| 105 | return strftime(time_fmt) | 111 | return current_time |
| 106 | 112 | ||
| 107 | 113 | ||
| 108 | def write_version_h(git_version, build_date, chibios_version, chibios_contrib_version): | 114 | def create_version_h(skip_git=False, skip_all=False): |
| 109 | """Generate and write quantum/version.h | 115 | """Generate version.h contents |
| 110 | """ | 116 | """ |
| 111 | version_h = [ | 117 | if skip_all: |
| 112 | f'#define QMK_VERSION "{git_version}"', | 118 | current_time = "1970-01-01-00:00:00" |
| 113 | f'#define QMK_BUILDDATE "{build_date}"', | 119 | else: |
| 114 | f'#define CHIBIOS_VERSION "{chibios_version}"', | 120 | current_time = strftime(time_fmt) |
| 115 | f'#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"', | 121 | |
| 116 | ] | 122 | if skip_git: |
| 123 | git_version = "NA" | ||
| 124 | chibios_version = "NA" | ||
| 125 | chibios_contrib_version = "NA" | ||
| 126 | else: | ||
| 127 | git_version = get_git_version(current_time) | ||
| 128 | chibios_version = get_git_version(current_time, "chibios", "os") | ||
| 129 | chibios_contrib_version = get_git_version(current_time, "chibios-contrib", "os") | ||
| 130 | |||
| 131 | version_h_lines = f"""/* This file was automatically generated. Do not edit or copy. | ||
| 132 | */ | ||
| 133 | |||
| 134 | #pragma once | ||
| 135 | |||
| 136 | #define QMK_VERSION "{git_version}" | ||
| 137 | #define QMK_BUILDDATE "{current_time}" | ||
| 138 | #define CHIBIOS_VERSION "{chibios_version}" | ||
| 139 | #define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}" | ||
| 140 | """ | ||
| 117 | 141 | ||
| 118 | version_h_file = Path('quantum/version.h') | 142 | return version_h_lines |
| 119 | version_h_file.write_text('\n'.join(version_h)) | ||
| 120 | 143 | ||
| 121 | 144 | ||
| 122 | def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_vars): | 145 | def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_vars): |
| @@ -149,13 +172,8 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_va | |||
| 149 | keymap_dir.mkdir(exist_ok=True, parents=True) | 172 | keymap_dir.mkdir(exist_ok=True, parents=True) |
| 150 | keymap_c.write_text(c_text) | 173 | keymap_c.write_text(c_text) |
| 151 | 174 | ||
| 152 | # Write the version.h file | 175 | version_h = Path('quantum/version.h') |
| 153 | git_version = get_git_version() | 176 | version_h.write_text(create_version_h()) |
| 154 | build_date = strftime('%Y-%m-%d-%H:%M:%S') | ||
| 155 | chibios_version = get_git_version("lib/chibios", "lib/chibios/os") | ||
| 156 | chibios_contrib_version = get_git_version("lib/chibios-contrib", "lib/chibios-contrib/os") | ||
| 157 | |||
| 158 | write_version_h(git_version, build_date, chibios_version, chibios_contrib_version) | ||
| 159 | 177 | ||
| 160 | # Return a command that can be run to make the keymap and flash if given | 178 | # Return a command that can be run to make the keymap and flash if given |
| 161 | verbose = 'true' if cli.config.general.verbose else 'false' | 179 | verbose = 'true' if cli.config.general.verbose else 'false' |
| @@ -181,10 +199,6 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_va | |||
| 181 | make_command.append(f'{key}={value}') | 199 | make_command.append(f'{key}={value}') |
| 182 | 200 | ||
| 183 | make_command.extend([ | 201 | make_command.extend([ |
| 184 | f'GIT_VERSION={git_version}', | ||
| 185 | f'BUILD_DATE={build_date}', | ||
| 186 | f'CHIBIOS_VERSION={chibios_version}', | ||
| 187 | f'CHIBIOS_CONTRIB_VERSION={chibios_contrib_version}', | ||
| 188 | f'KEYBOARD={user_keymap["keyboard"]}', | 202 | f'KEYBOARD={user_keymap["keyboard"]}', |
| 189 | f'KEYMAP={user_keymap["keymap"]}', | 203 | f'KEYMAP={user_keymap["keymap"]}', |
| 190 | f'KEYBOARD_FILESAFE={keyboard_filesafe}', | 204 | f'KEYBOARD_FILESAFE={keyboard_filesafe}', |
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index afdbc8142..b341e1c91 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py | |||
| @@ -258,6 +258,12 @@ def test_generate_rules_mk(): | |||
| 258 | assert 'MCU ?= atmega32u4' in result.stdout | 258 | assert 'MCU ?= atmega32u4' in result.stdout |
| 259 | 259 | ||
| 260 | 260 | ||
| 261 | def test_generate_version_h(): | ||
| 262 | result = check_subcommand('generate-version-h') | ||
| 263 | check_returncode(result) | ||
| 264 | assert '#define QMK_VERSION' in result.stdout | ||
| 265 | |||
| 266 | |||
| 261 | def test_generate_layouts(): | 267 | def test_generate_layouts(): |
| 262 | result = check_subcommand('generate-layouts', '-kb', 'handwired/pytest/basic') | 268 | result = check_subcommand('generate-layouts', '-kb', 'handwired/pytest/basic') |
| 263 | check_returncode(result) | 269 | check_returncode(result) |
