1bf215546Sopenharmony_ci#!/usr/bin/env python3
2bf215546Sopenharmony_ci
3bf215546Sopenharmony_ci# Copyright (C) 2010 LunarG Inc.
4bf215546Sopenharmony_ci# (C) Copyright 2015, NVIDIA CORPORATION.
5bf215546Sopenharmony_ci#
6bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci# Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci#
13bf215546Sopenharmony_ci# The above copyright notice and this permission notice shall be included
14bf215546Sopenharmony_ci# in all copies or substantial portions of the Software.
15bf215546Sopenharmony_ci#
16bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22bf215546Sopenharmony_ci# DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci#
24bf215546Sopenharmony_ci# Authors:
25bf215546Sopenharmony_ci#    Kyle Brenneman <kbrenneman@nvidia.com>
26bf215546Sopenharmony_ci#
27bf215546Sopenharmony_ci# Based on code ogiginally by:
28bf215546Sopenharmony_ci#    Chia-I Wu <olv@lunarg.com>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci"""
32bf215546Sopenharmony_ciGenerates the glapi_mapi_tmp.h header file from Khronos's XML file.
33bf215546Sopenharmony_ci"""
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ciimport sys
36bf215546Sopenharmony_ciimport xml.etree.ElementTree as etree
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ciimport genCommon
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cidef _main():
41bf215546Sopenharmony_ci    target = sys.argv[1]
42bf215546Sopenharmony_ci    xmlFiles = sys.argv[2:]
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci    roots = [ etree.parse(filename).getroot() for filename in xmlFiles ]
45bf215546Sopenharmony_ci    allFunctions = genCommon.getFunctionsFromRoots(roots)
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci    names = genCommon.getExportNamesFromRoots(target, roots)
48bf215546Sopenharmony_ci    functions = [f for f in allFunctions if(f.name in names)]
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci    if (target in ("gl", "gldispatch")):
51bf215546Sopenharmony_ci        assert(len(functions) == len(allFunctions))
52bf215546Sopenharmony_ci        assert(all(functions[i] == allFunctions[i] for i in range(len(functions))))
53bf215546Sopenharmony_ci        assert(all(functions[i].slot == i for i in range(len(functions))))
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci    print(r"""
56bf215546Sopenharmony_ci/* This file is automatically generated by mapi_abi.py.  Do not modify. */
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci#ifndef _GLAPI_TMP_H_
59bf215546Sopenharmony_ci#define _GLAPI_TMP_H_
60bf215546Sopenharmony_citypedef int GLclampx;
61bf215546Sopenharmony_ci#endif /* _GLAPI_TMP_H_ */
62bf215546Sopenharmony_ci""".lstrip("\n"))
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci    print(generate_defines(functions))
65bf215546Sopenharmony_ci    if target == "gldispatch":
66bf215546Sopenharmony_ci        print(generate_table(functions, allFunctions))
67bf215546Sopenharmony_ci        print(generate_noop_array(functions))
68bf215546Sopenharmony_ci        print(generate_public_stubs(functions))
69bf215546Sopenharmony_ci    print(generate_public_entries(functions))
70bf215546Sopenharmony_ci    if target == "gldispatch":
71bf215546Sopenharmony_ci        print(generate_public_entries_table(functions))
72bf215546Sopenharmony_ci    print(generate_undef_public_entries())
73bf215546Sopenharmony_ci    print(generate_stub_asm_gcc(functions))
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cidef generate_defines(functions):
76bf215546Sopenharmony_ci    text = r"""
77bf215546Sopenharmony_ci#ifdef MAPI_TMP_DEFINES
78bf215546Sopenharmony_ci#define GL_GLEXT_PROTOTYPES
79bf215546Sopenharmony_ci#include "GL/gl.h"
80bf215546Sopenharmony_ci#include "GL/glext.h"
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci""".lstrip("\n")
83bf215546Sopenharmony_ci    for func in functions:
84bf215546Sopenharmony_ci        text += "GLAPI {f.rt} APIENTRY {f.name}({f.decArgs});\n".format(f=func)
85bf215546Sopenharmony_ci    text += "#undef MAPI_TMP_DEFINES\n"
86bf215546Sopenharmony_ci    text += "#endif /* MAPI_TMP_DEFINES */\n"
87bf215546Sopenharmony_ci    return text
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cidef generate_table(functions, allFunctions):
90bf215546Sopenharmony_ci    text = "#ifdef MAPI_TMP_TABLE\n"
91bf215546Sopenharmony_ci    text += "#define MAPI_TABLE_NUM_STATIC %d\n" % (len(allFunctions))
92bf215546Sopenharmony_ci    text += "#define MAPI_TABLE_NUM_DYNAMIC %d\n" % (genCommon.MAPI_TABLE_NUM_DYNAMIC,)
93bf215546Sopenharmony_ci    text += "#undef MAPI_TMP_TABLE\n"
94bf215546Sopenharmony_ci    text += "#endif /* MAPI_TMP_TABLE */\n"
95bf215546Sopenharmony_ci    return text
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_cidef generate_noop_array(functions):
98bf215546Sopenharmony_ci    text = "#ifdef MAPI_TMP_NOOP_ARRAY\n"
99bf215546Sopenharmony_ci    text += "#ifdef DEBUG\n\n"
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci    for func in functions:
102bf215546Sopenharmony_ci        text += "static {f.rt} APIENTRY noop{f.basename}({f.decArgs})\n".format(f=func)
103bf215546Sopenharmony_ci        text += "{\n"
104bf215546Sopenharmony_ci        if (len(func.args) > 0):
105bf215546Sopenharmony_ci            text += "  "
106bf215546Sopenharmony_ci            for arg in func.args:
107bf215546Sopenharmony_ci                text += " (void) {a.name};".format(a=arg)
108bf215546Sopenharmony_ci            text += "\n"
109bf215546Sopenharmony_ci        text += "   noop_warn(\"{f.name}\");\n".format(f=func)
110bf215546Sopenharmony_ci        if (func.hasReturn()):
111bf215546Sopenharmony_ci            text += "   return ({f.rt}) 0;\n".format(f=func)
112bf215546Sopenharmony_ci        text += "}\n\n"
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci    text += "const mapi_func table_noop_array[] = {\n"
115bf215546Sopenharmony_ci    for func in functions:
116bf215546Sopenharmony_ci        text += "   (mapi_func) noop{f.basename},\n".format(f=func)
117bf215546Sopenharmony_ci    for i in range(genCommon.MAPI_TABLE_NUM_DYNAMIC - 1):
118bf215546Sopenharmony_ci        text += "   (mapi_func) noop_generic,\n"
119bf215546Sopenharmony_ci    text += "   (mapi_func) noop_generic\n"
120bf215546Sopenharmony_ci    text += "};\n\n"
121bf215546Sopenharmony_ci    text += "#else /* DEBUG */\n\n"
122bf215546Sopenharmony_ci    text += "const mapi_func table_noop_array[] = {\n"
123bf215546Sopenharmony_ci    for i in range(len(functions) + genCommon.MAPI_TABLE_NUM_DYNAMIC - 1):
124bf215546Sopenharmony_ci        text += "   (mapi_func) noop_generic,\n"
125bf215546Sopenharmony_ci    text += "   (mapi_func) noop_generic\n"
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci    text += "};\n\n"
128bf215546Sopenharmony_ci    text += "#endif /* DEBUG */\n"
129bf215546Sopenharmony_ci    text += "#undef MAPI_TMP_NOOP_ARRAY\n"
130bf215546Sopenharmony_ci    text += "#endif /* MAPI_TMP_NOOP_ARRAY */\n"
131bf215546Sopenharmony_ci    return text
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_cidef generate_public_stubs(functions):
134bf215546Sopenharmony_ci    text = "#ifdef MAPI_TMP_PUBLIC_STUBS\n"
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci    text += "static const struct mapi_stub public_stubs[] = {\n"
137bf215546Sopenharmony_ci    for func in functions:
138bf215546Sopenharmony_ci        text += "   { \"%s\", %d, NULL },\n" % (func.name, func.slot)
139bf215546Sopenharmony_ci    text += "};\n"
140bf215546Sopenharmony_ci    text += "#undef MAPI_TMP_PUBLIC_STUBS\n"
141bf215546Sopenharmony_ci    text += "#endif /* MAPI_TMP_PUBLIC_STUBS */\n"
142bf215546Sopenharmony_ci    return text
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_cidef generate_public_entries(functions):
145bf215546Sopenharmony_ci    text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci    for func in functions:
148bf215546Sopenharmony_ci        retStr = ("return " if func.hasReturn() else "")
149bf215546Sopenharmony_ci        text += r"""
150bf215546Sopenharmony_ciGLAPI {f.rt} APIENTRY {f.name}({f.decArgs})
151bf215546Sopenharmony_ci{{
152bf215546Sopenharmony_ci   const struct _glapi_table *_tbl = entry_current_get();
153bf215546Sopenharmony_ci   mapi_func _func = ((const mapi_func *) _tbl)[{f.slot}];
154bf215546Sopenharmony_ci   {retStr}(({f.rt} (APIENTRY *)({f.decArgs})) _func)({f.callArgs});
155bf215546Sopenharmony_ci}}
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci""".lstrip("\n").format(f=func, retStr=retStr)
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci    text += "\n"
160bf215546Sopenharmony_ci    text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"
161bf215546Sopenharmony_ci    return text
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_cidef generate_public_entries_table(functions):
164bf215546Sopenharmony_ci    text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"
165bf215546Sopenharmony_ci    text += "static const mapi_func public_entries[] = {\n"
166bf215546Sopenharmony_ci    for func in functions:
167bf215546Sopenharmony_ci        text += "   (mapi_func) %s,\n" % (func.name,)
168bf215546Sopenharmony_ci    text += "};\n"
169bf215546Sopenharmony_ci    text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"
170bf215546Sopenharmony_ci    return text
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_cidef generate_undef_public_entries():
173bf215546Sopenharmony_ci    text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"
174bf215546Sopenharmony_ci    text += "#undef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"
175bf215546Sopenharmony_ci    text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"
176bf215546Sopenharmony_ci    return text
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_cidef generate_stub_asm_gcc(functions):
179bf215546Sopenharmony_ci    text = "#ifdef MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN\n"
180bf215546Sopenharmony_ci    text += "__asm__(\n"
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci    for func in functions:
183bf215546Sopenharmony_ci        text += 'STUB_ASM_ENTRY("%s")"\\n"\n' % (func.name,)
184bf215546Sopenharmony_ci        text += '"\\t"STUB_ASM_CODE("%d")"\\n"\n\n' % (func.slot,)
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci    text += ");\n"
187bf215546Sopenharmony_ci    text += "#undef MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN\n"
188bf215546Sopenharmony_ci    text += "#endif /* MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN */\n"
189bf215546Sopenharmony_ci    return text
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ciif (__name__ == "__main__"):
192bf215546Sopenharmony_ci    _main()
193bf215546Sopenharmony_ci
194