From 519024829a8d8e39c9f0788eb350ad2227ada188 Mon Sep 17 00:00:00 2001 From: Sergey Minakov Date: Thu, 2 Jul 2020 12:45:14 +0300 Subject: [PATCH] iOS Export: turn .dylib into .framework on export --- platform/iphone/export/export.cpp | 90 +++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 4 deletions(-) diff --git a/platform/iphone/export/export.cpp b/platform/iphone/export/export.cpp index e1ea19f136..0b6a065d5f 100644 --- a/platform/iphone/export/export.cpp +++ b/platform/iphone/export/export.cpp @@ -762,8 +762,39 @@ Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir memdelete(filesystem_da); return ERR_FILE_NOT_FOUND; } - String additional_dir = p_is_framework && asset.ends_with(".dylib") ? "/dylibs/" : "/"; - String destination_dir = p_out_dir + additional_dir + asset.get_base_dir().replace("res://", ""); + + String base_dir = asset.get_base_dir().replace("res://", ""); + String destination_dir; + String destination; + String asset_path; + bool create_framework = false; + + if (p_is_framework && asset.ends_with(".dylib")) { + // For iOS we need to turn .dylib into .framework + // to be able to send application to AppStore + destination_dir = p_out_dir.plus_file("dylibs").plus_file(base_dir); + + String file_name = asset.get_basename().get_file(); + String framework_name = file_name + ".framework"; + + destination_dir = destination_dir.plus_file(framework_name); + destination = destination_dir.plus_file(file_name); + asset_path = destination_dir; + create_framework = true; + } else if (p_is_framework && (asset.ends_with(".framework") || asset.ends_with(".xcframework"))) { + destination_dir = p_out_dir.plus_file("dylibs").plus_file(base_dir); + + String file_name = asset.get_file(); + destination = destination_dir.plus_file(file_name); + asset_path = destination; + } else { + destination_dir = p_out_dir.plus_file(base_dir); + + String file_name = asset.get_file(); + destination = destination_dir.plus_file(file_name); + asset_path = destination; + } + if (!filesystem_da->dir_exists(destination_dir)) { Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir); if (make_dir_err) { @@ -773,15 +804,66 @@ Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir } } - String destination = destination_dir.plus_file(asset.get_file()); Error err = dir_exists ? da->copy_dir(asset, destination) : da->copy(asset, destination); memdelete(da); if (err) { memdelete(filesystem_da); return err; } - IOSExportAsset exported_asset = { destination, p_is_framework }; + IOSExportAsset exported_asset = { asset_path, p_is_framework }; r_exported_assets.push_back(exported_asset); + + if (create_framework) { + String file_name = asset.get_basename().get_file(); + String framework_name = file_name + ".framework"; + + // Performing `install_name_tool -id @rpath/{name}.framework/{name} ./{name}` on dylib + { + List install_name_args; + install_name_args.push_back("-id"); + install_name_args.push_back(String("@rpath").plus_file(framework_name).plus_file(file_name)); + install_name_args.push_back(destination); + + OS::get_singleton()->execute("install_name_tool", install_name_args, true); + } + + // Creating Info.plist + { + String info_plist_format = "\n" + "\n" + "\n" + "\n" + "CFBundleShortVersionString\n" + "1.0\n" + "CFBundleIdentifier\n" + "com.gdnative.framework.$name\n" + "CFBundleName\n" + "$name\n" + "CFBundleExecutable\n" + "$name\n" + "DTPlatformName\n" + "iphoneos\n" + "CFBundleInfoDictionaryVersion\n" + "6.0\n" + "CFBundleVersion\n" + "1\n" + "CFBundlePackageType\n" + "FMWK\n" + "MinimumOSVersion\n" + "10.0\n" + "\n" + ""; + + String info_plist = info_plist_format.replace("$name", file_name); + + FileAccess *f = FileAccess::open(asset_path.plus_file("Info.plist"), FileAccess::WRITE); + if (f) { + f->store_string(info_plist); + f->close(); + memdelete(f); + } + } + } } } memdelete(filesystem_da);