-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
309 lines (291 loc) · 11.3 KB
/
Copy pathparse.py
File metadata and controls
309 lines (291 loc) · 11.3 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
import sys
import shutil
from pathlib import Path
from clang import cindex
import PybindGen
def readVersionsConfig(configPath="versions.conf"):
versions = {}
if not os.path.exists(configPath):
return versions
with open(configPath, "r", encoding="utf-8") as file:
for line in file:
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
versions[key.strip()] = value.strip()
return versions
versions_config = readVersionsConfig()
hpp_root = "SFML/include"
includes = "-ISFML/include"
repo_root = "SFML"
parse_folders = ["Audio", "Graphics", "Network", "System", "Window"]
output_folder = "output"
cpp_version = "c++20"
python_version = os.environ.get("PYTHON_VERSION") or versions_config.get("PYTHON_VERSION")
if not python_version:
raise RuntimeError("PYTHON_VERSION not set in versions.conf")
common_module_name = "sf"
ignored_macros = [
"SFML_GRAPHICS_API",
"SFML_AUDIO_API",
"SFML_NETWORK_API",
"SFML_SYSTEM_API",
"SFML_WINDOW_API",
]
REPLACE_TYPE = {
"std::filesystem::path": "std::string",
}
SPECIFIC_TYPE = {
"std::vector<std::vector<sf::String>>": ["std::vector<std::vector<std::string>>", "toVectorVectorUTF8String(DATA)"],
"std::vector<sf::String>": ["std::vector<std::string>", "toVectorUTF8String(DATA)"],
"sf::String": ["std::string", "toSFString(DATA)"],
"String": ["std::string", "toSFString(DATA)"],
"void*": ["py::buffer", "static_cast<std::uint8_t*>(DATA.request().ptr)"],
"short*": ["std::vector<short>&", "DATA.data()"],
"int*": ["std::vector<int>&", "DATA.data()"],
"float*": ["std::vector<float>&", "DATA.data()"],
"std::int16_t*": ["std::vector<std::int16_t>&", "DATA.data()"],
"std::uint8_t*": ["std::vector<std::uint8_t>&", "DATA.data()"],
"sf::Vector2<float>*": ["std::vector<sf::Vector2<float>>&", "DATA.data()"],
"sf::Vector3<float>*": ["std::vector<sf::Vector3<float>>&", "DATA.data()"],
"sf::priv::Vector4<float>*": ["std::vector<sf::priv::Vector4<float>>&", "DATA.data()"],
"sf::priv::Matrix<3, 3>*": ["std::vector<sf::priv::Matrix<3, 3>>&", "DATA.data()"],
"sf::priv::Matrix<4, 4>*": ["std::vector<sf::priv::Matrix<4, 4>>&", "DATA.data()"],
"sf::Vertex*": ["std::vector<sf::Vertex>&", "DATA.data()"],
"unsigned char*": ["std::vector<std::uint8_t>&", "DATA.data()"],
"char*": ["std::string&", "DATA.data()"],
"wchar_t*": ["std::wstring&", "DATA.data()"],
"sf::WindowHandle": ["WindowHandle", "DATA"],
"WindowHandle": ["WindowHandle", "DATA"],
}
IGNORE_TYPE = ["VkInstance_T*", "VkInstance", "VkSurfaceKHR", "VkAllocationCallbacks", "std::locale", "char32_t*"]
IGNORE_RETURN_TYPE = ["GlFunctionPointer"]
SPECIFIC_RETURN_TYPE = {
"std::vector<std::vector<sf::String>>": ["toVectorVectorUTF8String(DATA)", "def_vector_vector_string_property"],
"std::vector<sf::String>": ["toVectorUTF8String(DATA)", "def_vector_string_property"],
"String": ["toUTF8String(DATA)", "def_string_property"],
"WindowHandle": ["WindowHandle::fromNative(DATA)", ""],
}
REPLACE_DEFAULT = {
" = sf::Style::None": " = 0",
" = sf::Style::Titlebar": " = 1 << 0",
" = sf::Style::Resize": " = 1 << 1",
" = sf::Style::Close": " = 1 << 2",
" = sf::Style::Default": " = 7",
}
IGNORED_MODULE = ["priv"]
TAB_STR = " "
SPECIAL_REPLACE = {
(
"v_sfRenderTexture",
'"getTexture"',
): '{}v_sfRenderTexture.def("getTexture", [](sf::RenderTexture &self) -> const sf::Texture& {{ return self.getTexture(); }},"{}", py::return_value_policy::reference_internal);'.format(
TAB_STR,
"\\n".join(
[
"\\\\brief Get a read-only reference to the target texture",
"",
"After drawing to the render-texture and calling Display,",
"you can retrieve the updated texture using this function,",
"and draw it using a sprite (for example).",
"The internal `sf::Texture` of a render-texture is always the",
"same instance, so that it is possible to call this function",
"once and keep a reference to the texture even after it is",
"modified.",
"",
"\\\\return Const reference to the texture",
]
),
),
(
"v_sfTransform",
'"getMatrix"',
): '{}v_sfTransform.def("getMatrix", [](sf::Transform& self) {{ const float* m = self.getMatrix(); return std::vector<float>(m, m + 16); }},"{}");'.format(
TAB_STR,
"\\n".join(
[
"\\\\brief Return the transform as a 4x4 matrix",
"",
"This function returns a pointer to an array of 16 floats",
"containing the transform elements as a 4x4 matrix, which",
"is directly compatible with OpenGL functions.",
"\\\\code",
"sf::Transform transform = ...;",
"glLoadMatrixf(transform.getMatrix());",
"\\\\endcode",
"",
"\\\\return Pointer to a 4x4 matri",
]
),
),
(
"v_sfImage",
'"getPixelsPtr"',
): '{}v_sfImage.def("getPixelsArray", [](sf::Image& self) {{ const std::uint8_t* pixels = self.getPixelsPtr(); auto size = self.getSize(); return std::vector<std::uint8_t>(pixels, pixels + size.x * size.y * 4); }}, "{}"); // Return a copy of the pixels array, the original method is getPixelsPtr()'.format(
TAB_STR,
"\\n".join(
[
"\\\\brief Get a read-only pointer to the array of pixels",
"",
"The returned value points to an array of RGBA pixels made of",
"8 bit integer components. The size of the array is",
"`width * height * 4 (getSize().x * getSize().y * 4)`.",
"Warning: the returned pointer may become invalid if you",
"modify the image, so you should never store it for too long.",
"If the image is empty, a null pointer is returned.",
"",
"\\\\return Read-only pointer to the array of pixels",
]
),
),
(
"v_sfShader",
"py::init<std::string, sf::Shader::Type>()",
): f"{TAB_STR}// Pass construction from memory GLSL code text.",
(
"v_sfShader",
"py::init<std::string, std::string>()",
): f"{TAB_STR}// Pass construction from memory GLSL code text.",
(
"v_sfShader",
"py::init<std::string, std::string, std::string>()",
): f"{TAB_STR}// Pass construction from memory GLSL code text.",
(
"v_sfPacket",
'"__rshift__", [](sf::Packet& self, std::string data)',
): f"{TAB_STR}// Pass std::string data to sf::Packet.__rshift__",
}
READWRITE_IGNORE = {"sf::SoundStream::Chunk": ["samples"]}
SELF_INCLUDE_FILES = [
"./include/string_utils.hpp",
"./include/wrap_utils.hpp",
"./include/utils.hpp",
"./include/bind_Vector.hpp",
"./include/bind_Rect.hpp",
"./include/bind_Matrix.hpp",
"./include/bind_Handle.hpp",
"./include/bind_Drawable.hpp",
]
hpp_excludes = {
"Audio": [
"AudioResource.hpp",
"Export.hpp",
"SoundFileFactory.hpp",
],
"Graphics": [
"Drawable.hpp",
"Export.hpp",
"Glsl.hpp",
"Rect.hpp",
],
"Network": [
"Export.hpp",
"SocketHandle.hpp",
],
"System": [
"Err.hpp",
"Exception.hpp",
"Export.hpp",
"NativeActivity.hpp",
"String.hpp",
"SuspendAwareClock.hpp",
"Utf.hpp",
"Vector2.hpp",
"Vector3.hpp",
],
"Window": [
"Export.hpp",
"GlResource.hpp",
"Vulkan.hpp",
],
}
if __name__ == "__main__":
project_root = os.path.abspath(".")
hpp_folders = PybindGen.scan_hpp_files(hpp_root, repo_root, parse_folders)
header_entries = []
for folder, hpp_files in hpp_folders.items():
for hpp_file in hpp_files:
if hpp_file in hpp_excludes.get(folder, []):
print(f"Skipping {hpp_file}")
continue
read_file = os.path.join(project_root, hpp_root, repo_root, folder, hpp_file)
header_entries.append((folder, hpp_file, read_file))
if not header_entries:
print("No header files found to parse. Exiting.")
sys.exit(0)
print(f"Extracting API from {len(header_entries)} headers in one translation unit.")
try:
parse_result = PybindGen.ProjectParser(
includes,
hpp_root,
[entry[2] for entry in header_entries],
cpp_version,
ignored_macros,
).get_result()
sorter = PybindGen.Sorter(
[entry[2] for entry in header_entries],
parse_result.translation_unit,
)
sorter.build_graph()
sorted_files = sorter.sort()
except (RuntimeError, cindex.LibclangError) as error:
print(f"\nAn error occurred: {error}", file=sys.stderr)
sys.exit(1)
if parse_result.diagnostics:
print("Clang diagnostics:")
for diagnostic in parse_result.diagnostics:
print(f" - {diagnostic}")
shutil.rmtree(output_folder, ignore_errors=True)
for folder, hpp_file, read_file in header_entries:
print(f"Processing {hpp_file}")
output_hpp = os.path.join(project_root, output_folder, "include", folder)
output_cpp = os.path.join(project_root, output_folder, "src", folder)
os.makedirs(output_cpp, exist_ok=True)
os.makedirs(output_hpp, exist_ok=True)
output_file = os.path.join(project_root, output_cpp, f"bind_{hpp_file.split('.')[0]}.cpp")
PybindGen.generate_binding_from_items(
common_module_name,
parse_result.declarations_for(read_file),
read_file,
output_file,
REPLACE_TYPE,
SPECIFIC_TYPE,
IGNORE_TYPE,
IGNORE_RETURN_TYPE,
SPECIFIC_RETURN_TYPE,
REPLACE_DEFAULT,
IGNORED_MODULE,
SPECIAL_REPLACE,
READWRITE_IGNORE,
)
PybindGen.generate_hpp_file_from_hpp(
read_file,
hpp_file,
os.path.join(project_root, output_hpp, f"bind_{hpp_file}"),
)
to_write_files = []
for file in SELF_INCLUDE_FILES:
file_name = Path(file).name
to_write_files.append(file_name)
for file in sorted_files:
file_name_path = Path(file).parts[-2:]
parent, file_name = file_name_path
flag = False
for folder, hpp_files in hpp_excludes.items():
if file_name in hpp_files:
flag = True
print(f"Found {file_name} in {folder}, skipping")
break
if flag:
continue
to_write_files.append(f"{parent}/bind_{file_name}")
print(f"Writing {len(to_write_files)} files to include folder.")
to_write_files.remove("bind_Vector.hpp")
to_write_files.insert(to_write_files.index("System/bind_Angle.hpp") + 1, "bind_Vector.hpp")
PybindGen.generate_pybind_main(
to_write_files,
os.path.join(project_root, output_folder, "main.cpp"),
)
PybindGen.generate_cmakelists(to_write_files, SELF_INCLUDE_FILES, python_version)