-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathLLMBuildProcessor.cs
More file actions
126 lines (112 loc) · 4.65 KB
/
LLMBuildProcessor.cs
File metadata and controls
126 lines (112 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.Collections.Generic;
#if UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_VISIONOS
using System.IO;
using UnityEditor.iOS.Xcode;
#endif
namespace LLMUnity
{
public class LLMBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
public int callbackOrder => 0;
// called before the build
public void OnPreprocessBuild(BuildReport report)
{
Application.logMessageReceived += OnBuildError;
LLMBuilder.Build(report.summary.platform);
AssetDatabase.Refresh();
}
// called during build to check for errors
private void OnBuildError(string condition, string stacktrace, LogType type)
{
if (type == LogType.Error) BuildCompleted();
}
#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);
libraryFileNames.Add("libllamalib_ios-arm64.a");
#elif UNITY_VISIONOS
string projPath = PBXProject.GetPBXProjectPath(outputPath).Replace("Unity-iPhone", "Unity-VisionOS");
libraryFileNames.Add("libllamalib_visionos-arm64.a");
#else
string projPath = Path.Combine(outputPath, Path.GetFileName(outputPath) + ".xcodeproj", "project.pbxproj");
if (!File.Exists(projPath)) return;
libraryFileNames.Add("libllamalib_osx-universal_acc.dylib");
libraryFileNames.Add("libllamalib_osx-universal_no-acc.dylib");
#endif
PBXProject project = new PBXProject();
project.ReadFromFile(projPath);
string unityMainTargetGuid = project.GetUnityMainTargetGuid();
string targetGuid = project.GetUnityFrameworkTargetGuid();
// Add Accelerate framework
project.AddFrameworkToProject(unityMainTargetGuid, "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);
}
if (libraryFiles.Count == 0)
{
Debug.LogError($"No library files found for the build");
}
else
{
foreach (string libraryFile in libraryFiles)
{
string relLibraryFile = LLMUnitySetup.RelativePath(libraryFile, outputPath);
string fileGuid = project.FindFileGuidByProjectPath(relLibraryFile);
if (string.IsNullOrEmpty(fileGuid))
{
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);
if (targetGuid != null) project.AddFileToBuild(targetGuid, fileGuid);
}
}
}
project.WriteToFile(projPath);
}
#endif
// called after the build
public void OnPostprocessBuild(BuildReport report)
{
#if UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_VISIONOS
PostprocessIOSBuild(report.summary.platform, report.summary.outputPath);
#endif
EditorApplication.delayCall += () =>
{
BuildCompleted();
};
}
public void BuildCompleted()
{
// Delay the reset operation to ensure Unity is no longer in the build process
EditorApplication.delayCall += () =>
{
Application.logMessageReceived -= OnBuildError;
LLMBuilder.Reset();
};
}
}
}