blob: 497291ae18caf5d0f5942791937953135a59aec1 [file] [log] [blame]
Cort Strattona81851c2017-11-06 19:13:53 -08001#!/usr/bin/env python3
2#
Mike Schuchardt840b5042019-07-11 08:11:47 -07003# 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 Strattona81851c2017-11-06 19:13:53 -08007#
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 Stratton7f521702018-02-01 23:25:21 -080021# Author: Jean-Francois Roy <jfroy@google.com>
Cort Strattona81851c2017-11-06 19:13:53 -080022
Cort Stratton7f521702018-02-01 23:25:21 -080023import argparse
24import hashlib
Cort Strattona81851c2017-11-06 19:13:53 -080025import subprocess
Mark Lobodzinski52913ec2018-05-10 11:00:09 -060026import uuid
Mike Schuchardtc8c73612019-08-01 10:05:00 -070027import json
Cort Strattona81851c2017-11-06 19:13:53 -080028
Cort Stratton7f521702018-02-01 23:25:21 -080029def generate(symbol_name, commit_id, output_header_file):
Cort Strattona81851c2017-11-06 19:13:53 -080030 # 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 Schuchardt840b5042019-07-11 08:11:47 -070041 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 Strattona81851c2017-11-06 19:13:53 -080045 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 Stratton04443a62017-11-22 16:04:23 -080065 contents += '#define %s "%s"\n' % (symbol_name, commit_id)
Cort Strattona81851c2017-11-06 19:13:53 -080066 header_file.write(contents)
Jamie Madill5ccfd6e2017-12-28 12:39:51 -050067
Cort Stratton7f521702018-02-01 23:25:21 -080068def get_commit_id_from_git(git_binary, source_dir):
Mark Lobodzinski52913ec2018-05-10 11:00:09 -060069 value = subprocess.check_output([git_binary, "rev-parse", "HEAD"], cwd=source_dir).decode('utf-8').strip()
70 return value
Cort Stratton7f521702018-02-01 23:25:21 -080071
72def is_sha1(str):
73 try: str_as_int = int(str, 16)
74 except ValueError: return False
75 return len(str) == 40
76
77def 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 Lobodzinski52913ec2018-05-10 11:00:09 -060088def get_commit_id_from_uuid():
Mike Schuchardtc8c73612019-08-01 10:05:00 -070089 unique_uuid = str(uuid.uuid4())
90 sha1 = hashlib.sha1();
91 sha1.update(unique_uuid.encode())
92 return sha1.hexdigest()
93
94def 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 Lobodzinski52913ec2018-05-10 11:00:09 -0600103
Cort Stratton7f521702018-02-01 23:25:21 -0800104def main():
105 parser = argparse.ArgumentParser()
Cort Stratton7f521702018-02-01 23:25:21 -0800106 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 Lobodzinski52913ec2018-05-10 11:00:09 -0600109 rev_method_group.add_argument("--from_uuid", action='store_true', help="base SHA1 on a dynamically generated UUID")
Mike Schuchardtc8c73612019-08-01 10:05:00 -0700110 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 Stratton7f521702018-02-01 23:25:21 -0800114 args = parser.parse_args()
115
Mike Schuchardtc8c73612019-08-01 10:05:00 -0700116 if ('json_file' in args) != ('json_keys' in args):
117 parser.error('--json_file and --json_keys must be provided together')
118
Cort Stratton7f521702018-02-01 23:25:21 -0800119 # 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 Lobodzinski52913ec2018-05-10 11:00:09 -0600127 # Call git.bat on Windows for compatibility.
Cort Stratton7f521702018-02-01 23:25:21 -0800128 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 Schuchardtc8c73612019-08-01 10:05:00 -0700132 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 Lobodzinski52913ec2018-05-10 11:00:09 -0600135 commit_id = get_commit_id_from_uuid()
Cort Stratton7f521702018-02-01 23:25:21 -0800136
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 Madill5ccfd6e2017-12-28 12:39:51 -0500142if __name__ == '__main__':
Cort Stratton7f521702018-02-01 23:25:21 -0800143 main()