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

Static library linking on mobile (fixes iOS signing) #289

Merged
merged 6 commits into from
Dec 17, 2024
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
21 changes: 21 additions & 0 deletions Editor/LLMBuildProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif

namespace LLMUnity
{
Expand Down Expand Up @@ -43,9 +46,27 @@ private void OnBuildError(string condition, string stacktrace, LogType type)
if (type == LogType.Error) BuildCompleted();
}

#if UNITY_IOS
/// <summary>
/// Adds the Accelerate framework (for ios)
/// </summary>
public static void AddAccelerate(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);
}
#endif

// called after the build
public void OnPostprocessBuild(BuildReport report)
{
#if UNITY_IOS
AddAccelerate(report.summary.outputPath);
#endif
BuildCompleted();
}

Expand Down
2 changes: 1 addition & 1 deletion Runtime/LLM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ public void ApplyLoras()
json = json.Substring(startIndex, endIndex - startIndex);

IntPtr stringWrapper = llmlib.StringWrapper_Construct();
llmlib.LLM_Lora_Weight(LLMObject, json, stringWrapper);
llmlib.LLM_LoraWeight(LLMObject, json, stringWrapper);
llmlib.StringWrapper_Delete(stringWrapper);
}

Expand Down
18 changes: 10 additions & 8 deletions Runtime/LLMBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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 Down Expand Up @@ -87,7 +88,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};
string[] allowedDirs = new string[] { LLMUnitySetup.GetAssetPath(), BuildTempDir, androidPluginDir, iOSPluginDir};
bool deleteOK = false;
foreach (string allowedDir in allowedDirs) deleteOK = deleteOK || LLMUnitySetup.IsSubPath(path, allowedDir);
if (!deleteOK)
Expand Down Expand Up @@ -148,10 +149,10 @@ static void AddActionAddMeta(string target)
}

/// <summary>
/// Hides all the library platforms apart from the target platform by moving out their library folders outside of StreamingAssets
/// Moves libraries in the correct place for building
/// </summary>
/// <param name="platform">target platform</param>
public static void HideLibraryPlatforms(string platform)
public static void BuildLibraryPlatforms(string platform)
{
List<string> platforms = new List<string>(){ "windows", "macos", "linux", "android", "ios", "setup" };
platforms.Remove(platform);
Expand All @@ -170,13 +171,14 @@ public static void HideLibraryPlatforms(string platform)
}
}

if (platform == "android")
if (platform == "android" || platform == "ios")
{
string source = Path.Combine(LLMUnitySetup.libraryPath, "android");
string target = Path.Combine(androidPluginDir, LLMUnitySetup.libraryName);
string pluginDir = platform == "android"? androidPluginDir: iOSPluginDir;
string source = Path.Combine(LLMUnitySetup.libraryPath, platform);
string target = Path.Combine(pluginDir, LLMUnitySetup.libraryName);
MoveAction(source, target);
MoveAction(source + ".meta", target + ".meta");
AddActionAddMeta(androidPluginDir);
AddActionAddMeta(pluginDir);
}
}

Expand All @@ -196,7 +198,7 @@ public static void Build(string platform)
{
DeletePath(BuildTempDir);
Directory.CreateDirectory(BuildTempDir);
HideLibraryPlatforms(platform);
BuildLibraryPlatforms(platform);
BuildModels();
}

Expand Down
Loading
Loading