| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# Copyright 2013-2023 The Khronos Group Inc. |
| 4 |
# Copyright 2023-2024 Google Inc. |
| 5 |
# |
| 6 |
# SPDX-License-Identifier: Apache-2.0 |
| 7 |
|
| 8 |
import argparse |
| 9 |
import os |
| 10 |
import re |
| 11 |
import sys |
| 12 |
import xml.etree.ElementTree as etree |
| 13 |
|
| 14 |
sys.path.append(os.path.abspath(os.path.dirname(__file__))) |
| 15 |
|
| 16 |
from cgenerator import CGeneratorOptions, COutputGenerator |
| 17 |
|
| 18 |
from generator import write |
| 19 |
from reg import Registry |
| 20 |
|
| 21 |
# gfxstream + cereal modules |
| 22 |
from cerealgenerator import CerealGenerator |
| 23 |
|
| 24 |
from typing import Optional |
| 25 |
|
| 26 |
def makeREstring(strings, default=None, strings_are_regex=False): |
| 27 |
"""Turn a list of strings into a regexp string matching exactly those strings.""" |
| 28 |
if strings or default is None: |
| 29 |
if not strings_are_regex: |
| 30 |
strings = (re.escape(s) for s in strings) |
| 31 |
return '^(' + '|'.join(strings) + ')$' |
| 32 |
return default |
| 33 |
|
| 34 |
|
| 35 |
def makeGenOpts(args): |
| 36 |
"""Returns a directory of [ generator function, generator options ] indexed |
| 37 |
by specified short names. The generator options incorporate the following |
| 38 |
parameters: |
| 39 |
|
| 40 |
args is an parsed argument object; see below for the fields that are used.""" |
| 41 |
global genOpts |
| 42 |
genOpts = {} |
| 43 |
|
| 44 |
# Output target directory |
| 45 |
directory = args.directory |
| 46 |
|
| 47 |
# Descriptive names for various regexp patterns used to select |
| 48 |
# versions and extensions |
| 49 |
allFormats = allFeatures = allExtensions = r'.*' |
| 50 |
|
| 51 |
# Turn lists of names/patterns into matching regular expressions |
| 52 |
emitExtensionsPat = makeREstring([], allExtensions) |
| 53 |
emitFormatsPat = makeREstring([], allFormats) |
| 54 |
featuresPat = makeREstring([], allFeatures) |
| 55 |
|
| 56 |
# Copyright text prefixing all headers (list of strings). |
| 57 |
# The SPDX formatting below works around constraints of the 'reuse' tool |
| 58 |
prefixStrings = [ |
| 59 |
'/*', |
| 60 |
'** Copyright 2025 The Android Open Source Project', |
| 61 |
'** Copyright 2015-2023 The Khronos Group Inc.', |
| 62 |
'**', |
| 63 |
'** SPDX-License-Identifier' + ': Apache-2.0', |
| 64 |
'*/', |
| 65 |
'' |
| 66 |
] |
| 67 |
|
| 68 |
# Text specific to Vulkan headers |
| 69 |
vkPrefixStrings = [ |
| 70 |
'/*', |
| 71 |
'** This header is generated from the Khronos Vulkan XML API Registry.', |
| 72 |
'**', |
| 73 |
'*/', |
| 74 |
'' |
| 75 |
] |
| 76 |
|
| 77 |
genOpts['cereal'] = [ |
| 78 |
CerealGenerator, |
| 79 |
CGeneratorOptions( |
| 80 |
directory = directory, |
| 81 |
versions = featuresPat, |
| 82 |
emitversions = featuresPat, |
| 83 |
addExtensions = None, |
| 84 |
emitExtensions = emitExtensionsPat, |
| 85 |
prefixText = prefixStrings + vkPrefixStrings, |
| 86 |
apientry = 'VKAPI_CALL ', |
| 87 |
apientryp = 'VKAPI_PTR *', |
| 88 |
alignFuncParam = 48) |
| 89 |
] |
| 90 |
|
| 91 |
gfxstreamPrefixStrings = [ |
| 92 |
'#pragma once', |
| 93 |
'#ifdef VK_GFXSTREAM_STRUCTURE_TYPE_EXT', |
| 94 |
'#include "vulkan_gfxstream_structure_type.h"', |
| 95 |
'#endif', |
| 96 |
] |
| 97 |
|
| 98 |
# gfxstream specific header |
| 99 |
genOpts['vulkan_gfxstream.h'] = [ |
| 100 |
COutputGenerator, |
| 101 |
CGeneratorOptions( |
| 102 |
filename = 'vulkan_gfxstream.h', |
| 103 |
directory = directory, |
| 104 |
versions = featuresPat, |
| 105 |
emitversions = None, |
| 106 |
addExtensions = makeREstring(['VK_GOOGLE_gfxstream'], None), |
| 107 |
emitExtensions = makeREstring(['VK_GOOGLE_gfxstream'], None), |
| 108 |
prefixText = prefixStrings + vkPrefixStrings + gfxstreamPrefixStrings, |
| 109 |
# Use #pragma once in the prefixText instead, so that we can put the copyright comments |
| 110 |
# at the beginning of the file. |
| 111 |
apientry = 'VKAPI_CALL ', |
| 112 |
apientryp = 'VKAPI_PTR *', |
| 113 |
alignFuncParam = 48) |
| 114 |
] |
| 115 |
|
| 116 |
def genTarget(args): |
| 117 |
"""Create an API generator and corresponding generator options based on |
| 118 |
the requested target and command line options. |
| 119 |
|
| 120 |
This is encapsulated in a function so it can be profiled and/or timed. |
| 121 |
The args parameter is an parsed argument object containing the following |
| 122 |
fields that are used: |
| 123 |
|
| 124 |
- target - target to generate |
| 125 |
- directory - directory to generate it in |
| 126 |
- extensions - list of additional extensions to include in generated interfaces""" |
| 127 |
|
| 128 |
# Create generator options with parameters specified on command line |
| 129 |
makeGenOpts(args) |
| 130 |
|
| 131 |
# Select a generator matching the requested target |
| 132 |
if args.target in genOpts: |
| 133 |
createGenerator = genOpts[args.target][0] |
| 134 |
options = genOpts[args.target][1] |
| 135 |
|
| 136 |
gen = createGenerator(errFile=errWarn, |
| 137 |
warnFile=errWarn, |
| 138 |
diagFile=diag) |
| 139 |
return (gen, options) |
| 140 |
else: |
| 141 |
return None |
| 142 |
|
| 143 |
|
| 144 |
# -feature name |
| 145 |
# -extension name |
| 146 |
# For both, "name" may be a single name, or a space-separated list |
| 147 |
# of names, or a regular expression. |
| 148 |
if __name__ == '__main__': |
| 149 |
parser = argparse.ArgumentParser() |
| 150 |
parser.add_argument('-registry', action='store', |
| 151 |
default='vk.xml', |
| 152 |
help='Use specified registry file instead of vk.xml') |
| 153 |
parser.add_argument('-registryGfxstream', action='store', |
| 154 |
default=None, |
| 155 |
help='Use specified gfxstream registry file') |
| 156 |
parser.add_argument('-o', action='store', dest='directory', |
| 157 |
default='.', |
| 158 |
help='Create target and related files in specified directory') |
| 159 |
parser.add_argument('target', metavar='target', nargs='?', |
| 160 |
help='Specify target') |
| 161 |
|
| 162 |
args = parser.parse_args() |
| 163 |
|
| 164 |
errWarn = sys.stderr |
| 165 |
diag = None |
| 166 |
|
| 167 |
# Create the API generator & generator options |
| 168 |
(gen, options) = genTarget(args) |
| 169 |
|
| 170 |
# Create the registry object with the specified generator and generator |
| 171 |
# options. The options are set before XML loading as they may affect it. |
| 172 |
reg = Registry(gen, options) |
| 173 |
|
| 174 |
# Parse the specified registry XML into an ElementTree object |
| 175 |
tree = etree.parse(args.registry) |
| 176 |
|
| 177 |
# Merge the gfxstream registry with the official Vulkan registry if the |
| 178 |
# target is the cereal generator |
| 179 |
if args.registryGfxstream is not None and args.target == 'cereal': |
| 180 |
treeGfxstream = etree.parse(args.registryGfxstream) |
| 181 |
treeRoot = tree.getroot() |
| 182 |
treeGfxstreamRoot = treeGfxstream.getroot() |
| 183 |
|
| 184 |
def getEntryName(entry) -> Optional[str]: |
| 185 |
name = entry.get("name") |
| 186 |
if name is not None: |
| 187 |
return name |
| 188 |
try: |
| 189 |
return entry.find("proto").find("name").text |
| 190 |
except AttributeError: |
| 191 |
return None |
| 192 |
|
| 193 |
for entriesName in ['types', 'commands', 'extensions']: |
| 194 |
treeEntries = treeRoot.find(entriesName) |
| 195 |
|
| 196 |
originalEntryDict = {} |
| 197 |
for entry in treeEntries: |
| 198 |
name = getEntryName(entry) |
| 199 |
if name is not None: |
| 200 |
originalEntryDict[name] = entry |
| 201 |
|
| 202 |
for entry in treeGfxstreamRoot.find(entriesName): |
| 203 |
name = getEntryName(entry) |
| 204 |
# New entry, just append to entry list |
| 205 |
if name not in originalEntryDict.keys(): |
| 206 |
treeEntries.append(entry) |
| 207 |
continue |
| 208 |
|
| 209 |
originalEntry = originalEntryDict[name] |
| 210 |
|
| 211 |
# Extending an existing entry. This happens for MVK. |
| 212 |
if entriesName == "extensions": |
| 213 |
for key, value in entry.attrib.items(): |
| 214 |
originalEntry.set(key, value) |
| 215 |
require = entry.find("require") |
| 216 |
if require is not None: |
| 217 |
for child in require: |
| 218 |
originalEntry.find("require").append(child) |
| 219 |
continue |
| 220 |
|
| 221 |
# Overwriting an existing entry. This happen for |
| 222 |
# VkNativeBufferANDROID |
| 223 |
if entriesName == "types" or entriesName == "commands": |
| 224 |
originalEntry.clear() |
| 225 |
originalEntry.attrib = entry.attrib |
| 226 |
for child in entry: |
| 227 |
originalEntry.append(child) |
| 228 |
|
| 229 |
# Load the XML tree into the registry object |
| 230 |
reg.loadElementTree(tree) |
| 231 |
reg.apiGen() |