py/makeversionhdr.py: Reinstate MICROPY_GIT_HASH in mpversion.h.

MICROPY_GIT_HASH was removed in 69e34b6b6b
but it is useful for, and used by, third-party code to tell which hash of
MicroPython is used.

Signed-off-by: Damien George <damien@micropython.org>
pull/13661/head
Damien George 2024-02-14 13:08:23 +11:00
rodzic 1ef2944b98
commit 9242e3d16d
1 zmienionych plików z 24 dodań i 3 usunięć

Wyświetl plik

@ -25,7 +25,6 @@ def get_version_info_from_git(repo_path):
# Python 2.6 doesn't have check_output, so check for that
try:
subprocess.check_output
subprocess.check_call
except AttributeError:
return None
@ -44,9 +43,25 @@ def get_version_info_from_git(repo_path):
return git_tag[0]
else:
return git_tag[0] + "-" + git_tag[1].replace("-", ".")
except subprocess.CalledProcessError:
except (subprocess.CalledProcessError, OSError):
return None
except OSError:
def get_hash_from_git(repo_path):
# Python 2.6 doesn't have check_output, so check for that.
try:
subprocess.check_output
except AttributeError:
return None
try:
return subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
cwd=repo_path,
stderr=subprocess.STDOUT,
universal_newlines=True,
).strip()
except (subprocess.CalledProcessError, OSError):
return None
@ -86,10 +101,13 @@ def get_version_info_from_mpconfig(repo_path):
def make_version_header(repo_path, filename):
git_tag = None
git_hash = None
if "MICROPY_GIT_TAG" in os.environ:
git_tag = os.environ["MICROPY_GIT_TAG"]
git_hash = os.environ.get("MICROPY_GIT_HASH")
if git_tag is None:
git_tag = get_version_info_from_git(repo_path)
git_hash = get_hash_from_git(repo_path)
if git_tag is None:
git_tag = get_version_info_from_mpconfig(repo_path)
@ -104,12 +122,15 @@ def make_version_header(repo_path, filename):
).date()
# Generate the file with the git and version info
# Note: MICROPY_GIT_HASH may be used by third-party code.
file_data = """\
// This file was generated by py/makeversionhdr.py
#define MICROPY_GIT_TAG "%s"
#define MICROPY_GIT_HASH "%s"
#define MICROPY_BUILD_DATE "%s"
""" % (
git_tag,
git_hash or "<no hash>",
build_date.strftime("%Y-%m-%d"),
)