blob: a457b310d48085dcb9367df54bacc1974a064248 [file] [log] [blame]
Brian Paula3b2e602017-10-09 13:47:20 -06001"""
2Generate the contents of the git_sha1.h file.
Brian Paula3b2e602017-10-09 13:47:20 -06003"""
4
Eric Engestrom70886222017-10-25 14:04:35 +01005import argparse
Brian Paula3b2e602017-10-09 13:47:20 -06006import os
Jose Fonsecab99dcbf2017-08-01 14:36:16 +01007import os.path
8import subprocess
9import sys
10
Brian Paula3b2e602017-10-09 13:47:20 -060011
12def get_git_sha1():
13 """Try to get the git SHA1 with git rev-parse."""
14 git_dir = os.path.join(os.path.dirname(sys.argv[0]), '..', '.git')
15 try:
16 git_sha1 = subprocess.check_output([
17 'git',
18 '--git-dir=' + git_dir,
19 'rev-parse',
20 'HEAD',
21 ], stderr=open(os.devnull, 'w')).decode("ascii")
Eric Engestromc9093702020-05-20 01:22:42 +020022 except Exception:
Brian Paula3b2e602017-10-09 13:47:20 -060023 # don't print anything if it fails
24 git_sha1 = ''
25 return git_sha1
26
Eric Engestromba449902020-05-20 01:23:35 +020027
Eric Engestromc5dd0222018-08-14 18:02:04 +010028def write_if_different(contents):
29 """
30 Avoid touching the output file if it doesn't need modifications
31 Useful to avoid triggering rebuilds when nothing has changed.
32 """
33 if os.path.isfile(args.output):
34 with open(args.output, 'r') as file:
35 if file.read() == contents:
36 return
37 with open(args.output, 'w') as file:
38 file.write(contents)
39
Eric Engestromba449902020-05-20 01:23:35 +020040
Eric Engestrom70886222017-10-25 14:04:35 +010041parser = argparse.ArgumentParser()
42parser.add_argument('--output', help='File to write the #define in',
Eric Engestromc2e00f92018-08-14 18:05:55 +010043 required=True)
Eric Engestrom70886222017-10-25 14:04:35 +010044args = parser.parse_args()
Brian Paula3b2e602017-10-09 13:47:20 -060045
46git_sha1 = os.environ.get('MESA_GIT_SHA1_OVERRIDE', get_git_sha1())[:10]
47if git_sha1:
Eric Engestrombc8abc12018-08-14 18:04:58 +010048 write_if_different('#define MESA_GIT_SHA1 " (git-' + git_sha1 + ')"')
Eric Engestrom2117d032017-10-29 22:06:28 +000049else:
Eric Engestrombc8abc12018-08-14 18:04:58 +010050 write_if_different('#define MESA_GIT_SHA1 ""')