-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathBuildPostProcess.cs
More file actions
422 lines (367 loc) · 17.2 KB
/
BuildPostProcess.cs
File metadata and controls
422 lines (367 loc) · 17.2 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
using System;
using System.Collections.Generic;
using System.IO;
using Sentry.Extensibility;
using Sentry.Unity.Editor.ConfigurationWindow;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Callbacks;
using System.Diagnostics;
using System.Linq;
namespace Sentry.Unity.Editor.Native;
public static class BuildPostProcess
{
[PostProcessBuild(1)]
public static void OnPostProcessBuild(BuildTarget target, string executablePath)
{
var targetGroup = BuildPipeline.GetBuildTargetGroup(target);
if (targetGroup is not BuildTargetGroup.Standalone
and not BuildTargetGroup.GameCoreXboxSeries
and not BuildTargetGroup.PS5
and not BuildTargetGroup.Switch)
{
return;
}
var cliOptions = SentryScriptableObject.LoadCliOptions();
var options = SentryScriptableObject.LoadOptions(isBuilding: true);
var logger = options?.DiagnosticLogger ?? new UnityLogger(options ?? new SentryUnityOptions());
if (options is null)
{
logger.LogWarning("Native support disabled because Sentry has not been configured. " +
"You can do that through the editor: {0}", SentryWindow.EditorMenuPath);
return;
}
if (!options.IsValid())
{
logger.LogDebug("Skipping native post build process.");
return;
}
var namedBuildTarget = NamedBuildTarget.FromBuildTargetGroup(targetGroup);
var isMono = PlayerSettings.GetScriptingBackend(namedBuildTarget) == ScriptingImplementation.Mono2x;
// The executable path resolves to the following when pointing Unity into a `build/platform/` directory:
// - Desktop: `./samples/unity-of-bugs/builds/windows/unityofbugs.exe`
// - Xbox: `./samples/unity-of-bugs/builds/xsx/`
// - PlayStation: `./samples/unity-of-bugs/builds/ps5/`
// - Switch: `./samples/unity-of-bugs/builds/switch/unity-of-bugs.nspd_root`
var buildOutputDir = targetGroup switch
{
BuildTargetGroup.Standalone => Path.GetDirectoryName(executablePath),
BuildTargetGroup.GameCoreXboxSeries => executablePath,
BuildTargetGroup.PS5 => executablePath,
BuildTargetGroup.Switch => Path.GetDirectoryName(executablePath),
_ => string.Empty
};
if (string.IsNullOrEmpty(buildOutputDir))
{
logger.LogError("Failed to find build output directory based on the executable path '{0}'." +
"\nSkipping adding crash-handler and uploading debug symbols.", executablePath);
return;
}
var executableName = targetGroup switch
{
BuildTargetGroup.Standalone => Path.GetFileName(executablePath),
// For Xbox/PS5, executablePath is the directory itself
BuildTargetGroup.GameCoreXboxSeries => Path.GetFileName(executablePath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)),
BuildTargetGroup.PS5 => Path.GetFileName(executablePath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)),
_ => string.Empty
};
UploadDebugSymbols(logger, target, buildOutputDir, executableName, options, cliOptions, isMono);
if (!IsEnabledForPlatform(target, options))
{
logger.LogDebug("Skipping adding the crash-handler. Native support for the current platform is disabled in the configuration.");
return;
}
try
{
AddCrashHandler(logger, target, buildOutputDir);
}
catch (Exception e)
{
logger.LogError(e, "Failed to add the crash-handler to the built application.");
throw new BuildFailedException("Sentry Native BuildPostProcess failed");
}
}
private static bool IsEnabledForPlatform(BuildTarget target, SentryUnityOptions options) => target switch
{
BuildTarget.StandaloneWindows => options.WindowsNativeSupportEnabled,
BuildTarget.StandaloneWindows64 => options.WindowsNativeSupportEnabled,
BuildTarget.StandaloneOSX => options.MacosNativeSupportEnabled,
BuildTarget.StandaloneLinux64 => options.LinuxNativeSupportEnabled,
BuildTarget.GameCoreXboxSeries or BuildTarget.GameCoreXboxOne => options.XboxNativeSupportEnabled,
BuildTarget.PS5 => options.PlayStationNativeSupportEnabled,
BuildTarget.Switch => options.SwitchNativeSupportEnabled,
_ => false,
};
private static void AddCrashHandler(IDiagnosticLogger logger, BuildTarget target, string buildOutputDir)
{
switch (target)
{
case BuildTarget.StandaloneWindows:
case BuildTarget.StandaloneWindows64:
logger.LogDebug("Adding crashpad.");
CopyHandler(logger, buildOutputDir, Path.Combine("Windows", "Sentry", "crashpad_handler.exe"));
CopyHandler(logger, buildOutputDir, Path.Combine("Windows", "Sentry", "crashpad_wer.dll"));
break;
case BuildTarget.StandaloneLinux64:
case BuildTarget.StandaloneOSX:
// No standalone crash handler for Linux/macOS - uses built-in handlers.
return;
case BuildTarget.GameCoreXboxSeries:
case BuildTarget.GameCoreXboxOne:
// No standalone crash handler for Xbox - comes with Breakpad
return;
case BuildTarget.PS5:
// No standalone crash handler for PlayStation
return;
case BuildTarget.Switch:
// No standalone crash handler for Switch - uses Nintendo's crash reporter
return;
default:
throw new ArgumentException($"Unsupported build target: {target}");
}
}
private static void CopyHandler(IDiagnosticLogger logger, string buildOutputDir, string handlerPath)
{
var fullHandlerPath = Path.GetFullPath(Path.Combine("Packages", SentryPackageInfo.GetName(), "Plugins", handlerPath));
var targetHandlerPath = Path.Combine(buildOutputDir, Path.GetFileName(fullHandlerPath));
logger.LogInfo("Copying handler '{0}' to {1}", Path.GetFileName(fullHandlerPath), targetHandlerPath);
File.Copy(fullHandlerPath, targetHandlerPath, true);
}
internal static void AddPath(List<string> paths, string path, IDiagnosticLogger logger, bool required = false)
{
if (Directory.Exists(path) || File.Exists(path))
{
paths.Add(path);
logger.LogDebug("Adding '{0}' to debug symbol upload", path);
}
else if (required)
{
logger.LogWarning("Required path not found for debug symbol upload: {0}", path);
}
else
{
logger.LogDebug("Optional path not found, skipping: {0}", path);
}
}
private static void UploadDebugSymbols(IDiagnosticLogger logger, BuildTarget target, string buildOutputDir,
string executableName, SentryUnityOptions options, SentryCliOptions? cliOptions, bool isMono)
{
var projectDir = Directory.GetParent(Application.dataPath)?.FullName ?? "";
if (cliOptions?.IsValid(logger, EditorUserBuildSettings.development) is not true)
{
if (options.Il2CppLineNumberSupportEnabled)
{
logger.LogWarning("The IL2CPP line number support requires the debug symbol upload to be enabled.");
}
return;
}
// Warn if build output appears to be inside the Unity project directory
if (Directory.Exists(Path.Combine(buildOutputDir, "Assets")) ||
Directory.Exists(Path.Combine(buildOutputDir, "ProjectSettings")) ||
Directory.Exists(Path.Combine(buildOutputDir, "Library")))
{
logger.LogWarning(
"Build output directory '{0}' appears to be the Unity project directory or contain project folders. " +
"This may cause issues with debug symbol upload. Consider using a dedicated build output folder outside the project.",
buildOutputDir);
}
logger.LogInfo("Uploading debugging information using sentry-cli in {0}", buildOutputDir);
var paths = new List<string>();
switch (target)
{
case BuildTarget.StandaloneWindows:
case BuildTarget.StandaloneWindows64:
// Core executables and libraries
AddPath(paths, Path.Combine(buildOutputDir, executableName), logger, required: true);
AddPath(paths, Path.Combine(buildOutputDir, "UnityPlayer.dll"), logger, required: true);
// Sentry native SDK symbols from package
AddPath(paths, Path.GetFullPath($"Packages/{SentryPackageInfo.GetName()}/Plugins/Windows/Sentry/sentry.pdb"), logger);
// Data - native plugins
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_Data"))
{
AddPath(paths, dir, logger);
}
if (isMono)
{
// Mono runtime
AddPath(paths, Path.Combine(buildOutputDir, "MonoBleedingEdge", "EmbedRuntime"), logger);
// Add all PDB files in build output root
foreach (var pdb in Directory.GetFiles(buildOutputDir, "*.pdb"))
{
AddPath(paths, pdb, logger);
}
}
else // IL2CPP
{
AddPath(paths, Path.Combine(buildOutputDir, "GameAssembly.dll"), logger, required: true);
// IL2CPP output and Managed
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_BackUpThisFolder_*"))
{
AddPath(paths, dir, logger);
}
}
// Burst
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_BurstDebugInformation_*"))
{
AddPath(paths, dir, logger);
}
break;
case BuildTarget.StandaloneLinux64:
// Core executables and libraries
AddPath(paths, Path.Combine(buildOutputDir, executableName), logger, required: true);
AddPath(paths, Path.Combine(buildOutputDir, "UnityPlayer.so"), logger, required: true);
// Sentry native SDK symbols from package
AddPath(paths, Path.GetFullPath($"Packages/{SentryPackageInfo.GetName()}/Plugins/Linux/Sentry/libsentry.dbg.so"), logger);
// Data - native plugins
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_Data"))
{
AddPath(paths, dir, logger);
}
if (!isMono) // IL2CPP
{
AddPath(paths, Path.Combine(buildOutputDir, "GameAssembly.so"), logger, required: true);
// IL2CPP output and Managed
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_BackUpThisFolder_*"))
{
AddPath(paths, dir, logger);
}
}
// Burst
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_BurstDebugInformation_*"))
{
AddPath(paths, dir, logger);
}
break;
case BuildTarget.StandaloneOSX:
// App bundle
AddPath(paths, Path.Combine(buildOutputDir, executableName), logger, required: true);
// Sentry dSYM from package
AddPath(paths, Path.GetFullPath($"Packages/{SentryPackageInfo.GetName()}/Plugins/macOS/Sentry/Sentry.dylib.dSYM"), logger);
if (!isMono) // IL2CPP
{
// IL2CPP output and Managed
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_BackUpThisFolder_*"))
{
AddPath(paths, dir, logger);
}
}
// Burst
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_BurstDebugInformation_*"))
{
AddPath(paths, dir, logger);
}
break;
case BuildTarget.GameCoreXboxSeries:
case BuildTarget.GameCoreXboxOne:
// Xbox builds go to a dedicated directory, safe to scan entirely
AddPath(paths, buildOutputDir, logger, required: true);
// User-provided Sentry plugin
AddPath(paths, Path.GetFullPath("Assets/Plugins/Sentry/"), logger);
break;
case BuildTarget.PS5:
// PlayStation builds go to a dedicated directory, safe to scan entirely
AddPath(paths, buildOutputDir, logger, required: true);
// User-provided Sentry plugin
AddPath(paths, Path.GetFullPath("Assets/Plugins/Sentry/"), logger);
break;
case BuildTarget.Switch:
// IL2CPP output, Managed DLLs/PDBs, and Symbols
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_BackUpThisFolder_*"))
{
AddPath(paths, dir, logger);
}
// Burst
foreach (var dir in Directory.GetDirectories(buildOutputDir, "*_BurstDebugInformation_*"))
{
AddPath(paths, dir, logger);
}
// When exporting as an NSP the assemblies are bundled inside the package. So we're also checking the build cache.
var beePath = Path.Combine(projectDir, "Library", "Bee", "artifacts", "SwitchPlayerBuildProgram");
AddPath(paths, beePath, logger);
// User-provided Sentry plugin
AddPath(paths, Path.GetFullPath("Assets/Plugins/Sentry/"), logger);
break;
default:
logger.LogError("Symbol upload for '{0}' is currently not supported.", target);
return;
}
// Possible duplicate but check for the .pdb files that Unity stores for script assemblies in `./Temp/ManagedSymbols/`.
var managedSymbolsDirectory = Path.Combine(projectDir, "Temp", "ManagedSymbols");
AddPath(paths, managedSymbolsDirectory, logger);
if (paths.Count == 0)
{
logger.LogWarning("No debug symbol paths found to upload.");
return;
}
// Build CLI arguments
var cliArgs = "debug-files upload ";
if (!isMono)
{
cliArgs += "--il2cpp-mapping ";
}
if (cliOptions.UploadSources)
{
cliArgs += "--include-sources ";
}
if (cliOptions.IgnoreCliErrors)
{
cliArgs += "--allow-failure ";
}
cliArgs = paths.Aggregate(cliArgs, (current, path) => current + $"\"{path}\" ");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = SentryCli.SetupSentryCli(),
WorkingDirectory = buildOutputDir,
Arguments = cliArgs,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
var propertiesFile = SentryCli.CreateSentryProperties(buildOutputDir, cliOptions, options);
try
{
process.StartInfo.EnvironmentVariables["SENTRY_PROPERTIES"] = propertiesFile;
DataReceivedEventHandler logForwarder = (object sender, DataReceivedEventArgs e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
var msg = e.Data.Trim();
var msgLower = msg.ToLowerInvariant();
var level = SentryLevel.Debug;
if (msgLower.StartsWith("error"))
{
level = SentryLevel.Error;
}
else if (msgLower.StartsWith("warn"))
{
level = SentryLevel.Warning;
}
else if (msgLower.StartsWith("info"))
{
level = SentryLevel.Info;
}
// Remove the level and timestamp from the beginning of the message.
// INFO 2022-06-20 15:10:03.613794800 +02:00
msg = Regex.Replace(msg, "^[a-zA-Z]+ +[0-9\\-]{10} [0-9:]{8}\\.[0-9]+ \\+[0-9:]{5} +", "");
logger.Log(level, "sentry-cli: {0}", null, msg);
}
};
process.OutputDataReceived += logForwarder;
process.ErrorDataReceived += logForwarder;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
finally
{
File.Delete(propertiesFile);
}
}
}