~ruther/qmk_firmware

da40242dbc2b03437774a58bb7b8d35f4b59a2cd — Zach White 4 years ago f0f6174
Generate version.h when compiling json files (#11581)

* generate version.h when compiling json files

* make flake8 happy

* fix formatting and verbose

* quiet up the compile output
1 files changed, 55 insertions(+), 3 deletions(-)

M lib/python/qmk/commands.py
M lib/python/qmk/commands.py => lib/python/qmk/commands.py +55 -3
@@ 7,12 7,15 @@ import subprocess
import shlex
import shutil
from pathlib import Path
from time import strftime

from milc import cli

import qmk.keymap
from qmk.constants import KEYBOARD_OUTPUT_PREFIX

time_fmt = '%Y-%m-%d-%H:%M:%S'


def _find_make():
    """Returns the correct make command for this environment.


@@ 62,6 65,39 @@ def create_make_command(keyboard, keymap, target=None, parallel=1, **env_vars):
    return [make_cmd, '-j', str(parallel), *env, ':'.join(make_args)]


def get_git_version(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 Path(check_dir).exists():
        git_describe = cli.run(git_describe_cmd, cwd=repo_dir)

        if git_describe.returncode == 0:
            return git_describe.stdout.strip()

        else:
            cli.args.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
            print(git_describe.stderr)
            return strftime(time_fmt)

    return strftime(time_fmt)


def write_version_h(git_version, build_date, chibios_version, chibios_contrib_version):
    """Generate and write quantum/version.h
    """
    version_h = [
        f'#define QMK_VERSION "{git_version}"',
        f'#define QMK_BUILD_DATE "{build_date}"',
        f'#define CHIBIOS_VERSION "{chibios_version}"',
        f'#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"',
    ]

    version_h_file = Path('quantum/version.h')
    version_h_file.write_text('\n'.join(version_h))


def compile_configurator_json(user_keymap, parallel=1, **env_vars):
    """Convert a configurator export JSON file into a C file and then compile it.



@@ 92,23 128,39 @@ def compile_configurator_json(user_keymap, parallel=1, **env_vars):
    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)

    # Return a command that can be run to make the keymap and flash if given
    verbose = 'true' if cli.config.general.verbose else 'false'
    color = 'true' if cli.config.general.color else 'false'
    make_command = [
        _find_make(),
    make_command = [_find_make()]

    if not cli.config.general.verbose:
        make_command.append('-s')

    make_command.extend([
        '-j',
        str(parallel),
        '-r',
        '-R',
        '-f',
        'build_keyboard.mk',
    ]
    ])

    for key, value in env_vars.items():
        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}',