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