git_sha1_gen.py revision 01e04c3f
1"""
2Generate the contents of the git_sha1.h file.
3The output of this script goes to stdout.
4"""
5
6
7import argparse
8import os
9import os.path
10import subprocess
11import sys
12
13
14def get_git_sha1():
15    """Try to get the git SHA1 with git rev-parse."""
16    git_dir = os.path.join(os.path.dirname(sys.argv[0]), '..', '.git')
17    try:
18        git_sha1 = subprocess.check_output([
19            'git',
20            '--git-dir=' + git_dir,
21            'rev-parse',
22            'HEAD',
23        ], stderr=open(os.devnull, 'w')).decode("ascii")
24    except:
25        # don't print anything if it fails
26        git_sha1 = ''
27    return git_sha1
28
29def write_if_different(contents):
30    """
31    Avoid touching the output file if it doesn't need modifications
32    Useful to avoid triggering rebuilds when nothing has changed.
33    """
34    if os.path.isfile(args.output):
35        with open(args.output, 'r') as file:
36            if file.read() == contents:
37                return
38    with open(args.output, 'w') as file:
39        file.write(contents)
40
41parser = argparse.ArgumentParser()
42parser.add_argument('--output', help='File to write the #define in',
43                    required=True)
44args = parser.parse_args()
45
46git_sha1 = os.environ.get('MESA_GIT_SHA1_OVERRIDE', get_git_sha1())[:10]
47if git_sha1:
48    write_if_different('#define MESA_GIT_SHA1 " (git-' + git_sha1 + ')"')
49else:
50    write_if_different('#define MESA_GIT_SHA1 ""')
51