2 Generate the contents of the git_sha1.h file.
3 The output of this script goes to stdout.
15 """Try to get the git SHA1 with git rev-parse."""
16 git_dir = os.path.join(os.path.dirname(sys.argv[0]), '.', '.git')
18 git_sha1 = subprocess.check_output([
20 '--git-dir=' + git_dir,
23 ], stderr=open(os.devnull, 'w')).decode("ascii")
25 # don't print anything if it fails
29 def write_if_different(contents):
31 Avoid touching the output file if it doesn't need modifications
32 Useful to avoid triggering rebuilds when nothing has changed.
34 if os.path.isfile(args.output):
35 with open(args.output, 'r') as file:
36 if file.read() == contents:
38 with open(args.output, 'w') as file:
41 parser = argparse.ArgumentParser()
42 parser.add_argument('--output', help='File to write the #define in',
44 args = parser.parse_args()
46 git_sha1 = os.environ.get('LLVM_REVISION_OVERRIDE', get_git_sha1())[:7]
48 write_if_different('#define LLVM_REVISION "git-' + git_sha1 + '"')
50 write_if_different('#define LLVM_REVISION ""')