-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathAssemblyStripper.cs
More file actions
427 lines (372 loc) · 20 KB
/
AssemblyStripper.cs
File metadata and controls
427 lines (372 loc) · 20 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor.Build.Reporting;
using UnityEditor.Modules;
using UnityEditor.Scripting;
using UnityEditor.Scripting.Compilers;
using UnityEngine;
using UnityEditor;
using UnityEditor.Utils;
using Debug = UnityEngine.Debug;
using PackageInfo = Unity.DataContract.PackageInfo;
namespace UnityEditorInternal
{
internal class AssemblyStripper
{
private static bool debugUnstripped
{
get
{
return false;
}
}
private static string[] Il2CppBlacklistPaths
{
get
{
return new[]
{
Path.Combine("..", "platform_native_link.xml")
};
}
}
private static string UnityLinkerPath
{
get
{
return Path.Combine(IL2CPPUtils.GetIl2CppFolder(), "build/UnityLinker.exe");
}
}
private static string GetModuleWhitelist(string module, string moduleStrippingInformationFolder)
{
return Paths.Combine(moduleStrippingInformationFolder, module + ".xml");
}
private static bool StripAssembliesTo(string[] assemblies, string[] searchDirs, string outputFolder, string workingDirectory, out string output, out string error, string linkerPath, IIl2CppPlatformProvider platformProvider, IEnumerable<string> additionalBlacklist, BuildTargetGroup buildTargetGroup, ManagedStrippingLevel managedStrippingLevel)
{
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);
additionalBlacklist = additionalBlacklist.Select(s => Path.IsPathRooted(s) ? s : Path.Combine(workingDirectory, s)).Where(File.Exists);
var userBlackLists = GetUserBlacklistFiles();
foreach (var ub in userBlackLists)
Console.WriteLine("UserBlackList: " + ub);
additionalBlacklist = additionalBlacklist.Concat(userBlackLists);
var args = new List<string>
{
"-out=\"" + outputFolder + "\"",
"-x=\"" + GetModuleWhitelist("Core", platformProvider.moduleStrippingInformationFolder) + "\"",
};
args.AddRange(additionalBlacklist.Select(path => "-x \"" + path + "\""));
args.AddRange(searchDirs.Select(d => "-d \"" + d + "\""));
args.AddRange(assemblies.Select(assembly => "--include-unity-root-assembly=\"" + Path.GetFullPath(assembly) + "\""));
args.Add($"--dotnetruntime={GetRuntimeArgumentValueForLinker(buildTargetGroup)}");
args.Add($"--dotnetprofile={GetProfileArgumentValueForLinker(buildTargetGroup)}");
args.Add("--use-editor-options");
if (EditorUserBuildSettings.allowDebugging)
args.Add("--editor-settings-flag=AllowDebugging");
if (EditorUserBuildSettings.development)
args.Add("--editor-settings-flag=Development");
// One final check to make sure we only run aggressive on latest runtime.
if ((managedStrippingLevel == ManagedStrippingLevel.Aggressive) && (PlayerSettingsEditor.IsLatestApiCompatibility(PlayerSettings.GetApiCompatibilityLevel(buildTargetGroup))))
{
// Prepare the arguments to run the UnityLinker. When in aggressive mode, need to also
// supply the IL2CPP compiler platform and compiler architecture. When the scripting backend
// is not IL2CPP, we have to map those strings and use a utility function to figure out proper strings.
// Currently only need to do this on the non aot platforms of Android, Windows, Mac, Linux.
var compilerPlatform = "";
var compilerArchitecture = "";
Il2CppNativeCodeBuilder il2cppNativeCodeBuilder = platformProvider.CreateIl2CppNativeCodeBuilder();
if (il2cppNativeCodeBuilder != null)
{
compilerPlatform = il2cppNativeCodeBuilder.CompilerPlatform;
compilerArchitecture = il2cppNativeCodeBuilder.CompilerArchitecture;
}
else
{
GetUnityLinkerPlatformStringsFromBuildTarget(platformProvider.target, out compilerPlatform, out compilerArchitecture);
}
args.Add("--aggressive");
args.Add($"--platform={compilerPlatform}");
if (platformProvider.target != BuildTarget.Android)
args.Add($"--architecture={compilerArchitecture}");
}
var additionalArgs = System.Environment.GetEnvironmentVariable("UNITYLINKER_ADDITIONAL_ARGS");
if (!string.IsNullOrEmpty(additionalArgs))
args.Add(additionalArgs);
additionalArgs = Debug.GetDiagnosticSwitch("VMUnityLinkerAdditionalArgs") as string;
if (!string.IsNullOrEmpty(additionalArgs))
args.Add(additionalArgs);
return RunAssemblyLinker(args, out output, out error, linkerPath, workingDirectory);
}
private static void GetUnityLinkerPlatformStringsFromBuildTarget(BuildTarget target, out string platform, out string architecture)
{
switch (target)
{
case BuildTarget.StandaloneWindows64:
platform = "WindowsDesktop";
architecture = "x64";
break;
case BuildTarget.StandaloneWindows:
platform = "WindowsDesktop";
architecture = "x86";
break;
case BuildTarget.Android:
// Do not supply architecture for Android.
// The build pipeline bundles multiple architectures for Android.
// Can't narrow down to a specific architecture at strip time, we work around
// that fact in the UnityLinker.
platform = "Android";
architecture = "";
break;
case BuildTarget.StandaloneLinux:
platform = "Linux";
architecture = "x86";
break;
case BuildTarget.StandaloneLinux64:
platform = "Linux";
architecture = "x64";
break;
case BuildTarget.StandaloneOSX:
platform = "MacOSX";
architecture = "x64";
break;
case BuildTarget.WSAPlayer:
platform = "WinRT";
// Could be multiple values. We don't have use of this information yet so don't bother with trying to figure out what it should be
architecture = "";
break;
case BuildTarget.iOS:
platform = "iOS";
architecture = "ARM64";
break;
default:
throw new NotSupportedException($"Aggressive stripping is not supported for mono backend on {target}.");
}
}
private static bool RunAssemblyLinker(IEnumerable<string> args, out string @out, out string err, string linkerPath, string workingDirectory)
{
var argString = args.Aggregate((buff, s) => buff + " " + s);
Console.WriteLine("Invoking UnityLinker with arguments: " + argString);
Runner.RunManagedProgram(linkerPath, argString, workingDirectory, null, null);
@out = "";
err = "";
return true;
}
private static List<string> GetUserAssemblies(RuntimeClassRegistry rcr, string managedDir)
{
return rcr.GetUserAssemblies().Where(s => rcr.IsDLLUsed(s)).Select(s => Path.Combine(managedDir, s)).ToList();
}
internal static void StripAssemblies(string managedAssemblyFolderPath, IIl2CppPlatformProvider platformProvider, RuntimeClassRegistry rcr, ManagedStrippingLevel managedStrippingLevel)
{
var assemblies = GetUserAssemblies(rcr, managedAssemblyFolderPath);
assemblies.AddRange(Directory.GetFiles(managedAssemblyFolderPath, "I18N*.dll", SearchOption.TopDirectoryOnly));
var assembliesToStrip = assemblies.ToArray();
var searchDirs = new[]
{
managedAssemblyFolderPath
};
RunAssemblyStripper(assemblies, managedAssemblyFolderPath, assembliesToStrip, searchDirs, UnityLinkerPath, platformProvider, rcr, managedStrippingLevel);
}
internal static void GenerateInternalCallSummaryFile(string icallSummaryPath, string managedAssemblyFolderPath, string strippedDLLPath)
{
var exe = Path.Combine(MonoInstallationFinder.GetFrameWorksFolder(), "Tools/InternalCallRegistrationWriter/InternalCallRegistrationWriter.exe");
var dlls = Directory.GetFiles(strippedDLLPath, "UnityEngine.*Module.dll").Concat(new[] {Path.Combine(strippedDLLPath, "UnityEngine.dll")});
var args = string.Format("-output=\"{0}\" -summary=\"{1}\" -assembly=\"{2}\"",
Path.Combine(managedAssemblyFolderPath, "UnityICallRegistration.cpp"),
icallSummaryPath,
dlls.Aggregate((dllArg, next) => dllArg + ";" + next)
);
Runner.RunManagedProgram(exe, args);
}
internal static IEnumerable<string> GetUserBlacklistFiles()
{
return Directory.GetFiles("Assets", "link.xml", SearchOption.AllDirectories).Select(s => Path.Combine(Directory.GetCurrentDirectory(), s));
}
private static bool AddWhiteListsForModules(IEnumerable<string> nativeModules, ref IEnumerable<string> blacklists, string moduleStrippingInformationFolder)
{
bool result = false;
foreach (var module in nativeModules)
{
var moduleWhitelist = GetModuleWhitelist(module, moduleStrippingInformationFolder);
if (File.Exists(moduleWhitelist))
{
if (!blacklists.Contains(moduleWhitelist))
{
blacklists = blacklists.Concat(new[] { moduleWhitelist });
result = true;
}
}
}
return result;
}
private static string GetRuntimeArgumentValueForLinker(BuildTargetGroup buildTargetGroup)
{
var backend = PlayerSettings.GetScriptingBackend(buildTargetGroup);
switch (backend)
{
case ScriptingImplementation.IL2CPP:
return "il2cpp";
case ScriptingImplementation.Mono2x:
return "mono";
default:
throw new NotImplementedException($"Don't know the backend value to pass to UnityLinker for {backend}");
}
}
private static string GetProfileArgumentValueForLinker(BuildTargetGroup buildTargetGroup)
{
return IL2CPPUtils.ApiCompatibilityLevelToDotNetProfileArgument(PlayerSettings.GetApiCompatibilityLevel(buildTargetGroup));
}
private static void RunAssemblyStripper(IEnumerable assemblies, string managedAssemblyFolderPath, string[] assembliesToStrip, string[] searchDirs, string monoLinkerPath, IIl2CppPlatformProvider platformProvider, RuntimeClassRegistry rcr, ManagedStrippingLevel managedStrippingLevel)
{
string output;
string error;
var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(platformProvider.target);
bool isMono = PlayerSettings.GetScriptingBackend(buildTargetGroup) == ScriptingImplementation.Mono2x;
bool stripEngineCode = rcr != null && PlayerSettings.stripEngineCode && platformProvider.supportsEngineStripping && !EditorUserBuildSettings.buildScriptsOnly;
IEnumerable<string> blacklists = Il2CppBlacklistPaths;
if (rcr != null)
{
blacklists = blacklists.Concat(new[]
{
WriteMethodsToPreserveBlackList(rcr, platformProvider.target),
WriteUnityEngineBlackList(),
MonoAssemblyStripping.GenerateLinkXmlToPreserveDerivedTypes(managedAssemblyFolderPath, rcr)
});
}
if (isMono)
{
// The old Mono assembly stripper uses per-platform link.xml files if available. Apply these here.
var buildToolsDirectory = BuildPipeline.GetBuildToolsDirectory(platformProvider.target);
if (!string.IsNullOrEmpty(buildToolsDirectory))
{
var platformDescriptor = Path.Combine(buildToolsDirectory, "link.xml");
if (File.Exists(platformDescriptor))
blacklists = blacklists.Concat(new[] {platformDescriptor});
}
}
if (!stripEngineCode)
{
// if we don't do stripping, add all modules blacklists.
foreach (var file in Directory.GetFiles(platformProvider.moduleStrippingInformationFolder, "*.xml"))
blacklists = blacklists.Concat(new[] {file});
}
// Generated link xml files that would have been empty will be nulled out. Need to filter these out before running the linker
blacklists = blacklists.Where(b => b != null);
var tempStripPath = Path.GetFullPath(Path.Combine(managedAssemblyFolderPath, "tempStrip"));
bool addedMoreBlacklists;
do
{
addedMoreBlacklists = false;
if (EditorUtility.DisplayCancelableProgressBar("Building Player", "Stripping assemblies", 0.0f))
throw new OperationCanceledException();
if (!StripAssembliesTo(
assembliesToStrip,
searchDirs,
tempStripPath,
managedAssemblyFolderPath,
out output,
out error,
monoLinkerPath,
platformProvider,
blacklists,
buildTargetGroup,
managedStrippingLevel))
throw new Exception("Error in stripping assemblies: " + assemblies + ", " + error);
if (platformProvider.supportsEngineStripping)
{
var icallSummaryPath = Path.Combine(managedAssemblyFolderPath, "ICallSummary.txt");
GenerateInternalCallSummaryFile(icallSummaryPath, managedAssemblyFolderPath, tempStripPath);
if (stripEngineCode)
{
// Find which modules we must include in the build based on Assemblies
HashSet<UnityType> nativeClasses;
HashSet<string> nativeModules;
CodeStrippingUtils.GenerateDependencies(tempStripPath, icallSummaryPath, rcr, stripEngineCode, out nativeClasses, out nativeModules, platformProvider);
// Add module-specific blacklists.
addedMoreBlacklists = AddWhiteListsForModules(nativeModules, ref blacklists, platformProvider.moduleStrippingInformationFolder);
}
}
// If we had to add more whitelists, we need to run AssemblyStripper again with the added whitelists.
}
while (addedMoreBlacklists);
// keep unstripped files for debugging purposes
var tempUnstrippedPath = Path.GetFullPath(Path.Combine(managedAssemblyFolderPath, "tempUnstripped"));
if (debugUnstripped)
Directory.CreateDirectory(tempUnstrippedPath);
foreach (var file in Directory.GetFiles(managedAssemblyFolderPath))
{
var extension = Path.GetExtension(file);
if (string.Equals(extension, ".dll", StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(extension, ".winmd", StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(extension, ".mdb", StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(extension, ".pdb", StringComparison.InvariantCultureIgnoreCase))
{
if (debugUnstripped)
File.Move(file, Path.Combine(tempUnstrippedPath, Path.GetFileName(file)));
else
File.Delete(file);
}
}
foreach (var file in Directory.GetFiles(tempStripPath))
File.Move(file, Path.Combine(managedAssemblyFolderPath, Path.GetFileName(file)));
foreach (var dir in Directory.GetDirectories(tempStripPath))
Directory.Move(dir, Path.Combine(managedAssemblyFolderPath, Path.GetFileName(dir)));
Directory.Delete(tempStripPath);
}
private static string WriteMethodsToPreserveBlackList(RuntimeClassRegistry rcr, BuildTarget target)
{
var contents = GetMethodPreserveBlacklistContents(rcr, target);
if (contents == null)
return null;
var methodPerserveBlackList = Path.GetTempFileName();
File.WriteAllText(methodPerserveBlackList, contents);
return methodPerserveBlackList;
}
private static string WriteUnityEngineBlackList()
{
// UnityEngine.dll would be stripped, as it contains no referenced symbols, only type forwarders.
// Since we need those type forwarders, we generate blacklist to preserve the assembly (but no members).
var unityEngineBlackList = Path.GetTempFileName();
File.WriteAllText(unityEngineBlackList, "<linker><assembly fullname=\"UnityEngine\" preserve=\"nothing\"/></linker>");
return unityEngineBlackList;
}
private static string GetMethodPreserveBlacklistContents(RuntimeClassRegistry rcr, BuildTarget target)
{
if (rcr.GetMethodsToPreserve().Count == 0)
return null;
var sb = new StringBuilder();
sb.AppendLine("<linker>");
var groupedByAssembly = rcr.GetMethodsToPreserve().GroupBy(m => m.assembly);
foreach (var assembly in groupedByAssembly)
{
var assemblyName = assembly.Key;
sb.AppendLine(string.Format("\t<assembly fullname=\"{0}\" ignoreIfMissing=\"1\">", assemblyName));
var groupedByType = assembly.GroupBy(m => m.fullTypeName);
foreach (var type in groupedByType)
{
sb.AppendLine(string.Format("\t\t<type fullname=\"{0}\">", type.Key));
foreach (var method in type)
sb.AppendLine(string.Format("\t\t\t<method name=\"{0}\"/>", method.methodName));
sb.AppendLine("\t\t</type>");
}
sb.AppendLine("\t</assembly>");
}
sb.AppendLine("</linker>");
return sb.ToString();
}
static public void InvokeFromBuildPlayer(BuildTarget buildTarget, RuntimeClassRegistry usedClasses, ManagedStrippingLevel managedStrippingLevel, BuildReport report)
{
var stagingAreaData = Paths.Combine("Temp", "StagingArea", "Data");
var platformProvider = new BaseIl2CppPlatformProvider(buildTarget, Path.Combine(stagingAreaData, "Libraries"), report);
var managedAssemblyFolderPath = Path.GetFullPath(Path.Combine(stagingAreaData, "Managed"));
AssemblyStripper.StripAssemblies(managedAssemblyFolderPath, platformProvider, usedClasses, managedStrippingLevel);
}
}
}