From 29b34d92bbaa467fa6f9b28071563ffb340702ba Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Sun, 5 Nov 2023 23:04:03 +0200 Subject: [PATCH] [iOS] Fix building as static library or xcframework, add iOS config and xcframework build script to the test project. --- include/godot_cpp/classes/wrapped.hpp | 2 +- include/godot_cpp/core/memory.hpp | 23 ++++++++++--------- src/core/memory.cpp | 11 +++++---- test/SConstruct | 11 +++++++++ test/generate_xcframework.sh | 7 ++++++ .../Resources/Info.plist | 0 .../Resources/Info.plist | 0 test/project/example.gdextension | 16 +++++++++++++ test/project/project.godot | 1 + 9 files changed, 54 insertions(+), 17 deletions(-) create mode 100755 test/generate_xcframework.sh rename test/project/bin/{libgdexample.osx.template_debug.framework => libgdexample.macos.template_debug.framework}/Resources/Info.plist (100%) rename test/project/bin/{libgdexample.osx.template_release.framework => libgdexample.macos.template_release.framework}/Resources/Info.plist (100%) diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index 75bd3b0..f8f921b 100644 --- a/include/godot_cpp/classes/wrapped.hpp +++ b/include/godot_cpp/classes/wrapped.hpp @@ -450,7 +450,7 @@ public: \ static void *_gde_binding_create_callback(void *p_token, void *p_instance) { \ /* Do not call memnew here, we don't want the post-initializer to be called */ \ - return new ("") m_class((GodotObject *)p_instance); \ + return new ("", "") m_class((GodotObject *)p_instance); \ } \ static void _gde_binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \ /* Explicitly call the deconstructor to ensure proper lifecycle for non-trivial members */ \ diff --git a/include/godot_cpp/core/memory.hpp b/include/godot_cpp/core/memory.hpp index 548f330..97a2b13 100644 --- a/include/godot_cpp/core/memory.hpp +++ b/include/godot_cpp/core/memory.hpp @@ -44,20 +44,21 @@ #define PAD_ALIGN 16 //must always be greater than this at much #endif -void *operator new(size_t p_size, const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool -void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool -void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory +// p_dummy argument is added to avoid conflicts with the engine functions when both engine and GDExtension are built as a static library on iOS. +void *operator new(size_t p_size, const char *p_dummy, const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool +void *operator new(size_t p_size, const char *p_dummy, void *(*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool +void *operator new(size_t p_size, const char *p_dummy, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory -_ALWAYS_INLINE_ void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description) { +_ALWAYS_INLINE_ void *operator new(size_t p_size, const char *p_dummy, void *p_pointer, size_t check, const char *p_description) { return p_pointer; } #ifdef _MSC_VER // When compiling with VC++ 2017, the above declarations of placement new generate many irrelevant warnings (C4291). // The purpose of the following definitions is to muffle these warnings, not to provide a usable implementation of placement delete. -void operator delete(void *p_mem, const char *p_description); -void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)); -void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description); +void operator delete(void *p_mem, const char *p_dummy, const char *p_description); +void operator delete(void *p_mem, const char *p_dummy, void *(*p_allocfunc)(size_t p_size)); +void operator delete(void *p_mem, const char *p_dummy, void *p_pointer, size_t check, const char *p_description); #endif namespace godot { @@ -85,10 +86,10 @@ _ALWAYS_INLINE_ T *_post_initialize(T *p_obj) { #define memrealloc(m_mem, m_size) ::godot::Memory::realloc_static(m_mem, m_size) #define memfree(m_mem) ::godot::Memory::free_static(m_mem) -#define memnew(m_class) ::godot::_post_initialize(new ("") m_class) +#define memnew(m_class) ::godot::_post_initialize(new ("", "") m_class) -#define memnew_allocator(m_class, m_allocator) ::godot::_post_initialize(new (m_allocator::alloc) m_class) -#define memnew_placement(m_placement, m_class) ::godot::_post_initialize(new (m_placement, sizeof(m_class), "") m_class) +#define memnew_allocator(m_class, m_allocator) ::godot::_post_initialize(new ("", m_allocator::alloc) m_class) +#define memnew_placement(m_placement, m_class) ::godot::_post_initialize(new ("", m_placement, sizeof(m_class), "") m_class) // Generic comparator used in Map, List, etc. template @@ -154,7 +155,7 @@ T *memnew_arr_template(size_t p_elements, const char *p_descr = "") { /* call operator new */ for (size_t i = 0; i < p_elements; i++) { - new (&elems[i], sizeof(T), p_descr) T; + new ("", &elems[i], sizeof(T), p_descr) T; } } diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 80e71ec..8118511 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -103,28 +103,29 @@ _GlobalNil _GlobalNilClass::_nil; } // namespace godot -void *operator new(size_t p_size, const char *p_description) { +// p_dummy argument is added to avoid conflicts with the engine functions when both engine and GDExtension are built as a static library on iOS. +void *operator new(size_t p_size, const char *p_dummy, const char *p_description) { return godot::Memory::alloc_static(p_size); } -void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) { +void *operator new(size_t p_size, const char *p_dummy, void *(*p_allocfunc)(size_t p_size)) { return p_allocfunc(p_size); } using namespace godot; #ifdef _MSC_VER -void operator delete(void *p_mem, const char *p_description) { +void operator delete(void *p_mem, const char *p_dummy, const char *p_description) { ERR_PRINT("Call to placement delete should not happen."); CRASH_NOW(); } -void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)) { +void operator delete(void *p_mem, const char *p_dummy, void *(*p_allocfunc)(size_t p_size)) { ERR_PRINT("Call to placement delete should not happen."); CRASH_NOW(); } -void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description) { +void operator delete(void *p_mem, const char *p_dummy, void *p_pointer, size_t check, const char *p_description) { ERR_PRINT("Call to placement delete should not happen."); CRASH_NOW(); } diff --git a/test/SConstruct b/test/SConstruct index 1732ecd..9c25917 100644 --- a/test/SConstruct +++ b/test/SConstruct @@ -23,6 +23,17 @@ if env["platform"] == "macos": ), source=sources, ) +elif env["platform"] == "ios": + if env["ios_simulator"]: + library = env.StaticLibrary( + "project/bin/libgdexample.{}.{}.simulator.a".format(env["platform"], env["target"]), + source=sources, + ) + else: + library = env.StaticLibrary( + "project/bin/libgdexample.{}.{}.a".format(env["platform"], env["target"]), + source=sources, + ) else: library = env.SharedLibrary( "project/bin/libgdexample{}{}".format(env["suffix"], env["SHLIBSUFFIX"]), diff --git a/test/generate_xcframework.sh b/test/generate_xcframework.sh new file mode 100755 index 0000000..7adddff --- /dev/null +++ b/test/generate_xcframework.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +scons arch=universal ios_simulator=yes platform=ios target=$1 $2 +scons arch=arm64 ios_simulator=no platform=ios target=$1 $2 + +xcodebuild -create-xcframework -library ./project/bin/libgdexample.ios.$1.a -library ./project/bin/libgdexample.ios.$1.simulator.a -output ./project/bin/libgdexample.ios.$1.xcframework +xcodebuild -create-xcframework -library ../bin/libgodot-cpp.ios.$1.arm64.a -library ../bin/libgodot-cpp.ios.$1.universal.simulator.a -output ./project/bin/libgodot-cpp.ios.$1.xcframework diff --git a/test/project/bin/libgdexample.osx.template_debug.framework/Resources/Info.plist b/test/project/bin/libgdexample.macos.template_debug.framework/Resources/Info.plist similarity index 100% rename from test/project/bin/libgdexample.osx.template_debug.framework/Resources/Info.plist rename to test/project/bin/libgdexample.macos.template_debug.framework/Resources/Info.plist diff --git a/test/project/bin/libgdexample.osx.template_release.framework/Resources/Info.plist b/test/project/bin/libgdexample.macos.template_release.framework/Resources/Info.plist similarity index 100% rename from test/project/bin/libgdexample.osx.template_release.framework/Resources/Info.plist rename to test/project/bin/libgdexample.macos.template_release.framework/Resources/Info.plist diff --git a/test/project/example.gdextension b/test/project/example.gdextension index 30279e6..4f599ce 100644 --- a/test/project/example.gdextension +++ b/test/project/example.gdextension @@ -11,8 +11,14 @@ windows.debug.x86_32 = "res://bin/libgdexample.windows.template_debug.x86_32.dll windows.release.x86_32 = "res://bin/libgdexample.windows.template_release.x86_32.dll" windows.debug.x86_64 = "res://bin/libgdexample.windows.template_debug.x86_64.dll" windows.release.x86_64 = "res://bin/libgdexample.windows.template_release.x86_64.dll" +windows.debug.arm64 = "res://bin/libgdexample.windows.template_debug.arm64.dll" +windows.release.arm64 = "res://bin/libgdexample.windows.template_release.arm64.dll" +linux.debug.x86_32 = "res://bin/libgdexample.linux.template_debug.x86_32.so" +linux.release.x86_32 = "res://bin/libgdexample.linux.template_release.x86_32.so" linux.debug.x86_64 = "res://bin/libgdexample.linux.template_debug.x86_64.so" linux.release.x86_64 = "res://bin/libgdexample.linux.template_release.x86_64.so" +linux.debug.arm32 = "res://bin/libgdexample.linux.template_debug.arm32.so" +linux.release.arm32 = "res://bin/libgdexample.linux.template_release.arm32.so" linux.debug.arm64 = "res://bin/libgdexample.linux.template_debug.arm64.so" linux.release.arm64 = "res://bin/libgdexample.linux.template_release.arm64.so" linux.debug.rv64 = "res://bin/libgdexample.linux.template_debug.rv64.so" @@ -21,5 +27,15 @@ android.debug.x86_64 = "res://bin/libgdexample.android.template_debug.x86_64.so" android.release.x86_64 = "res://bin/libgdexample.android.template_release.x86_64.so" android.debug.arm64 = "res://bin/libgdexample.android.template_debug.arm64.so" android.release.arm64 = "res://bin/libgdexample.android.template_release.arm64.so" +ios.debug = "res://bin/libgdexample.ios.template_debug.xcframework" +ios.release = "res://bin/libgdexample.ios.template_release.xcframework" web.debug.wasm32 = "res://bin/libgdexample.web.template_debug.wasm32.wasm" web.release.wasm32 = "res://bin/libgdexample.web.template_release.wasm32.wasm" + +[dependencies] +ios.debug = { + "res://bin/libgodot-cpp.ios.template_debug.xcframework": "" +} +ios.release = { + "res://bin/libgodot-cpp.ios.template_release.xcframework": "" +} diff --git a/test/project/project.godot b/test/project/project.godot index 3ed679b..4f51c07 100644 --- a/test/project/project.godot +++ b/test/project/project.godot @@ -21,4 +21,5 @@ paths=["res://example.gdextension"] [rendering] +textures/vram_compression/import_etc2_astc=true environment/defaults/default_environment="res://default_env.tres"