[Draft 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 SignalGenerationHooksand env = SConscript("godot-cpp/SConstruct", {"env": env, "customs": customs, "binding_hooks": SignalGenerationHooks()})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 binding_generator_hooks import BindingGeneratorHooks
class SignalGenerationHooks(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 |
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