Skip to content

Commit 9b7afcc

Browse files
committed
approach with no scons argument
1 parent 99dd124 commit 9b7afcc

7 files changed

Lines changed: 46 additions & 44 deletions

File tree

SConstruct

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ except Exception:
1919

2020
env.PrependENVPath("PATH", os.getenv("PATH"))
2121

22+
try:
23+
Import("binding_hooks")
24+
except Exception:
25+
# binding_hooks was not exported by the user's env
26+
binding_hooks = None
27+
28+
env["binding_hooks"] = binding_hooks
29+
2230
# Custom options and profile flags.
2331
customs = ["custom.py"]
2432
try:

binding_generator.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python
22

3+
import importlib.util
34
import json
45
import re
56
import shutil
7+
import sys
68
from pathlib import Path
79

810
from make_interface_header import generate_gdextension_interface_header
@@ -284,12 +286,34 @@ def print_file_list(api_filepath, output_dir, headers=False, sources=False):
284286

285287

286288
def generate_bindings(
287-
api_filepath, interface_filepath, use_template_get_node, bits="64", precision="single", output_dir="."
289+
api_filepath,
290+
interface_filepath,
291+
use_template_get_node,
292+
bits="64",
293+
precision="single",
294+
output_dir=".",
295+
hooks_path=None,
288296
):
289297
api = {}
290298
with open(api_filepath, encoding="utf-8") as api_file:
291299
api = json.load(api_file)
292-
_generate_bindings(api, api_filepath, interface_filepath, use_template_get_node, bits, precision, output_dir)
300+
custom_hooks = None
301+
if hooks_path:
302+
# load the file dynamically
303+
try:
304+
spec = importlib.util.spec_from_file_location("custom_binding_generator_hooks", hooks_path)
305+
loaded_module = importlib.util.module_from_spec(spec)
306+
sys.modules["custom_binding_generator_hooks"] = loaded_module
307+
spec.loader.exec_module(loaded_module)
308+
# assume the class is named 'CustomBindingGeneratorHooks'
309+
custom_hooks = loaded_module.CustomBindingGeneratorHooks()
310+
except Exception:
311+
raise Exception(
312+
"Failed to load custom binding generator hooks. Make sure your path points to a python file which defines a class named 'BindingGeneratorHooks'"
313+
)
314+
_generate_bindings(
315+
api, api_filepath, interface_filepath, use_template_get_node, bits, precision, output_dir, custom_hooks
316+
)
293317

294318

295319
def _generate_bindings(

cmake/GodotCPPModule.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function(
9191
BITS
9292
PRECISION
9393
OUTPUT_DIR
94+
BINDING_HOOK_FILE
9495
)
9596
# This code snippet will be squashed into a single line
9697
set(PYTHON_SCRIPT
@@ -101,7 +102,8 @@ function(
101102
use_template_get_node='${USE_TEMPLATE_GET_NODE}',
102103
bits='${BITS}',
103104
precision='${PRECISION}',
104-
output_dir='${OUTPUT_DIR}')"
105+
output_dir='${OUTPUT_DIR}',
106+
hooks_path='${BINDING_HOOK_FILE}')"
105107
)
106108

107109
message(DEBUG "Python:\n${PYTHON_SCRIPT}")

cmake/godotcpp.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ function(godotcpp_options)
145145
"Path to a custom GDExtension API JSON file (takes precedence over `GODOTCPP_GDEXTENSION_DIR` and `GODOTCPP_API_VERSION`) ( /path/to/custom_api_file )"
146146
)
147147

148+
set(GODOTCPP_BINDING_HOOK_FILE
149+
""
150+
CACHE FILEPATH
151+
"Path to a Python file defining custom binding generator hooks. The file has to contain a class named `CustomBindingGeneratorHooks`"
152+
)
153+
148154
#TODO generate_bindings
149155

150156
option(GODOTCPP_GENERATE_TEMPLATE_GET_NODE "Generate a template version of the Node class's get_node. (ON|OFF)" ON)
@@ -309,6 +315,7 @@ function(godotcpp_generate)
309315
"${BITS}"
310316
"${GODOTCPP_PRECISION}"
311317
"${CMAKE_CURRENT_BINARY_DIR}"
318+
"${GODOTCPP_BINDING_HOOK_FILE}"
312319
)
313320

314321
### Platform is derived from the toolchain target

custom_generator.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

tools/godotcpp.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import importlib.util
21
import os
32
import platform
43
import sys
@@ -14,7 +13,6 @@
1413
from SCons.Variables.BoolVariable import _text2bool
1514

1615
from binding_generator import _generate_bindings, _get_file_list, get_file_list
17-
from binding_generator_hooks import BindingGeneratorHooks
1816
from build_profile import generate_trimmed_api
1917
from doc_source_generator import scons_generate_doc_source
2018

@@ -166,16 +164,7 @@ def scons_generate_bindings(target, source, env):
166164

167165
api = generate_trimmed_api(str(source[0]), profile_filepath)
168166

169-
custom_hooks: BindingGeneratorHooks = None
170-
if "binding_hook_file" in env:
171-
binding_hook_file = env["binding_hook_file"]
172-
# yes apparently this is how you import a file dynamically ._.
173-
spec = importlib.util.spec_from_file_location("custom_binding_generator", binding_hook_file)
174-
loaded_module = importlib.util.module_from_spec(spec)
175-
sys.modules["custom_binding_generator"] = loaded_module
176-
spec.loader.exec_module(loaded_module)
177-
# assume the name 'BindingGeneratorHooksExtension' for the class
178-
custom_hooks = loaded_module.BindingGeneratorHooksExtension()
167+
binding_hooks = env.get("binding_hooks", None)
179168

180169
_generate_bindings(
181170
api,
@@ -185,7 +174,7 @@ def scons_generate_bindings(target, source, env):
185174
"32" if "32" in env["arch"] else "64",
186175
env["precision"],
187176
env["godot_cpp_gen_dir"],
188-
custom_hooks,
177+
binding_hooks,
189178
)
190179
return None
191180

@@ -244,15 +233,6 @@ def options(opts, env):
244233
else:
245234
raise ValueError("Could not detect platform automatically, please specify with platform=<platform>")
246235

247-
opts.Add(
248-
PathVariable(
249-
key="binding_hook_file",
250-
help="Path to python file containing custom binding generator hooks",
251-
default=env.get("binding_hook_file", None),
252-
validator=validate_file,
253-
)
254-
)
255-
256236
opts.Add(
257237
PathVariable(
258238
key="custom_tools",

0 commit comments

Comments
 (0)