xref: /aosp_15_r20/external/OpenCL-Headers/scripts/gen_headers.py (revision 3cc257528f335c5672e46865a03b8ee020e5fe2d)
1#!/usr/bin/python3
2
3# Copyright (c) 2021 The Khronos Group Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import gen
18import re
19
20from mako.template import Template
21from mako.exceptions import RichTraceback
22
23from collections import OrderedDict
24from collections import namedtuple
25
26def getDisableWarningIncludeString(include):
27    warningPush = """#if defined(_MSC_VER)
28#if _MSC_VER >=1500
29#pragma warning( push )
30#pragma warning( disable : 4201 )
31#pragma warning( disable : 5105 )
32#endif
33#endif
34"""
35    warningPop = """
36#if defined(_MSC_VER)
37#if _MSC_VER >=1500
38#pragma warning( pop )
39#endif
40#endif"""
41    return warningPush + include + warningPop
42
43def getWin32OnlyIncludeString(include):
44    return """#if defined(_WIN32)
45""" + include + """
46#endif"""
47
48if __name__ == "__main__":
49    args = gen.parse_args()
50    spec = gen.load_spec(args)
51
52    typedefs = gen.get_apitypedefs(spec)
53    macros = gen.get_apimacros(spec)
54    structs = gen.get_apistructs(spec)
55    enums = gen.get_apienums(spec)
56    apisigs = gen.get_apisigs(spec)
57
58    coreapis = gen.get_coreapis(spec, apisigs)
59    extapis = gen.get_extapis(spec, apisigs)
60
61    try:
62        cl_ext_h_template = Template(filename='cl_ext.h.mako', input_encoding='utf-8')
63        cl_function_types_h_template = Template(filename='cl_function_types.h.mako', input_encoding='utf-8')
64
65        print('Generating cl_dx9_media_sharing.h...')
66        text = cl_ext_h_template.render(
67            genExtensions={
68                'cl_khr_dx9_media_sharing',
69                'cl_intel_dx9_media_sharing',
70                'cl_intel_sharing_format_query_dx9',
71            },
72            guard="OPENCL_CL_DX9_MEDIA_SHARING_H_",
73            includes=getWin32OnlyIncludeString(getDisableWarningIncludeString("#include <d3d9.h>")),
74            generate_pfn_typedefs=False,
75            spec=spec,
76            typedefs=typedefs,
77            macros=macros,
78            structs=structs,
79            enums=enums,
80            apisigs=apisigs,
81            coreapis=coreapis,
82            extapis=extapis)
83        text = re.sub(r'\r\n', r'\n', text)
84        with open(args.directory + '/cl_dx9_media_sharing.h', 'w') as gen:
85            gen.write(text)
86
87        print('Generating cl_d3d10.h...')
88        text = cl_ext_h_template.render(
89            genExtensions={
90                'cl_khr_d3d10_sharing',
91                'cl_intel_sharing_format_query_d3d10',
92            },
93            guard="OPENCL_CL_D3D10_H_",
94            includes=getDisableWarningIncludeString("#include <d3d10.h>"),
95            generate_pfn_typedefs=False,
96            spec=spec,
97            typedefs=typedefs,
98            macros=macros,
99            structs=structs,
100            enums=enums,
101            apisigs=apisigs,
102            coreapis=coreapis,
103            extapis=extapis)
104        text = re.sub(r'\r\n', r'\n', text)
105        with open(args.directory + '/cl_d3d10.h', 'w') as gen:
106            gen.write(text)
107
108        print('Generating cl_d3d11.h...')
109        text = cl_ext_h_template.render(
110            genExtensions={
111                'cl_khr_d3d11_sharing',
112                'cl_intel_sharing_format_query_d3d11'
113            },
114            guard="OPENCL_CL_D3D11_H_",
115            includes=getDisableWarningIncludeString("#include <d3d11.h>"),
116            generate_pfn_typedefs=False,
117            spec=spec,
118            typedefs=typedefs,
119            macros=macros,
120            structs=structs,
121            enums=enums,
122            apisigs=apisigs,
123            coreapis=coreapis,
124            extapis=extapis)
125        text = re.sub(r'\r\n', r'\n', text)
126        with open(args.directory + '/cl_d3d11.h', 'w') as gen:
127            gen.write(text)
128
129        print('Generating cl_egl.h...')
130        text = cl_ext_h_template.render(
131            genExtensions={'cl_khr_egl_event', 'cl_khr_egl_image'},
132            guard="OPENCL_CL_EGL_H_",
133            generate_pfn_typedefs=False,
134            spec=spec,
135            typedefs=typedefs,
136            macros=macros,
137            structs=structs,
138            enums=enums,
139            apisigs=apisigs,
140            coreapis=coreapis,
141            extapis=extapis)
142        text = re.sub(r'\r\n', r'\n', text)
143        with open(args.directory + '/cl_egl.h', 'w') as gen:
144            gen.write(text)
145
146        print('Generating cl_gl.h...')
147        text = cl_ext_h_template.render(
148            genExtensions={
149                'cl_khr_gl_depth_images',
150                'cl_khr_gl_event',
151                'cl_khr_gl_msaa_sharing',
152                'cl_khr_gl_sharing',
153                'cl_intel_sharing_format_query_gl',
154            },
155            guard="OPENCL_CL_GL_H_",
156            generate_pfn_typedefs=False,
157            spec=spec,
158            typedefs=typedefs,
159            macros=macros,
160            structs=structs,
161            enums=enums,
162            apisigs=apisigs,
163            coreapis=coreapis,
164            extapis=extapis)
165        text = re.sub(r'\r\n', r'\n', text)
166        with open(args.directory + '/cl_gl.h', 'w') as gen:
167            gen.write(text)
168
169        print('Generating cl_layer.h...')
170        text = cl_ext_h_template.render(
171            genExtensions={'cl_loader_layers'},
172            guard="OPENCL_CL_LAYER_H_",
173            includes='#include <CL/cl_icd.h>',
174            generate_pfn_typedefs=True,
175            spec=spec,
176            typedefs=typedefs,
177            macros=macros,
178            structs=structs,
179            enums=enums,
180            apisigs=apisigs,
181            coreapis=coreapis,
182            extapis=extapis)
183        text = re.sub(r'\r\n', r'\n', text)
184        with open(args.directory + '/cl_layer.h', 'w') as gen:
185            gen.write(text)
186
187        print('Generating cl_va_api_media_sharing_intel.h...')
188        text = cl_ext_h_template.render(
189            genExtensions={
190                'cl_intel_va_api_media_sharing',
191                'cl_intel_sharing_format_query_va_api',
192            },
193            guard="OPENCL_CL_VA_API_MEDIA_SHARING_INTEL_H_",
194            includes='#include <va/va.h>',
195            generate_pfn_typedefs=False,
196            spec=spec,
197            typedefs=typedefs,
198            macros=macros,
199            structs=structs,
200            enums=enums,
201            apisigs=apisigs,
202            coreapis=coreapis,
203            extapis=extapis)
204        text = re.sub(r'\r\n', r'\n', text)
205        with open(args.directory + '/cl_va_api_media_sharing_intel.h', 'w') as gen:
206            gen.write(text)
207
208        print('Generating cl_ext.h...')
209        text = cl_ext_h_template.render(
210            genExtensions={},
211            guard="OPENCL_CL_EXT_H_",
212            generate_pfn_typedefs=False,
213            spec=spec,
214            typedefs=typedefs,
215            macros=macros,
216            structs=structs,
217            enums=enums,
218            apisigs=apisigs,
219            coreapis=coreapis,
220            extapis=extapis)
221        text = re.sub(r'\r\n', r'\n', text)
222        with open(args.directory + '/cl_ext.h', 'w') as gen:
223            gen.write(text)
224
225        print('Generating cl_function_types.h...')
226        text = cl_function_types_h_template.render(
227            spec=spec,
228            apisigs=apisigs,
229            coreapis=coreapis,
230            extapis=extapis)
231        text = re.sub(r'\r\n', r'\n', text)
232        with open(args.directory + '/cl_function_types.h', 'w') as gen:
233            gen.write(text)
234
235    except:
236        traceback = RichTraceback()
237        for (filename, lineno, function, line) in traceback.traceback:
238            print('%s(%s) : error in %s' % (filename, lineno, function))
239            print('    ', line)
240        print('%s: %s' % (str(traceback.error.__class__.__name__), traceback.error))
241