Skip to content

Commit c56e847

Browse files
committed
update wip
1 parent af2c0ac commit c56e847

File tree

3 files changed

+67
-67
lines changed

3 files changed

+67
-67
lines changed

binding_generator.py

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def generate_virtuals(target):
200200
f.write(txt)
201201

202202

203-
def get_file_list(api_filepath, output_dir, headers=False, sources=False, compat=False, profile_filepath=""):
203+
def get_file_list(api_filepath, output_dir, headers=False, sources=False, profile_filepath=""):
204204
api = {}
205205
files = []
206206
with open(api_filepath, encoding="utf-8") as api_file:
@@ -210,7 +210,6 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False, compat
210210

211211
core_gen_folder = Path(output_dir) / "gen" / "include" / "godot_cpp" / "core"
212212
include_gen_folder = Path(output_dir) / "gen" / "include" / "godot_cpp"
213-
include_gen_compat_folder = Path(output_dir) / "gen" / "include" / "godot_compat"
214213
source_gen_folder = Path(output_dir) / "gen" / "src"
215214

216215
files.append(str((core_gen_folder / "ext_wrappers.gen.inc").as_posix()))
@@ -224,12 +223,9 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False, compat
224223
continue
225224

226225
header_filename = include_gen_folder / "variant" / (camel_to_snake(builtin_class["name"]) + ".hpp")
227-
header_compat_filename = include_gen_compat_folder / "variant" / (camel_to_snake(builtin_class["name"]) + ".hpp")
228226
source_filename = source_gen_folder / "variant" / (camel_to_snake(builtin_class["name"]) + ".cpp")
229227
if headers:
230228
files.append(str(header_filename.as_posix()))
231-
if compat:
232-
files.append(str(header_compat_filename.as_posix()))
233229
if sources:
234230
files.append(str(source_filename.as_posix()))
235231

@@ -239,12 +235,9 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False, compat
239235
engine_class["name"] = "ClassDBSingleton"
240236
engine_class["alias_for"] = "ClassDB"
241237
header_filename = include_gen_folder / "classes" / (camel_to_snake(engine_class["name"]) + ".hpp")
242-
header_compat_filename = include_gen_compat_folder / "classes" / (camel_to_snake(engine_class["name"]) + ".hpp")
243238
source_filename = source_gen_folder / "classes" / (camel_to_snake(engine_class["name"]) + ".cpp")
244239
if headers:
245240
files.append(str(header_filename.as_posix()))
246-
if compat:
247-
files.append(str(header_compat_filename.as_posix()))
248241
if sources and is_class_included(engine_class["name"], build_profile):
249242
files.append(str(source_filename.as_posix()))
250243

@@ -255,39 +248,30 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False, compat
255248
snake_struct_name = camel_to_snake(struct_name)
256249

257250
header_filename = include_gen_folder / "classes" / (snake_struct_name + ".hpp")
258-
header_compat_filename = include_gen_compat_folder / "classes" / (snake_struct_name + ".hpp")
259251
if headers:
260252
files.append(str(header_filename.as_posix()))
261-
if compat:
262-
files.append(str(header_compat_filename.as_posix()))
263253

264254
if headers:
265-
relative_paths = [
266-
["variant", "builtin_types.hpp"],
267-
["variant", "builtin_binds.hpp"],
268-
["variant", "utility_functions.hpp"],
269-
["variant", "variant_size.hpp"],
270-
["variant", "builtin_vararg_methods.hpp"],
271-
["classes", "global_constants.hpp"],
272-
["classes", "global_constants_binds.hpp"],
273-
["core", "version.hpp"]
274-
]
275-
276-
for relative_path_parts in relative_paths:
277-
full_header_path = include_gen_folder.joinpath(*relative_path_parts)
278-
files.append(str(full_header_path.as_posix()))
279-
if compat:
280-
full_compat_path = include_gen_compat_folder.joinpath(*relative_path_parts)
281-
files.append(str(full_compat_path.as_posix()))
255+
for path in [
256+
include_gen_folder / "variant" / "builtin_types.hpp",
257+
include_gen_folder / "variant" / "builtin_binds.hpp",
258+
include_gen_folder / "variant" / "utility_functions.hpp",
259+
include_gen_folder / "variant" / "variant_size.hpp",
260+
include_gen_folder / "variant" / "builtin_vararg_methods.hpp",
261+
include_gen_folder / "classes" / "global_constants.hpp",
262+
include_gen_folder / "classes" / "global_constants_binds.hpp",
263+
include_gen_folder / "core" / "version.hpp",
264+
]:
265+
files.append(str(path.as_posix()))
282266
if sources:
283267
utility_functions_source_path = source_gen_folder / "variant" / "utility_functions.cpp"
284268
files.append(str(utility_functions_source_path.as_posix()))
285269

