summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-06-27 02:29:02 +1000
committerGitHub <noreply@github.com>2021-06-27 02:29:02 +1000
commitbbe43a91ebf193bbc8c09ba59209b0524367e68c (patch)
treeb4b1cac772b2601154153a39d2173334cdf363d9
parent546f5f2c4f38a4abf934e3df84985feca61f90c7 (diff)
CLI: Add subcommand to generate version.h (#13151)
-rw-r--r--Makefile27
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rw-r--r--lib/python/qmk/cli/generate/version_h.py28
-rw-r--r--lib/python/qmk/commands.py62
-rw-r--r--lib/python/qmk/tests/test_cli_commands.py6
5 files changed, 79 insertions, 45 deletions
diff --git a/Makefile b/Makefile
index e007ae3679..866bcaaf3f 100644
--- a/Makefile
+++ b/Makefile
@@ -548,29 +548,14 @@ git-submodule:
git submodule sync --recursive
git submodule update --init --recursive --progress
-ifdef SKIP_VERSION
-SKIP_GIT := yes
-endif
-
# Generate the version.h file
-ifndef SKIP_GIT
- GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
- CHIBIOS_VERSION := $(shell cd lib/chibios && git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
- CHIBIOS_CONTRIB_VERSION := $(shell cd lib/chibios-contrib && git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
-else
- GIT_VERSION := NA
- CHIBIOS_VERSION := NA
- CHIBIOS_CONTRIB_VERSION := NA
+ifdef SKIP_GIT
+VERSION_H_FLAGS := --skip-git
endif
-ifndef SKIP_VERSION
-BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
-else
-BUILD_DATE := 2020-01-01-00:00:00
+ifdef SKIP_VERSION
+VERSION_H_FLAGS := --skip-all
+SKIP_GIT := yes
endif
-
-$(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
-$(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
-$(shell echo '#define CHIBIOS_VERSION "$(CHIBIOS_VERSION)"' >> $(ROOT_DIR)/quantum/version.h)
-$(shell echo '#define CHIBIOS_CONTRIB_VERSION "$(CHIBIOS_CONTRIB_VERSION)"' >> $(ROOT_DIR)/quantum/version.h)
+$(shell $(QMK_BIN) generate-version-h $(VERSION_H_FLAGS) -q -o quantum/version.h)
include $(ROOT_DIR)/testlist.mk
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index 7f5e0a1fa6..2c3c9c4211 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -49,6 +49,7 @@ subcommands = [
'qmk.cli.generate.layouts',
'qmk.cli.generate.rgb_breathe_table',
'qmk.cli.generate.rules_mk',
+ 'qmk.cli.generate.version_h',
'qmk.cli.hello',
'qmk.cli.info',
'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 0000000000..b8e52588c4
--- /dev/null
+++ b/lib/python/qmk/cli/generate/version_h.py
@@ -0,0 +1,28 @@
+"""Used by the make system to generate version.h for use in code.
+"""
+from milc import cli
+
+from qmk.commands import create_version_h
+from qmk.path import normpath
+
+
+@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")
+@cli.argument('--skip-git', arg_only=True, action='store_true', help='Skip Git operations')
+@cli.argument('--skip-all', arg_only=True, action='store_true', help='Use placeholder values for all defines (implies --skip-git)')
+@cli.subcommand('Used by the make system to generate version.h for use in code', hidden=True)
+def generate_version_h(cli):
+ """Generates the version.h file.
+ """
+ if cli.args.skip_all:
+ cli.args.skip_git = True
+
+ version_h = create_version_h(cli.args.skip_git, cli.args.skip_all)
+
+ if cli.args.output:
+ cli.args.output.write_text(version_h)
+
+ if not cli.args.quiet:
+ cli.log.info('Wrote version.h to %s.', cli.args.output)
+ else:
+ print(version_h)
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py
index 3a35c11031..104c875457 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):
return create_make_target(':'.join(make_args), parallel, **env_vars)
-def get_git_version(repo_dir='.', check_dir='.'):
+def get_git_version(current_time, repo_dir='.', check_dir='.'):
"""Returns the current git version for a repo, or the current time.
"""
git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
+ if repo_dir != '.':
+ repo_dir = Path('lib') / repo_dir
+
+ if check_dir != '.':
+ check_dir = repo_dir / check_dir
+
if Path(check_dir).exists():
git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir)
@@ -100,23 +106,40 @@ def get_git_version(repo_dir='.', check_dir='.'):
else:
cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
print(git_describe.stderr)
- return strftime(time_fmt)
+ return current_time
- return strftime(time_fmt)
+ return current_time
-def write_version_h(git_version, build_date, chibios_version, chibios_contrib_version):
- """Generate and write quantum/version.h
+def create_version_h(skip_git=False, skip_all=False):
+ """Generate version.h contents
"""
- version_h = [
- f'#define QMK_VERSION "{git_version}"',
- f'#define QMK_BUILDDATE "{build_date}"',
- f'#define CHIBIOS_VERSION "{chibios_version}"',
- f'#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"',
- ]
+ if skip_all:
+ current_time = "1970-01-01-00:00:00"
+ else:
+ current_time = strftime(time_fmt)
+
+ if skip_git:
+ git_version = "NA"
+ chibios_version = "NA"
+ chibios_contrib_version = "NA"
+ else:
+ git_version = get_git_version(current_time)
+ chibios_version = get_git_version(current_time, "chibios", "os")
+ chibios_contrib_version = get_git_version(current_time, "chibios-contrib", "os")
+
+ version_h_lines = f"""/* This file was automatically generated. Do not edit or copy.
+ */
+
+#pragma once
+
+#define QMK_VERSION "{git_version}"
+#define QMK_BUILDDATE "{current_time}"
+#define CHIBIOS_VERSION "{chibios_version}"
+#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"
+"""
- version_h_file = Path('quantum/version.h')
- version_h_file.write_text('\n'.join(version_h))
+ return version_h_lines
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
keymap_dir.mkdir(exist_ok=True, parents=True)
keymap_c.write_text(c_text)
- # Write the version.h file
- git_version = get_git_version()
- build_date = strftime('%Y-%m-%d-%H:%M:%S')
- chibios_version = get_git_version("lib/chibios", "lib/chibios/os")
- chibios_contrib_version = get_git_version("lib/chibios-contrib", "lib/chibios-contrib/os")
-
- write_version_h(git_version, build_date, chibios_version, chibios_contrib_version)
+ version_h = Path('quantum/version.h')
+ version_h.write_text(create_version_h())
# Return a command that can be run to make the keymap and flash if given
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
make_command.append(f'{key}={value}')
make_command.extend([
- f'GIT_VERSION={git_version}',
- f'BUILD_DATE={build_date}',
- f'CHIBIOS_VERSION={chibios_version}',
- f'CHIBIOS_CONTRIB_VERSION={chibios_contrib_version}',
f'KEYBOARD={user_keymap["keyboard"]}',
f'KEYMAP={user_keymap["keymap"]}',
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 afdbc81429..b341e1c912 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():
assert 'MCU ?= atmega32u4' in result.stdout
+def test_generate_version_h():
+ result = check_subcommand('generate-version-h')
+ check_returncode(result)
+ assert '#define QMK_VERSION' in result.stdout
+
+
def test_generate_layouts():
result = check_subcommand('generate-layouts', '-kb', 'handwired/pytest/basic')
check_returncode(result)