git_sha1_gen: accept MESA_GIT_SHA1_OVERRIDE env var
[mesa.git] / bin / git_sha1_gen.py
1 #!/usr/bin/env python
2
3 """
4 Generate the contents of the git_sha1.h file.
5 The output of this script goes to stdout.
6 """
7
8
9 import os
10 import os.path
11 import subprocess
12 import sys
13
14
15 def get_git_sha1():
16 """Try to get the git SHA1 with git rev-parse."""
17 git_dir = os.path.join(os.path.dirname(sys.argv[0]), '..', '.git')
18 try:
19 git_sha1 = subprocess.check_output([
20 'git',
21 '--git-dir=' + git_dir,
22 'rev-parse',
23 'HEAD',
24 ], stderr=open(os.devnull, 'w')).decode("ascii")
25 except:
26 # don't print anything if it fails
27 git_sha1 = ''
28 return git_sha1
29
30
31 git_sha1 = os.environ.get('MESA_GIT_SHA1_OVERRIDE', get_git_sha1())[:10]
32 if git_sha1:
33 sys.stdout.write('#define MESA_GIT_SHA1 "git-%s"\n' % git_sha1.rstrip())