Repository navigation
Expand file tree
/
Copy pathbuildinfo.py
More file actions
37 lines (33 loc) · 1.79 KB
/
Copy pathbuildinfo.py
File metadata and controls
37 lines (33 loc) · 1.79 KB
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
# PlatformIO pre-build script: writes src/buildinfo.cpp with the firmware version
# (git describe) and the build time, so they can be shown in /api/state and the web
# app. Only this small file is recompiled on every build; flags stay unchanged so
# the rest of the project is not rebuilt.
Import("env")
import os, subprocess, datetime
root = env.subst("$PROJECT_DIR")
def git(*args):
try:
return subprocess.check_output(["git", *args], cwd=root, stderr=subprocess.DEVNULL).decode().strip()
except Exception:
return ""
# A CI build of a tag uses that tag: with several tags on one commit (v1.1.3 and
# v1.1.4) plain git describe may pick the other one.
tag = os.environ.get("GITHUB_REF_NAME", "") if os.environ.get("GITHUB_REF_TYPE") == "tag" else ""
version = (tag and git("describe", "--tags", "--always", "--dirty", "--match", tag)) \
or git("describe", "--tags", "--always", "--dirty") or "unknown"
branch = git("rev-parse", "--abbrev-ref", "HEAD") or "unknown"
built = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
pio_env = env.subst("$PIOENV")
stove_model = (env.GetProjectOption("custom_stove_model", "") or "unknown").replace('"', "'")
content = f'''// Generated by scripts/buildinfo.py on every build - do not edit, not in git.
// extern: a namespace-scope const has internal linkage in C++ otherwise.
extern const char* const BUILD_VERSION = "{version}";
extern const char* const BUILD_BRANCH = "{branch}";
extern const char* const BUILD_TIME = "{built}";
extern const char* const BUILD_ENV = "{pio_env}";
extern const char* const BUILD_STOVE_MODEL = "{stove_model}";
'''
path = os.path.join(root, "src", "buildinfo.cpp")
with open(path, "w") as f:
f.write(content)
print(f"Build info: {version} ({branch}) {built} [{pio_env}] for {stove_model}")