Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 -i |
| 2 | # |
| 3 | # Copyright (c) 2015-2016 The Khronos Group Inc. |
| 4 | # Copyright (c) 2015-2016 Valve Corporation |
| 5 | # Copyright (c) 2015-2016 LunarG, Inc. |
| 6 | # Copyright (c) 2015-2016 Google Inc. |
| 7 | # |
| 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: Mark Lobodzinski <mark@lunarg.com> |
| 21 | |
| 22 | import os,re,sys |
| 23 | import xml.etree.ElementTree as etree |
| 24 | from generator import * |
| 25 | from collections import namedtuple |
| 26 | |
| 27 | # |
| 28 | # HelperFileOutputGeneratorOptions - subclass of GeneratorOptions. |
| 29 | class HelperFileOutputGeneratorOptions(GeneratorOptions): |
| 30 | def __init__(self, |
| 31 | filename = None, |
| 32 | directory = '.', |
| 33 | apiname = None, |
| 34 | profile = None, |
| 35 | versions = '.*', |
| 36 | emitversions = '.*', |
| 37 | defaultExtensions = None, |
| 38 | addExtensions = None, |
| 39 | removeExtensions = None, |
| 40 | sortProcedure = regSortFeatures, |
| 41 | prefixText = "", |
| 42 | genFuncPointers = True, |
| 43 | protectFile = True, |
| 44 | protectFeature = True, |
| 45 | protectProto = None, |
| 46 | protectProtoStr = None, |
| 47 | apicall = '', |
| 48 | apientry = '', |
| 49 | apientryp = '', |
| 50 | alignFuncParam = 0, |
| 51 | library_name = '', |
| 52 | helper_file_type = ''): |
| 53 | GeneratorOptions.__init__(self, filename, directory, apiname, profile, |
| 54 | versions, emitversions, defaultExtensions, |
| 55 | addExtensions, removeExtensions, sortProcedure) |
| 56 | self.prefixText = prefixText |
| 57 | self.genFuncPointers = genFuncPointers |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 58 | self.protectFile = protectFile |
| 59 | self.protectFeature = protectFeature |
| 60 | self.protectProto = protectProto |
| 61 | self.protectProtoStr = protectProtoStr |
| 62 | self.apicall = apicall |
| 63 | self.apientry = apientry |
| 64 | self.apientryp = apientryp |
| 65 | self.alignFuncParam = alignFuncParam |
| 66 | self.library_name = library_name |
| 67 | self.helper_file_type = helper_file_type |
| 68 | # |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 69 | # HelperFileOutputGenerator - subclass of OutputGenerator. Outputs Vulkan helper files |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 70 | class HelperFileOutputGenerator(OutputGenerator): |
| 71 | """Generate Windows def file based on XML element attributes""" |
| 72 | def __init__(self, |
| 73 | errFile = sys.stderr, |
| 74 | warnFile = sys.stderr, |
| 75 | diagFile = sys.stdout): |
| 76 | OutputGenerator.__init__(self, errFile, warnFile, diagFile) |
| 77 | # Internal state - accumulators for different inner block text |
Mark Lobodzinski | 5380d13 | 2016-12-28 14:45:34 -0700 | [diff] [blame] | 78 | self.enum_output = '' # string built up of enum string routines |
| 79 | self.struct_size_h_output = '' # string built up of struct size header output |
| 80 | self.struct_size_c_output = '' # string built up of struct size source output |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 81 | # Internal state - accumulators for different inner block text |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 82 | self.structNames = [] # List of Vulkan struct typenames |
| 83 | self.structTypes = dict() # Map of Vulkan struct typename to required VkStructureType |
| 84 | self.handleTypes = set() # Set of handle type names |
| 85 | self.commands = [] # List of CommandData records for all Vulkan commands |
| 86 | self.structMembers = [] # List of StructMemberData records for all Vulkan structs |
| 87 | self.flags = set() # Map of flags typenames |
| 88 | # Named tuples to store struct and command data |
| 89 | self.StructType = namedtuple('StructType', ['name', 'value']) |
| 90 | self.CommandParam = namedtuple('CommandParam', ['type', 'name', 'ispointer', 'isconst', 'iscount', 'len', 'extstructs', 'cdecl', 'islocal', 'iscreate', 'isdestroy']) |
| 91 | self.CommandData = namedtuple('CommandData', ['name', 'return_type', 'params', 'cdecl']) |
Mark Lobodzinski | 5380d13 | 2016-12-28 14:45:34 -0700 | [diff] [blame] | 92 | self.StructMemberData = namedtuple('StructMemberData', ['name', 'members', 'ifdef_protect']) |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 93 | # |
| 94 | # Called once at the beginning of each run |
| 95 | def beginFile(self, genOpts): |
| 96 | OutputGenerator.beginFile(self, genOpts) |
| 97 | # User-supplied prefix text, if any (list of strings) |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 98 | self.helper_file_type = genOpts.helper_file_type |
| 99 | self.library_name = genOpts.library_name |
| 100 | # File Comment |
| 101 | file_comment = '// *** THIS FILE IS GENERATED - DO NOT EDIT ***\n' |
| 102 | file_comment += '// See helper_file_generator.py for modifications\n' |
| 103 | write(file_comment, file=self.outFile) |
| 104 | # Copyright Notice |
| 105 | copyright = '' |
| 106 | copyright += '\n' |
| 107 | copyright += '/***************************************************************************\n' |
| 108 | copyright += ' *\n' |
| 109 | copyright += ' * Copyright (c) 2015-2016 The Khronos Group Inc.\n' |
| 110 | copyright += ' * Copyright (c) 2015-2016 Valve Corporation\n' |
| 111 | copyright += ' * Copyright (c) 2015-2016 LunarG, Inc.\n' |
| 112 | copyright += ' * Copyright (c) 2015-2016 Google Inc.\n' |
| 113 | copyright += ' *\n' |
| 114 | copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n' |
| 115 | copyright += ' * you may not use this file except in compliance with the License.\n' |
| 116 | copyright += ' * You may obtain a copy of the License at\n' |
| 117 | copyright += ' *\n' |
| 118 | copyright += ' * http://www.apache.org/licenses/LICENSE-2.0\n' |
| 119 | copyright += ' *\n' |
| 120 | copyright += ' * Unless required by applicable law or agreed to in writing, software\n' |
| 121 | copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n' |
| 122 | copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' |
| 123 | copyright += ' * See the License for the specific language governing permissions and\n' |
| 124 | copyright += ' * limitations under the License.\n' |
| 125 | copyright += ' *\n' |
| 126 | copyright += ' * Author: Mark Lobodzinski <mark@lunarg.com>\n' |
Mark Lobodzinski | a9c963d | 2016-12-28 07:45:35 -0700 | [diff] [blame] | 127 | copyright += ' * Author: Courtney Goeltzenleuchter <courtneygo@google.com>\n' |
| 128 | copyright += ' * Author: Tobin Ehlis <tobine@google.com>\n' |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 129 | copyright += ' *\n' |
| 130 | copyright += ' ****************************************************************************/\n' |
| 131 | write(copyright, file=self.outFile) |
| 132 | # |
| 133 | # Write generate and write def file content to output file |
| 134 | def endFile(self): |
| 135 | dest_file = '' |
| 136 | dest_file += self.OutputDestFile() |
| 137 | write(dest_file, file=self.outFile); |
| 138 | # Finish processing in superclass |
| 139 | OutputGenerator.endFile(self) |
| 140 | # |
| 141 | # Grab group (e.g. C "enum" type) info to output for enum-string conversion helper |
| 142 | def genGroup(self, groupinfo, groupName): |
| 143 | OutputGenerator.genGroup(self, groupinfo, groupName) |
| 144 | groupElem = groupinfo.elem |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 145 | # For enum_string_header |
| 146 | if self.helper_file_type == 'enum_string_header': |
Mark Lobodzinski | 824ce64 | 2016-12-28 08:49:46 -0700 | [diff] [blame] | 147 | value_list = [] |
| 148 | for elem in groupElem.findall('enum'): |
| 149 | if elem.get('supported') != 'disabled': |
| 150 | item_name = elem.get('name') |
| 151 | value_list.append(item_name) |
| 152 | if value_list is not None: |
| 153 | self.enum_output += self.GenerateEnumStringConversion(groupName, value_list) |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 154 | # |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 155 | # Called for each type -- if the type is a struct/union, grab the metadata |
| 156 | def genType(self, typeinfo, name): |
| 157 | OutputGenerator.genType(self, typeinfo, name) |
| 158 | typeElem = typeinfo.elem |
| 159 | # If the type is a struct type, traverse the imbedded <member> tags generating a structure. |
| 160 | # Otherwise, emit the tag text. |
| 161 | category = typeElem.get('category') |
| 162 | if (category == 'struct' or category == 'union'): |
| 163 | self.structNames.append(name) |
| 164 | self.genStruct(typeinfo, name) |
| 165 | # |
| 166 | # Generate a VkStructureType based on a structure typename |
| 167 | def genVkStructureType(self, typename): |
| 168 | # Add underscore between lowercase then uppercase |
| 169 | value = re.sub('([a-z0-9])([A-Z])', r'\1_\2', typename) |
| 170 | # Change to uppercase |
| 171 | value = value.upper() |
| 172 | # Add STRUCTURE_TYPE_ |
| 173 | return re.sub('VK_', 'VK_STRUCTURE_TYPE_', value) |
| 174 | # |
| 175 | # Check if the parameter passed in is a pointer |
| 176 | def paramIsPointer(self, param): |
| 177 | ispointer = False |
| 178 | for elem in param: |
| 179 | if ((elem.tag is not 'type') and (elem.tail is not None)) and '*' in elem.tail: |
| 180 | ispointer = True |
| 181 | return ispointer |
| 182 | # |
| 183 | # Retrieve the type and name for a parameter |
| 184 | def getTypeNameTuple(self, param): |
| 185 | type = '' |
| 186 | name = '' |
| 187 | for elem in param: |
| 188 | if elem.tag == 'type': |
| 189 | type = noneStr(elem.text) |
| 190 | elif elem.tag == 'name': |
| 191 | name = noneStr(elem.text) |
| 192 | return (type, name) |
| 193 | # |
| 194 | # Retrieve the value of the len tag |
| 195 | def getLen(self, param): |
| 196 | result = None |
| 197 | len = param.attrib.get('len') |
| 198 | if len and len != 'null-terminated': |
| 199 | # For string arrays, 'len' can look like 'count,null-terminated', indicating that we |
| 200 | # have a null terminated array of strings. We strip the null-terminated from the |
| 201 | # 'len' field and only return the parameter specifying the string count |
| 202 | if 'null-terminated' in len: |
| 203 | result = len.split(',')[0] |
| 204 | else: |
| 205 | result = len |
| 206 | # Spec has now notation for len attributes, using :: instead of platform specific pointer symbol |
| 207 | result = str(result).replace('::', '->') |
| 208 | return result |
| 209 | # |
| 210 | # Generate local ready-access data describing Vulkan structures and unions from the XML metadata |
| 211 | def genStruct(self, typeinfo, typeName): |
| 212 | OutputGenerator.genStruct(self, typeinfo, typeName) |
| 213 | members = typeinfo.elem.findall('.//member') |
| 214 | # Iterate over members once to get length parameters for arrays |
| 215 | lens = set() |
| 216 | for member in members: |
| 217 | len = self.getLen(member) |
| 218 | if len: |
| 219 | lens.add(len) |
| 220 | # Generate member info |
| 221 | membersInfo = [] |
| 222 | for member in members: |
| 223 | # Get the member's type and name |
| 224 | info = self.getTypeNameTuple(member) |
| 225 | type = info[0] |
| 226 | name = info[1] |
| 227 | cdecl = self.makeCParamDecl(member, 0) |
| 228 | # Process VkStructureType |
| 229 | if type == 'VkStructureType': |
| 230 | # Extract the required struct type value from the comments |
| 231 | # embedded in the original text defining the 'typeinfo' element |
| 232 | rawXml = etree.tostring(typeinfo.elem).decode('ascii') |
| 233 | result = re.search(r'VK_STRUCTURE_TYPE_\w+', rawXml) |
| 234 | if result: |
| 235 | value = result.group(0) |
| 236 | else: |
| 237 | value = self.genVkStructureType(typeName) |
| 238 | # Store the required type value |
| 239 | self.structTypes[typeName] = self.StructType(name=name, value=value) |
| 240 | # Store pointer/array/string info |
| 241 | membersInfo.append(self.CommandParam(type=type, |
| 242 | name=name, |
| 243 | ispointer=self.paramIsPointer(member), |
| 244 | isconst=True if 'const' in cdecl else False, |
| 245 | iscount=True if name in lens else False, |
| 246 | len=self.getLen(member), |
| 247 | extstructs=member.attrib.get('validextensionstructs') if name == 'pNext' else None, |
| 248 | cdecl=cdecl, |
| 249 | islocal=False, |
| 250 | iscreate=False, |
| 251 | isdestroy=False)) |
Mark Lobodzinski | 5380d13 | 2016-12-28 14:45:34 -0700 | [diff] [blame] | 252 | self.structMembers.append(self.StructMemberData(name=typeName, members=membersInfo, ifdef_protect=self.featureExtraProtect)) |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 253 | # |
| 254 | # Enum_string_header: Create a routine to convert an enumerated value into a string |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 255 | def GenerateEnumStringConversion(self, groupName, value_list): |
| 256 | outstring = '\n' |
| 257 | outstring += 'static inline const char* string_%s(%s input_value)\n' % (groupName, groupName) |
| 258 | outstring += '{\n' |
| 259 | outstring += ' switch ((%s)input_value)\n' % groupName |
| 260 | outstring += ' {\n' |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 261 | for item in value_list: |
| 262 | outstring += ' case %s:\n' % item |
| 263 | outstring += ' return "%s";\n' % item |
| 264 | outstring += ' default:\n' |
| 265 | outstring += ' return "Unhandled %s";\n' % groupName |
| 266 | outstring += ' }\n' |
| 267 | outstring += '}\n' |
| 268 | return outstring |
| 269 | # |
Mark Lobodzinski | 1a2f1a3 | 2016-12-28 15:41:15 -0700 | [diff] [blame^] | 270 | # Combine enum string helper header file preamble with body text and return |
| 271 | def GenerateEnumStringHelperHeader(self): |
| 272 | enum_string_helper_header = '\n' |
| 273 | enum_string_helper_header += '#pragma once\n' |
| 274 | enum_string_helper_header += '#ifdef _WIN32\n' |
| 275 | enum_string_helper_header += '#pragma warning( disable : 4065 )\n' |
| 276 | enum_string_helper_header += '#endif\n' |
| 277 | enum_string_helper_header += '\n' |
| 278 | enum_string_helper_header += '#include <vulkan/vulkan.h>\n' |
| 279 | enum_string_helper_header += '\n' |
| 280 | enum_string_helper_header += self.enum_output |
| 281 | return enum_string_helper_header |
| 282 | # |
Mark Lobodzinski | 5380d13 | 2016-12-28 14:45:34 -0700 | [diff] [blame] | 283 | # struct_size_header: build function prototypes for header file |
| 284 | def GenerateStructSizeHeader(self): |
| 285 | outstring = '' |
| 286 | outstring += 'size_t get_struct_chain_size(const void* struct_ptr);\n' |
| 287 | for item in self.structMembers: |
| 288 | lower_case_name = item.name.lower() |
| 289 | if item.ifdef_protect != None: |
| 290 | outstring += '#ifdef %s\n' % item.ifdef_protect |
| 291 | outstring += 'size_t vk_size_%s(const %s* struct_ptr);\n' % (item.name.lower(), item.name) |
| 292 | if item.ifdef_protect != None: |
| 293 | outstring += '#endif // %s\n' % item.ifdef_protect |
| 294 | outstring += '#ifdef __cplusplus\n' |
| 295 | outstring += '}\n' |
| 296 | outstring += '#endif' |
| 297 | return outstring |
| 298 | # |
Mark Lobodzinski | 1a2f1a3 | 2016-12-28 15:41:15 -0700 | [diff] [blame^] | 299 | # Combine struct size helper header file preamble with body text and return |
| 300 | def GenerateStructSizeHelperHeader(self): |
| 301 | struct_size_helper_header = '\n' |
| 302 | struct_size_helper_header += '#ifdef __cplusplus\n' |
| 303 | struct_size_helper_header += 'extern "C" {\n' |
| 304 | struct_size_helper_header += '#endif\n' |
| 305 | struct_size_helper_header += '\n' |
| 306 | struct_size_helper_header += '#include <stdio.h>\n' |
| 307 | struct_size_helper_header += '#include <stdlib.h>\n' |
| 308 | struct_size_helper_header += '#include <vulkan/vulkan.h>\n' |
| 309 | struct_size_helper_header += '\n' |
| 310 | struct_size_helper_header += '// Function Prototypes\n' |
| 311 | struct_size_helper_header += self.GenerateStructSizeHeader() |
| 312 | return struct_size_helper_header |
| 313 | |
| 314 | # |
| 315 | # struct_size_helper source -- create bodies of struct size helper functions |
| 316 | def GenerateStructSizeSource(self): |
| 317 | outstring = '' |
| 318 | for item in self.structMembers: |
| 319 | |
| 320 | if item.name == 'VkBindSparseInfo': |
| 321 | stop = 'here' |
| 322 | |
| 323 | lower_case_name = item.name.lower() |
| 324 | if item.ifdef_protect != None: |
| 325 | outstring += '#ifdef %s\n' % item.ifdef_protect |
| 326 | outstring += 'size_t vk_size_%s(const %s* struct_ptr) {\n' % (item.name.lower(), item.name) |
| 327 | outstring += ' size_t struct_size = 0;\n' |
| 328 | outstring += ' if (struct_ptr) {\n' |
| 329 | outstring += ' struct_size = sizeof(%s);\n' % item.name |
| 330 | outstring += ' }\n' |
| 331 | outstring += ' return struct_size\n' |
| 332 | outstring += '}\n' |
| 333 | if item.ifdef_protect != None: |
| 334 | outstring += '#endif // %s\n' % item.ifdef_protect |
| 335 | outstring += '#ifdef __cplusplus\n' |
| 336 | outstring += '}\n' |
| 337 | outstring += '#endif' |
| 338 | return outstring |
| 339 | # |
| 340 | # Combine struct size helper source file preamble with body text and return |
| 341 | def GenerateStructSizeHelperSource(self): |
| 342 | struct_size_helper_source = '\n' |
| 343 | struct_size_helper_source += '#include "vk_struct_size_helper.h"\n' |
| 344 | struct_size_helper_source += '#include <string.h>\n' |
| 345 | struct_size_helper_source += '#include <assert.h>\n' |
| 346 | struct_size_helper_source += '\n' |
| 347 | struct_size_helper_source += '// Function Definitions\n' |
| 348 | struct_size_helper_source += self.GenerateStructSizeSource() |
| 349 | return struct_size_helper_source |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | # |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 355 | # Create a helper file and return it as a string |
| 356 | def OutputDestFile(self): |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 357 | out_file_entries = '' |
| 358 | if self.helper_file_type == 'enum_string_header': |
Mark Lobodzinski | 1a2f1a3 | 2016-12-28 15:41:15 -0700 | [diff] [blame^] | 359 | return self.GenerateEnumStringHelperHeader() |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 360 | elif self.helper_file_type == 'struct_size_header': |
Mark Lobodzinski | 1a2f1a3 | 2016-12-28 15:41:15 -0700 | [diff] [blame^] | 361 | return self.GenerateStructSizeHelperHeader() |
Mark Lobodzinski | 46d388f | 2016-12-28 10:46:26 -0700 | [diff] [blame] | 362 | elif self.helper_file_type == 'struct_size_source': |
Mark Lobodzinski | 1a2f1a3 | 2016-12-28 15:41:15 -0700 | [diff] [blame^] | 363 | return self.GenerateStructSizeHelperSource() |