• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3"""Gets the current revision and writes it to VCSRevision.h."""
4
5from __future__ import print_function
6
7import argparse
8import os
9import subprocess
10import sys
11
12
13THIS_DIR = os.path.abspath(os.path.dirname(__file__))
14LLVM_DIR = os.path.dirname(os.path.dirname(os.path.dirname(THIS_DIR)))
15
16
17def which(program):
18    # distutils.spawn.which() doesn't find .bat files,
19    # https://bugs.python.org/issue2200
20    for path in os.environ["PATH"].split(os.pathsep):
21        candidate = os.path.join(path, program)
22        if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
23            return candidate
24    return None
25
26
27def main():
28    parser = argparse.ArgumentParser(description=__doc__)
29    parser.add_argument('-d', '--depfile',
30                        help='if set, writes a depfile that causes this script '
31                             'to re-run each time the current revision changes')
32    parser.add_argument('--write-git-rev', action='store_true',
33                        help='if set, writes git revision, else writes #undef')
34    parser.add_argument('--name', action='append',
35                        help='if set, writes a depfile that causes this script '
36                             'to re-run each time the current revision changes')
37    parser.add_argument('vcs_header', help='path to the output file to write')
38    args = parser.parse_args()
39
40    vcsrevision_contents = ''
41    if args.write_git_rev:
42        git, use_shell = which('git'), False
43        if not git: git = which('git.exe')
44        if not git: git, use_shell = which('git.bat'), True
45        git_dir = subprocess.check_output(
46                [git, 'rev-parse', '--git-dir'],
47                cwd=LLVM_DIR, shell=use_shell).decode().strip()
48        if not os.path.isdir(git_dir):
49            print('.git dir not found at "%s"' % git_dir, file=sys.stderr)
50            return 1
51
52        rev = subprocess.check_output(
53                [git, 'rev-parse', '--short', 'HEAD'],
54                cwd=git_dir, shell=use_shell).decode().strip()
55        url = subprocess.check_output(
56                [git, 'remote', 'get-url', 'origin'],
57                cwd=git_dir, shell=use_shell).decode().strip()
58        for name in args.name:
59            vcsrevision_contents += '#define %s_REVISION "%s"\n' % (name, rev)
60            vcsrevision_contents += '#define %s_REPOSITORY "%s"\n' % (name, url)
61    else:
62        for name in args.name:
63            vcsrevision_contents += '#undef %s_REVISION\n' % name
64            vcsrevision_contents += '#undef %s_REPOSITORY\n' % name
65
66    # If the output already exists and is identical to what we'd write,
67    # return to not perturb the existing file's timestamp.
68    if os.path.exists(args.vcs_header) and \
69            open(args.vcs_header).read() == vcsrevision_contents:
70        return 0
71
72    # http://neugierig.org/software/blog/2014/11/binary-revisions.html
73    if args.depfile:
74        build_dir = os.getcwd()
75        with open(args.depfile, 'w') as depfile:
76            depfile.write('%s: %s\n' % (
77                args.vcs_header,
78                os.path.relpath(os.path.join(git_dir, 'logs', 'HEAD'),
79                                build_dir)))
80    open(args.vcs_header, 'w').write(vcsrevision_contents)
81
82
83if __name__ == '__main__':
84    sys.exit(main())
85