LocalFacebookSdk.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #if UNITY_EDITOR && UNITY_IOS
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEditor.Build;
  6. using UnityEditor.iOS.Xcode;
  7. using UnityEngine;
  8. internal static class LocalFacebookSdk
  9. {
  10. internal static void Configure(PBXProject project, string mainTarget, string frameworkTarget, string buildPath)
  11. {
  12. string sdkRoot = Path.Combine(Application.dataPath, "FacebookSDK/Plugins/iOS/XCFrameworks");
  13. string[] modules =
  14. {
  15. "FBAEMKit", "FBSDKCoreKit_Basics", "FBSDKCoreKit",
  16. "FBSDKLoginKit", "FBSDKShareKit", "FBSDKGamingServicesKit"
  17. };
  18. foreach (string module in modules)
  19. {
  20. string assetPath = "Assets/FacebookSDK/Plugins/iOS/XCFrameworks/" + module + ".xcframework";
  21. var importer = AssetImporter.GetAtPath(assetPath) as PluginImporter;
  22. if (importer == null || !importer.GetCompatibleWithPlatform(BuildTarget.iOS))
  23. throw new BuildFailedException("Local Facebook SDK must be enabled for iOS: " + assetPath);
  24. // PluginImporter exports and links the complete static XCFramework.
  25. // Static archives must not be added to Embed Frameworks.
  26. string xcframework = Path.Combine(sdkRoot, module + ".xcframework");
  27. if (!File.Exists(Path.Combine(xcframework, "Info.plist")))
  28. throw new BuildFailedException("Missing local Facebook XCFramework: " + xcframework);
  29. // Static linking does not copy framework resources. Preserve manifests
  30. // in separate app resource bundles for Xcode privacy report aggregation.
  31. string manifest = Path.Combine(xcframework,
  32. "ios-arm64_arm64e/" + module + ".framework/PrivacyInfo.xcprivacy");
  33. if (!File.Exists(manifest))
  34. {
  35. if (module != "FBSDKGamingServicesKit")
  36. throw new BuildFailedException("Missing Facebook privacy manifest: " + manifest);
  37. continue;
  38. }
  39. string bundlePath = "FacebookSDKResources/" + module + "Privacy.bundle";
  40. string bundleDirectory = Path.Combine(buildPath, bundlePath);
  41. Directory.CreateDirectory(bundleDirectory);
  42. File.Copy(manifest, Path.Combine(bundleDirectory, "PrivacyInfo.xcprivacy"), true);
  43. var info = new PlistDocument();
  44. info.root.SetString("CFBundleIdentifier", "com.facebook.sdk." + module.Replace("_", "-") + ".privacy");
  45. info.root.SetString("CFBundleName", module + "Privacy");
  46. info.root.SetString("CFBundlePackageType", "BNDL");
  47. info.root.SetString("CFBundleVersion", "1");
  48. info.WriteToFile(Path.Combine(bundleDirectory, "Info.plist"));
  49. string bundleGuid = project.FindFileGuidByProjectPath(bundlePath);
  50. if (string.IsNullOrEmpty(bundleGuid))
  51. bundleGuid = project.AddFile(bundlePath, bundlePath, PBXSourceTree.Source);
  52. project.AddFileToBuild(mainTarget, bundleGuid);
  53. }
  54. foreach (string framework in new[]
  55. {
  56. "Security.framework", "SafariServices.framework", "AuthenticationServices.framework",
  57. "AdSupport.framework", "StoreKit.framework", "WebKit.framework", "Accelerate.framework"
  58. })
  59. project.AddFrameworkToProject(frameworkTarget, framework, false);
  60. project.AddFrameworkToProject(frameworkTarget, "AdServices.framework", true);
  61. foreach (string flag in new[] { "$(inherited)", "-ObjC", "-lz", "-lc++", "-lsqlite3" })
  62. project.AddBuildProperty(frameworkTarget, "OTHER_LDFLAGS", flag);
  63. project.SetBuildProperty(frameworkTarget, "CLANG_ENABLE_MODULES", "YES");
  64. project.SetBuildProperty(frameworkTarget, "SWIFT_VERSION", "5.0");
  65. // Swift autolinking locates compatibility archives in the selected Xcode
  66. // toolchain for either device or simulator; no hardcoded Xcode paths.
  67. foreach (string target in new[] { mainTarget, frameworkTarget })
  68. {
  69. project.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(inherited)");
  70. project.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS",
  71. "\"$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)\"");
  72. project.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "\"$(SDKROOT)/usr/lib/swift\"");
  73. project.AddBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "$(inherited)");
  74. project.AddBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "/usr/lib/swift");
  75. project.AddBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "@executable_path/Frameworks");
  76. }
  77. // Apply ARC only to our credentials bridge, preserving other plug-ins.
  78. string bridgeGuid = project.FindFileGuidByProjectPath(
  79. "Libraries/AppRuntimes/Plugins/iOS/FacebookDynamicCredentials.m");
  80. if (!string.IsNullOrEmpty(bridgeGuid))
  81. {
  82. var flags = project.GetCompileFlagsForFile(frameworkTarget, bridgeGuid) ?? new List<string>();
  83. flags.Remove("-fno-objc-arc");
  84. if (!flags.Contains("-fobjc-arc"))
  85. flags.Add("-fobjc-arc");
  86. project.SetCompileFlagsForFile(frameworkTarget, bridgeGuid, flags);
  87. }
  88. }
  89. }
  90. #endif