Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.2] Add support of iOS's dynamic libraries to GDNative #39996

Merged
merged 5 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions misc/dist/ios_xcode/godot_ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
D0BCFE7818AEBFEB004A7AAE /* $binary.pck in Resources */ = {isa = PBXBuildFile; fileRef = D0BCFE7718AEBFEB004A7AAE /* $binary.pck */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
90A13CD024AA68E500E8464F /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
$pbx_embeded_frameworks
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
1F1575711F582BE20003B888 /* dylibs */ = {isa = PBXFileReference; lastKnownFileType = folder; name = dylibs; path = "$binary/dylibs"; sourceTree = "<group>"; };
DEADBEEF1F582BE20003B888 /* $binary.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = godot; path = "$binary.a"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -105,6 +119,7 @@
D0BCFE3018AEBDA2004A7AAE /* Sources */,
D0BCFE3118AEBDA2004A7AAE /* Frameworks */,
D0BCFE3218AEBDA2004A7AAE /* Resources */,
90A13CD024AA68E500E8464F /* Embed Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -230,7 +245,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1F1575721F582BE20003B888 /* dylibs in Resources */,
D07CD44E1C5D589C00B7FB28 /* Images.xcassets in Resources */,
D0BCFE7818AEBFEB004A7AAE /* $binary.pck in Resources */,
D0BCFE4618AEBDA2004A7AAE /* InfoPlist.strings in Resources */,
Expand Down Expand Up @@ -284,7 +298,9 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "$code_sign_identity_debug";
COPY_PHASE_STRIP = NO;
ENABLE_BITCODE = NO;
"FRAMEWORK_SEARCH_PATHS[arch=*]" = "$binary/**";
"FRAMEWORK_SEARCH_PATHS[arch=*]" = (
"$(PROJECT_DIR)/**",
);
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -327,7 +343,9 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "$code_sign_identity_release";
COPY_PHASE_STRIP = YES;
ENABLE_BITCODE = NO;
"FRAMEWORK_SEARCH_PATHS[arch=*]" = "$binary/**";
"FRAMEWORK_SEARCH_PATHS[arch=*]" = (
"$(PROJECT_DIR)/**",
);
ENABLE_NS_ASSERTIONS = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down Expand Up @@ -357,6 +375,10 @@
DEVELOPMENT_TEAM = $team_id;
INFOPLIST_FILE = "$binary/$binary-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
Expand All @@ -383,6 +405,10 @@
DEVELOPMENT_TEAM = $team_id;
INFOPLIST_FILE = "$binary/$binary-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
Expand Down
2 changes: 2 additions & 0 deletions misc/dist/ios_xcode/godot_ios/godot_ios-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<string>$signature</string>
<key>CFBundleVersion</key>
<string>$version</string>
<key>ITSAppUsesNonExemptEncryption</key>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Godot includes some encryption features, both in its thirdparty libraries and some of it are exposed to the public scripting API (notably via the Crypto singleton). Did you assess if they fall within the "exempt" category for Apple?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I've seen from the source code, Godot does not use any self-written encryption algorithm, so it's should be ok. So far any app that I've published was okay with using any standard encryption.

<false />
<key>LSRequiresIPhoneOS</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
Expand Down
20 changes: 19 additions & 1 deletion modules/gdnative/gdnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,26 @@ bool GDNative::initialize() {
return false;
}
#ifdef IPHONE_ENABLED
// on iOS we use static linking
// On iOS we use static linking by default.
String path = "";

// On iOS dylibs is not allowed, but can be replaced with .framework or .xcframework.
// If they are used, we can run dlopen on them.
// They should be located under Frameworks directory, so we need to replace library path.
if (!lib_path.ends_with(".a")) {
path = ProjectSettings::get_singleton()->globalize_path(lib_path);

if (!FileAccess::exists(path)) {
String lib_name = lib_path.get_basename().get_file();
String framework_path_format = "Frameworks/$name.framework/$name";

Dictionary format_dict;
format_dict["name"] = lib_name;
String framework_path = framework_path_format.format(format_dict, "$_");

path = OS::get_singleton()->get_executable_path().get_base_dir().plus_file(framework_path);
}
}
#elif defined(ANDROID_ENABLED)
// On Android dynamic libraries are located separately from resource assets,
// we should pass library name to dlopen(). The library name is flattened
Expand Down
20 changes: 17 additions & 3 deletions modules/gdnative/gdnative_library_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,25 @@ void GDNativeLibraryEditor::_on_item_button(Object *item, int column, int id) {

if (id == BUTTON_SELECT_LIBRARY || id == BUTTON_SELECT_DEPENDENCES) {

TreeItem *treeItem = Object::cast_to<TreeItem>(item)->get_parent();
EditorFileDialog::Mode mode = EditorFileDialog::MODE_OPEN_FILE;
if (id == BUTTON_SELECT_DEPENDENCES)

if (id == BUTTON_SELECT_DEPENDENCES) {
mode = EditorFileDialog::MODE_OPEN_FILES;
} else if (treeItem->get_text(0) == "iOS") {
mode = EditorFileDialog::MODE_OPEN_ANY;
}

file_dialog->set_meta("target", target);
file_dialog->set_meta("section", section);
file_dialog->clear_filters();
file_dialog->add_filter(Object::cast_to<TreeItem>(item)->get_parent()->get_metadata(0));

String filter_string = treeItem->get_metadata(0);
Vector<String> filters = filter_string.split(",", false, 0);
for (int i = 0; i < filters.size(); i++) {
file_dialog->add_filter(filters[i]);
}

file_dialog->set_mode(mode);
file_dialog->popup_centered_ratio();

Expand Down Expand Up @@ -332,7 +343,9 @@ GDNativeLibraryEditor::GDNativeLibraryEditor() {
platform_ios.name = "iOS";
platform_ios.entries.push_back("armv7");
platform_ios.entries.push_back("arm64");
platform_ios.library_extension = "*.dylib";
// iOS can use both Static and Dynamic libraries.
// Frameworks is actually a folder with files.
platform_ios.library_extension = "*.framework; Framework, *.xcframework; Binary Framework, *.a; Static Library, *.dylib; Dynamic Library";
platforms["iOS"] = platform_ios;
}

Expand Down Expand Up @@ -383,6 +396,7 @@ GDNativeLibraryEditor::GDNativeLibraryEditor() {
file_dialog->set_resizable(true);
add_child(file_dialog);
file_dialog->connect("file_selected", this, "_on_library_selected");
file_dialog->connect("dir_selected", this, "_on_library_selected");
file_dialog->connect("files_selected", this, "_on_dependencies_selected");

new_architecture_dialog = memnew(ConfirmationDialog);
Expand Down
109 changes: 74 additions & 35 deletions modules/gdnative/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,47 +144,86 @@ void GDNativeExportPlugin::_export_file(const String &p_path, const String &p_ty
}
}

// Add symbols for staticaly linked libraries on iOS
if (p_features.has("iOS")) {
// Register symbols in the "fake" dynamic lookup table, because dlsym does not work well on iOS.
LibrarySymbol expected_symbols[] = {
{ "gdnative_init", true },
{ "gdnative_terminate", false },
{ "nativescript_init", false },
{ "nativescript_frame", false },
{ "nativescript_thread_enter", false },
{ "nativescript_thread_exit", false },
{ "gdnative_singleton", false }
};
String declare_pattern = "extern \"C\" void $name(void)$weak;\n";
String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
"extern void add_ios_init_callback(void (*cb)());\n";
String linker_flags = "";
for (unsigned long i = 0; i < sizeof(expected_symbols) / sizeof(expected_symbols[0]); ++i) {
String full_name = lib->get_symbol_prefix() + expected_symbols[i].name;
String code = declare_pattern.replace("$name", full_name);
code = code.replace("$weak", expected_symbols[i].is_required ? "" : " __attribute__((weak))");
additional_code += code;

if (!expected_symbols[i].is_required) {
if (linker_flags.length() > 0) {
linker_flags += " ";

bool should_fake_dynamic = false;

List<String> entry_keys;
config->get_section_keys("entry", &entry_keys);

for (List<String>::Element *E = entry_keys.front(); E; E = E->next()) {
String key = E->get();

Vector<String> tags = key.split(".");

bool skip = false;
for (int i = 0; i < tags.size(); i++) {
bool has_feature = p_features.has(tags[i]);

if (!has_feature) {
skip = true;
break;
}
linker_flags += "-Wl,-U,_" + full_name;
}
}

additional_code += String("void $prefixinit() {\n").replace("$prefix", lib->get_symbol_prefix());
String register_pattern = " if (&$name) register_dynamic_symbol((char *)\"$name\", (void *)$name);\n";
for (unsigned long i = 0; i < sizeof(expected_symbols) / sizeof(expected_symbols[0]); ++i) {
String full_name = lib->get_symbol_prefix() + expected_symbols[i].name;
additional_code += register_pattern.replace("$name", full_name);
if (skip) {
continue;
}

String entry_lib_path = config->get_value("entry", key);
if (entry_lib_path.begins_with("res://") && entry_lib_path.ends_with(".a")) {
// If we find static library that was used for export
// we should add a fake loopup table.
// In case of dynamic library being used,
// this symbols will not cause any issues with library loading.
should_fake_dynamic = true;
break;
}
}
additional_code += "}\n";
additional_code += String("struct $prefixstruct {$prefixstruct() {add_ios_init_callback($prefixinit);}};\n").replace("$prefix", lib->get_symbol_prefix());
additional_code += String("$prefixstruct $prefixstruct_instance;\n").replace("$prefix", lib->get_symbol_prefix());

add_ios_cpp_code(additional_code);
add_ios_linker_flags(linker_flags);
if (should_fake_dynamic) {
// Register symbols in the "fake" dynamic lookup table, because dlsym does not work well on iOS.
LibrarySymbol expected_symbols[] = {
{ "gdnative_init", true },
{ "gdnative_terminate", false },
{ "nativescript_init", false },
{ "nativescript_frame", false },
{ "nativescript_thread_enter", false },
{ "nativescript_thread_exit", false },
{ "gdnative_singleton", false }
};
String declare_pattern = "extern \"C\" void $name(void)$weak;\n";
String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
"extern void add_ios_init_callback(void (*cb)());\n";
String linker_flags = "";
for (unsigned long i = 0; i < sizeof(expected_symbols) / sizeof(expected_symbols[0]); ++i) {
String full_name = lib->get_symbol_prefix() + expected_symbols[i].name;
String code = declare_pattern.replace("$name", full_name);
code = code.replace("$weak", expected_symbols[i].is_required ? "" : " __attribute__((weak))");
additional_code += code;

if (!expected_symbols[i].is_required) {
if (linker_flags.length() > 0) {
linker_flags += " ";
}
linker_flags += "-Wl,-U,_" + full_name;
}
}

additional_code += String("void $prefixinit() {\n").replace("$prefix", lib->get_symbol_prefix());
String register_pattern = " if (&$name) register_dynamic_symbol((char *)\"$name\", (void *)$name);\n";
for (unsigned long i = 0; i < sizeof(expected_symbols) / sizeof(expected_symbols[0]); ++i) {
String full_name = lib->get_symbol_prefix() + expected_symbols[i].name;
additional_code += register_pattern.replace("$name", full_name);
}
additional_code += "}\n";
additional_code += String("struct $prefixstruct {$prefixstruct() {add_ios_init_callback($prefixinit);}};\n").replace("$prefix", lib->get_symbol_prefix());
additional_code += String("$prefixstruct $prefixstruct_instance;\n").replace("$prefix", lib->get_symbol_prefix());

add_ios_cpp_code(additional_code);
add_ios_linker_flags(linker_flags);
}
}
}

Expand Down
Loading