| 1 |
#!/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 |
# Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 7 |
# Copyright (c) 2023-2023 RasterGrid Kft. |
| 8 |
# |
| 9 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 |
# you may not use this file except in compliance with the License. |
| 11 |
# You may obtain a copy of the License at |
| 12 |
# |
| 13 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
# |
| 15 |
# Unless required by applicable law or agreed to in writing, software |
| 16 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
# See the License for the specific language governing permissions and |
| 19 |
# limitations under the License. |
| 20 |
# |
| 21 |
# Author: Mike Schuchardt <[email protected]> |
| 22 |
|
| 23 |
import argparse |
| 24 |
import filecmp |
| 25 |
import os |
| 26 |
import subprocess |
| 27 |
import shutil |
| 28 |
import sys |
| 29 |
import tempfile |
| 30 |
import datetime |
| 31 |
import re |
| 32 |
import common_codegen |
| 33 |
|
| 34 |
from xml.etree import ElementTree |
| 35 |
|
| 36 |
# Because we have special logic to import the Registry from input arguments and the BaseGenerator comes from the registry, we have to delay defining it until *after* |
| 37 |
# the Registry has been imported. Yes this is awkward, but it was the least awkward way to make --verify work. |
| 38 |
generators = {} |
| 39 |
|
| 40 |
def RunGenerators(api: str, registry: str, directory: str, styleFile: str, targetFilter: str, flatOutput: bool): |
| 41 |
|
| 42 |
try: |
| 43 |
common_codegen.RunShellCmd(f'clang-format --version') |
| 44 |
has_clang_format = True |
| 45 |
except: |
| 46 |
has_clang_format = False |
| 47 |
if not has_clang_format: |
| 48 |
print("WARNING: Unable to find clang-format!") |
| 49 |
|
| 50 |
# These live in the Vulkan-Docs repo, but are pulled in via the |
| 51 |
# Vulkan-Headers/registry folder |
| 52 |
# At runtime we inject python path to find these helper scripts |
| 53 |
scripts = os.path.dirname(registry) |
| 54 |
scripts_directory_path = os.path.dirname(os.path.abspath(__file__)) |
| 55 |
registry_headers_path = os.path.join(scripts_directory_path, scripts) |
| 56 |
sys.path.insert(0, registry_headers_path) |
| 57 |
try: |
| 58 |
from reg import Registry |
| 59 |
except: |
| 60 |
print("ModuleNotFoundError: No module named 'reg'") # normal python error message |
| 61 |
print(f'{registry_headers_path} is not pointing to the Vulkan-Headers registry directory.') |
| 62 |
print("Inside Vulkan-Headers there is a registry/reg.py file that is used.") |
| 63 |
sys.exit(1) # Return without call stack so easy to spot error |
| 64 |
|
| 65 |
from base_generator import BaseGeneratorOptions |
| 66 |
from generators.dispatch_table_helper_generator import DispatchTableHelperGenerator |
| 67 |
from generators.helper_file_generator import HelperFileGenerator |
| 68 |
from generators.loader_extension_generator import LoaderExtensionGenerator |
| 69 |
from generators.vk_result_to_string_generator import VkResultToStringGenerator |
| 70 |
|
| 71 |
# These set fields that are needed by both OutputGenerator and BaseGenerator, |
| 72 |
# but are uniform and don't need to be set at a per-generated file level |
| 73 |
from base_generator import (SetTargetApiName, SetMergedApiNames) |
| 74 |
SetTargetApiName(api) |
| 75 |
|
| 76 |
# Generated directory and dispatch table helper file name may be API specific (e.g. Vulkan SC) |
| 77 |
generated_directory = 'loader/generated' |
| 78 |
dispatch_table_helper_filename = 'vk_dispatch_table_helper.h' |
| 79 |
result_to_string_filename = 'vk_result_to_string_helper.h' |
| 80 |
|
| 81 |
generators.update({ |
| 82 |
'vk_layer_dispatch_table.h': { |
| 83 |
'generator' : LoaderExtensionGenerator, |
| 84 |
'genCombined': False, |
| 85 |
'directory' : generated_directory, |
| 86 |
}, |
| 87 |
'vk_loader_extensions.c': { |
| 88 |
'generator' : LoaderExtensionGenerator, |
| 89 |
'genCombined': False, |
| 90 |
'directory' : generated_directory, |
| 91 |
}, |
| 92 |
'vk_loader_extensions.h': { |
| 93 |
'generator' : LoaderExtensionGenerator, |
| 94 |
'genCombined': False, |
| 95 |
'directory' : generated_directory, |
| 96 |
}, |
| 97 |
'vk_object_types.h': { |
| 98 |
'generator' : HelperFileGenerator, |
| 99 |
'genCombined': True, |
| 100 |
'directory' : generated_directory, |
| 101 |
}, |
| 102 |
f'{dispatch_table_helper_filename}': { |
| 103 |
'generator' : DispatchTableHelperGenerator, |
| 104 |
'genCombined': False, |
| 105 |
'directory' : 'tests/framework/layer/generated', |
| 106 |
}, |
| 107 |
f'{result_to_string_filename}': { |
| 108 |
'generator' : VkResultToStringGenerator, |
| 109 |
'genCombined': False, |
| 110 |
'directory' : 'tests/framework/generated', |
| 111 |
} |
| 112 |
}) |
| 113 |
|
| 114 |
unknownTargets = [x for x in (targetFilter if targetFilter else []) if x not in generators.keys()] |
| 115 |
if unknownTargets: |
| 116 |
print(f'ERROR: No generator options for unknown target(s): {", ".join(unknownTargets)}', file=sys.stderr) |
| 117 |
return 1 |
| 118 |
|
| 119 |
# Filter if --target is passed in |
| 120 |
targets = [x for x in generators.keys() if not targetFilter or x in targetFilter] |
| 121 |
|
| 122 |
for index, target in enumerate(targets, start=1): |
| 123 |
print(f'[{index}|{len(targets)}] Generating {target}') |
| 124 |
|
| 125 |
# First grab a class constructor object and create an instance |
| 126 |
generator = generators[target]['generator'] |
| 127 |
gen = generator() |
| 128 |
|
| 129 |
# This code and the 'genCombined' generator metadata is used by downstream |
| 130 |
# users to generate code with all Vulkan APIs merged into the target API variant |
| 131 |
# (e.g. Vulkan SC) when needed. The constructed apiList is also used to filter |
| 132 |
# out non-applicable extensions later below. |
| 133 |
apiList = [api] |
| 134 |
if api != 'vulkan' and generators[target]['genCombined']: |
| 135 |
SetMergedApiNames('vulkan') |
| 136 |
apiList.append('vulkan') |
| 137 |
else: |
| 138 |
SetMergedApiNames(None) |
| 139 |
|
| 140 |
# For people who want to generate all the files in a single director |
| 141 |
if flatOutput: |
| 142 |
outDirectory = os.path.abspath(os.path.join(directory)) |
| 143 |
else: |
| 144 |
outDirectory = os.path.abspath(os.path.join(directory, generators[target]['directory'])) |
| 145 |
|
| 146 |
options = BaseGeneratorOptions( |
| 147 |
customFileName = target, |
| 148 |
customDirectory = outDirectory) |
| 149 |
|
| 150 |
if not os.path.exists(outDirectory): |
| 151 |
os.makedirs(outDirectory) |
| 152 |
|
| 153 |
# Create the registry object with the specified generator and generator |
| 154 |
# options. The options are set before XML loading as they may affect it. |
| 155 |
reg = Registry(gen, options) |
| 156 |
|
| 157 |
# Parse the specified registry XML into an ElementTree object |
| 158 |
tree = ElementTree.parse(registry) |
| 159 |
|
| 160 |
# Load the XML tree into the registry object |
| 161 |
reg.loadElementTree(tree) |
| 162 |
|
| 163 |
# Finally, use the output generator to create the requested target |
| 164 |
reg.apiGen() |
| 165 |
|
| 166 |
# Run clang-format on the file |
| 167 |
if has_clang_format and styleFile: |
| 168 |
common_codegen.RunShellCmd(f'clang-format -i --style=file:{styleFile} {os.path.join(outDirectory, target)}') |
| 169 |
|
| 170 |
|
| 171 |
def main(argv): |
| 172 |
|
| 173 |
# files to exclude from --verify check |
| 174 |
verify_exclude = [] # None currently |
| 175 |
|
| 176 |
|
| 177 |
parser = argparse.ArgumentParser(description='Generate source code for this repository') |
| 178 |
parser.add_argument('registry', metavar='REGISTRY_PATH', help='path to the Vulkan-Headers registry directory') |
| 179 |
parser.add_argument('--api', |
| 180 |
default='vulkan', |
| 181 |
choices=['vulkan'], |
| 182 |
help='Specify API name to generate') |
| 183 |
parser.add_argument('--generated-version', help='sets the header version used to generate the repo') |
| 184 |
group = parser.add_mutually_exclusive_group() |
| 185 |
group.add_argument('--target', nargs='+', help='only generate file names passed in') |
| 186 |
group.add_argument('-i', '--incremental', action='store_true', help='only update repo files that change') |
| 187 |
group.add_argument('-v', '--verify', action='store_true', help='verify repo files match generator output') |
| 188 |
group.add_argument('-o', action='store', dest='directory', help='Create target and related files in specified directory') |
| 189 |
args = parser.parse_args(argv) |
| 190 |
|
| 191 |
repo_dir = common_codegen.repo_relative('.') |
| 192 |
|
| 193 |
registry = os.path.abspath(os.path.join(args.registry, 'vk.xml')) |
| 194 |
if not os.path.isfile(registry): |
| 195 |
registry = os.path.abspath(os.path.join(args.registry, 'Vulkan-Headers/registry/vk.xml')) |
| 196 |
if not os.path.isfile(registry): |
| 197 |
print(f'cannot find vk.xml in {args.registry}') |
| 198 |
return -1 |
| 199 |
|
| 200 |
# Need pass style file incase running with --verify and it can't find the file automatically in the temp directory |
| 201 |
style_file = os.path.join(repo_dir, '.clang-format') |
| 202 |
|
| 203 |
# get directory where generators will run |
| 204 |
if args.verify or args.incremental: |
| 205 |
# generate in temp directory so we can compare or copy later |
| 206 |
temp_obj = tempfile.TemporaryDirectory(prefix='loader_codegen_') |
| 207 |
temp_dir = temp_obj.name |
| 208 |
gen_dir = temp_dir |
| 209 |
elif args.directory: |
| 210 |
gen_dir = args.directory |
| 211 |
else: |
| 212 |
# generate directly in the repo |
| 213 |
gen_dir = repo_dir |
| 214 |
|
| 215 |
RunGenerators(api=args.api,registry=registry, directory=gen_dir, styleFile=style_file, targetFilter=args.target, flatOutput=False) |
| 216 |
|
| 217 |
# optional post-generation steps |
| 218 |
if args.verify: |
| 219 |
|
| 220 |
# compare contents of temp dir and repo |
| 221 |
temp_files = {} |
| 222 |
repo_files = {} |
| 223 |
for details in generators.values(): |
| 224 |
if details['directory'] not in temp_files: |
| 225 |
temp_files[details['directory']] = set() |
| 226 |
temp_files[details['directory']].update(set(os.listdir(os.path.join(temp_dir, details['directory'])))) |
| 227 |
if details['directory'] not in repo_files: |
| 228 |
repo_files[details['directory']] = set() |
| 229 |
repo_files[details['directory']].update(set(os.listdir(os.path.join(repo_dir, details['directory']))) - set(verify_exclude)) |
| 230 |
|
| 231 |
# compare contents of temp dir and repo |
| 232 |
files_match = True |
| 233 |
for filename, details in generators.items(): |
| 234 |
if filename not in repo_files[details['directory']]: |
| 235 |
print('ERROR: Missing repo file', filename) |
| 236 |
files_match = False |
| 237 |
elif filename not in temp_files[details['directory']]: |
| 238 |
print('ERROR: Missing generator for', filename) |
| 239 |
files_match = False |
| 240 |
elif not filecmp.cmp(os.path.join(temp_dir, details['directory'], filename), |
| 241 |
os.path.join(repo_dir, details['directory'], filename), |
| 242 |
shallow=False): |
| 243 |
print('ERROR: Repo files do not match generator output for', filename) |
| 244 |
files_match = False |
| 245 |
|
| 246 |
# return code for test scripts |
| 247 |
if files_match: |
| 248 |
print('SUCCESS: Repo files match generator output') |
| 249 |
return 0 |
| 250 |
return 1 |
| 251 |
|
| 252 |
elif args.incremental: |
| 253 |
# copy missing or differing files from temp directory to repo |
| 254 |
for filename, details in generators.items(): |
| 255 |
temp_filename = os.path.join(temp_dir, details['directory'], filename) |
| 256 |
repo_filename = os.path.join(repo_dir, details['directory'], filename) |
| 257 |
if not os.path.exists(repo_filename) or \ |
| 258 |
not filecmp.cmp(temp_filename, repo_filename, shallow=False): |
| 259 |
print('update', repo_filename) |
| 260 |
shutil.copyfile(temp_filename, repo_filename) |
| 261 |
|
| 262 |
# write out the header version used to generate the code to a checked in CMake file |
| 263 |
if args.generated_version: |
| 264 |
# Update the CMake project version |
| 265 |
with open(common_codegen.repo_relative('CMakeLists.txt'), "r+") as f: |
| 266 |
data = f.read() |
| 267 |
f.seek(0) |
| 268 |
f.write(re.sub("project.*VERSION.*", f"project(VULKAN_LOADER VERSION {args.generated_version} LANGUAGES C)", data)) |
| 269 |
f.truncate() |
| 270 |
|
| 271 |
with open(common_codegen.repo_relative('loader/loader.rc.in'), "r") as rc_file: |
| 272 |
rc_file_contents = rc_file.read() |
| 273 |
rc_ver = ', '.join(args.generated_version.split('.') + ['0']) |
| 274 |
rc_file_contents = rc_file_contents.replace('${LOADER_VER_FILE_VERSION}', f'{rc_ver}') |
| 275 |
rc_file_contents = rc_file_contents.replace('${LOADER_VER_FILE_DESCRIPTION_STR}', f'"{args.generated_version}.Dev Build"') |
| 276 |
rc_file_contents = rc_file_contents.replace('${LOADER_VER_FILE_VERSION_STR}', f'"Vulkan Loader - Dev Build"') |
| 277 |
rc_file_contents = rc_file_contents.replace('${LOADER_CUR_COPYRIGHT_YEAR}', f'{datetime.date.today().year}') |
| 278 |
with open(common_codegen.repo_relative('loader/loader.rc'), "w") as rc_file_out: |
| 279 |
rc_file_out.write(rc_file_contents) |
| 280 |
rc_file_out.close() |
| 281 |
|
| 282 |
return 0 |
| 283 |
|
| 284 |
if __name__ == '__main__': |
| 285 |
sys.exit(main(sys.argv[1:])) |
| 286 |
|