Skip to content

Fix code signing on iOS #298

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

Merged
merged 3 commits into from
Jan 17, 2025
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
44 changes: 36 additions & 8 deletions Editor/LLMBuildProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.IO;

#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
Expand Down Expand Up @@ -48,24 +50,50 @@ private void OnBuildError(string condition, string stacktrace, LogType type)

#if UNITY_IOS
/// <summary>
/// Adds the Accelerate framework (for ios)
/// Postprocess the iOS Build
/// </summary>
public static void AddAccelerate(string outputPath)
public static void PostprocessIOSBuild(string outputPath)
{
string projPath = PBXProject.GetPBXProjectPath(outputPath);
PBXProject proj = new PBXProject();
proj.ReadFromFile(projPath);
proj.AddFrameworkToProject(proj.GetUnityMainTargetGuid(), "Accelerate.framework", false);
proj.AddFrameworkToProject(proj.GetUnityFrameworkTargetGuid(), "Accelerate.framework", false);
proj.WriteToFile(projPath);
PBXProject project = new PBXProject();
project.ReadFromFile(projPath);

string targetGuid = project.GetUnityFrameworkTargetGuid();
string frameworkTargetGuid = project.GetUnityFrameworkTargetGuid();
string unityMainTargetGuid = project.GetUnityMainTargetGuid();
string embedFrameworksGuid = project.GetResourcesBuildPhaseByTarget(frameworkTargetGuid);

// Add Accelerate framework
project.AddFrameworkToProject(unityMainTargetGuid, "Accelerate.framework", false);
project.AddFrameworkToProject(targetGuid, "Accelerate.framework", false);

// Remove libundreamai_ios.a from Embed Frameworks
string libraryFile = Path.Combine("Libraries", LLMBuilder.PluginLibraryDir("iOS", true), "libundreamai_ios.a");
string fileGuid = project.FindFileGuidByProjectPath(libraryFile);
if (string.IsNullOrEmpty(fileGuid)) Debug.LogError($"Library file {libraryFile} not found in project");
else
{
foreach (var phaseGuid in project.GetAllBuildPhasesForTarget(unityMainTargetGuid))
{
if (project.GetBuildPhaseName(phaseGuid) == "Embed Frameworks")
{
project.RemoveFileFromBuild(phaseGuid, fileGuid);
break;
}
}
project.RemoveFileFromBuild(unityMainTargetGuid, fileGuid);
}

project.WriteToFile(projPath);
}

#endif

// called after the build
public void OnPostprocessBuild(BuildReport report)
{
#if UNITY_IOS
AddAccelerate(report.summary.outputPath);
PostprocessIOSBuild(report.summary.outputPath);
#endif
BuildCompleted();
}
Expand Down
21 changes: 16 additions & 5 deletions Runtime/LLMBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public class LLMBuilder
{
static List<StringPair> movedPairs = new List<StringPair>();
public static string BuildTempDir = Path.Combine(Application.temporaryCachePath, "LLMUnityBuild");
public static string androidPluginDir = Path.Combine(Application.dataPath, "Plugins", "Android", "LLMUnity");
public static string iOSPluginDir = Path.Combine(Application.dataPath, "Plugins", "iOS", "LLMUnity");
static string movedCache = Path.Combine(BuildTempDir, "moved.json");

[InitializeOnLoadMethod]
Expand All @@ -26,6 +24,18 @@ private static void InitializeOnLoad()
Reset();
}

public static string PluginDir(string platform, bool relative = false)
{
string pluginDir = Path.Combine("Plugins", platform, "LLMUnity");
if (!relative) pluginDir = Path.Combine(Application.dataPath, pluginDir);
return pluginDir;
}

public static string PluginLibraryDir(string platform, bool relative = false)
{
return Path.Combine(PluginDir(platform, relative), LLMUnitySetup.libraryName);
}

/// <summary>
/// Performs an action for a file or a directory recursively
/// </summary>
Expand Down Expand Up @@ -88,7 +98,7 @@ public static void MovePath(string source, string target)
/// <param name="path">path</param>
public static bool DeletePath(string path)
{
string[] allowedDirs = new string[] { LLMUnitySetup.GetAssetPath(), BuildTempDir, androidPluginDir, iOSPluginDir};
string[] allowedDirs = new string[] { LLMUnitySetup.GetAssetPath(), BuildTempDir, PluginDir("Android"), PluginDir("iOS")};
bool deleteOK = false;
foreach (string allowedDir in allowedDirs) deleteOK = deleteOK || LLMUnitySetup.IsSubPath(path, allowedDir);
if (!deleteOK)
Expand Down Expand Up @@ -175,9 +185,10 @@ public static void BuildLibraryPlatforms(string platform)

if (platform == "android" || platform == "ios")
{
string pluginDir = platform == "android"? androidPluginDir: iOSPluginDir;
string pluginPlatform = platform == "android" ? "Android" : "iOS";
string source = Path.Combine(LLMUnitySetup.libraryPath, platform);
string target = Path.Combine(pluginDir, LLMUnitySetup.libraryName);
string target = PluginLibraryDir(pluginPlatform);
string pluginDir = PluginDir(pluginPlatform);
MoveAction(source, target);
MoveAction(source + ".meta", target + ".meta");
AddActionAddMeta(pluginDir);
Expand Down
Loading