Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
Cort Stratton | b9aa8f1 | 2017-11-27 16:43:14 -0800 | [diff] [blame] | 3 | # Copyright (c) 2015-2017 The Khronos Group Inc. |
| 4 | # Copyright (c) 2015-2017 Valve Corporation |
| 5 | # Copyright (c) 2015-2017 LunarG, Inc. |
| 6 | # Copyright (c) 2015-2017 Google Inc. |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | # |
| 20 | # Author: Cort Stratton <cort@google.com> |
Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame^] | 21 | # Author: Jean-Francois Roy <jfroy@google.com> |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 22 | |
Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame^] | 23 | import argparse |
| 24 | import hashlib |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 25 | import subprocess |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 26 | |
Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame^] | 27 | def generate(symbol_name, commit_id, output_header_file): |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 28 | # Write commit ID to output header file |
| 29 | with open(output_header_file, "w") as header_file: |
| 30 | # File Comment |
| 31 | file_comment = '// *** THIS FILE IS GENERATED - DO NOT EDIT ***\n' |
| 32 | file_comment += '// See external_revision_generator.py for modifications\n' |
| 33 | header_file.write(file_comment) |
| 34 | # Copyright Notice |
| 35 | copyright = '' |
| 36 | copyright += '\n' |
| 37 | copyright += '/***************************************************************************\n' |
| 38 | copyright += ' *\n' |
| 39 | copyright += ' * Copyright (c) 2015-2017 The Khronos Group Inc.\n' |
| 40 | copyright += ' * Copyright (c) 2015-2017 Valve Corporation\n' |
| 41 | copyright += ' * Copyright (c) 2015-2017 LunarG, Inc.\n' |
| 42 | copyright += ' * Copyright (c) 2015-2017 Google Inc.\n' |
| 43 | copyright += ' *\n' |
| 44 | copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n' |
| 45 | copyright += ' * you may not use this file except in compliance with the License.\n' |
| 46 | copyright += ' * You may obtain a copy of the License at\n' |
| 47 | copyright += ' *\n' |
| 48 | copyright += ' * http://www.apache.org/licenses/LICENSE-2.0\n' |
| 49 | copyright += ' *\n' |
| 50 | copyright += ' * Unless required by applicable law or agreed to in writing, software\n' |
| 51 | copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n' |
| 52 | copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' |
| 53 | copyright += ' * See the License for the specific language governing permissions and\n' |
| 54 | copyright += ' * limitations under the License.\n' |
| 55 | copyright += ' *\n' |
| 56 | copyright += ' * Author: Chris Forbes <chrisforbes@google.com>\n' |
| 57 | copyright += ' * Author: Cort Stratton <cort@google.com>\n' |
| 58 | copyright += ' *\n' |
| 59 | copyright += ' ****************************************************************************/\n' |
| 60 | header_file.write(copyright) |
| 61 | # Contents |
| 62 | contents = '#pragma once\n\n' |
Cort Stratton | 04443a6 | 2017-11-22 16:04:23 -0800 | [diff] [blame] | 63 | contents += '#define %s "%s"\n' % (symbol_name, commit_id) |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 64 | header_file.write(contents) |
Jamie Madill | 5ccfd6e | 2017-12-28 12:39:51 -0500 | [diff] [blame] | 65 | |
Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame^] | 66 | def get_commit_id_from_git(git_binary, source_dir): |
| 67 | return subprocess.check_output([git_binary, "rev-parse", "HEAD"], cwd=source_dir).decode('utf-8').strip() |
| 68 | |
| 69 | def is_sha1(str): |
| 70 | try: str_as_int = int(str, 16) |
| 71 | except ValueError: return False |
| 72 | return len(str) == 40 |
| 73 | |
| 74 | def get_commit_id_from_file(rev_file): |
| 75 | with open(rev_file, 'r') as rev_stream: |
| 76 | rev_contents = rev_stream.read() |
| 77 | rev_contents_stripped = rev_contents.strip() |
| 78 | if is_sha1(rev_contents_stripped): |
| 79 | return rev_contents_stripped; |
| 80 | # otherwise, SHA1 the entire (unstripped) file contents |
| 81 | sha1 = hashlib.sha1(); |
| 82 | sha1.update(rev_contents.encode('utf-8')) |
| 83 | return sha1.hexdigest() |
| 84 | |
| 85 | def main(): |
| 86 | parser = argparse.ArgumentParser() |
| 87 | parser.add_argument("-s", "--symbol_name", metavar="SYMBOL_NAME", required=True, help="C symbol name") |
| 88 | parser.add_argument("-o", "--output_header_file", metavar="OUTPUT_HEADER_FILE", required=True, help="output header file path") |
| 89 | rev_method_group = parser.add_mutually_exclusive_group(required=True) |
| 90 | rev_method_group.add_argument("--git_dir", metavar="SOURCE_DIR", help="git working copy directory") |
| 91 | rev_method_group.add_argument("--rev_file", metavar="REVISION_FILE", help="source revision file path (must contain a SHA1 hash") |
| 92 | args = parser.parse_args() |
| 93 | |
| 94 | # We can either parse the latest Git commit ID out of the specified repository (preferred where possible), |
| 95 | # or computing the SHA1 hash of the contents of a file passed on the command line and (where necessary -- |
| 96 | # e.g. when building the layers outside of a Git environment). |
| 97 | if args.git_dir is not None: |
| 98 | # Extract commit ID from the specified source directory |
| 99 | try: |
| 100 | commit_id = get_commit_id_from_git('git', args.git_dir) |
| 101 | except WindowsError: |
| 102 | # Call git.bat on Windows for compatiblity. |
| 103 | commit_id = get_commit_id_from_git('git.bat', args.git_dir) |
| 104 | elif args.rev_file is not None: |
| 105 | # Read the commit ID from a file. |
| 106 | commit_id = get_commit_id_from_file(args.rev_file) |
| 107 | |
| 108 | if not is_sha1(commit_id): |
| 109 | raise ValueError("commit ID for " + args.symbol_name + " must be a SHA1 hash.") |
| 110 | |
| 111 | generate(args.symbol_name, commit_id, args.output_header_file) |
| 112 | |
Jamie Madill | 5ccfd6e | 2017-12-28 12:39:51 -0500 | [diff] [blame] | 113 | if __name__ == '__main__': |
Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame^] | 114 | main() |