verilator/src/config_rev

66 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
# mypy: disallow-untyped-defs
# pylint: disable=C0103,C0114,C0116
######################################################################
#
# Copyright 2005-2025 by Wilson Snyder. This program is free software; you
# can redistribute it and/or modify it under the terms of either the GNU
# Lesser General Public License Version 3 or the Perl Artistic License
# Version 2.0.
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
#
######################################################################
# DESCRIPTION: Query git to get version number
import argparse
import os
import re
import sys
from datetime import datetime, timezone
def get_rev() -> str:
if 'VERILATOR_SRC_VERSION' in os.environ:
# e.g. Ubuntu package build
return os.environ['VERILATOR_SRC_VERSION']
data = os.popen('git describe --always').read()
if 'SOURCE_DATE_EPOCH' in os.environ:
# e.g. Package builds that don't set VERILATOR_SRC_VERSION
stamp = datetime.fromtimestamp(int(os.getenv("SOURCE_DATE_EPOCH", "0")), tz=timezone.utc)
else:
stamp = datetime.now(timezone.utc)
result = "vUNKNOWN-built" + stamp.strftime("%Y%m%d")
m = re.search(r'^(v[0-9].*)', data)
if m:
# e.g. in a complate verilator checkout with tags; ignore build time
result = m.group(1)
result = re.sub('_', '.', result)
else:
# e.g. in a sparse submodule checkout without tags; append hash
m = re.search(r'^([a-f0-9]+)$', data)
if m:
result += "-" + m.group(1)
data = os.popen('git status').read()
if (re.search('Changed but not updated', data, flags=re.IGNORECASE)
or re.search('Changes to be committed', data, flags=re.IGNORECASE)
or re.search('Changes not staged', data, flags=re.IGNORECASE)):
result += " (mod)"
return result
parser = argparse.ArgumentParser()
parser.add_argument('directory')
Args = parser.parse_args()
os.chdir(Args.directory)
rev = get_rev()
print("config_rev using " + rev, file=sys.stderr)
print("static const char* const DTVERSION_rev = \"" + rev + "\";")