brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · 3a627ee Raw
102 lines · python
1#!/usr/bin/env python32 3"""Gets the current revision and writes it to VCSRevision.h."""4 5import argparse6import os7import subprocess8import sys9import shutil10 11 12THIS_DIR = os.path.abspath(os.path.dirname(__file__))13LLVM_DIR = os.path.dirname(os.path.dirname(os.path.dirname(THIS_DIR)))14 15 16def main():17    parser = argparse.ArgumentParser(description=__doc__)18    parser.add_argument(19        "-d",20        "--depfile",21        help="if set, writes a depfile that causes this script "22        "to re-run each time the current revision changes",23    )24    parser.add_argument(25        "--write-git-rev",26        action="store_true",27        help="if set, writes git revision, else writes #undef",28    )29    parser.add_argument(30        "--name",31        action="append",32        help="if set, writes a depfile that causes this script "33        "to re-run each time the current revision changes",34    )35    parser.add_argument("vcs_header", help="path to the output file to write")36    args = parser.parse_args()37 38    vcsrevision_contents = ""39    if args.write_git_rev:40        git, use_shell = shutil.which("git"), False41        if not git:42            git = shutil.which("git.exe")43        if not git:44            git, use_shell = shutil.which("git.bat"), True45        git_dir = (46            subprocess.check_output(47                [git, "rev-parse", "--git-dir"], cwd=LLVM_DIR, shell=use_shell48            )49            .decode()50            .strip()51        )52        if not os.path.isdir(git_dir):53            print('.git dir not found at "%s"' % git_dir, file=sys.stderr)54            return 155 56        rev = (57            subprocess.check_output(58                [git, "rev-parse", "--short", "HEAD"], cwd=git_dir, shell=use_shell59            )60            .decode()61            .strip()62        )63        url = (64            subprocess.check_output(65                [git, "remote", "get-url", "origin"], cwd=git_dir, shell=use_shell66            )67            .decode()68            .strip()69        )70        for name in args.name:71            vcsrevision_contents += '#define %s_REVISION "%s"\n' % (name, rev)72            vcsrevision_contents += '#define %s_REPOSITORY "%s"\n' % (name, url)73    else:74        for name in args.name:75            vcsrevision_contents += "#undef %s_REVISION\n" % name76            vcsrevision_contents += "#undef %s_REPOSITORY\n" % name77 78    # If the output already exists and is identical to what we'd write,79    # return to not perturb the existing file's timestamp.80    if (81        os.path.exists(args.vcs_header)82        and open(args.vcs_header).read() == vcsrevision_contents83    ):84        return 085 86    # http://neugierig.org/software/blog/2014/11/binary-revisions.html87    if args.depfile:88        build_dir = os.getcwd()89        with open(args.depfile, "w") as depfile:90            depfile.write(91                "%s: %s\n"92                % (93                    args.vcs_header,94                    os.path.relpath(os.path.join(git_dir, "logs", "HEAD"), build_dir),95                )96            )97    open(args.vcs_header, "w").write(vcsrevision_contents)98 99 100if __name__ == "__main__":101    sys.exit(main())102