keyboard stuff
1"""Used by the make system to generate version.h for use in code.
2"""
3from time import strftime
4
5from milc import cli
6
7from qmk.path import normpath
8from qmk.commands import dump_lines
9from qmk.git import git_get_qmk_hash, git_get_version, git_is_dirty
10from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
11from qmk.util import triplet_to_bcd
12
13TIME_FMT = '%Y-%m-%d-%H:%M:%S'
14
15
16@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
17@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
18@cli.argument('--skip-git', arg_only=True, action='store_true', help='Skip Git operations')
19@cli.argument('--skip-all', arg_only=True, action='store_true', help='Use placeholder values for all defines (implies --skip-git)')
20@cli.subcommand('Used by the make system to generate version.h for use in code', hidden=True)
21def generate_version_h(cli):
22 """Generates the version.h file.
23 """
24 if cli.args.skip_all:
25 cli.args.skip_git = True
26
27 if cli.args.skip_all:
28 current_time = "1970-01-01-00:00:00"
29 else:
30 current_time = strftime(TIME_FMT)
31
32 if cli.args.skip_git:
33 git_dirty = False
34 git_version = "NA"
35 git_qmk_hash = "NA"
36 git_bcd_version = "0x00000000"
37 chibios_version = "NA"
38 chibios_contrib_version = "NA"
39 else:
40 git_dirty = git_is_dirty()
41 git_version = git_get_version() or current_time
42 git_qmk_hash = git_get_qmk_hash() or "Unknown"
43 git_bcd_version = triplet_to_bcd(git_version)
44 chibios_version = git_get_version("chibios", "os") or current_time
45 chibios_contrib_version = git_get_version("chibios-contrib", "os") or current_time
46
47 # Build the version.h file.
48 version_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']
49
50 version_h_lines.append(
51 f"""
52#define QMK_VERSION "{git_version}"
53#define QMK_BUILDDATE "{current_time}"
54#define QMK_VERSION_BCD {git_bcd_version}
55#define QMK_GIT_HASH "{git_qmk_hash}{'*' if git_dirty else ''}"
56#define CHIBIOS_VERSION "{chibios_version}"
57#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"
58"""
59 )
60
61 # Show the results
62 dump_lines(cli.args.output, version_h_lines, cli.args.quiet)