Skip to content

Commit f4fdb8e

Browse files
committed
WIP create_bindings script
1 parent 9b73b51 commit f4fdb8e

1 file changed

Lines changed: 133 additions & 4 deletions

File tree

‎create_binding.py‎

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,52 @@
55
import re
66

77

8-
re_opaque_type = re.compile( "^typedef\s+struct\s+[A-Za-z_]+\s+([A-Za-z]+)" )
9-
re_opaque_type = re.compile( "^typedef\s+struct\s+[A-Za-z_]+" )#\s+([A-Za-z]+)" )
8+
9+
main_template = '''
10+
#include <pybind11/pybind11.h>
11+
12+
#include <optix.h>
13+
#include "pyoptix.h"
14+
15+
namespace py = pybind11;
16+
17+
namespace pyoptix
18+
{{
19+
// Opaque type struct wrappers
20+
{opaque_types_structs}
21+
}} // end namespace pyoptix
22+
23+
PYBIND11_MODULE( optix, m )
24+
{{
25+
m.doc() = R"pbdoc(
26+
OptiX API
27+
-----------------------
28+
29+
.. currentmodule:: optix
30+
31+
.. autosummary::
32+
:toctree: _generate
33+
34+
)pbdoc";
35+
36+
// Opaque types
37+
{opaque_types_bindings}
38+
}}
39+
'''
40+
41+
42+
43+
re_opaque_type = re.compile( "^typedef\s+struct\s+[A-Za-z_]+\*\s+([A-Za-z]+)" )
44+
45+
re_typedef_end = re.compile( "^\s*}\s+([A-Za-z]+)\s*;" )
46+
47+
re_enum_type = re.compile( "^typedef\s+enum\s+([A-Za-z]+)" )
48+
re_enum_value = re.compile( "^\s*([0-9A-Za-z_]+)" )
49+
50+
re_struct_type = re.compile( "^typedef\s+struct\s+([A-Za-z]+)" )
51+
re_struct_value = re.compile( "^\s*([^/]+)\s+([A-Za-z]+);" )
52+
53+
1054

1155
if len( sys.argv ) != 2:
1256
print( "Usage: {} <path/to/optix/include>".format( sys.argv[0] ) )
@@ -15,13 +59,98 @@
1559
optix_include = sys.argv[1]
1660
print( "Looking for optix headers in '{}'".format( optix_include ) )
1761

62+
opaque_types = []
63+
struct_types = {}
64+
enum_types = {}
65+
1866
with open( os.path.join( optix_include, 'optix_7_types.h' ), 'r' ) as types_file:
1967
print( "Found optix types header ..." )
68+
69+
cur_struct = None
70+
cur_enum = None
71+
2072
for line in types_file:
73+
74+
if cur_enum:
75+
76+
match = re_typedef_end.match( line )
77+
if match:
78+
for v in enum_types[cur_enum]:
79+
print( "\t\t{}".format( v ) )
80+
cur_enum = None
81+
else:
82+
match = re_enum_value.match( line )
83+
if match:
84+
enum_types[cur_enum].append( match.groups()[0] )
85+
continue
86+
87+
if cur_struct:
88+
89+
match = re_typedef_end.match( line )
90+
if match:
91+
for v in struct_types[cur_struct]:
92+
print( "\t\t{}".format( v ) )
93+
cur_struct = None
94+
else:
95+
match = re_struct_value.match( line )
96+
if match:
97+
struct_types[cur_struct].append( match.groups() )
98+
continue
99+
21100
match = re_opaque_type.match( line )
22101
if match:
23-
print( "Opaque type: {}".format( line ) )
24-
print( "Opaque type: {}".format( match.groups() ) )
102+
opaque_types.append( match.groups()[0] )
103+
print( "\tFound opaque type: {}".format( match.groups()[0] ) )
104+
continue
105+
106+
match = re_enum_type.match( line )
107+
if match:
108+
cur_enum = match.groups()[0]
109+
enum_types[ cur_enum ] = []
110+
print( "\tFound enum type: {}".format( cur_enum ) )
111+
continue
112+
113+
match = re_struct_type.match( line )
114+
if match:
115+
cur_struct = match.groups()[0]
116+
print( "\tFound struct type: {}".format( cur_struct ) )
117+
struct_types[ cur_struct ] = []
118+
continue
119+
120+
121+
opaque_type_struct_template = '''
122+
struct {pyoptix_name}
123+
{{
124+
{optix_name} {var_name};
125+
}};
126+
'''
127+
128+
opaque_type_binding_template = '''
129+
py::class_<optix_name>( m, "{pyoptix_name}" );
130+
'''
131+
opaque_types_structs = []
132+
opaque_types_bindings = []
133+
134+
for optix_name in opaque_types:
135+
pyoptix_name = optix_name.replace( "Optix", "", 1 )
136+
var_name = pyoptix_name[0].lower() + pyoptix_name[1:]
137+
opaque_types_structs.append( opaque_type_struct_template.format(
138+
optix_name = optix_name,
139+
pyoptix_name = pyoptix_name,
140+
var_name = var_name
141+
)
142+
opaque_types_bindings.append( opaque_type_binding_template.format(
143+
optix_name = optix_name,
144+
pyoptix_name = pyoptix_name
145+
)
146+
)
147+
148+
25149

26150

27151

152+
main = main_template.format(
153+
opaque_types_structs = "".join( opaque_types_structs ),
154+
opaque_types_bindings = "".join( opaque_types_bindings )
155+
)
156+
print( main )

0 commit comments

Comments
 (0)