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 |
| 58 | self.prefixText = None |
| 59 | self.protectFile = protectFile |
| 60 | self.protectFeature = protectFeature |
| 61 | self.protectProto = protectProto |
| 62 | self.protectProtoStr = protectProtoStr |
| 63 | self.apicall = apicall |
| 64 | self.apientry = apientry |
| 65 | self.apientryp = apientryp |
| 66 | self.alignFuncParam = alignFuncParam |
| 67 | self.library_name = library_name |
| 68 | self.helper_file_type = helper_file_type |
| 69 | # |
| 70 | # HelperFileOutputGenerator - subclass of OutputGenerator. |
| 71 | # Outputs Vulkan helper files |
| 72 | class HelperFileOutputGenerator(OutputGenerator): |
| 73 | """Generate Windows def file based on XML element attributes""" |
| 74 | def __init__(self, |
| 75 | errFile = sys.stderr, |
| 76 | warnFile = sys.stderr, |
| 77 | diagFile = sys.stdout): |
| 78 | OutputGenerator.__init__(self, errFile, warnFile, diagFile) |
| 79 | # Internal state - accumulators for different inner block text |
| 80 | self.enum_output = '' # string built up of enum string routines |
| 81 | self.enum_list = () |
| 82 | # |
| 83 | # Called once at the beginning of each run |
| 84 | def beginFile(self, genOpts): |
| 85 | OutputGenerator.beginFile(self, genOpts) |
| 86 | # User-supplied prefix text, if any (list of strings) |
| 87 | if (genOpts.prefixText): |
| 88 | for s in genOpts.prefixText: |
| 89 | write(s, file=self.outFile) |
| 90 | self.helper_file_type = genOpts.helper_file_type |
| 91 | self.library_name = genOpts.library_name |
| 92 | # File Comment |
| 93 | file_comment = '// *** THIS FILE IS GENERATED - DO NOT EDIT ***\n' |
| 94 | file_comment += '// See helper_file_generator.py for modifications\n' |
| 95 | write(file_comment, file=self.outFile) |
| 96 | # Copyright Notice |
| 97 | copyright = '' |
| 98 | copyright += '\n' |
| 99 | copyright += '/***************************************************************************\n' |
| 100 | copyright += ' *\n' |
| 101 | copyright += ' * Copyright (c) 2015-2016 The Khronos Group Inc.\n' |
| 102 | copyright += ' * Copyright (c) 2015-2016 Valve Corporation\n' |
| 103 | copyright += ' * Copyright (c) 2015-2016 LunarG, Inc.\n' |
| 104 | copyright += ' * Copyright (c) 2015-2016 Google Inc.\n' |
| 105 | copyright += ' *\n' |
| 106 | copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n' |
| 107 | copyright += ' * you may not use this file except in compliance with the License.\n' |
| 108 | copyright += ' * You may obtain a copy of the License at\n' |
| 109 | copyright += ' *\n' |
| 110 | copyright += ' * http://www.apache.org/licenses/LICENSE-2.0\n' |
| 111 | copyright += ' *\n' |
| 112 | copyright += ' * Unless required by applicable law or agreed to in writing, software\n' |
| 113 | copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n' |
| 114 | copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' |
| 115 | copyright += ' * See the License for the specific language governing permissions and\n' |
| 116 | copyright += ' * limitations under the License.\n' |
| 117 | copyright += ' *\n' |
| 118 | copyright += ' * Author: Mark Lobodzinski <mark@lunarg.com>\n' |
Mark Lobodzinski | a9c963d | 2016-12-28 07:45:35 -0700 | [diff] [blame] | 119 | copyright += ' * Author: Courtney Goeltzenleuchter <courtneygo@google.com>\n' |
| 120 | copyright += ' * Author: Tobin Ehlis <tobine@google.com>\n' |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 121 | copyright += ' *\n' |
| 122 | copyright += ' ****************************************************************************/\n' |
| 123 | write(copyright, file=self.outFile) |
| 124 | # |
| 125 | # Write generate and write def file content to output file |
| 126 | def endFile(self): |
| 127 | dest_file = '' |
| 128 | dest_file += self.OutputDestFile() |
| 129 | write(dest_file, file=self.outFile); |
| 130 | # Finish processing in superclass |
| 131 | OutputGenerator.endFile(self) |
| 132 | # |
| 133 | # Grab group (e.g. C "enum" type) info to output for enum-string conversion helper |
| 134 | def genGroup(self, groupinfo, groupName): |
| 135 | OutputGenerator.genGroup(self, groupinfo, groupName) |
| 136 | groupElem = groupinfo.elem |
| 137 | value_list = [] |
| 138 | for elem in groupElem.findall('enum'): |
Mark Lobodzinski | 59a37cc | 2016-12-27 14:35:48 -0700 | [diff] [blame] | 139 | if elem.get('supported') != 'disabled': |
| 140 | item_name = elem.get('name') |
| 141 | value_list.append(item_name) |
Mark Lobodzinski | 7ada59c | 2016-12-27 11:11:54 -0700 | [diff] [blame] | 142 | if value_list is not None: |
| 143 | self.enum_output += self.GenerateEnumStringConversion(groupName, value_list) |
| 144 | # |
| 145 | # Create a routine to convert an enumerated value into a string |
| 146 | def GenerateEnumStringConversion(self, groupName, value_list): |
| 147 | outstring = '\n' |
| 148 | outstring += 'static inline const char* string_%s(%s input_value)\n' % (groupName, groupName) |
| 149 | outstring += '{\n' |
| 150 | outstring += ' switch ((%s)input_value)\n' % groupName |
| 151 | outstring += ' {\n' |
| 152 | |
| 153 | for item in value_list: |
| 154 | outstring += ' case %s:\n' % item |
| 155 | outstring += ' return "%s";\n' % item |
| 156 | outstring += ' default:\n' |
| 157 | outstring += ' return "Unhandled %s";\n' % groupName |
| 158 | outstring += ' }\n' |
| 159 | outstring += '}\n' |
| 160 | return outstring |
| 161 | # |
| 162 | # Create a helper file and return it as a string |
| 163 | def OutputDestFile(self): |
| 164 | out_file_entries = '\n' |
| 165 | out_file_entries += '#pragma once\n' |
| 166 | out_file_entries += '#ifdef _WIN32\n' |
| 167 | out_file_entries += '#pragma warning( disable : 4065 )\n' |
| 168 | out_file_entries += '#endif\n' |
| 169 | out_file_entries += '\n' |
| 170 | out_file_entries += '#include <vulkan/vulkan.h>\n' |
| 171 | out_file_entries += '\n' |
| 172 | out_file_entries += self.enum_output |
| 173 | return out_file_entries |