diff --git a/py/makeversionhdr.py b/py/makeversionhdr.py index 2030bb02c4..cb7325e416 100644 --- a/py/makeversionhdr.py +++ b/py/makeversionhdr.py @@ -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 "", build_date.strftime("%Y-%m-%d"), )