3 """Gets the current revision and writes it to VCSRevision.h."""
5 from __future__
import print_function
13 THIS_DIR
= os
.path
.abspath(os
.path
.dirname(__file__
))
14 LLVM_DIR
= os
.path
.dirname(os
.path
.dirname(os
.path
.dirname(THIS_DIR
)))
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
):
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()
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
)
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
)
62 for name
in args
.name
:
63 vcsrevision_contents
+= '#undef %s_REVISION\n' % name
64 vcsrevision_contents
+= '#undef %s_REPOSITORY\n' % name
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
:
72 # http://neugierig.org/software/blog/2014/11/binary-revisions.html
74 build_dir
= os
.getcwd()
75 with
open(args
.depfile
, 'w') as depfile
:
76 depfile
.write('%s: %s\n' % (
78 os
.path
.relpath(os
.path
.join(git_dir
, 'logs', 'HEAD'),
80 open(args
.vcs_header
, 'w').write(vcsrevision_contents
)
83 if __name__
== '__main__':