[Feature] Hook System for Binding Generation#2023
Conversation
|
This second approach uses no SCons arguments, as suggested by @Ivorforce Users that want to define their own hooks would still do so in a dedicated file, still extending from custom_generator import CustomBindingGeneratorHooksand env = SConscript("godot-cpp/SConstruct", {"env": env, "customs": customs, "binding_hooks": CustomBindingGeneratorHooks()})This second snippet is similar to the thing that is already present in the template here: https://github.com/godotengine/godot-cpp-template/blob/8283d9632c00ba4654d4e3201f651df01ef51172/SConstruct#L38. Below you can find an example of a custom binding hook, this would be placed in the project's root directory so that import sys
sys.path.insert(0, "godot-cpp")
from tools.binding_generator_hooks import BindingGeneratorHooks
class CustomBindingGeneratorHooks(BindingGeneratorHooks):
def alter_engine_class_header(self, class_api, lines):
signals = []
if "signals" in class_api:
for signal_api in class_api["signals"]:
name = signal_api["name"]
signal_constant = "\tstatic constexpr char SIGNAL_" + name.upper() + '[] = "' + name + '";'
signals.append(signal_constant)
try:
idx = lines.index("public:") + 1
for signal_const in signals:
lines.insert(idx, signal_const)
idx += 1
except ValueError:
print("no public keyword found, not adding signals")
return lines |
dsnopek
left a comment
There was a problem hiding this comment.
Thanks!
Overall, I think this is a pretty good direction.
However, I'm not crazy about adding binding_generator_hooks.py at the top-level - we're always trying to reduce the amount of stuff we have at the top-level. So, if we can have that in a sub-directory, that would be ideal
And, in order for this to be merged, we'll need to:
- Add support for CMake - it'll likely need to be done differently, maybe giving a file name and using a fixed class name, or something like that?
- Discuss the API of
BindingGeneratorHooksto ensure it covers everything we want to cover and that everyone is happy with it
|
Alright, before I'll continue with implementation two things:
|
|
My personal opinion is:
|
9b7afcc to
2f70932
Compare
|
Moved Regarding CMake: Current API of
|
dsnopek
left a comment
There was a problem hiding this comment.
Thanks!
This is looking good to me. At this point, this just needs wider discussion (I've added it to the agenda for the next GDExtension team meeting) and some review from folks who know the build system stuff better
This draft aims to collect more feedback and ideas on a hook system for binding generation. The core idea is that users should be able to extend/modify the way the bindings are generated. The current implementation adds a new command to SCons, called
binding_hook_file, which takes a file path to a python file. The specified python file should define a class that extendsBindingGeneratorHooksand is calledBindingGeneratorHooksExtension. It can then override the methods provided there to modify the way the bindings are generated. As an example, there iscustom_generator.pyincluded in this draft (won't be included in the final PR), which adds names of signals as constants for each header file. It can be tested usingscons binding_hook_file=custom_generator.py.The current implementation was already discussed here: https://chat.godotengine.org/channel/gdextension/thread/2gR6bngEu57tWLQG9