From 55010a2d9f91798f08bd2deded9445a4354ee3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilles=20Roudi=C3=A8re?= Date: Wed, 12 Oct 2022 16:21:34 +0200 Subject: [PATCH] Implement a way to dump the gdnative_interface.h file from the executable --- .gitignore | 1 + core/extension/SCsub | 7 +++- core/extension/make_interface_dumper.py | 47 +++++++++++++++++++++++++ main/main.cpp | 20 +++++++++++ misc/dist/linux/godot.6 | 6 ++++ misc/dist/shell/_godot.zsh-completion | 1 + misc/dist/shell/godot.bash-completion | 1 + misc/dist/shell/godot.fish | 1 + 8 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 core/extension/make_interface_dumper.py diff --git a/.gitignore b/.gitignore index 1a83e4707e8..f430ac10e94 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ platform/windows/godot_res.res # Generated by Godot binary .import/ +/gdnative_interface.h extension_api.json logs/ diff --git a/core/extension/SCsub b/core/extension/SCsub index 23727c1b769..a8f68a15334 100644 --- a/core/extension/SCsub +++ b/core/extension/SCsub @@ -3,10 +3,15 @@ Import("env") import make_wrappers +import make_interface_dumper from platform_methods import run_in_subprocess env.CommandNoCache(["ext_wrappers.gen.inc"], "make_wrappers.py", run_in_subprocess(make_wrappers.run)) - +env.CommandNoCache( + "gdnative_interface_dump.gen.h", + ["gdnative_interface.h", "make_interface_dumper.py"], + run_in_subprocess(make_interface_dumper.run), +) env_extension = env.Clone() diff --git a/core/extension/make_interface_dumper.py b/core/extension/make_interface_dumper.py new file mode 100644 index 00000000000..cc85c728d5c --- /dev/null +++ b/core/extension/make_interface_dumper.py @@ -0,0 +1,47 @@ +def run(target, source, env): + src = source[0] + dst = target[0] + f = open(src, "r", encoding="utf-8") + g = open(dst, "w", encoding="utf-8") + + g.write( + """/* THIS FILE IS GENERATED DO NOT EDIT */ +#ifndef GDNATIVE_INTERFACE_DUMP_H +#define GDNATIVE_INTERFACE_DUMP_H + +#ifdef TOOLS_ENABLED + +#include "core/io/file_access.h" +#include "core/string/ustring.h" + +class GDNativeInterfaceDump { + private: + static constexpr char const *gdnative_interface_dump =""" + ) + for line in f: + g.write('"' + line.rstrip().replace('"', '\\"') + '\\n"\n') + g.write(";\n") + + g.write( + """ + public: + static void generate_gdnative_interface_file(const String &p_path) { + Ref fa = FileAccess::open(p_path, FileAccess::WRITE); + CharString cs(gdnative_interface_dump); + fa->store_buffer((const uint8_t *)cs.ptr(), cs.length()); + }; +}; + +#endif // TOOLS_ENABLED + +#endif // GDNATIVE_INTERFACE_DUMP_H +""" + ) + g.close() + f.close() + + +if __name__ == "__main__": + from platform_methods import subprocess_main + + subprocess_main(globals()) diff --git a/main/main.cpp b/main/main.cpp index 08a9b4c310c..85415e24d2c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -36,6 +36,7 @@ #include "core/crypto/crypto.h" #include "core/debugger/engine_debugger.h" #include "core/extension/extension_api_dump.h" +#include "core/extension/gdnative_interface_dump.gen.h" #include "core/extension/native_extension_manager.h" #include "core/input/input.h" #include "core/input/input_map.h" @@ -199,6 +200,7 @@ static MovieWriter *movie_writer = nullptr; static bool disable_vsync = false; static bool print_fps = false; #ifdef TOOLS_ENABLED +static bool dump_gdnative_interface = false; static bool dump_extension_api = false; #endif bool profile_gpu = false; @@ -414,6 +416,7 @@ void Main::print_help(const char *p_binary) { OS::get_singleton()->print(" --doctool [] Dump the engine API reference to the given (defaults to current dir) in XML format, merging if existing files are found.\n"); OS::get_singleton()->print(" --no-docbase Disallow dumping the base types (used with --doctool).\n"); OS::get_singleton()->print(" --build-solutions Build the scripting solutions (e.g. for C# projects). Implies --editor and requires a valid project to edit.\n"); + OS::get_singleton()->print(" --dump-gdextension-interface Generate GDExtension header file 'gdnative_interface.h' in the current folder. This file is the base file required to implement a GDExtension.\n"); OS::get_singleton()->print(" --dump-extension-api Generate JSON dump of the Godot API for GDExtension bindings named 'extension_api.json' in the current folder.\n"); OS::get_singleton()->print(" --startup-benchmark Benchmark the startup time and print it to console.\n"); OS::get_singleton()->print(" --startup-benchmark-file Benchmark the startup time and save it to a given file in JSON format.\n"); @@ -1054,6 +1057,16 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph auto_build_solutions = true; editor = true; cmdline_tool = true; + } else if (I->get() == "--dump-gdextension-interface") { + // Register as an editor instance to use low-end fallback if relevant. + editor = true; + cmdline_tool = true; + dump_gdnative_interface = true; + print_line("Dumping gdnative interface header file"); + // Hack. Not needed but otherwise we end up detecting that this should + // run the project instead of a cmdline tool. + // Needs full refactoring to fix properly. + main_args.push_back(I->get()); } else if (I->get() == "--dump-extension-api") { // Register as an editor instance to use low-end fallback if relevant. editor = true; @@ -2549,8 +2562,15 @@ bool Main::start() { return false; } + if (dump_gdnative_interface) { + GDNativeInterfaceDump::generate_gdnative_interface_file("gdnative_interface.h"); + } + if (dump_extension_api) { NativeExtensionAPIDump::generate_extension_json_file("extension_api.json"); + } + + if (dump_gdnative_interface || dump_extension_api) { return false; } diff --git a/misc/dist/linux/godot.6 b/misc/dist/linux/godot.6 index 07e2a389a74..766be6d0a72 100644 --- a/misc/dist/linux/godot.6 +++ b/misc/dist/linux/godot.6 @@ -150,6 +150,12 @@ Disallow dumping the base types (used with \fB\-\-doctool\fR). \fB\-\-build\-solutions\fR Build the scripting solutions (e.g. for C# projects). Implies \-\-editor and requires a valid project to edit. .TP +\fB\-\-dump\-gdextension\-interface\fR +Generate GDExtension header file 'gdnative_interface.h' in the current folder. This file is the base file required to implement a GDExtension. +.TP +\fB\-\-dump\-extension\-api\fR +Generate JSON dump of the Godot API for GDExtension bindings named 'extension_api.json' in the current folder. +.TP \fB\-\-test\fR Run a unit test ('string', 'math', 'physics', 'physics_2d', 'render', 'oa_hash_map', 'gui', 'shaderlang', 'gd_tokenizer', 'gd_parser', 'gd_compiler', 'gd_bytecode', 'ordered_hash_map', 'astar'). .SH FILES diff --git a/misc/dist/shell/_godot.zsh-completion b/misc/dist/shell/_godot.zsh-completion index 2bc6fe93176..18b74115bf0 100644 --- a/misc/dist/shell/_godot.zsh-completion +++ b/misc/dist/shell/_godot.zsh-completion @@ -84,5 +84,6 @@ _arguments \ '--doctool[dump the engine API reference to the given path in XML format, merging if existing files are found]:path to base Godot build directory (optional):_dirs' \ '--no-docbase[disallow dumping the base types (used with --doctool)]' \ '--build-solutions[build the scripting solutions (e.g. for C# projects)]' \ + '--dump-gdextension-interface[generate GDExtension header file 'gdnative_interface.h' in the current folder. This file is the base file required to implement a GDExtension.]' \ '--dump-extension-api[generate JSON dump of the Godot API for GDExtension bindings named "extension_api.json" in the current folder]' \ '--test[run all unit tests; run with "--test --help" for more information]' diff --git a/misc/dist/shell/godot.bash-completion b/misc/dist/shell/godot.bash-completion index bc5fa600f5f..871c8f8eace 100644 --- a/misc/dist/shell/godot.bash-completion +++ b/misc/dist/shell/godot.bash-completion @@ -86,6 +86,7 @@ _complete_godot_options() { --doctool --no-docbase --build-solutions +--dump-gdextension-interface --dump-extension-api --test " -- "$1")) diff --git a/misc/dist/shell/godot.fish b/misc/dist/shell/godot.fish index 9ac692eace2..9cbec8a4f6d 100644 --- a/misc/dist/shell/godot.fish +++ b/misc/dist/shell/godot.fish @@ -106,5 +106,6 @@ complete -c godot -l validate-conversion-3to4 -d "Shows what elements will be re complete -c godot -l doctool -d "Dump the engine API reference to the given path in XML format, merging if existing files are found" -r complete -c godot -l no-docbase -d "Disallow dumping the base types (used with --doctool)" complete -c godot -l build-solutions -d "Build the scripting solutions (e.g. for C# projects)" +complete -c godot -l dump-gdextension-interface -d "Generate GDExtension header file 'gdnative_interface.h' in the current folder. This file is the base file required to implement a GDExtension" complete -c godot -l dump-extension-api -d "Generate JSON dump of the Godot API for GDExtension bindings named 'extension_api.json' in the current folder" complete -c godot -l test -d "Run all unit tests; run with '--test --help' for more information" -x