1#!/usr/bin/python3 -i
2#
3# Copyright (c) 2021 The Khronos Group Inc.
4# Copyright (c) 2021 Valve Corporation
5# Copyright (c) 2021 LunarG, Inc.
6# Copyright (c) 2021 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: Charles Giessen <charles@lunarg.com>
21
22import os,re,sys
23import xml.etree.ElementTree as etree
24from generator import *
25from collections import namedtuple
26from common_codegen import *
27
28#
29# LoaderVersioningGeneratorOptions - subclass of GeneratorOptions.
30class LoaderVersioningGeneratorOptions(GeneratorOptions):
31    def __init__(self,
32                 conventions = None,
33                 filename = None,
34                 directory = '.',
35                 genpath = None,
36                 apiname = None,
37                 profile = None,
38                 versions = '.*',
39                 emitversions = '.*',
40                 defaultExtensions = None,
41                 addExtensions = None,
42                 removeExtensions = None,
43                 emitExtensions = None,
44                 sortProcedure = regSortFeatures,
45                 prefixText = "",
46                 genFuncPointers = True,
47                 protectFile = True,
48                 protectFeature = True,
49                 apicall = '',
50                 apientry = '',
51                 apientryp = '',
52                 alignFuncParam = 0,
53                 library_name = '',
54                 expandEnumerants = True):
55        GeneratorOptions.__init__(self,
56                conventions = conventions,
57                filename = filename,
58                directory = directory,
59                genpath = genpath,
60                apiname = apiname,
61                profile = profile,
62                versions = versions,
63                emitversions = emitversions,
64                defaultExtensions = defaultExtensions,
65                addExtensions = addExtensions,
66                removeExtensions = removeExtensions,
67                emitExtensions = emitExtensions,
68                sortProcedure = sortProcedure)
69        self.prefixText       = prefixText
70        self.genFuncPointers  = genFuncPointers
71        self.protectFile      = protectFile
72        self.protectFeature   = protectFeature
73        self.apicall          = apicall
74        self.apientry         = apientry
75        self.apientryp        = apientryp
76        self.alignFuncParam   = alignFuncParam
77        self.library_name     = library_name
78#
79# LoaderVersioningGenerator - subclass of OutputGenerator. Outputs cmake file containing vulkan version used when generating files
80class LoaderVersioningGenerator(OutputGenerator):
81    """Generate helper file based on XML element attributes"""
82    def __init__(self,
83                 errFile = sys.stderr,
84                 warnFile = sys.stderr,
85                 diagFile = sys.stdout):
86        OutputGenerator.__init__(self, errFile, warnFile, diagFile)
87
88    #
89    # Called once at the beginning of each run
90    def beginFile(self, genOpts):
91        OutputGenerator.beginFile(self, genOpts)
92        # User-supplied prefix text, if any (list of strings)
93        self.library_name = genOpts.library_name
94
95        version_major = ''
96        version_minor = ''
97        version_patch = ''
98        for elem in self.registry.reg.find('types').findall('type'):
99            if elem.get('category') == 'define':
100                if elem.get('name') == 'VK_HEADER_VERSION_COMPLETE':
101                    # Parses the following string:
102                    #define <name>VK_HEADER_VERSION_COMPLETE</name> <type>VK_MAKE_API_VERSION</type>(0, 1, 2, VK_HEADER_VERSION)</type>
103                    # The 0th index is the VARIANT version, 1st & 2nd are the Major & Minor
104                    version_major = re.findall("[0-9]+", ''.join(elem.itertext()))[1]
105                    version_minor = re.findall("[0-9]+", ''.join(elem.itertext()))[2]
106                if elem.get('name') == 'VK_HEADER_VERSION':
107                    # Parses the following string:
108                    #define <name>VK_HEADER_VERSION</name> 189</type>
109                    version_patch = re.findall("[0-9]+", ''.join(elem.itertext()))[0]
110
111        # File Comment
112        file_comment = '# *** THIS FILE IS GENERATED - DO NOT EDIT ***\n'
113        file_comment += '# See loader_versioning_generator.py for modifications\n'
114        write(file_comment, file=self.outFile)
115        # Copyright Notice
116        cmake_version_copyright = '''############################################################################
117#
118# Copyright (c) 2021 The Khronos Group Inc.
119# Copyright (c) 2021 Valve Corporation
120# Copyright (c) 2021 LunarG, Inc.
121# Copyright (c) 2021 Google Inc.
122#
123# Licensed under the Apache License, Version 2.0 (the "License");
124# you may not use this file except in compliance with the License.
125# You may obtain a copy of the License at
126#
127#     http://www.apache.org/licenses/LICENSE-2.0
128#
129# Unless required by applicable law or agreed to in writing, software
130# distributed under the License is distributed on an "AS IS" BASIS,
131# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132# See the License for the specific language governing permissions and
133# limitations under the License.
134#
135# Author: Charles Giessen <charles@lunarg.com>
136#
137############################################################################
138'''
139        write(cmake_version_copyright, file=self.outFile)
140        write(f'set(LOADER_GENERATED_HEADER_VERSION \"{version_major}.{version_minor}.{version_patch}\")', file=self.outFile)
141
142    #
143    # Write generated file content to output file
144    def endFile(self):
145        dest_file = ''
146        # Remove blank lines at EOF
147        if dest_file.endswith('\n'):
148            dest_file = dest_file[:-1]
149        write(dest_file, file=self.outFile)
150        # Finish processing in superclass
151        OutputGenerator.endFile(self)