Skip to content
Open
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
68 changes: 48 additions & 20 deletions Editor/LLMBuildProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
#if UNITY_IOS || UNITY_VISIONOS
using System.Collections.Generic;
#if UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_VISIONOS
using System.IO;
using UnityEditor.iOS.Xcode;
#endif
Expand All @@ -27,56 +28,83 @@ private void OnBuildError(string condition, string stacktrace, LogType type)
if (type == LogType.Error) BuildCompleted();
}

#if UNITY_IOS || UNITY_VISIONOS
#if UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_VISIONOS
/// <summary>
/// Postprocess the iOS Build
/// </summary>
public static void PostprocessIOSBuild(BuildTarget buildTarget, string outputPath)
{
List<string> libraryFileNames = new List<string>();
#if UNITY_IOS
string projPath = PBXProject.GetPBXProjectPath(outputPath);
#if UNITY_VISIONOS
projPath = projPath.Replace("Unity-iPhone", "Unity-VisionOS");
libraryFileNames.Add("libundreamai_ios.a");
#elif UNITY_VISIONOS
string projPath = PBXProject.GetPBXProjectPath(outputPath).Replace("Unity-iPhone", "Unity-VisionOS");
libraryFileNames.Add("libundreamai_visionos.a");
#else
string projPath = Path.Combine(outputPath, Path.GetFileName(outputPath) + ".xcodeproj", "project.pbxproj");
if (!File.Exists(projPath)) return;
libraryFileNames.Add("libundreamai_macos-arm64-acc.dylib");
libraryFileNames.Add("libundreamai_macos-arm64-no_acc.dylib");
#endif

PBXProject project = new PBXProject();
project.ReadFromFile(projPath);

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

// Add Accelerate framework
project.AddFrameworkToProject(unityMainTargetGuid, "Accelerate.framework", false);
project.AddFrameworkToProject(targetGuid, "Accelerate.framework", false);
if (targetGuid != null) project.AddFrameworkToProject(targetGuid, "Accelerate.framework", false);

List<string> libraryFiles = new List<string>();
foreach (string libraryFileName in libraryFileNames)
{
string lib = LLMUnitySetup.SearchDirectory(outputPath, libraryFileName);
if (lib != null) libraryFiles.Add(lib);
}

string libraryFile = LLMUnitySetup.RelativePath(LLMUnitySetup.SearchDirectory(outputPath, $"libundreamai_{buildTarget.ToString().ToLower()}.a"), outputPath);
string fileGuid = project.FindFileGuidByProjectPath(libraryFile);
if (string.IsNullOrEmpty(fileGuid)) Debug.LogError($"Library file {libraryFile} not found in project");
if (libraryFiles.Count == 0)
{
Debug.LogError($"No library files found for the build");
}
else
{
foreach (var phaseGuid in project.GetAllBuildPhasesForTarget(unityMainTargetGuid))
foreach (string libraryFile in libraryFiles)
{
if (project.GetBuildPhaseName(phaseGuid) == "Embed Frameworks")
string relLibraryFile = LLMUnitySetup.RelativePath(libraryFile, outputPath);
string fileGuid = project.FindFileGuidByProjectPath(relLibraryFile);
if (string.IsNullOrEmpty(fileGuid))
{
project.RemoveFileFromBuild(phaseGuid, fileGuid);
break;
Debug.LogError($"Library file {relLibraryFile} not found in project");
}
}
else
{
foreach (var phaseGuid in project.GetAllBuildPhasesForTarget(unityMainTargetGuid))
{
if (project.GetBuildPhaseName(phaseGuid) == "Embed Frameworks")
{
project.RemoveFileFromBuild(phaseGuid, fileGuid);
break;
}
}

project.AddFileToBuild(unityMainTargetGuid, fileGuid);
project.AddFileToBuild(targetGuid, fileGuid);
project.AddFileToBuild(unityMainTargetGuid, fileGuid);
if (targetGuid != null) project.AddFileToBuild(targetGuid, fileGuid);
}
}
}

project.WriteToFile(projPath);
AssetDatabase.ImportAsset(projPath);
}

#endif

// called after the build
public void OnPostprocessBuild(BuildReport report)
{
#if UNITY_IOS || UNITY_VISIONOS
#if UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_VISIONOS
PostprocessIOSBuild(report.summary.platform, report.summary.outputPath);
#endif
EditorApplication.delayCall += () =>
Expand Down