Mauro Rossi | 0187681 | 2018-09-08 18:32:44 +0200 | [diff] [blame] | 1 | """ |
| 2 | Generate the contents of the git_sha1.h file. |
| 3 | The output of this script goes to stdout. |
| 4 | """ |
| 5 | |
| 6 | |
| 7 | import argparse |
| 8 | import os |
| 9 | import os.path |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
| 13 | |
| 14 | def 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 | |
| 29 | def 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 | |
| 41 | parser = argparse.ArgumentParser() |
| 42 | parser.add_argument('--output', help='File to write the #define in', |
| 43 | required=True) |
| 44 | args = parser.parse_args() |
| 45 | |
| 46 | git_sha1 = os.environ.get('LLVM_REVISION_OVERRIDE', get_git_sha1())[:7] |
| 47 | if git_sha1: |
| 48 | write_if_different('#define LLVM_REVISION "git-' + git_sha1 + '"') |
| 49 | else: |
| 50 | write_if_different('#define LLVM_REVISION ""') |