Tim Van Patten | 9da287f | 2019-11-07 10:27:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2019 The SwiftShader Authors. All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | # Generate commit.h with git commit hash. |
| 17 | # |
| 18 | |
| 19 | import subprocess as sp |
| 20 | import sys |
| 21 | import os |
| 22 | |
| 23 | usage = """\ |
| 24 | Usage: commit_id.py check - check if git is present |
| 25 | commit_id.py gen <file_to_write> - generate commit.h""" |
| 26 | |
| 27 | |
| 28 | def grab_output(command, cwd): |
| 29 | return sp.Popen(command, stdout=sp.PIPE, shell=True, cwd=cwd).communicate()[0].strip() |
| 30 | |
| 31 | |
| 32 | if len(sys.argv) < 2: |
| 33 | sys.exit(usage) |
| 34 | |
| 35 | operation = sys.argv[1] |
| 36 | cwd = sys.path[0] |
| 37 | |
| 38 | if operation == 'check': |
| 39 | index_path = os.path.join(cwd, '.git', 'index') |
| 40 | if os.path.exists(index_path): |
| 41 | print("1") |
| 42 | else: |
| 43 | print("0") |
| 44 | sys.exit(0) |
| 45 | |
| 46 | if len(sys.argv) < 3 or operation != 'gen': |
| 47 | sys.exit(usage) |
| 48 | |
| 49 | output_file = sys.argv[2] |
| 50 | commit_id_size = 12 |
| 51 | |
| 52 | commit_id = 'invalid-hash' |
| 53 | commit_date = 'invalid-date' |
| 54 | |
| 55 | try: |
| 56 | commit_id = grab_output('git rev-parse --short=%d HEAD' % commit_id_size, cwd) |
| 57 | commit_date = grab_output('git show -s --format=%ci HEAD', cwd) |
| 58 | except: |
| 59 | pass |
| 60 | |
| 61 | hfile = open(output_file, 'w') |
| 62 | |
| 63 | hfile.write('#define SWIFTSHADER_COMMIT_HASH "%s"\n' % commit_id) |
| 64 | hfile.write('#define SWIFTSHADER_COMMIT_HASH_SIZE %d\n' % commit_id_size) |
| 65 | hfile.write('#define SWIFTSHADER_COMMIT_DATE "%s"\n' % commit_date) |
| 66 | hfile.write('#define SWIFTSHADER_VERSION_STRING \\\n' |
| 67 | 'MACRO_STRINGIFY(MAJOR_VERSION) \".\" \\\n' |
| 68 | 'MACRO_STRINGIFY(MINOR_VERSION) \".\" \\\n' |
| 69 | 'MACRO_STRINGIFY(PATCH_VERSION) \".\" \\\n' |
| 70 | 'SWIFTSHADER_COMMIT_HASH\n') |
| 71 | |
| 72 | hfile.close() |