-
Notifications
You must be signed in to change notification settings - Fork 122
/
gitVersion.py
56 lines (43 loc) · 1.58 KB
/
gitVersion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""PlatformIO script to generate a gitversion.h file and adds its path to
CPPPATH.
Note: The PlatformIO documentation suggests to simply add the Git version as
dynamic build flag (=define). This however leads to constant rebuilds of the
complete project.
"""
import subprocess
from pathlib import Path
Import("env") # pylint: disable=undefined-variable
OUTPUT_PATH = (
Path(env.subst("$BUILD_DIR")) / "generated" / "gitrevision.h"
) # pylint: disable=undefined-variable
TEMPLATE = """
#ifndef __GIT_REVISION_H__
#define __GIT_REVISION_H__
constexpr const char gitRevision[] = "Git-revision: {git_revision}";
constexpr const char gitRevShort[] = "\\"{git_revision}\\"";
#endif
"""
def git_revision():
"""Returns Git revision or unknown."""
try:
return subprocess.check_output(
["git", "describe", "--always", "--dirty"],
text=True,
stderr=subprocess.PIPE,
).strip()
except (subprocess.CalledProcessError, OSError) as err:
print(
f" Warning: Setting Git revision to 'unknown': {err.stderr.split(':', 1)[1].strip()}"
)
return "unknown"
def generate():
"""Generates header file."""
print("GENERATING GIT REVISION HEADER FILE")
gitrev = git_revision()
print(f' "{gitrev}" -> {OUTPUT_PATH}')
OUTPUT_PATH.parent.mkdir(exist_ok=True, parents=True)
with OUTPUT_PATH.open("w") as output_file:
output_file.write(TEMPLATE.format(git_revision=gitrev))
generate()
env.Append(CPPPATH=OUTPUT_PATH.parent) # pylint: disable=undefined-variable