From c49ba1e1544a5d05b8a3c0c7cf6304454c4209b8 Mon Sep 17 00:00:00 2001 From: Antonios Makropoulos Date: Tue, 6 May 2025 20:14:57 +0300 Subject: [PATCH] Add undream libraries in macOS Xcode --- Editor/LLMBuildProcessor.cs | 68 ++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/Editor/LLMBuildProcessor.cs b/Editor/LLMBuildProcessor.cs index 4ac99091..981477e7 100644 --- a/Editor/LLMBuildProcessor.cs +++ b/Editor/LLMBuildProcessor.cs @@ -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 @@ -27,48 +28,75 @@ 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 /// /// Postprocess the iOS Build /// public static void PostprocessIOSBuild(BuildTarget buildTarget, string outputPath) { + List libraryFileNames = new List(); +#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 libraryFiles = new List(); + 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 @@ -76,7 +104,7 @@ public static void PostprocessIOSBuild(BuildTarget buildTarget, string outputPat // 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 += () =>