Skip to content

Instantly share code, notes, and snippets.

@mofosyne
Created November 17, 2019 17:38
Show Gist options
  • Save mofosyne/a0e96035e230d86dd7f94070890d027c to your computer and use it in GitHub Desktop.
Save mofosyne/a0e96035e230d86dd7f94070890d027c to your computer and use it in GitHub Desktop.
This is a sketch of a python3 script that checks git tag written in a modified semantic version format and returns a C file with the parsed version code and the git hash
#!/usr/bin/env python3
# This script checks git tag written in a modified semantic version format
# and returns a C file with the parsed version code and the git hash
# The git tag MUST conform to this format v<major>.<minor>.<patch>-<rcX>
import subprocess
import sys, os
import re
tagname = subprocess.check_output(["git", "describe", "--tags"]).strip().decode("utf-8") ;
tagnamelong = subprocess.check_output(["git", "describe", "--tags", "--long"]).strip().decode("utf-8") ;
shorthash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).strip().decode("utf-8") ;
################################################################################
# Constants
MAGIC_NUMBER = "0x43505300";
HEADER_VERSION = 0;
HEADER_LENGTH = 1024;
FW_LENGTH = 896*1024;
VENDOR_NAME = "VENDOR_NAME";
FW_NAME = "APP";
################################################################################
# Based on https://github.com/python-semver/python-semver/blob/master/semver.py
# format: v<major>.<minor>.<patch>-<rc.X> e.g. v0.0.1-rc13 or v0.0.1-rc.13
_REGEX = re.compile(
r"""
^
v
(?P<major>(?:0|[1-9][0-9]*))
\.
(?P<minor>(?:0|[1-9][0-9]*))
\.
(?P<patch>(?:0|[1-9][0-9]*))
(\-(?P<prerelease>
(?:0|[1-9A-Za-z-][0-9A-Za-z-]*)
(\.(?:0|[1-9A-Za-z-][0-9A-Za-z-]*))*
))?
(\+(?P<build>
[0-9A-Za-z-]+
(\.[0-9A-Za-z-]+)*
))?
$
""", re.VERBOSE)
match = _REGEX.match(tagname);
if match is None:
print(f"git tag is not valid semantic version: {tagname}");
sys.exit(os.EX_USAGE); # Failed
# Version
version_parts = match.groupdict();
VERSION_MAJOR = version_parts["major"]; # Required
VERSION_MINOR = version_parts["minor"]; # Required
VERSION_PATCH = version_parts["patch"]; # Required
VERSION_PRERELEASE = 0; # Optional
if version_parts["prerelease"] != None:
release_candidate = re.search('rc\.?([0-9]+)', version_parts["prerelease"], re.IGNORECASE)
if release_candidate:
VERSION_PRERELEASE = release_candidate.group(1)
VERSION_BUILD = shorthash;
################################################################################
if VERSION_PRERELEASE == 0:
SEM_VER_PRINT = f"{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_PATCH}+{VERSION_BUILD}";
else:
SEM_VER_PRINT = f"{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_PATCH}-rc{VERSION_PRERELEASE}+{VERSION_BUILD}";
c_source = f"""\
/*
Autogenerated Firmware Header
`git describe --tags --long` --> {tagnamelong}
`git rev-parse --short HEAD` --> {shorthash}
Version (https://semver.org/ Semantic Versioning 2.0.0):
{SEM_VER_PRINT}
*/
fw_header_t __attribute__((section(".firmware_header")))
running_firmware_header = {{
.magic = {MAGIC_NUMBER}
.hdrver = {HEADER_VERSION},
.hdrlen = {HEADER_LENGTH},
.applen = {FW_LENGTH},
.name = "{FW_NAME}",
.version_major = {VERSION_MAJOR},
.version_minor = {VERSION_MINOR},
.version_patch = {VERSION_PATCH},
.version_prerelease = {VERSION_PRERELEASE},
.version_build = "{VERSION_BUILD}"
}};
"""
print(c_source);
sys.exit(os.EX_OK); # Completed
@mofosyne
Copy link
Author

Example output:

/*
  Autogenerated Firmware Header

  `git describe --tags --long` --> v0.0.1-0-g5bf2243
  `git rev-parse --short HEAD` --> 5bf2243

  Version (https://semver.org/ Semantic Versioning 2.0.0):
    0.0.1+5bf2243
*/

fw_header_t __attribute__((section(".firmware_header")))
running_firmware_header = {
  .magic              = 0x43505300
  .hdrver             = 0,
  .hdrlen             = 1024,
  .applen             = 917504,
  .name               = "APP",
  .version_major      = 0,
  .version_minor      = 0,
  .version_patch      = 1,
  .version_prerelease = 0,
  .version_build      = "5bf2243"
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment