| Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| Mike Schuchardt | 840b504 | 2019-07-11 08:11:47 -0700 | [diff] [blame] | 3 | # Copyright (c) 2015-2019 The Khronos Group Inc. |
| 4 | # Copyright (c) 2015-2019 Valve Corporation |
| 5 | # Copyright (c) 2015-2019 LunarG, Inc. |
| 6 | # Copyright (c) 2015-2019 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 |
| Mark Lobodzinski | 52913ec | 2018-05-10 11:00:09 -0600 | [diff] [blame] | 26 | import uuid |
| Mike Schuchardt | c8c7361 | 2019-08-01 10:05:00 -0700 | [diff] [blame] | 27 | import json |
| Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 28 | |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 29 | def generate(symbol_name, commit_id, output_header_file): |
| Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 30 | # Write commit ID to output header file |
| 31 | with open(output_header_file, "w") as header_file: |
| 32 | # File Comment |
| 33 | file_comment = '// *** THIS FILE IS GENERATED - DO NOT EDIT ***\n' |
| 34 | file_comment += '// See external_revision_generator.py for modifications\n' |
| 35 | header_file.write(file_comment) |
| 36 | # Copyright Notice |
| 37 | copyright = '' |
| 38 | copyright += '\n' |
| 39 | copyright += '/***************************************************************************\n' |
| 40 | copyright += ' *\n' |
| Mike Schuchardt | 840b504 | 2019-07-11 08:11:47 -0700 | [diff] [blame] | 41 | copyright += ' * Copyright (c) 2015-2019 The Khronos Group Inc.\n' |
| 42 | copyright += ' * Copyright (c) 2015-2019 Valve Corporation\n' |
| 43 | copyright += ' * Copyright (c) 2015-2019 LunarG, Inc.\n' |
| 44 | copyright += ' * Copyright (c) 2015-2019 Google Inc.\n' |
| Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 45 | copyright += ' *\n' |
| 46 | copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n' |
| 47 | copyright += ' * you may not use this file except in compliance with the License.\n' |
| 48 | copyright += ' * You may obtain a copy of the License at\n' |
| 49 | copyright += ' *\n' |
| 50 | copyright += ' * http://www.apache.org/licenses/LICENSE-2.0\n' |
| 51 | copyright += ' *\n' |
| 52 | copyright += ' * Unless required by applicable law or agreed to in writing, software\n' |
| 53 | copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n' |
| 54 | copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' |
| 55 | copyright += ' * See the License for the specific language governing permissions and\n' |
| 56 | copyright += ' * limitations under the License.\n' |
| 57 | copyright += ' *\n' |
| 58 | copyright += ' * Author: Chris Forbes <chrisforbes@google.com>\n' |
| 59 | copyright += ' * Author: Cort Stratton <cort@google.com>\n' |
| 60 | copyright += ' *\n' |
| 61 | copyright += ' ****************************************************************************/\n' |
| 62 | header_file.write(copyright) |
| 63 | # Contents |
| 64 | contents = '#pragma once\n\n' |
| Cort Stratton | 04443a6 | 2017-11-22 16:04:23 -0800 | [diff] [blame] | 65 | contents += '#define %s "%s"\n' % (symbol_name, commit_id) |
| Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 66 | header_file.write(contents) |
| Jamie Madill | 5ccfd6e | 2017-12-28 12:39:51 -0500 | [diff] [blame] | 67 | |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 68 | def get_commit_id_from_git(git_binary, source_dir): |
| Mark Lobodzinski | 52913ec | 2018-05-10 11:00:09 -0600 | [diff] [blame] | 69 | value = subprocess.check_output([git_binary, "rev-parse", "HEAD"], cwd=source_dir).decode('utf-8').strip() |
| 70 | return value |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 71 | |
| 72 | def is_sha1(str): |
| 73 | try: str_as_int = int(str, 16) |
| 74 | except ValueError: return False |
| 75 | return len(str) == 40 |
| 76 | |
| 77 | def get_commit_id_from_file(rev_file): |
| 78 | with open(rev_file, 'r') as rev_stream: |
| 79 | rev_contents = rev_stream.read() |
| 80 | rev_contents_stripped = rev_contents.strip() |
| 81 | if is_sha1(rev_contents_stripped): |
| 82 | return rev_contents_stripped; |
| 83 | # otherwise, SHA1 the entire (unstripped) file contents |
| 84 | sha1 = hashlib.sha1(); |
| 85 | sha1.update(rev_contents.encode('utf-8')) |
| 86 | return sha1.hexdigest() |
| 87 | |
| Mark Lobodzinski | 52913ec | 2018-05-10 11:00:09 -0600 | [diff] [blame] | 88 | def get_commit_id_from_uuid(): |
| Mike Schuchardt | c8c7361 | 2019-08-01 10:05:00 -0700 | [diff] [blame] | 89 | unique_uuid = str(uuid.uuid4()) |
| 90 | sha1 = hashlib.sha1(); |
| 91 | sha1.update(unique_uuid.encode()) |
| 92 | return sha1.hexdigest() |
| 93 | |
| 94 | def get_commit_id_from_json(json_file, json_keys): |
| 95 | with open(json_file) as json_stream: |
| 96 | json_data = json.load(json_stream) |
| 97 | for key in json_keys.split(','): |
| 98 | if type(json_data) == list: |
| 99 | json_data = json_data[int(key)] |
| 100 | else: |
| 101 | json_data = json_data[key] |
| 102 | return json_data |
| Mark Lobodzinski | 52913ec | 2018-05-10 11:00:09 -0600 | [diff] [blame] | 103 | |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 104 | def main(): |
| 105 | parser = argparse.ArgumentParser() |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 106 | rev_method_group = parser.add_mutually_exclusive_group(required=True) |
| 107 | rev_method_group.add_argument("--git_dir", metavar="SOURCE_DIR", help="git working copy directory") |
| 108 | rev_method_group.add_argument("--rev_file", metavar="REVISION_FILE", help="source revision file path (must contain a SHA1 hash") |
| Mark Lobodzinski | 52913ec | 2018-05-10 11:00:09 -0600 | [diff] [blame] | 109 | rev_method_group.add_argument("--from_uuid", action='store_true', help="base SHA1 on a dynamically generated UUID") |
| Mike Schuchardt | c8c7361 | 2019-08-01 10:05:00 -0700 | [diff] [blame] | 110 | rev_method_group.add_argument("--json_file", metavar="JSON_FILE", help="path to json file") |
| 111 | parser.add_argument("-s", "--symbol_name", metavar="SYMBOL_NAME", required=True, help="C symbol name") |
| 112 | parser.add_argument("-o", "--output_header_file", metavar="OUTPUT_HEADER_FILE", required=True, help="output header file path") |
| 113 | parser.add_argument("--json_keys", action='store', metavar="JSON_KEYS", help="comma-separated list of keys specifying SHA1 location in root json object for --json_file option") |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 114 | args = parser.parse_args() |
| 115 | |
| Mike Schuchardt | c8c7361 | 2019-08-01 10:05:00 -0700 | [diff] [blame] | 116 | if ('json_file' in args) != ('json_keys' in args): |
| 117 | parser.error('--json_file and --json_keys must be provided together') |
| 118 | |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 119 | # We can either parse the latest Git commit ID out of the specified repository (preferred where possible), |
| 120 | # or computing the SHA1 hash of the contents of a file passed on the command line and (where necessary -- |
| 121 | # e.g. when building the layers outside of a Git environment). |
| 122 | if args.git_dir is not None: |
| 123 | # Extract commit ID from the specified source directory |
| 124 | try: |
| 125 | commit_id = get_commit_id_from_git('git', args.git_dir) |
| 126 | except WindowsError: |
| Mark Lobodzinski | 52913ec | 2018-05-10 11:00:09 -0600 | [diff] [blame] | 127 | # Call git.bat on Windows for compatibility. |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 128 | commit_id = get_commit_id_from_git('git.bat', args.git_dir) |
| 129 | elif args.rev_file is not None: |
| 130 | # Read the commit ID from a file. |
| 131 | commit_id = get_commit_id_from_file(args.rev_file) |
| Mike Schuchardt | c8c7361 | 2019-08-01 10:05:00 -0700 | [diff] [blame] | 132 | elif args.json_file is not None: |
| 133 | commit_id = get_commit_id_from_json(args.json_file, args.json_keys) |
| 134 | elif args.from_uuid: |
| Mark Lobodzinski | 52913ec | 2018-05-10 11:00:09 -0600 | [diff] [blame] | 135 | commit_id = get_commit_id_from_uuid() |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 136 | |
| 137 | if not is_sha1(commit_id): |
| 138 | raise ValueError("commit ID for " + args.symbol_name + " must be a SHA1 hash.") |
| 139 | |
| 140 | generate(args.symbol_name, commit_id, args.output_header_file) |
| 141 | |
| Jamie Madill | 5ccfd6e | 2017-12-28 12:39:51 -0500 | [diff] [blame] | 142 | if __name__ == '__main__': |
| Cort Stratton | 7f52170 | 2018-02-01 23:25:21 -0800 | [diff] [blame] | 143 | main() |