-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbootstrap.py
282 lines (229 loc) · 9.5 KB
/
bootstrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import argparse
import pathlib
import re
import subprocess
import sys
# Generate the nanoarrow_c.pxd file used by the Cython extensions
class PxdGenerator:
def __init__(self):
self._define_regexes()
def generate_pxd(self, file_in, file_out):
file_in_name = pathlib.Path(file_in).name
# Read the header
content = None
with open(file_in, "r") as input:
content = input.read()
# Strip comments
content = self.re_comment.sub("", content)
# Replace NANOARROW_MAX_FIXED_BUFFERS with its value
content = self._preprocess_content(content)
# Find typedefs, types, and function definitions
typedefs = self._find_typedefs(content)
types = self._find_types(content)
func_defs = self._find_func_defs(content)
# Make corresponding cython definitions
typedefs_cython = [self._typdef_to_cython(t, " ") for t in typedefs]
types_cython = [self._type_to_cython(t, " ") for t in types]
func_defs_cython = [self._func_def_to_cython(d, " ") for d in func_defs]
# Unindent the header
header = self.re_newline_plus_indent.sub("\n", self._pxd_header())
# Write nanoarrow_c.pxd
with open(file_out, "wb") as output:
output.write(header.encode("UTF-8"))
output.write(
f'\ncdef extern from "{file_in_name}" nogil:\n'.encode("UTF-8")
)
# A few things we add in manually
self._write_defs(output)
for type in types_cython:
output.write(type.encode("UTF-8"))
output.write(b"\n\n")
for typedef in typedefs_cython:
output.write(typedef.encode("UTF-8"))
output.write(b"\n")
output.write(b"\n")
for func_def in func_defs_cython:
output.write(func_def.encode("UTF-8"))
output.write(b"\n")
def _preprocess_content(self, content):
return content
def _write_defs(self, output):
pass
def _define_regexes(self):
self.re_comment = re.compile(r"\s*//[^\n]*")
self.re_typedef = re.compile(r"typedef(?P<typedef>[^;]+)")
self.re_type = re.compile(
r"(?P<type>struct|union|enum) (?P<name>Arrow[^ ]+) {(?P<body>[^}]*)}"
)
self.re_func_def = re.compile(
r"\n(static inline )?(?P<const>const )?(struct |enum )?"
r"(?P<return_type>[A-Za-z0-9_*]+) "
r"(?P<name>Arrow[A-Za-z0-9]+)\((?P<args>[^\)]*)\);"
)
self.re_tagged_type = re.compile(
r"(?P<type>struct|union|enum) (?P<name>Arrow[A-Za-z]+)"
)
self.re_struct_delim = re.compile(r";\s*")
self.re_enum_delim = re.compile(r",\s*")
self.re_whitespace = re.compile(r"\s+")
self.re_newline_plus_indent = re.compile(r"\n +")
def _strip_comments(self, content):
return self.re_comment.sub("", content)
def _find_typedefs(self, content):
return [m.groupdict() for m in self.re_typedef.finditer(content)]
def _find_types(self, content):
return [m.groupdict() for m in self.re_type.finditer(content)]
def _find_func_defs(self, content):
return [m.groupdict() for m in self.re_func_def.finditer(content)]
def _typdef_to_cython(self, t, indent=""):
typedef = t["typedef"]
typedef = self.re_tagged_type.sub(r"\2", typedef)
return f"{indent}ctypedef {typedef}"
def _type_to_cython(self, t, indent=""):
type = t["type"]
name = t["name"]
body = self.re_tagged_type.sub(r"\2", t["body"].strip())
if type == "enum":
items = [item for item in self.re_enum_delim.split(body) if item]
else:
items = [item for item in self.re_struct_delim.split(body) if item]
cython_body = f"\n{indent} ".join([""] + items)
return f"{indent}{type} {name}:{cython_body}"
def _func_def_to_cython(self, d, indent=""):
return_type = d["return_type"].strip()
if d["const"]:
return_type = "const " + return_type
name = d["name"]
args = re.sub(r"\s+", " ", d["args"].strip())
args = self.re_tagged_type.sub(r"\2", args)
# Cython doesn't do (void)
if args == "void":
args = ""
return f"{indent}{return_type} {name}({args})"
def _pxd_header(self):
return """
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# cython: language_level = 3
"""
class NanoarrowPxdGenerator(PxdGenerator):
def _preprocess_content(self, content):
content = re.sub(r"NANOARROW_MAX_FIXED_BUFFERS", "3", content)
content = re.sub(r"NANOARROW_BINARY_VIEW_INLINE_SIZE", "12", content)
content = re.sub(r"NANOARROW_BINARY_VIEW_PREFIX_SIZE", "4", content)
return content
def _pxd_header(self):
return (
super()._pxd_header()
+ """
from libc.stdint cimport int8_t, uint8_t, int16_t, uint16_t
from libc.stdint cimport int32_t, uint32_t, int64_t, uint64_t
"""
)
def _write_defs(self, output):
output.write(b"\n")
output.write(b" cdef int NANOARROW_OK\n")
output.write(b" cdef int NANOARROW_MAX_FIXED_BUFFERS\n")
output.write(b" cdef int ARROW_FLAG_DICTIONARY_ORDERED\n")
output.write(b" cdef int ARROW_FLAG_NULLABLE\n")
output.write(b" cdef int ARROW_FLAG_MAP_KEYS_SORTED\n")
output.write(b"\n")
class NanoarrowDevicePxdGenerator(PxdGenerator):
def _preprocess_content(self, content):
self.device_names = re.findall("#define (ARROW_DEVICE_[A-Z0-9_]+)", content)
return super()._preprocess_content(content)
def _find_typedefs(self, content):
return []
def _pxd_header(self):
return (
super()._pxd_header()
+ """
from libc.stdint cimport int32_t, int64_t
from nanoarrow_c cimport *
"""
)
def _write_defs(self, output):
output.write(b"\n")
output.write(b" ctypedef int32_t ArrowDeviceType\n")
output.write(b"\n")
for name in self.device_names:
output.write(f" cdef ArrowDeviceType {name}\n".encode())
output.write(b"\n")
def copy_or_generate_nanoarrow_c(target_dir: pathlib.Path):
vendored_files = [
"nanoarrow.h",
"nanoarrow.c",
"nanoarrow_ipc.h",
"nanoarrow_ipc.c",
"nanoarrow_device.h",
"nanoarrow_device.c",
]
dst = {name: target_dir / name for name in vendored_files}
this_dir = pathlib.Path(__file__).parent.resolve()
arrow_proj_dir = this_dir / "subprojects" / "arrow-nanoarrow"
subprocess.run(
[
sys.executable,
arrow_proj_dir / "ci" / "scripts" / "bundle.py",
"--symbol-namespace",
"PythonPkg",
"--header-namespace",
"",
"--source-output-dir",
target_dir,
"--include-output-dir",
target_dir,
"--with-device",
"--with-ipc",
],
)
if not dst["nanoarrow.h"].exists():
raise ValueError("Attempt to vendor nanoarrow.c/h failed")
# Runs the pxd generator with some information about the file name
def generate_nanoarrow_pxds(target_dir: pathlib.Path):
NanoarrowPxdGenerator().generate_pxd(
target_dir / "nanoarrow.h", target_dir / "nanoarrow_c.pxd"
)
NanoarrowDevicePxdGenerator().generate_pxd(
target_dir / "nanoarrow_device.h",
target_dir / "nanoarrow_device_c.pxd",
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--output-dir", help="Target directory where files should be written"
)
args = parser.parse_args()
target_dir = pathlib.Path(args.output_dir).resolve()
copy_or_generate_nanoarrow_c(target_dir)
generate_nanoarrow_pxds(target_dir)