Skip to content

Commit

Permalink
CLI: Add subcommand to generate version.h (qmk#13151)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored and nhongooi committed Dec 5, 2021
1 parent 73149ad commit 8afdaf0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 45 deletions.
27 changes: 6 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/python/qmk/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'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',
Expand Down
28 changes: 28 additions & 0 deletions lib/python/qmk/cli/generate/version_h.py
Original file line number Diff line number Diff line change
@@ -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)
62 changes: 38 additions & 24 deletions lib/python/qmk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down Expand Up @@ -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'
Expand All @@ -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}',
Expand Down
6 changes: 6 additions & 0 deletions lib/python/qmk/tests/test_cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8afdaf0

Please sign in to comment.