blob: 6edb9738e55c0b9b1adfa78fd0839ec0234d39b2 [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
Cort Strattona81851c2017-11-06 19:13:53 -080027
Cort Stratton7f521702018-02-01 23:25:21 -080028def generate(symbol_name, commit_id, output_header_file):
Cort Strattona81851c2017-11-06 19:13:53 -080029 # Write commit ID to output header file
30 with open(output_header_file, "w") as header_file:
31 # File Comment
32 file_comment = '// *** THIS FILE IS GENERATED - DO NOT EDIT ***\n'
33 file_comment += '// See external_revision_generator.py for modifications\n'
34 header_file.write(file_comment)
35 # Copyright Notice
36 copyright = ''
37 copyright += '\n'
38 copyright += '/***************************************************************************\n'
39 copyright += ' *\n'
Mike Schuchardt840b5042019-07-11 08:11:47 -070040 copyright += ' * Copyright (c) 2015-2019 The Khronos Group Inc.\n'
41 copyright += ' * Copyright (c) 2015-2019 Valve Corporation\n'
42 copyright += ' * Copyright (c) 2015-2019 LunarG, Inc.\n'
43 copyright += ' * Copyright (c) 2015-2019 Google Inc.\n'
Cort Strattona81851c2017-11-06 19:13:53 -080044 copyright += ' *\n'
45 copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n'
46 copyright += ' * you may not use this file except in compliance with the License.\n'
47 copyright += ' * You may obtain a copy of the License at\n'
48 copyright += ' *\n'
49 copyright += ' * http://www.apache.org/licenses/LICENSE-2.0\n'
50 copyright += ' *\n'
51 copyright += ' * Unless required by applicable law or agreed to in writing, software\n'
52 copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n'
53 copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
54 copyright += ' * See the License for the specific language governing permissions and\n'
55 copyright += ' * limitations under the License.\n'
56 copyright += ' *\n'
57 copyright += ' * Author: Chris Forbes <chrisforbes@google.com>\n'
58 copyright += ' * Author: Cort Stratton <cort@google.com>\n'
59 copyright += ' *\n'
60 copyright += ' ****************************************************************************/\n'
61 header_file.write(copyright)
62 # Contents
63 contents = '#pragma once\n\n'
Cort Stratton04443a62017-11-22 16:04:23 -080064 contents += '#define %s "%s"\n' % (symbol_name, commit_id)
Cort Strattona81851c2017-11-06 19:13:53 -080065 header_file.write(contents)
Jamie Madill5ccfd6e2017-12-28 12:39:51 -050066
Cort Stratton7f521702018-02-01 23:25:21 -080067def get_commit_id_from_git(git_binary, source_dir):
Mark Lobodzinski52913ec2018-05-10 11:00:09 -060068 value = subprocess.check_output([git_binary, "rev-parse", "HEAD"], cwd=source_dir).decode('utf-8').strip()
69 return value
Cort Stratton7f521702018-02-01 23:25:21 -080070
71def is_sha1(str):
72 try: str_as_int = int(str, 16)
73 except ValueError: return False
74 return len(str) == 40
75
76def get_commit_id_from_file(rev_file):
77 with open(rev_file, 'r') as rev_stream:
78 rev_contents = rev_stream.read()
79 rev_contents_stripped = rev_contents.strip()
80 if is_sha1(rev_contents_stripped):
81 return rev_contents_stripped;
82 # otherwise, SHA1 the entire (unstripped) file contents
83 sha1 = hashlib.sha1();
84 sha1.update(rev_contents.encode('utf-8'))
85 return sha1.hexdigest()
86
Mark Lobodzinski52913ec2018-05-10 11:00:09 -060087def get_commit_id_from_uuid():
88 unique_uuid = str(uuid.uuid4())
89 sha1 = hashlib.sha1();
90 sha1.update(unique_uuid.encode())
91 return sha1.hexdigest()
92
Cort Stratton7f521702018-02-01 23:25:21 -080093def main():
94 parser = argparse.ArgumentParser()
95 parser.add_argument("-s", "--symbol_name", metavar="SYMBOL_NAME", required=True, help="C symbol name")
96 parser.add_argument("-o", "--output_header_file", metavar="OUTPUT_HEADER_FILE", required=True, help="output header file path")
97 rev_method_group = parser.add_mutually_exclusive_group(required=True)
98 rev_method_group.add_argument("--git_dir", metavar="SOURCE_DIR", help="git working copy directory")
99 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 -0600100 rev_method_group.add_argument("--from_uuid", action='store_true', help="base SHA1 on a dynamically generated UUID")
Cort Stratton7f521702018-02-01 23:25:21 -0800101 args = parser.parse_args()
102
103 # We can either parse the latest Git commit ID out of the specified repository (preferred where possible),
104 # or computing the SHA1 hash of the contents of a file passed on the command line and (where necessary --
105 # e.g. when building the layers outside of a Git environment).
106 if args.git_dir is not None:
107 # Extract commit ID from the specified source directory
108 try:
109 commit_id = get_commit_id_from_git('git', args.git_dir)
110 except WindowsError:
Mark Lobodzinski52913ec2018-05-10 11:00:09 -0600111 # Call git.bat on Windows for compatibility.
Cort Stratton7f521702018-02-01 23:25:21 -0800112 commit_id = get_commit_id_from_git('git.bat', args.git_dir)
113 elif args.rev_file is not None:
114 # Read the commit ID from a file.
115 commit_id = get_commit_id_from_file(args.rev_file)
Mark Lobodzinski52913ec2018-05-10 11:00:09 -0600116 elif args.from_uuid is not None:
117 commit_id = get_commit_id_from_uuid()
Cort Stratton7f521702018-02-01 23:25:21 -0800118
119 if not is_sha1(commit_id):
120 raise ValueError("commit ID for " + args.symbol_name + " must be a SHA1 hash.")
121
122 generate(args.symbol_name, commit_id, args.output_header_file)
123
Jamie Madill5ccfd6e2017-12-28 12:39:51 -0500124if __name__ == '__main__':
Cort Stratton7f521702018-02-01 23:25:21 -0800125 main()