blob: 4b3b6062ab98ced2d8c0d874ffdbfb3c870b6c5e [file] [log] [blame]
Mike Schuchardtc64a5412019-07-02 00:27:05 -07001#!/usr/bin/env python3
2# Copyright (c) 2019 The Khronos Group Inc.
3# Copyright (c) 2019 Valve Corporation
4# Copyright (c) 2019 LunarG, Inc.
5# Copyright (c) 2019 Google Inc.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# Author: Mike Schuchardt <mikes@lunarg.com>
20
21import argparse
22import filecmp
23import os
24import shutil
25import subprocess
26import sys
27import tempfile
28
29# files to exclude from --verify check
30verify_exclude = ['.clang-format']
31
32# helper to define paths relative to this file
33def script_relative(path):
34 return os.path.abspath(os.path.join(os.path.dirname(__file__), path))
35
36def main(argv):
37 parser = argparse.ArgumentParser(description='Generate source code for this repository')
38 parser.add_argument('registry', metavar='REGISTRY_PATH', help='path to the Vulkan-Headers registry directory')
39 group = parser.add_mutually_exclusive_group()
40 group.add_argument('-i', '--incremental', action='store_true', help='only update repo files that change')
41 group.add_argument('-v', '--verify', action='store_true', help='verify repo files match generator output')
42 args = parser.parse_args(argv)
43
44 gen_cmds = [*[[script_relative('lvl_genvk.py'),
45 '-registry', os.path.abspath(os.path.join(args.registry, 'vk.xml')),
46 '-quiet',
47 filename] for filename in ["chassis.cpp",
48 "chassis.h",
49 "layer_chassis_dispatch.cpp",
50 "layer_chassis_dispatch.h",
51 "object_tracker.cpp",
52 "object_tracker.h",
53 "parameter_validation.cpp",
54 "parameter_validation.h",
55 "thread_safety.cpp",
56 "thread_safety.h",
57 "vk_dispatch_table_helper.h",
58 "vk_enum_string_helper.h",
59 "vk_extension_helper.h",
60 "vk_layer_dispatch_table.h",
61 "vk_object_types.h",
62 "vk_safe_struct.cpp",
63 "vk_safe_struct.h",
64 "vk_typemap_helper.h"]],
65 [script_relative('vk_validation_stats.py'),
66 os.path.abspath(os.path.join(args.registry, 'validusage.json')),
67 '-export_header'],
68 [script_relative('external_revision_generator.py'),
69 '--rev_file', script_relative('known_good.json'),
70 '-s', 'SPIRV_TOOLS_COMMIT_ID',
71 '-o', 'spirv_tools_commit_id.h']]
72
73 repo_dir = script_relative('../layers/generated')
74
75 # get directory where generators will run
76 if args.verify or args.incremental:
77 # generate in temp directory so we can compare or copy later
78 temp_obj = tempfile.TemporaryDirectory(prefix='VulkanVL_generated_source_')
79 temp_dir = temp_obj.name
80 gen_dir = temp_dir
81 else:
82 # generate directly in the repo
83 gen_dir = repo_dir
84
85 # run each code generator
86 for cmd in gen_cmds:
87 print(' '.join(cmd))
88 try:
89 subprocess.check_call([sys.executable] + cmd,
90 # ignore generator output, vk_validation_stats.py is especially noisy
91 stdout=subprocess.DEVNULL,
92 cwd=gen_dir)
93 except Exception as e:
94 print('ERROR:', str(e))
95 return 1
96
97 # optional post-generation steps
98 if args.verify:
99 # compare contents of temp dir and repo
100 temp_files = set(os.listdir(temp_dir))
101 repo_files = set(os.listdir(repo_dir))
102 files_match = True
103 for filename in sorted((temp_files | repo_files) - set(verify_exclude)):
104 if filename not in repo_files:
105 print('ERROR: Missing repo file', filename)
106 files_match = False
107 elif filename not in temp_files:
108 print('ERROR: Missing generator for', filename)
109 files_match = False
110 elif not filecmp.cmp(os.path.join(temp_dir, filename),
111 os.path.join(repo_dir, filename),
112 shallow=False):
113 print('ERROR: Repo files do not match generator output for', filename)
114 files_match = False
115
116 # return code for test scripts
117 if files_match:
118 print('SUCCESS: Repo files match generator output')
119 return 0
120 return 1
121
122 elif args.incremental:
123 # copy missing or differing files from temp directory to repo
124 for filename in os.listdir(temp_dir):
125 temp_filename = os.path.join(temp_dir, filename)
126 repo_filename = os.path.join(repo_dir, filename)
127 if not os.path.exists(repo_filename) or \
128 not filecmp.cmp(temp_filename, repo_filename, shallow=False):
129 print('update', repo_filename)
130 shutil.copyfile(temp_filename, repo_filename)
131
132 return 0
133
134if __name__ == '__main__':
135 sys.exit(main(sys.argv[1:]))
136