blob: c097a2b5b6a10f75027784a6306b0e7d05eaab56 [file] [log] [blame]
Mauro Rossi01876812018-09-08 18:32:44 +02001"""
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('LLVM_REVISION_OVERRIDE', get_git_sha1())[:7]
47if git_sha1:
48 write_if_different('#define LLVM_REVISION "git-' + git_sha1 + '"')
49else:
50 write_if_different('#define LLVM_REVISION ""')