286270
return files
287271

288272

289-
def print_file_list(api_filepath, output_dir, headers=False, sources=False, compat=False, profile_filepath=""):
290-
print(*get_file_list(api_filepath, output_dir, headers, sources, compat, profile_filepath), sep=";", end=None)
273+
def print_file_list(api_filepath, output_dir, headers=False, sources=False, profile_filepath=""):
274+
print(*get_file_list(api_filepath, output_dir, headers, sources, profile_filepath), sep=";", end=None)
291275

292276

293277
def parse_build_profile(profile_filepath, api):
@@ -384,7 +368,7 @@ def scons_emit_files(target, source, env):
384368
if profile_filepath and not Path(profile_filepath).is_absolute():
385369
profile_filepath = str((Path(env.Dir("#").abspath) / profile_filepath).as_posix())
386370

387-
files = [env.File(f) for f in get_file_list(str(source[0]), target[0].abspath, True, True, env["godot_repo"] != "", profile_filepath)]
371+
files = [env.File(f) for f in get_file_list(str(source[0]), target[0].abspath, True, True, profile_filepath)]
388372
env.Clean(target, files)
389373
env["godot_cpp_gen_dir"] = target[0].abspath
390374
return files, source
@@ -423,7 +407,7 @@ def generate_bindings(api_filepath, use_template_get_node, bits="64", precision=
423407
generate_engine_classes_bindings(api, target_dir, use_template_get_node)
424408
generate_utility_functions(api, target_dir)
425409
if godot_repo != "":
426-
generate_compat_includes(godot_repo, output_dir)
410+
generate_compat_includes(godot_repo, output_dir, target_dir)
427411

428412

429413
CLASS_ALIASES = {
@@ -1567,40 +1551,49 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
15671551
header_file.write("\n".join(result))
15681552

15691553

1570-
def generate_compat_includes(godot_repo: Path, output_dir: Path):
1571-
file_types_mapping_godot_cpp_gen = map_header_files(output_dir)
1554+
def generate_compat_includes(godot_repo: Path, output_dir: Path, target_dir: Path):
1555+
target_dir = output_dir
1556+
file_types_mapping_godot_cpp_gen = map_header_files(target_dir)
15721557
file_types_mapping_godot = map_header_files(godot_repo)
15731558
# Match the headers
15741559
file_types_mapping = match_headers(file_types_mapping_godot_cpp_gen, file_types_mapping_godot)
1575-
15761560
for file_godot_cpp_name, file_godot_names in file_types_mapping.items():
1577-
file_godot_cpp_name = Path(file_godot_cpp_name).as_posix()
1578-
header_filepath = file_godot_cpp_name.replace("godot_cpp", "godot_compat")
1579-
if "gen/include" not in header_filepath:
1580-
header_filepath = header_filepath.replace("include/", "gen/include/")
1581-
Path(os.path.dirname(Path(output_dir) / Path(header_filepath))).mkdir(parents=True, exist_ok=True)
15821561
result = []
1583-
snake_header_name = camel_to_snake(header_filepath)
1584-
add_header(f"{snake_header_name}.hpp", result)
1562+
with (Path(target_dir) / Path(file_godot_cpp_name)).open("r", encoding="utf-8") as header_file:
1563+
content = header_file.readlines()
1564+
# Find the last line (#endif guard)
1565+
last_endif_idx = len(content) - 1
1566+
# Look for the marker for the header guard (usually #define)
1567+
marker_pos = next((i for i, line in enumerate(content) if line.startswith("#define")), -1)
1568+
1569+
if marker_pos != -1:
1570+
# Add content before the last #endif
1571+
before_marker = content[:marker_pos + 1] # Include up to the marker line
1572+
after_marker = content[marker_pos + 1:last_endif_idx] # Content excluding the final #endif
1573+
1574+
result.extend(before_marker) # Append original content up to marker
1575+
result.append("\n") # Blank line for separation
1576+
1577+
# Insert the #ifdef block
1578+
result.append(f"#ifdef GODOT_MODULE_COMPAT\n")
1579+
if len(file_godot_names) == 0:
1580+
print("No header found for", file_godot_cpp_name)
1581+
for file_godot_name in file_godot_names:
1582+
result.append(f"#include <{Path(file_godot_name).as_posix()}>\n")
1583+
result.append(f"#else\n")
1584+
1585+
result.extend(after_marker) # Append the remaining content inside the guard
1586+
1587+
# Add the namespace and endif
1588+
result.append(f"#endif\n")
1589+
1590+
# Finally, append the original final #endif line
1591+
result.append(content[last_endif_idx].strip())
15851592

1586-
header_guard = f"GODOT_COMPAT_{os.path.splitext(os.path.basename(header_filepath).upper())[0]}_HPP"
1587-
result.append(f"#ifndef {header_guard}")
1588-
result.append(f"#define {header_guard}")
1589-
result.append("")
1590-
result.append(f"#ifdef GODOT_MODULE_COMPAT")
1591-
for file_godot_name in file_godot_names:
1592-
result.append(f"#include <{Path(file_godot_name).as_posix()}>")
1593-
1594-
result.append(f"#else")
1595-
file_godot_cpp_name = file_godot_cpp_name.replace("gen/include/", "")
1596-
file_godot_cpp_name = file_godot_cpp_name.replace("include/", "")
1597-
result.append(f"#include <{file_godot_cpp_name}>")
1598-
result.append(f"using namespace godot;")
1599-
result.append(f"#endif")
1600-
result.append("")
1601-
result.append(f"#endif // ! {header_guard}")
1602-
with (Path(output_dir) / Path(header_filepath)).open("w+", encoding="utf-8") as header_file:
1603-
header_file.write("\n".join(result))
1593+
else:
1594+
print(f"Marker not found in {file_godot_cpp_name}")
1595+
with (Path(target_dir) / Path(file_godot_cpp_name)).open("w+", encoding="utf-8") as header_file:
1596+
header_file.write("".join(result))
16041597

16051598

16061599
def generate_engine_class_header(class_api, used_classes, fully_used_classes, use_template_get_node):

compat_generator.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,28 @@
77

88

99
def parse_header_file(file_path):
10-
types = {"classes": [], "structs": [], "defines": []}
10+
types = {"classes": [], "structs": [], "defines": [], "enums": []}
1111

1212
with open(file_path, "r", encoding="utf-8") as file:
1313
content = file.read()
1414

1515
# Regular expressions to match different types
16-
struct_pattern = r"struct\s+[\w\s]*?([a-zA-Z_]\w*)\s*[:{]"
16+
struct_pattern = r"struct.*\s(\S+)\s*\{"
17+
type_pattern = r"typedef.*\s(\S+)\s*\;"
18+
enum_pattern = r"enum.*\s(\S+)\s*\{"
1719
class_pattern = r"class\s+[\w\s]*?([a-zA-Z_]\w*)\s*[:{]"
1820
define_pattern = r"#define\s+([a-zA-Z_]\w*)"
1921

2022
# Extract classes
2123
types["classes"] += re.findall(class_pattern, content)
2224

2325
# Extract structs
24-
types["structs"] += re.findall(struct_pattern, content)
26+
struct_names = re.findall(struct_pattern, content)
27+
types["structs"].extend(struct_names)
28+
type_names = re.findall(type_pattern, content)
29+
types["structs"].extend(type_names)
30+
enum_names = re.findall(enum_pattern, content)
31+
types["enums"].extend(enum_names)
2532

2633
# Extract defines
2734
define_matches = re.findall(define_pattern, content)
@@ -35,7 +42,6 @@ def parse_header_file(file_path):
3542

3643
def map_header_files(directory):
3744
file_types_mapping = {}
38-
3945
for root, dirs, files in os.walk(directory):
4046
if "thirdparty" in dirs:
4147
dirs.remove("thirdparty")
@@ -57,8 +63,8 @@ def map_header_files(directory):
5763

5864

5965
if __name__ == "__main__":
60-
# Get current directory
61-
current_directory = os.getcwd()
66+
# Get current directory for godot-cpp
67+
current_directory = os.path.join(os.getcwd(), "")
6268
mapping_name = ""
6369
if len(sys.argv) > 1:
6470
mapping_name = f"_godot"

header_matcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def match_headers(mapping1, mapping2):
1111
matches[header_file] = []
1212
if (any(class_name in data2["classes"] for class_name in data1["classes"]) or
1313
any(define_name in data2["defines"] for define_name in data1["defines"]) or
14-
any(define_name in data2["structs"] for define_name in data1["structs"])):
14+
any(define_name in data2["structs"] for define_name in data1["structs"])):# or
15+
#any(define_name in data2["enums"] for define_name in data1["enums"])):
1516
matches[header_file].append(header_file2)
1617
return matches
1718

0 commit comments

Comments
 (0)