# Injects the firmware version from `git describe` (semver tags like v0.1.0). import subprocess Import("env") # noqa: F821 (provided by PlatformIO) try: version = subprocess.check_output( ["git", "describe", "--tags", "--always", "--dirty"], text=True ).strip() except Exception: version = "unknown" if env["PIOENV"].endswith("-debug"): # noqa: F821 version += "+debug" # a Debug Build says so wherever the version shows env.Append(CPPDEFINES=[("RORO_VERSION", '\\"%s\\"' % version)]) # noqa: F821 # Keep every build's ELF, named by version and the first 16 hex digits of its SHA-256 (the core dump # names the crashed firmware by the same digest), so a crash can be decoded after later builds. def archive_elf(source, target, env): import hashlib import os import shutil elf = str(target[0]) digest = hashlib.sha256(open(elf, "rb").read()).hexdigest()[:16] folder = os.path.join(env.subst("$PROJECT_DIR"), ".pio", "elves") os.makedirs(folder, exist_ok=True) shutil.copy(elf, os.path.join(folder, "%s.%s.elf" % (version, digest))) env.AddPostAction("$BUILD_DIR/${PROGNAME}.elf", archive_elf) # noqa: F821