Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BurstAotCompiler.cs 100KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  1. #if UNITY_EDITOR && ENABLE_BURST_AOT
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using UnityEditor;
  10. using UnityEditor.Android;
  11. using UnityEditor.Build;
  12. using UnityEditor.Build.Reporting;
  13. using UnityEditor.Compilation;
  14. using UnityEditor.Scripting;
  15. using UnityEditor.Scripting.ScriptCompilation;
  16. using UnityEditor.Scripting.Compilers;
  17. using UnityEditor.UnityLinker;
  18. using UnityEditor.Utils;
  19. using UnityEngine;
  20. using CompilerMessageType = UnityEditor.Scripting.Compilers.CompilerMessageType;
  21. using Debug = UnityEngine.Debug;
  22. using System.Runtime.InteropServices;
  23. #if UNITY_EDITOR_OSX
  24. using System.ComponentModel;
  25. using Unity.Burst.LowLevel;
  26. using UnityEditor.Callbacks;
  27. #endif
  28. namespace Unity.Burst.Editor
  29. {
  30. using static BurstCompilerOptions;
  31. internal class TargetCpus
  32. {
  33. public List<BurstTargetCpu> Cpus;
  34. public TargetCpus()
  35. {
  36. Cpus = new List<BurstTargetCpu>();
  37. }
  38. public TargetCpus(BurstTargetCpu single)
  39. {
  40. Cpus = new List<BurstTargetCpu>(1)
  41. {
  42. single
  43. };
  44. }
  45. public bool IsX86()
  46. {
  47. foreach (var cpu in Cpus)
  48. {
  49. switch (cpu)
  50. {
  51. case BurstTargetCpu.X86_SSE2:
  52. case BurstTargetCpu.X86_SSE4:
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. public override string ToString()
  59. {
  60. var result = "";
  61. var first = true;
  62. foreach (var cpu in Cpus)
  63. {
  64. if (first)
  65. {
  66. result += $"{cpu}";
  67. first = false;
  68. }
  69. else
  70. {
  71. result += $", {cpu}";
  72. }
  73. }
  74. return result;
  75. }
  76. public TargetCpus Clone()
  77. {
  78. var copy = new TargetCpus
  79. {
  80. Cpus = new List<BurstTargetCpu>(Cpus.Count)
  81. };
  82. foreach (var cpu in Cpus)
  83. {
  84. copy.Cpus.Add(cpu);
  85. }
  86. return copy;
  87. }
  88. }
  89. #if !ENABLE_GENERATE_NATIVE_PLUGINS_FOR_ASSEMBLIES_API
  90. internal class LinkXMLGenerator : IUnityLinkerProcessor
  91. {
  92. public int callbackOrder => 1;
  93. public string GenerateAdditionalLinkXmlFile(BuildReport report, UnityLinkerBuildPipelineData data)
  94. {
  95. var linkXml = Path.GetFullPath(Path.Combine("Temp", BurstAotCompiler.BurstLinkXmlName));
  96. return linkXml;
  97. }
  98. public void OnBeforeRun(BuildReport report, UnityLinkerBuildPipelineData data)
  99. {
  100. }
  101. public void OnAfterRun(BuildReport report, UnityLinkerBuildPipelineData data)
  102. {
  103. }
  104. }
  105. #endif
  106. #if ENABLE_GENERATE_NATIVE_PLUGINS_FOR_ASSEMBLIES_API
  107. internal class BurstAOTCompilerPostprocessor : IGenerateNativePluginsForAssemblies
  108. #else
  109. internal class BurstAOTCompilerPostprocessor : IPostBuildPlayerScriptDLLs
  110. #endif
  111. {
  112. public int callbackOrder => 0;
  113. #if ENABLE_GENERATE_NATIVE_PLUGINS_FOR_ASSEMBLIES_API
  114. public IGenerateNativePluginsForAssemblies.PrepareResult PrepareOnMainThread(IGenerateNativePluginsForAssemblies.PrepareArgs args)
  115. {
  116. if (ForceDisableBurstCompilation)
  117. return new();
  118. DoSetup(args.report);
  119. var target = BurstPlatformAotSettings.ResolveTarget(settings.summary.platform);
  120. return new()
  121. {
  122. additionalInputFiles = new[]
  123. {
  124. // Any files in this list will be scanned for changes, and any changes in these files will trigger
  125. // a rerun of the Burst compiler on the player build (even if the script assemblies have not changed).
  126. //
  127. // We add the settings so that changing any Burst setting will trigger a rebuild.
  128. BurstPlatformAotSettings.GetPath(target),
  129. // Like above, but specifically for settings unique to player-builds (like SDK versions and like)
  130. // Those settings are extracted in `DoSetup`
  131. BurstAotCompiler.BurstAOTSettings.GetPath(target),
  132. // We don't want to scan every file in the Burst package (though every file could potentially change
  133. // behavior). When working on Burst code locally, you may need to select "Clean Build" in the Build
  134. // settings window to force a rebuild to pick up the changes.
  135. //
  136. // But we add the compiler executable to have at least on file in the package. This should be good
  137. // enough for users. Because any change in Burst will come with a change of the Burst package
  138. // version, which will change the pathname for this file (which will then trigger a rebuild, even
  139. // if the contents have not changed).
  140. BurstLoader.BclConfiguration.ExecutablePath,
  141. },
  142. displayName = "Running Burst Compiler"
  143. };
  144. }
  145. public IGenerateNativePluginsForAssemblies.GenerateResult GenerateNativePluginsForAssemblies(IGenerateNativePluginsForAssemblies.GenerateArgs args)
  146. {
  147. if (ForceDisableBurstCompilation)
  148. return new ();
  149. if (Directory.Exists(BurstAotCompiler.OutputBaseFolder))
  150. Directory.Delete(BurstAotCompiler.OutputBaseFolder, true);
  151. var assemblies = args.assemblyFiles.Select(path => new Assembly(
  152. Path.GetFileNameWithoutExtension(path),
  153. path,
  154. Array.Empty<string>(),
  155. Array.Empty<string>(),
  156. Array.Empty<Assembly>(),
  157. Array.Empty<string>(),
  158. UnityEditor.Compilation.AssemblyFlags.None))
  159. // We don't run Burst on UnityEngine assemblies, so we skip them to save time
  160. .Where(a => !a.name.StartsWith("UnityEngine."))
  161. .ToArray();
  162. return new () { generatedPlugins = DoGenerate(assemblies).ToArray() };
  163. }
  164. #else
  165. public void OnPostBuildPlayerScriptDLLs(BuildReport report)
  166. {
  167. if (ForceDisableBurstCompilation)
  168. {
  169. return;
  170. }
  171. var step = report.BeginBuildStep("burst");
  172. try
  173. {
  174. DoSetup(report);
  175. DoGenerate(BurstAotCompiler.GetPlayerAssemblies(report))
  176. .ToList(); // Force enumeration
  177. }
  178. finally
  179. {
  180. report.EndBuildStep(step);
  181. }
  182. }
  183. #endif
  184. private BurstAotCompiler.BurstAOTSettings settings;
  185. public void DoSetup(BuildReport report)
  186. {
  187. settings = new BurstAotCompiler.BurstAOTSettings()
  188. {
  189. summary = report.summary,
  190. productName = PlayerSettings.productName
  191. };
  192. settings.aotSettingsForTarget = BurstPlatformAotSettings.GetOrCreateSettings(settings.summary.platform);
  193. settings.isSupported = BurstAotCompiler.IsSupportedPlatform(settings.summary.platform, settings.aotSettingsForTarget);
  194. if (settings.isSupported)
  195. {
  196. settings.targetPlatform = BurstAotCompiler.GetTargetPlatformAndDefaultCpu(settings.summary.platform,
  197. out settings.targetCpus, settings.aotSettingsForTarget);
  198. settings.combinations =
  199. BurstAotCompiler.CollectCombinations(settings.targetPlatform, settings.targetCpus,
  200. settings.summary);
  201. settings.scriptingBackend =
  202. #if UNITY_2021_2_OR_NEWER
  203. PlayerSettings.GetScriptingBackend(NamedBuildTarget.FromBuildTargetGroup(BuildPipeline.GetBuildTargetGroup(settings.summary.platform)));
  204. #else
  205. PlayerSettings.GetScriptingBackend(BuildPipeline.GetBuildTargetGroup(settings.summary.platform));
  206. #endif
  207. #if UNITY_IOS
  208. if (settings.targetPlatform == TargetPlatform.iOS)
  209. {
  210. settings.extraOptions = new List<string>();
  211. settings.extraOptions.Add(GetOption(OptionLinkerOptions, $"min-ios-version={PlayerSettings.iOS.targetOSVersionString}"));
  212. settings.extraOptions.Add(GetOption(OptionPlatformConfiguration, PlayerSettings.iOS.targetOSVersionString));
  213. }
  214. #endif
  215. #if UNITY_TVOS
  216. if (settings.targetPlatform == TargetPlatform.tvOS)
  217. {
  218. settings.extraOptions = new List<string>();
  219. settings.extraOptions.Add(GetOption(OptionLinkerOptions, $"min-tvos-version={PlayerSettings.tvOS.targetOSVersionString}"));
  220. settings.extraOptions.Add(GetOption(OptionPlatformConfiguration, PlayerSettings.tvOS.targetOSVersionString));
  221. }
  222. #endif
  223. #if UNITY_VISIONOS
  224. if (settings.targetPlatform == TargetPlatform.visionOS || settings.targetPlatform == TargetPlatform.visionSimulator)
  225. {
  226. settings.extraOptions = new List<string>();
  227. #if UNITY_2023_3_OR_NEWER
  228. var targetOSVersionString = PlayerSettings.VisionOS.targetOSVersionString;
  229. #else
  230. var playerSettings = typeof(PlayerSettings);
  231. var visionOs = playerSettings.GetNestedType("VisionOS");
  232. if (visionOs == null) throw new Exception("Editor does not appear to support visionOS");
  233. var targetOSVersionStringProperty = visionOs.GetProperty("targetOSVersionString", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
  234. if (targetOSVersionStringProperty == null) throw new Exception("Property `targetOSVersionString` not found");
  235. var targetOSVersionString = targetOSVersionStringProperty.GetValue(null, null);
  236. #endif
  237. settings.extraOptions.Add(GetOption(OptionLinkerOptions, $"min-visionos-version={targetOSVersionString}"));
  238. settings.extraOptions.Add(GetOption(OptionPlatformConfiguration, targetOSVersionString));
  239. }
  240. #endif
  241. #if UNITY_2022_2_OR_NEWER && UNITY_ANDROID
  242. if (settings.targetPlatform == TargetPlatform.Android)
  243. {
  244. // Enable Armv9 security features (PAC/BTI) if needed
  245. settings.aotSettingsForTarget.EnableArmv9SecurityFeatures = PlayerSettings.Android.enableArmv9SecurityFeatures;
  246. if (PlayerSettings.Android.enableArmv9SecurityFeatures)
  247. {
  248. settings.extraOptions ??= new List<string>();
  249. settings.extraOptions.Add(GetOption(OptionPlatformConfiguration, "armv9-sec"));
  250. }
  251. }
  252. #endif
  253. if (settings.targetPlatform == TargetPlatform.UWP)
  254. {
  255. settings.extraOptions = new List<string>();
  256. if (!string.IsNullOrEmpty(EditorUserBuildSettings.wsaUWPVisualStudioVersion))
  257. {
  258. settings.extraOptions.Add(GetOption(OptionLinkerOptions, $"vs-version={EditorUserBuildSettings.wsaUWPVisualStudioVersion}"));
  259. }
  260. if (!string.IsNullOrEmpty(EditorUserBuildSettings.wsaUWPSDK))
  261. {
  262. settings.extraOptions.Add(GetOption(OptionLinkerOptions, $"target-sdk-version={EditorUserBuildSettings.wsaUWPSDK}"));
  263. }
  264. settings.extraOptions.Add(GetOption(OptionPlatformConfiguration, $"{EditorUserBuildSettings.wsaUWPVisualStudioVersion}:{EditorUserBuildSettings.wsaUWPSDK}:{EditorUserBuildSettings.wsaMinUWPSDK}"));
  265. }
  266. #if PLATFORM_QNX
  267. if (settings.targetPlatform == TargetPlatform.QNX)
  268. {
  269. settings.extraOptions = new List<string>();
  270. settings.extraOptions.Add(GetOption(OptionPlatformConfiguration, GetQNXTargetOsVersion()));
  271. }
  272. #endif
  273. settings.Save();
  274. }
  275. }
  276. private static string GetQNXTargetOsVersion()
  277. {
  278. var flags = System.Reflection.BindingFlags.Public |
  279. System.Reflection.BindingFlags.Static |
  280. System.Reflection.BindingFlags.FlattenHierarchy;
  281. var property = typeof(EditorUserBuildSettings).GetProperty("selectedQnxOsVersion", flags);
  282. if (null == property)
  283. {
  284. return "NOT_FOUND";
  285. }
  286. var value = (int)property.GetValue(null, null);
  287. switch (value)
  288. {
  289. case /*UnityEditor.QNXOsVersion.Neutrino70*/ 0: return "Neutrino70";
  290. case /*UnityEditor.QNXOsVersion.Neutrino71*/ 1: return "Neutrino71";
  291. default: return $"UNKNOWN_{value}";
  292. }
  293. }
  294. public IEnumerable<string> DoGenerate(Assembly[] assemblies)
  295. {
  296. if (!settings.isSupported)
  297. return Array.Empty<string>();
  298. return BurstAotCompiler.OnPostBuildPlayerScriptDLLsImpl(settings, assemblies);
  299. }
  300. }
  301. #if !ENABLE_GENERATE_NATIVE_PLUGINS_FOR_ASSEMBLIES_API
  302. internal class BurstAndroidGradlePostprocessor : IPostGenerateGradleAndroidProject
  303. {
  304. int IOrderedCallback.callbackOrder => 1;
  305. void IPostGenerateGradleAndroidProject.OnPostGenerateGradleAndroidProject(string path)
  306. {
  307. var aotSettingsForTarget = BurstPlatformAotSettings.GetOrCreateSettings(BuildTarget.Android);
  308. // Early exit if burst is not activated
  309. if (BurstCompilerOptions.ForceDisableBurstCompilation || !aotSettingsForTarget.EnableBurstCompilation)
  310. {
  311. return;
  312. }
  313. // Copy bursted .so's from tempburstlibs to the actual location in the gradle project
  314. var sourceLocation = Path.GetFullPath(Path.Combine("Temp", "StagingArea", "tempburstlibs"));
  315. var targetLocation = Path.GetFullPath(Path.Combine(path, "src", "main", "jniLibs"));
  316. FileUtil.CopyDirectoryRecursive(sourceLocation, targetLocation, true);
  317. }
  318. }
  319. // For static builds, there are two different approaches:
  320. // Postprocessing adds the libraries after Unity is done building,
  321. // for platforms that need to build a project file, etc.
  322. // Preprocessing simply adds the libraries to the Unity build,
  323. // for platforms where Unity can directly build an app.
  324. internal class StaticPreProcessor : IPreprocessBuildWithReport
  325. {
  326. private const string TempSourceLibrary = @"Temp/StagingArea/SourcePlugins";
  327. public int callbackOrder { get { return 0; } }
  328. public void OnPreprocessBuild(BuildReport report)
  329. {
  330. var aotSettingsForTarget = BurstPlatformAotSettings.GetOrCreateSettings(report.summary.platform);
  331. // Early exit if burst is not activated
  332. if (BurstCompilerOptions.ForceDisableBurstCompilation || !aotSettingsForTarget.EnableBurstCompilation)
  333. {
  334. return;
  335. }
  336. if(report.summary.platform == BuildTarget.Switch)
  337. {
  338. if(!Directory.Exists(TempSourceLibrary))
  339. {
  340. Directory.CreateDirectory(TempSourceLibrary);
  341. Directory.CreateDirectory(TempSourceLibrary);
  342. }
  343. BurstAotCompiler.WriteStaticLinkCppFile(TempSourceLibrary);
  344. }
  345. }
  346. }
  347. #endif
  348. /// <summary>
  349. /// Integration of the burst AOT compiler into the Unity build player pipeline
  350. /// </summary>
  351. internal class BurstAotCompiler
  352. {
  353. #if ENABLE_GENERATE_NATIVE_PLUGINS_FOR_ASSEMBLIES_API
  354. // When using the new player build API, don't write to Temp/StagingArea.
  355. // We still need code in Unity to support old versions of Burst not using the new API.
  356. // for that case, we will just pick up files written to the Temp/StagingArea.
  357. // So in order to not pick up files twice, use a different output location for the new
  358. // API.
  359. internal const string OutputBaseFolder = @"Temp/BurstOutput/";
  360. #else
  361. private const string OutputBaseFolder = @"Temp/StagingArea/";
  362. #endif
  363. private const string TempStagingManaged = OutputBaseFolder + @"Data/Managed/";
  364. private const string LibraryPlayerScriptAssemblies = "Library/PlayerScriptAssemblies";
  365. private const string TempManagedSymbols = @"Temp/ManagedSymbols/";
  366. internal const string BurstLinkXmlName = "burst.link.xml";
  367. internal struct BurstAOTSettings
  368. {
  369. public BuildSummary summary;
  370. public BurstPlatformAotSettings aotSettingsForTarget;
  371. public TargetPlatform targetPlatform;
  372. public TargetCpus targetCpus;
  373. public List<BurstAotCompiler.BurstOutputCombination> combinations;
  374. public ScriptingImplementation scriptingBackend;
  375. public string productName;
  376. public bool isSupported;
  377. public List<string> extraOptions;
  378. // Hash any fields that might have an effect on whether Bursted code needs to be recompiled
  379. // Note that the BurstPlatformAotSettings are saved and used separately, so they don't need to
  380. // be included in this hash.
  381. public Hash128 Hash()
  382. {
  383. var hc = new Hash128();
  384. hc.Append((int)(summary.options & (BuildOptions.InstallInBuildFolder | BuildOptions.Development)));
  385. hc.Append((int)targetPlatform);
  386. hc.Append(isSupported ? 1 : 0);
  387. if (targetCpus?.Cpus != null)
  388. {
  389. hc.Append(targetCpus.Cpus.Count);
  390. foreach (var cpu in targetCpus.Cpus)
  391. {
  392. hc.Append((int)cpu);
  393. }
  394. }
  395. if (combinations != null)
  396. {
  397. hc.Append(combinations.Count);
  398. foreach (var comb in combinations)
  399. {
  400. comb.HashInto(ref hc);
  401. }
  402. }
  403. if (extraOptions != null)
  404. {
  405. hc.Append(extraOptions.Count);
  406. foreach (var opt in extraOptions)
  407. {
  408. hc.Append(opt);
  409. }
  410. }
  411. hc.Append((int)scriptingBackend);
  412. hc.Append(productName);
  413. return hc;
  414. }
  415. public void Save()
  416. {
  417. var path = GetPath(BurstPlatformAotSettings.ResolveTarget(summary.platform));
  418. var hash = Hash();
  419. try
  420. {
  421. if (File.Exists(path))
  422. {
  423. // If the hashes match, don't touch the file so Bee will consider it unchanged
  424. var bytes = File.ReadAllBytes(path);
  425. var storedHash = new Hash128(BitConverter.ToUInt64(bytes, 0), BitConverter.ToUInt64(bytes, 8));
  426. if (storedHash == hash)
  427. {
  428. return;
  429. }
  430. }
  431. using (var f = new BufferedStream(File.OpenWrite(path)))
  432. {
  433. f.Write(BitConverter.GetBytes(hash.u64_0), 0, 8);
  434. f.Write(BitConverter.GetBytes(hash.u64_1), 0, 8);
  435. }
  436. }
  437. catch (Exception)
  438. {
  439. // If we for some reason fail to save the settings, delete the existing ones (if any) to be sure
  440. // we invalidate the cache and cause a recompilation
  441. try
  442. {
  443. if (File.Exists(path))
  444. {
  445. File.Delete(path);
  446. }
  447. }
  448. catch (Exception)
  449. {
  450. // Welp
  451. }
  452. }
  453. }
  454. public static string GetPath(BuildTarget? target)
  455. {
  456. var root = "Library/BurstCache";
  457. if (target.HasValue)
  458. {
  459. return $"{root}/AotSettings_{target.Value}.hash";
  460. }
  461. else
  462. {
  463. return $"{root}/AotSettings.hash";
  464. }
  465. }
  466. }
  467. static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
  468. {
  469. // Get information about the source directory
  470. var dir = new DirectoryInfo(sourceDir);
  471. // Check if the source directory exists
  472. if (!dir.Exists)
  473. throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
  474. // Cache directories before we start copying
  475. DirectoryInfo[] dirs = dir.GetDirectories();
  476. // Create the destination directory
  477. Directory.CreateDirectory(destinationDir);
  478. // Get the files in the source directory and copy to the destination directory
  479. foreach (FileInfo file in dir.GetFiles())
  480. {
  481. string targetFilePath = Path.Combine(destinationDir, file.Name);
  482. file.CopyTo(targetFilePath);
  483. }
  484. // If recursive and copying subdirectories, recursively call this method
  485. if (recursive)
  486. {
  487. foreach (DirectoryInfo subDir in dirs)
  488. {
  489. string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
  490. CopyDirectory(subDir.FullName, newDestinationDir, true);
  491. }
  492. }
  493. }
  494. internal static IEnumerable<string> OnPostBuildPlayerScriptDLLsImpl(BurstAOTSettings settings, Assembly[] playerAssemblies)
  495. {
  496. var buildTarget = settings.summary.platform;
  497. string burstMiscAlongsidePath = "";
  498. if ((settings.summary.options & BuildOptions.InstallInBuildFolder) == 0)
  499. {
  500. burstMiscAlongsidePath = BurstPlatformAotSettings.FetchOutputPath(settings.summary, settings.productName);
  501. }
  502. HashSet<string> assemblyDefines = new HashSet<string>();
  503. // Early exit if burst is not activated or the platform is not supported
  504. if (BurstCompilerOptions.ForceDisableBurstCompilation || !settings.aotSettingsForTarget.EnableBurstCompilation)
  505. {
  506. return Array.Empty<string>();
  507. }
  508. var isDevelopmentBuild = (settings.summary.options & BuildOptions.Development) != 0;
  509. var commonOptions = new List<string>();
  510. var stagingFolder = Path.GetFullPath(TempStagingManaged);
  511. // grab the location of the root of the player folder - for handling nda platforms that require keys
  512. var keyFolder = BuildPipeline.GetPlaybackEngineDirectory(buildTarget, BuildOptions.None);
  513. commonOptions.Add(GetOption(OptionAotKeyFolder, keyFolder));
  514. commonOptions.Add(GetOption(OptionAotDecodeFolder, Path.Combine(Environment.CurrentDirectory, "Library", "Burst")));
  515. // Extract the TargetPlatform and Cpus from the current build settings
  516. commonOptions.Add(GetOption(OptionPlatform, settings.targetPlatform));
  517. // --------------------------------------------------------------------------------------------------------
  518. // 1) Calculate AssemblyFolders
  519. // These are the folders to look for assembly resolution
  520. // --------------------------------------------------------------------------------------------------------
  521. var assemblyFolders = new List<string> { stagingFolder };
  522. foreach (var assembly in playerAssemblies)
  523. AddAssemblyFolder(assembly.outputPath, stagingFolder, buildTarget, assemblyFolders);
  524. if (buildTarget == BuildTarget.WSAPlayer || buildTarget == BuildTarget.GameCoreXboxOne || buildTarget == BuildTarget.GameCoreXboxSeries)
  525. {
  526. // On UWP, not all assemblies are copied to StagingArea, so we want to
  527. // find all directories that we can reference assemblies from
  528. // If we don't do this, we will crash with AssemblyResolutionException
  529. // when following type references.
  530. foreach (var assembly in playerAssemblies)
  531. {
  532. foreach (var assemblyRef in assembly.compiledAssemblyReferences)
  533. AddAssemblyFolder(assemblyRef, stagingFolder, buildTarget, assemblyFolders);
  534. }
  535. }
  536. if (settings.extraOptions != null)
  537. {
  538. commonOptions.AddRange(settings.extraOptions);
  539. }
  540. // Copy assembly used during staging to have a trace
  541. if (BurstLoader.IsDebugging)
  542. {
  543. try
  544. {
  545. var copyAssemblyFolder = Path.Combine(Environment.CurrentDirectory, "Logs", "StagingAssemblies");
  546. try
  547. {
  548. if (Directory.Exists(copyAssemblyFolder)) Directory.Delete(copyAssemblyFolder);
  549. }
  550. catch
  551. {
  552. }
  553. if (!Directory.Exists(copyAssemblyFolder)) Directory.CreateDirectory(copyAssemblyFolder);
  554. foreach (var file in Directory.EnumerateFiles(stagingFolder))
  555. {
  556. File.Copy(file, Path.Combine(copyAssemblyFolder, Path.GetFileName(file)));
  557. }
  558. }
  559. catch
  560. {
  561. }
  562. }
  563. // --------------------------------------------------------------------------------------------------------
  564. // 2) Calculate root assemblies
  565. // These are the assemblies that the compiler will look for methods to compile
  566. // This list doesn't typically include .NET runtime assemblies but only assemblies compiled as part
  567. // of the current Unity project
  568. // --------------------------------------------------------------------------------------------------------
  569. var rootAssemblies = new List<string>();
  570. foreach (var playerAssembly in playerAssemblies)
  571. {
  572. #if ENABLE_GENERATE_NATIVE_PLUGINS_FOR_ASSEMBLIES_API
  573. var playerAssemblyPath = Path.GetFullPath(playerAssembly.outputPath);
  574. #else
  575. // the file at path `playerAssembly.outputPath` is actually not on the disk
  576. // while it is in the staging folder because OnPostBuildPlayerScriptDLLs is being called once the files are already
  577. // transferred to the staging folder, so we are going to work from it but we are reusing the file names that we got earlier
  578. var playerAssemblyPath = Path.Combine(stagingFolder, Path.GetFileName(playerAssembly.outputPath));
  579. #endif
  580. if (!File.Exists(playerAssemblyPath))
  581. {
  582. Debug.LogWarning($"Unable to find player assembly: {playerAssembly.outputPath}");
  583. }
  584. else
  585. {
  586. rootAssemblies.Add(playerAssemblyPath);
  587. commonOptions.Add(GetOption(OptionAssemblyDefines, $"{playerAssembly.name};{string.Join(";", playerAssembly.defines)}"));
  588. }
  589. }
  590. commonOptions.AddRange(rootAssemblies.Select(root => GetOption(OptionRootAssembly, root)));
  591. // --------------------------------------------------------------------------------------------------------
  592. // 4) Compile each combination
  593. //
  594. // Here bcl.exe is called for each target CPU combination
  595. // --------------------------------------------------------------------------------------------------------
  596. string debugLogFile = null;
  597. if (BurstLoader.IsDebugging)
  598. {
  599. // Reset log files
  600. try
  601. {
  602. var logDir = Path.Combine(Environment.CurrentDirectory, "Logs");
  603. debugLogFile = Path.Combine(logDir, "burst_bcl_editor.log");
  604. if (!Directory.Exists(logDir)) Directory.CreateDirectory(logDir);
  605. File.WriteAllText(debugLogFile, string.Empty);
  606. }
  607. catch
  608. {
  609. debugLogFile = null;
  610. }
  611. }
  612. if ((settings.summary.options & BuildOptions.InstallInBuildFolder) == 0)
  613. {
  614. CreateFolderForMiscFiles(burstMiscAlongsidePath);
  615. }
  616. // Log the targets generated by BurstReflection.FindExecuteMethods
  617. foreach (var combination in settings.combinations)
  618. {
  619. // Gets the output folder
  620. var stagingOutputFolder = Path.GetFullPath(Path.Combine(OutputBaseFolder, combination.OutputPath));
  621. var outputFilePrefix = Path.Combine(stagingOutputFolder, combination.LibraryName);
  622. var options = new List<string>(commonOptions)
  623. {
  624. GetOption(OptionAotOutputPath, outputFilePrefix),
  625. GetOption(OptionTempDirectory, Path.Combine(Environment.CurrentDirectory, "Temp", "Burst"))
  626. };
  627. foreach (var cpu in combination.TargetCpus.Cpus)
  628. {
  629. options.Add(GetOption(OptionTarget, cpu));
  630. }
  631. if (settings.targetPlatform == TargetPlatform.iOS || settings.targetPlatform == TargetPlatform.tvOS || settings.targetPlatform == TargetPlatform.Switch || settings.targetPlatform == TargetPlatform.visionOS)
  632. {
  633. options.Add(GetOption(OptionStaticLinkage));
  634. #if ENABLE_GENERATE_NATIVE_PLUGINS_FOR_ASSEMBLIES_API
  635. WriteStaticLinkCppFile($"{OutputBaseFolder}/{combination.OutputPath}");
  636. #endif
  637. }
  638. if (settings.targetPlatform == TargetPlatform.Windows)
  639. {
  640. options.Add(GetOption(OptionLinkerOptions, $"PdbAltPath=\"{settings.productName}_{combination.OutputPath}/{Path.GetFileNameWithoutExtension(combination.LibraryName)}.pdb\""));
  641. }
  642. #if UNITY_2022_2_OR_NEWER && UNITY_ANDROID
  643. if (settings.targetPlatform == TargetPlatform.Android)
  644. {
  645. // Enable Armv9 security features (PAC/BTI) if needed
  646. if (settings.aotSettingsForTarget.EnableArmv9SecurityFeatures)
  647. options.Add(GetOption(OptionBranchProtection, "Standard"));
  648. }
  649. #endif
  650. options.AddRange(assemblyFolders.Select(assemblyFolder => GetOption(OptionAotAssemblyFolder, assemblyFolder)));
  651. // Set the flag to print a message on missing MonoPInvokeCallback attribute on IL2CPP only
  652. if (settings.scriptingBackend == ScriptingImplementation.IL2CPP)
  653. {
  654. options.Add(GetOption(OptionPrintLogOnMissingPInvokeCallbackAttribute));
  655. }
  656. // Log the targets generated by BurstReflection.FindExecuteMethods
  657. if (BurstLoader.IsDebugging && debugLogFile != null)
  658. {
  659. try
  660. {
  661. var writer = new StringWriter();
  662. writer.WriteLine("-----------------------------------------------------------");
  663. writer.WriteLine("Combination: " + combination);
  664. writer.WriteLine("-----------------------------------------------------------");
  665. foreach (var option in options)
  666. {
  667. writer.WriteLine(option);
  668. }
  669. writer.WriteLine("Assemblies in AssemblyFolders:");
  670. foreach (var assemblyFolder in assemblyFolders)
  671. {
  672. writer.WriteLine("|- Folder: " + assemblyFolder);
  673. foreach (var assemblyOrDll in Directory.EnumerateFiles(assemblyFolder, "*.dll"))
  674. {
  675. var fileInfo = new FileInfo(assemblyOrDll);
  676. writer.WriteLine(" |- " + assemblyOrDll + " Size: " + fileInfo.Length + " Date: " + fileInfo.LastWriteTime);
  677. }
  678. }
  679. File.AppendAllText(debugLogFile, writer.ToString());
  680. }
  681. catch
  682. {
  683. // ignored
  684. }
  685. }
  686. // Allow burst to find managed symbols in the backup location in case the symbols are stripped in the build location
  687. options.Add(GetOption(OptionAotPdbSearchPaths, TempManagedSymbols));
  688. if (isDevelopmentBuild && Environment.GetEnvironmentVariable("UNITY_BURST_ENABLE_SAFETY_CHECKS_IN_PLAYER_BUILD") != null)
  689. {
  690. options.Add("--global-safety-checks-setting=ForceOn");
  691. }
  692. options.Add(GetOption(OptionGenerateLinkXml, Path.Combine("Temp", BurstLinkXmlName)));
  693. if (!string.IsNullOrWhiteSpace(settings.aotSettingsForTarget.DisabledWarnings))
  694. {
  695. options.Add(GetOption(OptionDisableWarnings, settings.aotSettingsForTarget.DisabledWarnings));
  696. }
  697. if (isDevelopmentBuild || settings.aotSettingsForTarget.EnableDebugInAllBuilds)
  698. {
  699. if (!isDevelopmentBuild)
  700. {
  701. Debug.LogWarning(
  702. "Symbols are being generated for burst compiled code, please ensure you intended this - see Burst AOT settings.");
  703. }
  704. options.Add(GetOption(OptionDebug,
  705. (settings.aotSettingsForTarget.DebugDataKind == DebugDataKind.Full) && (!combination.WorkaroundFullDebugInfo) ? "Full" : "LineOnly"));
  706. }
  707. if (!settings.aotSettingsForTarget.EnableOptimisations)
  708. {
  709. options.Add(GetOption(OptionDisableOpt));
  710. }
  711. else
  712. {
  713. switch (settings.aotSettingsForTarget.OptimizeFor)
  714. {
  715. case OptimizeFor.Default:
  716. case OptimizeFor.Balanced:
  717. options.Add(GetOption(OptionOptLevel, 2));
  718. break;
  719. case OptimizeFor.Performance:
  720. options.Add(GetOption(OptionOptLevel, 3));
  721. break;
  722. case OptimizeFor.Size:
  723. options.Add(GetOption(OptionOptForSize));
  724. options.Add(GetOption(OptionOptLevel, 3));
  725. break;
  726. case OptimizeFor.FastCompilation:
  727. options.Add(GetOption(OptionOptLevel, 1));
  728. break;
  729. }
  730. }
  731. if (BurstLoader.IsDebugging)
  732. {
  733. options.Add(GetOption("debug-logging"));
  734. }
  735. // Add list of assemblies to ignore
  736. var disabledAssemblies = BurstAssemblyDisable.GetDisabledAssemblies(BurstAssemblyDisable.DisableType.Player, BurstPlatformAotSettings.ResolveTarget(buildTarget).ToString());
  737. foreach (var discard in disabledAssemblies)
  738. {
  739. options.Add(GetOption(OptionDiscardAssemblies, discard));
  740. }
  741. // Write current options to the response file
  742. var responseFile = Path.GetTempFileName();
  743. File.WriteAllLines(responseFile, options);
  744. if (BurstLoader.IsDebugging)
  745. {
  746. Debug.Log($"bcl.exe {OptionBurstcSwitch} @{responseFile}\n\nResponse File:\n" + string.Join("\n", options));
  747. }
  748. try
  749. {
  750. var burstcSwitch = OptionBurstcSwitch;
  751. if (!string.IsNullOrEmpty(
  752. Environment.GetEnvironmentVariable("UNITY_BURST_DISABLE_INCREMENTAL_PLAYER_BUILDS")))
  753. {
  754. burstcSwitch = "";
  755. }
  756. if (BurstLoader.BclConfiguration.IsExecutableNative)
  757. {
  758. BclRunner.RunNativeProgram(
  759. BurstLoader.BclConfiguration.ExecutablePath,
  760. $"{burstcSwitch} {BclRunner.EscapeForShell("@" + responseFile)}",
  761. new BclOutputErrorParser());
  762. }
  763. else
  764. {
  765. BclRunner.RunManagedProgram(
  766. BurstLoader.BclConfiguration.ExecutablePath,
  767. $"{burstcSwitch} {BclRunner.EscapeForShell("@" + responseFile)}",
  768. new BclOutputErrorParser());
  769. }
  770. // Additionally copy the pdb to the root of the player build so run in editor also locates the symbols
  771. var pdbPath = $"{Path.Combine(stagingOutputFolder, combination.LibraryName)}.pdb";
  772. if (File.Exists(pdbPath))
  773. {
  774. var dstPath = Path.Combine(OutputBaseFolder, $"{combination.LibraryName}.pdb");
  775. File.Copy(pdbPath, dstPath, overwrite: true);
  776. }
  777. }
  778. catch (BuildFailedException)
  779. {
  780. throw;
  781. }
  782. catch (Exception e)
  783. {
  784. throw new BuildFailedException(e);
  785. }
  786. }
  787. PostProcessCombinations(settings.targetPlatform, settings.combinations, settings.summary);
  788. var pdbsRemainInBuild = isDevelopmentBuild || settings.aotSettingsForTarget.EnableDebugInAllBuilds || settings.targetPlatform == TargetPlatform.UWP;
  789. // Finally move out any symbols/misc files from the final output
  790. if ((settings.summary.options & BuildOptions.InstallInBuildFolder) == 0)
  791. {
  792. return CollateMiscFiles(settings.combinations, burstMiscAlongsidePath, pdbsRemainInBuild);
  793. }
  794. return Array.Empty<string>();
  795. }
  796. private static void AddAssemblyFolder(string assemblyRef, string stagingFolder, BuildTarget buildTarget,
  797. List<string> assemblyFolders)
  798. {
  799. // Exclude folders with assemblies already compiled in the `folder`
  800. var assemblyName = Path.GetFileName(assemblyRef);
  801. if (assemblyName != null && File.Exists(Path.Combine(stagingFolder, assemblyName)))
  802. {
  803. return;
  804. }
  805. var directory = Path.GetDirectoryName(assemblyRef);
  806. if (directory != null)
  807. {
  808. var fullPath = Path.GetFullPath(directory);
  809. if (IsMonoReferenceAssemblyDirectory(fullPath) || IsDotNetStandardAssemblyDirectory(fullPath))
  810. {
  811. // Don't pass reference assemblies to burst because they contain methods without implementation
  812. // If burst accidentally resolves them, it will emit calls to burst_abort.
  813. fullPath = Path.Combine(EditorApplication.applicationContentsPath, "MonoBleedingEdge/lib/mono");
  814. #if UNITY_2021_2_OR_NEWER
  815. // In 2021.2 we got multiple mono distributions, per platform.
  816. fullPath = Path.Combine(fullPath, "unityaot-" + BuildTargetDiscovery.GetPlatformProfileSuffix(buildTarget));
  817. #else
  818. fullPath = Path.Combine(fullPath, "unityaot");
  819. #endif
  820. fullPath = Path.GetFullPath(fullPath); // GetFullPath will normalize path separators to OS native format
  821. if (!assemblyFolders.Contains(fullPath))
  822. assemblyFolders.Add(fullPath);
  823. fullPath = Path.Combine(fullPath, "Facades");
  824. if (!assemblyFolders.Contains(fullPath))
  825. assemblyFolders.Add(fullPath);
  826. }
  827. else if (!assemblyFolders.Contains(fullPath))
  828. {
  829. assemblyFolders.Add(fullPath);
  830. }
  831. }
  832. }
  833. private static void CreateFolderForMiscFiles(string finalFolder)
  834. {
  835. try
  836. {
  837. if (Directory.Exists(finalFolder)) Directory.Delete(finalFolder,true);
  838. }
  839. catch
  840. {
  841. }
  842. Directory.CreateDirectory(finalFolder);
  843. }
  844. private static IEnumerable<string> CollateMiscFiles(List<BurstOutputCombination> combinations, string finalFolder, bool retainPdbs)
  845. {
  846. foreach (var combination in combinations)
  847. {
  848. var inputPath = Path.GetFullPath(Path.Combine(OutputBaseFolder, combination.OutputPath));
  849. var outputPath = Path.Combine(finalFolder, combination.OutputPath);
  850. Directory.CreateDirectory(outputPath);
  851. if (!Directory.Exists(inputPath))
  852. continue;
  853. var files = Directory.GetFiles(inputPath);
  854. var directories = Directory.GetDirectories(inputPath);
  855. foreach (var fileName in files)
  856. {
  857. var lowerCase = fileName.ToLower();
  858. if ( (!retainPdbs && lowerCase.EndsWith(".pdb")) || lowerCase.EndsWith(".dsym") || lowerCase.EndsWith(".txt"))
  859. {
  860. // Move the file out of the staging area so its not included in the build
  861. File.Move(fileName, Path.Combine(outputPath, Path.GetFileName(fileName)));
  862. }
  863. else if (!combination.CollateDirectory)
  864. {
  865. yield return fileName;
  866. }
  867. }
  868. foreach (var fileName in directories)
  869. {
  870. var lowerCase = fileName.ToLower();
  871. if ( (!retainPdbs && lowerCase.EndsWith(".pdb")) || lowerCase.EndsWith(".dsym") || lowerCase.EndsWith(".txt"))
  872. {
  873. // Move the folder out of the staging area so its not included in the build
  874. Directory.Move(fileName, Path.Combine(outputPath, Path.GetFileName(fileName)));
  875. }
  876. else if (!combination.CollateDirectory)
  877. {
  878. yield return fileName;
  879. }
  880. }
  881. if (combination.CollateDirectory)
  882. yield return inputPath;
  883. }
  884. }
  885. private static bool AndroidHasX86(AndroidArchitecture architecture)
  886. {
  887. // Deal with rename that occured
  888. AndroidArchitecture val;
  889. if (AndroidArchitecture.TryParse("X86", out val))
  890. {
  891. return (architecture & val)!=0;
  892. }
  893. else if (AndroidArchitecture.TryParse("x86", out val))
  894. {
  895. return (architecture & val)!=0;
  896. }
  897. return false;
  898. }
  899. private static bool AndroidHasX86_64(AndroidArchitecture architecture)
  900. {
  901. // Deal with rename that occured
  902. AndroidArchitecture val;
  903. if (AndroidArchitecture.TryParse("X86_64", out val))
  904. {
  905. return (architecture & val)!=0;
  906. }
  907. else if (AndroidArchitecture.TryParse("x86_64", out val))
  908. {
  909. return (architecture & val)!=0;
  910. }
  911. return false;
  912. }
  913. private enum SimulatorPlatforms
  914. {
  915. iOS,
  916. tvOS
  917. }
  918. private static bool IsForSimulator(BuildTarget target)
  919. {
  920. switch (target)
  921. {
  922. case BuildTarget.iOS:
  923. return IsForSimulator(SimulatorPlatforms.iOS);
  924. case BuildTarget.tvOS:
  925. return IsForSimulator(SimulatorPlatforms.tvOS);
  926. default:
  927. return false;
  928. }
  929. }
  930. private static bool IsForSimulator(TargetPlatform targetPlatform)
  931. {
  932. switch (targetPlatform)
  933. {
  934. case TargetPlatform.iOS:
  935. return IsForSimulator(SimulatorPlatforms.iOS);
  936. case TargetPlatform.tvOS:
  937. return IsForSimulator(SimulatorPlatforms.tvOS);
  938. default:
  939. return false;
  940. }
  941. }
  942. private static bool IsForSimulator(SimulatorPlatforms simulatorPlatforms)
  943. {
  944. switch (simulatorPlatforms)
  945. {
  946. case SimulatorPlatforms.iOS:
  947. return UnityEditor.PlayerSettings.iOS.sdkVersion == iOSSdkVersion.SimulatorSDK;
  948. case SimulatorPlatforms.tvOS:
  949. return UnityEditor.PlayerSettings.tvOS.sdkVersion == tvOSSdkVersion.Simulator;
  950. }
  951. return false;
  952. }
  953. public static void WriteStaticLinkCppFile(string dir)
  954. {
  955. Directory.CreateDirectory(dir);
  956. string cppPath = Path.Combine(dir, "lib_burst_generated.cpp");
  957. // Additionally we need a small cpp file (weak symbols won't unfortunately override directly from the libs
  958. //presumably due to link order?
  959. File.WriteAllText(cppPath, @"
  960. extern ""C""
  961. {
  962. void Staticburst_initialize(void* );
  963. void* StaticBurstStaticMethodLookup(void* );
  964. int burst_enable_static_linkage = 1;
  965. void burst_initialize(void* i) { Staticburst_initialize(i); }
  966. void* BurstStaticMethodLookup(void* i) { return StaticBurstStaticMethodLookup(i); }
  967. }
  968. ");
  969. }
  970. /// <summary>
  971. /// Collect CPU combinations for the specified TargetPlatform and TargetCPU
  972. /// </summary>
  973. /// <param name="targetPlatform">The target platform (e.g Windows)</param>
  974. /// <param name="targetCpus">The target CPUs (e.g X64_SSE4)</param>
  975. /// <param name="report">Error reporting</param>
  976. /// <returns>The list of CPU combinations</returns>
  977. internal static List<BurstOutputCombination> CollectCombinations(TargetPlatform targetPlatform, TargetCpus targetCpus, BuildSummary summary)
  978. {
  979. var combinations = new List<BurstOutputCombination>();
  980. if (targetPlatform == TargetPlatform.macOS)
  981. {
  982. // NOTE: OSX has a special folder for the plugin
  983. // Declared in GetStagingAreaPluginsFolder
  984. // PlatformDependent\OSXPlayer\Extensions\Managed\OSXDesktopStandalonePostProcessor.cs
  985. var outputPath = Path.Combine(Path.GetFileName(summary.outputPath), "Contents", "Plugins");
  986. // Based on : PlatformDependent/OSXPlayer/Extension/OSXStandaloneBuildWindowExtension.cs
  987. var aotSettings = BurstPlatformAotSettings.GetOrCreateSettings(BuildTarget.StandaloneOSX);
  988. var buildTargetName = BuildPipeline.GetBuildTargetName(BuildTarget.StandaloneOSX);
  989. var architecture = EditorUserBuildSettings.GetPlatformSettings(buildTargetName, "Architecture").ToLowerInvariant();
  990. switch (architecture)
  991. {
  992. case "x64":
  993. combinations.Add(new BurstOutputCombination(outputPath, aotSettings.GetDesktopCpu64Bit()));
  994. break;
  995. case "arm64":
  996. // According to
  997. // https://web.archive.org/web/20220504192056/https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/AArch64TargetParser.def#L240
  998. // M1 is equivalent to Armv8.5-A, so it supports everything from HALFFP target
  999. // (there's no direct confirmation on crypto because it's not mandatory)
  1000. combinations.Add(new BurstOutputCombination(outputPath, new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64_HALFFP)));
  1001. break;
  1002. default:
  1003. combinations.Add(new BurstOutputCombination(Path.Combine(outputPath, "x64"), aotSettings.GetDesktopCpu64Bit()));
  1004. combinations.Add(new BurstOutputCombination(Path.Combine(outputPath, "arm64"), new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64_HALFFP)));
  1005. break;
  1006. }
  1007. }
  1008. else if (targetPlatform == TargetPlatform.iOS || targetPlatform == TargetPlatform.tvOS || targetPlatform == TargetPlatform.visionOS || targetPlatform == TargetPlatform.visionSimulator)
  1009. {
  1010. if (IsForSimulator(targetPlatform))
  1011. {
  1012. Debug.LogWarning("Burst Does not currently support the simulator, burst is disabled for this build.");
  1013. }
  1014. else if (Application.platform != RuntimePlatform.OSXEditor)
  1015. {
  1016. Debug.LogWarning("Burst Cross Compilation to iOS/tvOS for standalone player, is only supported on OSX Editor at this time, burst is disabled for this build.");
  1017. }
  1018. else
  1019. {
  1020. // Looks like a way to detect iOS CPU capabilities in runtime (like getauxval()) is sysctlbyname()
  1021. // https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics
  1022. // TODO: add support for it when needed, for now using the lowest common denominator
  1023. // https://web.archive.org/web/20220504192056/https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/AArch64TargetParser.def#L240
  1024. // This LLVM code implies A11 is the first Armv8.2-A CPU
  1025. // However, it doesn't support dotprod, so we can't consider it equivalent to our HALFFP variant
  1026. // A13 (equivalent to Armv8.4-A) and M1 seem to be the first CPUs we can claim HALFFP compatible
  1027. // Since we need to support older CPUs, have to use the "basic" Armv8A here
  1028. combinations.Add(new BurstOutputCombination("StaticLibraries", new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64)));
  1029. }
  1030. }
  1031. else if (targetPlatform == TargetPlatform.Android)
  1032. {
  1033. // TODO: would be better to query AndroidNdkRoot (but thats not exposed from unity)
  1034. string ndkRoot = null;
  1035. var targetAPILevel = PlayerSettings.Android.GetMinTargetAPILevel();
  1036. #if UNITY_ANDROID
  1037. ndkRoot = UnityEditor.Android.AndroidExternalToolsSettings.ndkRootPath;
  1038. #else
  1039. // 2019.1 now has an embedded ndk
  1040. if (EditorPrefs.HasKey("NdkUseEmbedded"))
  1041. {
  1042. if (EditorPrefs.GetBool("NdkUseEmbedded"))
  1043. {
  1044. ndkRoot = Path.Combine(BuildPipeline.GetPlaybackEngineDirectory(BuildTarget.Android, BuildOptions.None), "NDK");
  1045. }
  1046. else
  1047. {
  1048. ndkRoot = EditorPrefs.GetString("AndroidNdkRootR16b");
  1049. }
  1050. }
  1051. #endif
  1052. // If we still don't have a valid root, try the old key
  1053. if (string.IsNullOrEmpty(ndkRoot))
  1054. {
  1055. ndkRoot = EditorPrefs.GetString("AndroidNdkRoot");
  1056. }
  1057. // Verify the directory at least exists, if not we fall back to ANDROID_NDK_ROOT current setting
  1058. if (!string.IsNullOrEmpty(ndkRoot) && !Directory.Exists(ndkRoot))
  1059. {
  1060. ndkRoot = null;
  1061. }
  1062. // Always set the ANDROID_NDK_ROOT (if we got a valid result from above), so BCL knows where to find the Android toolchain and its the one the user expects
  1063. if (!string.IsNullOrEmpty(ndkRoot))
  1064. {
  1065. Environment.SetEnvironmentVariable("ANDROID_NDK_ROOT", ndkRoot);
  1066. }
  1067. Environment.SetEnvironmentVariable("BURST_ANDROID_MIN_API_LEVEL", $"{targetAPILevel}");
  1068. // Setting tempburstlibs/ as the interim target directory
  1069. // Don't target libs/ directly because incremental build pipeline doesn't expect the so's at that path
  1070. // Rather, so's are copied to the actual location in the gradle project in BurstAndroidGradlePostprocessor
  1071. var androidTargetArch = PlayerSettings.Android.targetArchitectures;
  1072. if ((androidTargetArch & AndroidArchitecture.ARMv7) != 0)
  1073. {
  1074. combinations.Add(new BurstOutputCombination("tempburstlibs/armeabi-v7a", new TargetCpus(BurstTargetCpu.ARMV7A_NEON32), collateDirectory: true));
  1075. }
  1076. if ((androidTargetArch & AndroidArchitecture.ARM64) != 0)
  1077. {
  1078. var aotSettingsForTarget = BurstPlatformAotSettings.GetOrCreateSettings(summary.platform);
  1079. combinations.Add(new BurstOutputCombination("tempburstlibs/arm64-v8a", aotSettingsForTarget.GetAndroidCpuArm64(), collateDirectory: true));
  1080. }
  1081. #if UNITY_2019_4_OR_NEWER
  1082. if (AndroidHasX86(androidTargetArch))
  1083. {
  1084. combinations.Add(new BurstOutputCombination("tempburstlibs/x86", new TargetCpus(BurstTargetCpu.X86_SSE4), collateDirectory: true));
  1085. }
  1086. if (AndroidHasX86_64(androidTargetArch))
  1087. {
  1088. combinations.Add(new BurstOutputCombination("tempburstlibs/x86_64", new TargetCpus(BurstTargetCpu.X64_SSE4), collateDirectory: true));
  1089. }
  1090. #endif
  1091. }
  1092. else if (targetPlatform == TargetPlatform.UWP)
  1093. {
  1094. var aotSettingsForTarget = BurstPlatformAotSettings.GetOrCreateSettings(summary.platform);
  1095. if (EditorUserBuildSettings.wsaUWPBuildType == WSAUWPBuildType.ExecutableOnly)
  1096. {
  1097. combinations.Add(new BurstOutputCombination($"Plugins/{GetUWPTargetArchitecture()}", targetCpus, collateDirectory: true));
  1098. }
  1099. else
  1100. {
  1101. combinations.Add(new BurstOutputCombination("Plugins/x64", aotSettingsForTarget.GetDesktopCpu64Bit(), collateDirectory: true));
  1102. combinations.Add(new BurstOutputCombination("Plugins/x86", aotSettingsForTarget.GetDesktopCpu32Bit(), collateDirectory: true));
  1103. combinations.Add(new BurstOutputCombination("Plugins/ARM", new TargetCpus(BurstTargetCpu.THUMB2_NEON32), collateDirectory: true));
  1104. combinations.Add(new BurstOutputCombination("Plugins/ARM64", new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64), collateDirectory: true));
  1105. }
  1106. }
  1107. #if !UNITY_2022_2_OR_NEWER
  1108. else if (targetPlatform == TargetPlatform.Lumin)
  1109. {
  1110. // Set the LUMINSDK_UNITY so bcl.exe will be able to find the SDK
  1111. if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("LUMINSDK_UNITY")))
  1112. {
  1113. var sdkRoot = EditorPrefs.GetString("LuminSDKRoot");
  1114. if (!string.IsNullOrEmpty(sdkRoot))
  1115. {
  1116. Environment.SetEnvironmentVariable("LUMINSDK_UNITY", sdkRoot);
  1117. }
  1118. }
  1119. combinations.Add(new BurstOutputCombination("Data/Plugins/", targetCpus));
  1120. }
  1121. #endif
  1122. else if (targetPlatform == TargetPlatform.Switch)
  1123. {
  1124. combinations.Add(new BurstOutputCombination("NativePlugins/", targetCpus));
  1125. }
  1126. else
  1127. {
  1128. if (targetPlatform == TargetPlatform.Windows)
  1129. {
  1130. // This is what is expected by PlatformDependent\Win\Plugins.cpp
  1131. if (targetCpus.IsX86())
  1132. {
  1133. combinations.Add(new BurstOutputCombination("Data/Plugins/x86", targetCpus, collateDirectory: true));
  1134. }
  1135. else
  1136. {
  1137. var windowsArchitecture = GetWindows64BitTargetArchitecture();
  1138. if (string.Equals(windowsArchitecture, "ARM64", StringComparison.OrdinalIgnoreCase))
  1139. {
  1140. combinations.Add(new BurstOutputCombination("Data/Plugins/ARM64", targetCpus, collateDirectory: true, workaroundBrokenDebug: true));
  1141. }
  1142. else
  1143. {
  1144. combinations.Add(new BurstOutputCombination("Data/Plugins/x86_64", targetCpus, collateDirectory: true));
  1145. }
  1146. }
  1147. }
  1148. else
  1149. {
  1150. // Safeguard
  1151. combinations.Add(new BurstOutputCombination("Data/Plugins/", targetCpus));
  1152. }
  1153. }
  1154. return combinations;
  1155. }
  1156. private static void PostProcessCombinations(TargetPlatform targetPlatform, List<BurstOutputCombination> combinations, BuildSummary summary)
  1157. {
  1158. if (targetPlatform == TargetPlatform.macOS && combinations.Count > 1)
  1159. {
  1160. // Figure out which files we need to lipo
  1161. string outputSymbolsDir = null;
  1162. var outputDir = Path.Combine(OutputBaseFolder, Path.GetFileName(summary.outputPath), "Contents", "Plugins");
  1163. var sliceCount = combinations.Count;
  1164. var binarySlices = new string[sliceCount];
  1165. var debugSymbolSlices = new string[sliceCount];
  1166. for (int i = 0; i < sliceCount; i++)
  1167. {
  1168. var slice = combinations[i];
  1169. var binaryFileName = slice.LibraryName + ".bundle";
  1170. var binaryPath = Path.Combine(OutputBaseFolder, slice.OutputPath, binaryFileName);
  1171. binarySlices[i] = binaryPath;
  1172. // Only attempt to lipo symbols if they actually exist
  1173. var dsymPath = binaryPath + ".dsym";
  1174. var debugSymbolsPath = Path.Combine(dsymPath, "Contents", "Resources", "DWARF", binaryFileName);
  1175. if (File.Exists(debugSymbolsPath))
  1176. {
  1177. if (string.IsNullOrWhiteSpace(outputSymbolsDir))
  1178. {
  1179. // Copy over the symbols from the first combination for metadata files which we aren't merging, like Info.plist
  1180. var outputDsymPath = Path.Combine(outputDir, binaryFileName + ".dsym");
  1181. CopyDirectory(dsymPath, outputDsymPath, true);
  1182. outputSymbolsDir = Path.Combine(outputDsymPath, "Contents", "Resources", "DWARF");
  1183. }
  1184. debugSymbolSlices[i] = debugSymbolsPath;
  1185. }
  1186. }
  1187. // lipo combinations together
  1188. var outBinaryFileName = combinations[0].LibraryName + ".bundle";
  1189. RunLipo(binarySlices, Path.Combine(outputDir, outBinaryFileName));
  1190. if (!string.IsNullOrWhiteSpace(outputSymbolsDir))
  1191. RunLipo(debugSymbolSlices, Path.Combine(outputSymbolsDir, outBinaryFileName));
  1192. // Remove single-slice binary so they don't end up in the build
  1193. for (int i = 0; i < sliceCount; i++)
  1194. Directory.Delete(Path.GetDirectoryName(binarySlices[i]), true);
  1195. // Since we have combined the files, we need to adjust combinations for the next step
  1196. var outFolder = Path.GetDirectoryName(combinations[0].OutputPath); // remove platform folder
  1197. combinations.Clear();
  1198. combinations.Add(new BurstOutputCombination(outFolder, new TargetCpus()));
  1199. }
  1200. }
  1201. private static void RunLipo(string[] inputFiles, string outputFile)
  1202. {
  1203. var outputDir = Path.GetDirectoryName(outputFile);
  1204. Directory.CreateDirectory(outputDir);
  1205. var cmdLine = new StringBuilder();
  1206. foreach (var input in inputFiles)
  1207. {
  1208. if (string.IsNullOrEmpty(input))
  1209. continue;
  1210. cmdLine.Append(BclRunner.EscapeForShell(input));
  1211. cmdLine.Append(' ');
  1212. }
  1213. cmdLine.Append("-create -output ");
  1214. cmdLine.Append(BclRunner.EscapeForShell(outputFile));
  1215. string lipoPath;
  1216. var currentEditorPlatform = Application.platform;
  1217. switch (currentEditorPlatform)
  1218. {
  1219. case RuntimePlatform.LinuxEditor:
  1220. lipoPath = Path.Combine(BurstLoader.BclConfiguration.FolderPath, "hostlin", "llvm-lipo");
  1221. break;
  1222. case RuntimePlatform.OSXEditor:
  1223. lipoPath = Path.Combine(BurstLoader.BclConfiguration.FolderPath, "hostmac", "llvm-lipo");
  1224. break;
  1225. case RuntimePlatform.WindowsEditor:
  1226. lipoPath = Path.Combine(BurstLoader.BclConfiguration.FolderPath, "hostwin", "llvm-lipo.exe");
  1227. break;
  1228. default:
  1229. throw new NotSupportedException("Unknown Unity editor platform: " + currentEditorPlatform);
  1230. }
  1231. BclRunner.RunNativeProgram(lipoPath, cmdLine.ToString(), null);
  1232. }
  1233. internal static Assembly[] GetPlayerAssemblies(BuildReport report)
  1234. {
  1235. // We need to build the list of root assemblies based from the "PlayerScriptAssemblies" folder.
  1236. // This is so we compile the versions of the library built for the individual platforms, not the editor version.
  1237. var oldOutputDir = EditorCompilationInterface.GetCompileScriptsOutputDirectory();
  1238. try
  1239. {
  1240. EditorCompilationInterface.SetCompileScriptsOutputDirectory(LibraryPlayerScriptAssemblies);
  1241. var shouldIncludeTestAssemblies = report.summary.options.HasFlag(BuildOptions.IncludeTestAssemblies);
  1242. #if UNITY_2021_1_OR_NEWER
  1243. // Workaround that with 'Server Build' ticked in the build options, since there is no 'AssembliesType.Server'
  1244. // enum, we need to manually add the BuildingForHeadlessPlayer compilation option.
  1245. #if UNITY_2022_1_OR_NEWER
  1246. var isHeadless = report.summary.subtarget == (int)StandaloneBuildSubtarget.Server;
  1247. #elif UNITY_2021_2_OR_NEWER
  1248. // A really really really gross hack - thanks Cristian Mazo! Querying the BuildOptions.EnableHeadlessMode is
  1249. // obselete, but accessing its integer value is not... Note: this is just the temporary workaround to unblock
  1250. // us (as of 1st June 2021, I say this with **much hope** that it is indeed temporary!).
  1251. var isHeadless = report.summary.options.HasFlag((BuildOptions)16384);
  1252. #else
  1253. var isHeadless = report.summary.options.HasFlag(BuildOptions.EnableHeadlessMode);
  1254. #endif
  1255. if (isHeadless)
  1256. {
  1257. var compilationOptions = EditorCompilationInterface.GetAdditionalEditorScriptCompilationOptions();
  1258. compilationOptions |= EditorScriptCompilationOptions.BuildingForHeadlessPlayer;
  1259. if (shouldIncludeTestAssemblies)
  1260. {
  1261. compilationOptions |= EditorScriptCompilationOptions.BuildingIncludingTestAssemblies;
  1262. }
  1263. return CompilationPipeline.ToAssemblies(CompilationPipeline.GetScriptAssemblies(EditorCompilationInterface.Instance, compilationOptions));
  1264. }
  1265. else
  1266. {
  1267. return CompilationPipeline.GetAssemblies(shouldIncludeTestAssemblies ? AssembliesType.Player : AssembliesType.PlayerWithoutTestAssemblies);
  1268. }
  1269. #else
  1270. // Workaround that with 'Server Build' ticked in the build options, since there is no 'AssembliesType.Server'
  1271. // enum, we need to manually add the 'UNITY_SERVER' define to the player assembly search list.
  1272. if (report.summary.options.HasFlag(BuildOptions.EnableHeadlessMode))
  1273. {
  1274. var compilationOptions = EditorCompilationInterface.GetAdditionalEditorScriptCompilationOptions();
  1275. if (shouldIncludeTestAssemblies)
  1276. {
  1277. compilationOptions |= EditorScriptCompilationOptions.BuildingIncludingTestAssemblies;
  1278. }
  1279. return CompilationPipeline.GetPlayerAssemblies(EditorCompilationInterface.Instance, compilationOptions, new string[] { "UNITY_SERVER" });
  1280. }
  1281. else
  1282. {
  1283. return CompilationPipeline.GetAssemblies(shouldIncludeTestAssemblies ? AssembliesType.Player : AssembliesType.PlayerWithoutTestAssemblies);
  1284. }
  1285. #endif
  1286. }
  1287. finally
  1288. {
  1289. EditorCompilationInterface.SetCompileScriptsOutputDirectory(oldOutputDir); // restore output directory back to original value
  1290. }
  1291. }
  1292. private static bool IsMonoReferenceAssemblyDirectory(string path)
  1293. {
  1294. var editorDir = Path.GetFullPath(EditorApplication.applicationContentsPath);
  1295. return path.IndexOf(editorDir, StringComparison.OrdinalIgnoreCase) != -1 && path.IndexOf("MonoBleedingEdge", StringComparison.OrdinalIgnoreCase) != -1 && path.IndexOf("-api", StringComparison.OrdinalIgnoreCase) != -1;
  1296. }
  1297. private static bool IsDotNetStandardAssemblyDirectory(string path)
  1298. {
  1299. var editorDir = Path.GetFullPath(EditorApplication.applicationContentsPath);
  1300. return path.IndexOf(editorDir, StringComparison.OrdinalIgnoreCase) != -1 && path.IndexOf("netstandard", StringComparison.OrdinalIgnoreCase) != -1 && path.IndexOf("shims", StringComparison.OrdinalIgnoreCase) != -1;
  1301. }
  1302. internal static TargetPlatform GetTargetPlatformAndDefaultCpu(BuildTarget target, out TargetCpus targetCpu, BurstPlatformAotSettings aotSettingsForTarget)
  1303. {
  1304. var platform = TryGetTargetPlatform(target, out targetCpu, aotSettingsForTarget);
  1305. if (!platform.HasValue)
  1306. {
  1307. throw new NotSupportedException("The target platform " + target + " is not supported by the burst compiler");
  1308. }
  1309. return platform.Value;
  1310. }
  1311. internal static bool IsSupportedPlatform(BuildTarget target, BurstPlatformAotSettings aotSettingsForTarget)
  1312. {
  1313. return TryGetTargetPlatform(target, out var _, aotSettingsForTarget).HasValue;
  1314. }
  1315. private static TargetPlatform? TryGetTargetPlatform(BuildTarget target, out TargetCpus targetCpus, BurstPlatformAotSettings aotSettingsForTarget)
  1316. {
  1317. switch (target)
  1318. {
  1319. case BuildTarget.StandaloneWindows:
  1320. targetCpus = aotSettingsForTarget.GetDesktopCpu32Bit();
  1321. return TargetPlatform.Windows;
  1322. case BuildTarget.StandaloneWindows64:
  1323. var windowsArchitecture = GetWindows64BitTargetArchitecture();
  1324. if (string.Equals(windowsArchitecture, "x64", StringComparison.OrdinalIgnoreCase))
  1325. {
  1326. targetCpus = aotSettingsForTarget.GetDesktopCpu64Bit();
  1327. }
  1328. else if (string.Equals(windowsArchitecture, "ARM64", StringComparison.OrdinalIgnoreCase))
  1329. {
  1330. targetCpus = new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64);
  1331. }
  1332. else
  1333. {
  1334. throw new InvalidOperationException("Unknown Windows 64 Bit CPU architecture: " + windowsArchitecture);
  1335. }
  1336. return TargetPlatform.Windows;
  1337. case BuildTarget.StandaloneOSX:
  1338. targetCpus = aotSettingsForTarget.GetDesktopCpu64Bit();
  1339. return TargetPlatform.macOS;
  1340. case BuildTarget.StandaloneLinux64:
  1341. targetCpus = aotSettingsForTarget.GetDesktopCpu64Bit();
  1342. return TargetPlatform.Linux;
  1343. case BuildTarget.WSAPlayer:
  1344. {
  1345. var uwpArchitecture = GetUWPTargetArchitecture();
  1346. if (string.Equals(uwpArchitecture, "x64", StringComparison.OrdinalIgnoreCase))
  1347. {
  1348. targetCpus = aotSettingsForTarget.GetDesktopCpu64Bit();
  1349. }
  1350. else if (string.Equals(uwpArchitecture, "x86", StringComparison.OrdinalIgnoreCase))
  1351. {
  1352. targetCpus = aotSettingsForTarget.GetDesktopCpu32Bit();
  1353. }
  1354. else if (string.Equals(uwpArchitecture, "ARM", StringComparison.OrdinalIgnoreCase))
  1355. {
  1356. targetCpus = new TargetCpus(BurstTargetCpu.THUMB2_NEON32);
  1357. }
  1358. else if (string.Equals(uwpArchitecture, "ARM64", StringComparison.OrdinalIgnoreCase))
  1359. {
  1360. targetCpus = new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64);
  1361. }
  1362. else
  1363. {
  1364. throw new InvalidOperationException("Unknown UWP CPU architecture: " + uwpArchitecture);
  1365. }
  1366. return TargetPlatform.UWP;
  1367. }
  1368. case BuildTarget.GameCoreXboxOne:
  1369. targetCpus = new TargetCpus(BurstTargetCpu.AVX);
  1370. return TargetPlatform.GameCoreXboxOne;
  1371. case BuildTarget.GameCoreXboxSeries:
  1372. targetCpus = new TargetCpus(BurstTargetCpu.AVX2);
  1373. return TargetPlatform.GameCoreXboxSeries;
  1374. case BuildTarget.PS4:
  1375. targetCpus = new TargetCpus(BurstTargetCpu.X64_SSE4);
  1376. return TargetPlatform.PS4;
  1377. case BuildTarget.Android:
  1378. targetCpus = new TargetCpus(BurstTargetCpu.ARMV7A_NEON32);
  1379. return TargetPlatform.Android;
  1380. case BuildTarget.iOS:
  1381. targetCpus = new TargetCpus(BurstTargetCpu.ARMV7A_NEON32);
  1382. return TargetPlatform.iOS;
  1383. case BuildTarget.tvOS:
  1384. targetCpus = new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64);
  1385. return TargetPlatform.tvOS;
  1386. #if !UNITY_2022_2_OR_NEWER
  1387. case BuildTarget.Lumin:
  1388. targetCpus = new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64);
  1389. return TargetPlatform.Lumin;
  1390. #endif
  1391. case BuildTarget.Switch:
  1392. targetCpus = new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64);
  1393. return TargetPlatform.Switch;
  1394. case BuildTarget.PS5:
  1395. targetCpus = new TargetCpus(BurstTargetCpu.AVX2);
  1396. return TargetPlatform.PS5;
  1397. }
  1398. #if UNITY_2023_3_OR_NEWER
  1399. const int buildTargetVisionOS = (int)BuildTarget.VisionOS;
  1400. #else
  1401. const int buildTargetVisionOS = 47;
  1402. #endif
  1403. if ((int)target == buildTargetVisionOS)
  1404. {
  1405. targetCpus = new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64);
  1406. // An SDK Version of "0" indicates that the visionOS simulator SDK is not being
  1407. // used, so assume this is the device SDK instead.
  1408. return GetVisionSdkVersion() == 0 ? TargetPlatform.visionOS : TargetPlatform.visionSimulator;
  1409. }
  1410. #if UNITY_2022_1_OR_NEWER
  1411. const int qnxTarget = (int)BuildTarget.QNX;
  1412. #else
  1413. const int qnxTarget = 46;
  1414. #endif
  1415. if (qnxTarget == (int)target)
  1416. {
  1417. // QNX is supported on 2019.4 (shadow branch), 2020.3 (shadow branch) and 2022.1+ (official).
  1418. var qnxArchitecture = GetQNXTargetArchitecture();
  1419. if ("Arm64" == qnxArchitecture)
  1420. {
  1421. targetCpus = new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64);
  1422. }
  1423. else if ("X64" == qnxArchitecture)
  1424. {
  1425. targetCpus = new TargetCpus(BurstTargetCpu.X64_SSE4);
  1426. }
  1427. else if ("X86" == qnxArchitecture)
  1428. {
  1429. targetCpus = new TargetCpus(BurstTargetCpu.X86_SSE4);
  1430. }
  1431. else if ("Arm32" == qnxArchitecture)
  1432. {
  1433. targetCpus = new TargetCpus(BurstTargetCpu.ARMV7A_NEON32);
  1434. }
  1435. else
  1436. {
  1437. throw new InvalidOperationException("Unknown QNX CPU architecture: " + qnxArchitecture);
  1438. }
  1439. return TargetPlatform.QNX;
  1440. }
  1441. #if UNITY_2021_2_OR_NEWER
  1442. const int embeddedLinuxTarget = (int)BuildTarget.EmbeddedLinux;
  1443. #else
  1444. const int embeddedLinuxTarget = 45;
  1445. #endif
  1446. if (embeddedLinuxTarget == (int)target)
  1447. {
  1448. //EmbeddedLinux is supported on 2019.4 (shadow branch), 2020.3 (shadow branch) and 2021.2+ (official).
  1449. var embeddedLinuxArchitecture = GetEmbeddedLinuxTargetArchitecture();
  1450. if ("Arm64" == embeddedLinuxArchitecture)
  1451. {
  1452. targetCpus = new TargetCpus(BurstTargetCpu.ARMV8A_AARCH64);
  1453. }
  1454. else if ("X64" == embeddedLinuxArchitecture)
  1455. {
  1456. targetCpus = new TargetCpus(BurstTargetCpu.X64_SSE2); //lowest supported for now
  1457. }
  1458. else if (("X86" == embeddedLinuxArchitecture) || ("Arm32" == embeddedLinuxArchitecture))
  1459. {
  1460. //32bit platforms cannot be support with the current SDK/Toolchain combination.
  1461. //i686-embedded-linux-gnu/8.3.0\libgcc.a(_moddi3.o + _divdi3.o): contains a compressed section, but zlib is not available
  1462. //_moddi3.o + _divdi3.o are required by LLVM for 64bit operations on 32bit platforms.
  1463. throw new InvalidOperationException($"No EmbeddedLinux Burst Support on {embeddedLinuxArchitecture} architecture.");
  1464. }
  1465. else
  1466. {
  1467. throw new InvalidOperationException("Unknown EmbeddedLinux CPU architecture: " + embeddedLinuxArchitecture);
  1468. }
  1469. return TargetPlatform.EmbeddedLinux;
  1470. }
  1471. targetCpus = new TargetCpus(BurstTargetCpu.Auto);
  1472. return null;
  1473. }
  1474. private static string GetWindows64BitTargetArchitecture()
  1475. {
  1476. var buildTargetName = BuildPipeline.GetBuildTargetName(BuildTarget.StandaloneWindows64);
  1477. var architecture = EditorUserBuildSettings.GetPlatformSettings(buildTargetName, "Architecture").ToLowerInvariant();
  1478. if (string.Equals(architecture, "x64", StringComparison.OrdinalIgnoreCase) ||
  1479. string.Equals(architecture, "ARM64", StringComparison.OrdinalIgnoreCase))
  1480. {
  1481. return architecture;
  1482. }
  1483. // Default to x64 if editor user build setting is garbage
  1484. return "x64";
  1485. }
  1486. private static string GetUWPTargetArchitecture()
  1487. {
  1488. var architecture = EditorUserBuildSettings.wsaArchitecture;
  1489. if (string.Equals(architecture, "x64", StringComparison.OrdinalIgnoreCase) ||
  1490. string.Equals(architecture, "x86", StringComparison.OrdinalIgnoreCase) ||
  1491. string.Equals(architecture, "ARM", StringComparison.OrdinalIgnoreCase) ||
  1492. string.Equals(architecture, "ARM64", StringComparison.OrdinalIgnoreCase))
  1493. {
  1494. return architecture;
  1495. }
  1496. // Default to x64 if editor user build setting is garbage
  1497. return "x64";
  1498. }
  1499. private static string GetEmbeddedLinuxTargetArchitecture()
  1500. {
  1501. var flags = System.Reflection.BindingFlags.Public |
  1502. System.Reflection.BindingFlags.Static |
  1503. System.Reflection.BindingFlags.FlattenHierarchy;
  1504. var property = typeof(EditorUserBuildSettings).GetProperty("selectedEmbeddedLinuxArchitecture", flags);
  1505. if (null == property)
  1506. {
  1507. return "NOT_FOUND";
  1508. }
  1509. var value = (int)property.GetValue(null, null);
  1510. switch (value)
  1511. {
  1512. case /*UnityEditor.EmbeddedLinuxArchitecture.Arm64*/ 0: return "Arm64";
  1513. case /*UnityEditor.EmbeddedLinuxArchitecture.Arm32*/ 1: return "Arm32";
  1514. case /*UnityEditor.EmbeddedLinuxArchitecture.X64*/ 2: return "X64";
  1515. case /*UnityEditor.EmbeddedLinuxArchitecture.X86*/ 3: return "X86";
  1516. default: return $"UNKNOWN_{value}";
  1517. }
  1518. }
  1519. private static string GetQNXTargetArchitecture()
  1520. {
  1521. var flags = System.Reflection.BindingFlags.Public |
  1522. System.Reflection.BindingFlags.Static |
  1523. System.Reflection.BindingFlags.FlattenHierarchy;
  1524. var property = typeof(EditorUserBuildSettings).GetProperty("selectedQnxArchitecture", flags);
  1525. if (null == property)
  1526. {
  1527. return "NOT_FOUND";
  1528. }
  1529. var value = (int)property.GetValue(null, null);
  1530. switch (value)
  1531. {
  1532. case /*UnityEditor.QNXArchitecture.Arm64*/ 0: return "Arm64";
  1533. case /*UnityEditor.QNXArchitecture.Arm32*/ 1: return "Arm32";
  1534. case /*UnityEditor.QNXArchitecture.X64*/ 2: return "X64";
  1535. case /*UnityEditor.QNXArchitecture.X86*/ 3: return "X86";
  1536. default: return $"UNKNOWN_{value}";
  1537. }
  1538. }
  1539. private static int GetVisionSdkVersion()
  1540. {
  1541. var playerSettings = typeof(PlayerSettings);
  1542. var visionOs = playerSettings.GetNestedType("VisionOS");
  1543. if (visionOs == null) throw new Exception("Editor does not appear to support visionOS");
  1544. var property = visionOs.GetProperty("sdkVersion", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
  1545. if (property == null) throw new Exception("Property `sdkVersion` not found");
  1546. var sdkVersion = property.GetValue(null, null);
  1547. return (int)sdkVersion;
  1548. }
  1549. /// <summary>
  1550. /// Defines an output path (for the generated code) and the target CPU
  1551. /// </summary>
  1552. internal struct BurstOutputCombination
  1553. {
  1554. public readonly TargetCpus TargetCpus;
  1555. public readonly string OutputPath;
  1556. public readonly string LibraryName;
  1557. public readonly bool CollateDirectory;
  1558. public readonly bool WorkaroundFullDebugInfo;
  1559. public BurstOutputCombination(string outputPath, TargetCpus targetCpus, string libraryName = DefaultLibraryName, bool collateDirectory = false, bool workaroundBrokenDebug=false)
  1560. {
  1561. TargetCpus = targetCpus.Clone();
  1562. OutputPath = outputPath;
  1563. LibraryName = libraryName;
  1564. CollateDirectory = collateDirectory;
  1565. WorkaroundFullDebugInfo = workaroundBrokenDebug;
  1566. }
  1567. public void HashInto(ref Hash128 hc)
  1568. {
  1569. hc.Append(TargetCpus.Cpus.Count);
  1570. foreach (var cpu in TargetCpus.Cpus)
  1571. {
  1572. hc.Append((int)cpu);
  1573. }
  1574. hc.Append(OutputPath);
  1575. hc.Append(LibraryName);
  1576. hc.Append(CollateDirectory ? 1 : 0);
  1577. hc.Append(WorkaroundFullDebugInfo ? 1 : 0);
  1578. }
  1579. public override string ToString()
  1580. {
  1581. return $"{nameof(TargetCpus)}: {TargetCpus}, {nameof(OutputPath)}: {OutputPath}, {nameof(LibraryName)}: {LibraryName}";
  1582. }
  1583. }
  1584. private class BclRunner
  1585. {
  1586. private static readonly Regex MatchVersion = new Regex(@"com.unity.burst@(\d+.*?)[\\/]");
  1587. public static void RunManagedProgram(string exe, string args, CompilerOutputParserBase parser)
  1588. {
  1589. RunManagedProgram(exe, args, Application.dataPath + "/..", parser);
  1590. }
  1591. private static void RunManagedProgram(
  1592. string exe,
  1593. string args,
  1594. string workingDirectory,
  1595. CompilerOutputParserBase parser)
  1596. {
  1597. Program p;
  1598. bool unreliableExitCode = false;
  1599. if (Application.platform == RuntimePlatform.WindowsEditor)
  1600. {
  1601. if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
  1602. {
  1603. // For Windows on Arm 64, we need to be explicit and specify the architecture we want our
  1604. //managed process to run with - unfortunately, doing this means the exit code of the process
  1605. //no longer reflects if compilation was successful, so unreliableExitCode works around this
  1606. args = "/c start /machine arm64 /b /WAIT " + exe + " " + args;
  1607. exe = "cmd";
  1608. unreliableExitCode = true;
  1609. }
  1610. ProcessStartInfo si = new ProcessStartInfo()
  1611. {
  1612. Arguments = args,
  1613. CreateNoWindow = true,
  1614. FileName = exe,
  1615. };
  1616. p = new Program(si);
  1617. }
  1618. else
  1619. {
  1620. p = (Program) new ManagedProgram(MonoInstallationFinder.GetMonoInstallation("MonoBleedingEdge"), (string) null, exe, args, false, null);
  1621. }
  1622. RunProgram(p, exe, args, workingDirectory, parser, unreliableExitCode);
  1623. }
  1624. public static void RunNativeProgram(string exe, string args, CompilerOutputParserBase parser)
  1625. {
  1626. RunNativeProgram(exe, args, Application.dataPath + "/..", parser);
  1627. }
  1628. private static void RunNativeProgram(string exePath, string arguments, string workingDirectory, CompilerOutputParserBase parser)
  1629. {
  1630. // On non Windows platform, make sure that the command is executable
  1631. // This is a workaround - occasionally the execute bits are lost from our package
  1632. if (Application.platform != RuntimePlatform.WindowsEditor && Path.IsPathRooted(exePath))
  1633. {
  1634. var escapedExePath = EscapeForShell(exePath, singleQuoteWrapped: true);
  1635. var shArgs = $"-c '[ ! -x {escapedExePath} ] && chmod 755 {escapedExePath}'";
  1636. var p = new Program(new ProcessStartInfo("sh", shArgs) { CreateNoWindow = true});
  1637. p.GetProcessStartInfo().WorkingDirectory = workingDirectory;
  1638. p.Start();
  1639. p.WaitForExit();
  1640. }
  1641. var startInfo = new ProcessStartInfo(exePath, arguments);
  1642. startInfo.CreateNoWindow = true;
  1643. RunProgram(new Program(startInfo), exePath, arguments, workingDirectory, parser, false);
  1644. }
  1645. public static string EscapeForShell(string s, bool singleQuoteWrapped = false)
  1646. {
  1647. // On Windows it's enough to enclose the path in double quotes (double quotes are not allowed in paths)
  1648. if (Application.platform == RuntimePlatform.WindowsEditor) return $"\"{s}\"";
  1649. // On non-windows platforms we enclose in single-quotes and escape any existing single quotes with: '\'':
  1650. // John's Folder => 'John'\''s Folder'
  1651. var sb = new StringBuilder();
  1652. var escaped = s.Replace("'", "'\\''");
  1653. sb.Append('\'');
  1654. sb.Append(escaped);
  1655. sb.Append('\'');
  1656. // If the outer-context is already wrapped in single-quotes, we need to double escape things:
  1657. // John's Folder => 'John'\''s Folder'
  1658. // => '\''John'\''\'\'''\''s Folder'\''
  1659. if (singleQuoteWrapped)
  1660. {
  1661. // Pain
  1662. return sb.ToString().Replace("'", "'\\''");
  1663. }
  1664. return sb.ToString();
  1665. }
  1666. public static void RunProgram(
  1667. Program p,
  1668. string exe,
  1669. string args,
  1670. string workingDirectory,
  1671. CompilerOutputParserBase parser,
  1672. bool unreliableExitCode)
  1673. {
  1674. Stopwatch stopwatch = new Stopwatch();
  1675. stopwatch.Start();
  1676. using (p)
  1677. {
  1678. p.GetProcessStartInfo().WorkingDirectory = workingDirectory;
  1679. p.Start();
  1680. p.WaitForExit();
  1681. stopwatch.Stop();
  1682. Console.WriteLine("{0} exited after {1} ms.", (object)exe, (object)stopwatch.ElapsedMilliseconds);
  1683. IEnumerable<UnityEditor.Scripting.Compilers.CompilerMessage> compilerMessages = null;
  1684. string[] errorOutput = p.GetErrorOutput();
  1685. string[] standardOutput = p.GetStandardOutput();
  1686. if (parser != null)
  1687. {
  1688. compilerMessages = parser.Parse(errorOutput, standardOutput, true, "n/a (burst)");
  1689. }
  1690. var errorMessageBuilder = new StringBuilder();
  1691. if (compilerMessages != null)
  1692. {
  1693. foreach (UnityEditor.Scripting.Compilers.CompilerMessage compilerMessage in compilerMessages)
  1694. {
  1695. switch (compilerMessage.type)
  1696. {
  1697. case CompilerMessageType.Warning:
  1698. Debug.LogWarning(compilerMessage.message, compilerMessage.file, compilerMessage.line, compilerMessage.column);
  1699. break;
  1700. case CompilerMessageType.Error:
  1701. Debug.LogPlayerBuildError(compilerMessage.message, compilerMessage.file, compilerMessage.line, compilerMessage.column);
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. var exitFailed = p.ExitCode != 0;
  1707. if (unreliableExitCode)
  1708. {
  1709. // Bcl +burstc will emit a Done as the final stdout if everything was ok.
  1710. exitFailed = (standardOutput.Length==0) || (standardOutput[standardOutput.Length - 1].Trim() != "Done");
  1711. }
  1712. if (exitFailed)
  1713. {
  1714. // We try to output the version in the heading error if we can
  1715. var matchVersion = MatchVersion.Match(exe);
  1716. errorMessageBuilder.Append(matchVersion.Success ?
  1717. "Burst compiler (" + matchVersion.Groups[1].Value + ") failed running" :
  1718. "Burst compiler failed running");
  1719. errorMessageBuilder.AppendLine();
  1720. errorMessageBuilder.AppendLine();
  1721. // Don't output the path if we are not burst-debugging or the exe exist
  1722. if (BurstLoader.IsDebugging || !File.Exists(exe))
  1723. {
  1724. errorMessageBuilder.Append(exe).Append(" ").Append(args);
  1725. errorMessageBuilder.AppendLine();
  1726. errorMessageBuilder.AppendLine();
  1727. }
  1728. errorMessageBuilder.AppendLine("stdout:");
  1729. foreach (string str in standardOutput)
  1730. errorMessageBuilder.AppendLine(str);
  1731. errorMessageBuilder.AppendLine("stderr:");
  1732. foreach (string str in errorOutput)
  1733. errorMessageBuilder.AppendLine(str);
  1734. throw new BuildFailedException(errorMessageBuilder.ToString());
  1735. }
  1736. Console.WriteLine(p.GetAllOutput());
  1737. }
  1738. }
  1739. }
  1740. /// <summary>
  1741. /// Internal class used to parse bcl output errors
  1742. /// </summary>
  1743. private class BclOutputErrorParser : CompilerOutputParserBase
  1744. {
  1745. // Format of an error message:
  1746. //
  1747. //C:\work\burst\src\Burst.Compiler.IL.Tests\Program.cs(17,9): error: Loading a managed string literal is not supported by burst
  1748. // at Buggy.NiceBug() (at C:\work\burst\src\Burst.Compiler.IL.Tests\Program.cs:17)
  1749. //
  1750. //
  1751. // [1] [2] [3] [4] [5]
  1752. // path line col type message
  1753. private static readonly Regex MatchLocation = new Regex(@"^(.*?)\((\d+)\s*,\s*(\d+)\):\s*([\w\s]+)\s*:\s*(.*)");
  1754. // Matches " at "
  1755. private static readonly Regex MatchAt = new Regex(@"^\s+at\s+");
  1756. public override IEnumerable<UnityEditor.Scripting.Compilers.CompilerMessage> Parse(
  1757. string[] errorOutput,
  1758. string[] standardOutput,
  1759. bool compilationHadFailure,
  1760. string assemblyName)
  1761. {
  1762. var messages = new List<UnityEditor.Scripting.Compilers.CompilerMessage>();
  1763. var textBuilder = new StringBuilder();
  1764. for (var i = 0; i < errorOutput.Length; i++)
  1765. {
  1766. string line = errorOutput[i];
  1767. var message = new UnityEditor.Scripting.Compilers.CompilerMessage {assemblyName = assemblyName};
  1768. // If we are able to match a location, we can decode it including the following attached " at " lines
  1769. textBuilder.Clear();
  1770. var match = MatchLocation.Match(line);
  1771. if (match.Success)
  1772. {
  1773. var path = match.Groups[1].Value;
  1774. int.TryParse(match.Groups[2].Value, out message.line);
  1775. int.TryParse(match.Groups[3].Value, out message.column);
  1776. if (match.Groups[4].Value.Contains("error"))
  1777. {
  1778. message.type = CompilerMessageType.Error;
  1779. }
  1780. else
  1781. {
  1782. message.type = CompilerMessageType.Warning;
  1783. }
  1784. message.file = !string.IsNullOrEmpty(path) ? path : "unknown";
  1785. // Replace '\' with '/' to let the editor open the file
  1786. message.file = message.file.Replace('\\', '/');
  1787. // Make path relative to project path path
  1788. var projectPath = Path.GetDirectoryName(Application.dataPath)?.Replace('\\', '/');
  1789. if (projectPath != null && message.file.StartsWith(projectPath))
  1790. {
  1791. message.file = message.file.Substring(projectPath.EndsWith("/") ? projectPath.Length : projectPath.Length + 1);
  1792. }
  1793. // debug
  1794. // textBuilder.AppendLine("line: " + message.line + " column: " + message.column + " error: " + message.type + " file: " + message.file);
  1795. textBuilder.Append(match.Groups[5].Value);
  1796. }
  1797. else
  1798. {
  1799. // Don't output any blank line
  1800. if (string.IsNullOrWhiteSpace(line))
  1801. {
  1802. continue;
  1803. }
  1804. // Otherwise we output an error, but without source location information
  1805. // so that at least the user can see it directly in the log errors
  1806. message.type = CompilerMessageType.Error;
  1807. message.line = 0;
  1808. message.column = 0;
  1809. message.file = "unknown";
  1810. textBuilder.Append(line);
  1811. }
  1812. // Collect attached location call context information ("at ...")
  1813. // we do it for both case (as if we have an exception in bcl we want to print this in a single line)
  1814. bool isFirstAt = true;
  1815. for (int j = i + 1; j < errorOutput.Length; j++)
  1816. {
  1817. var nextLine = errorOutput[j];
  1818. // Empty lines are ignored by the stack trace parser.
  1819. if (string.IsNullOrWhiteSpace(nextLine))
  1820. {
  1821. i++;
  1822. continue;
  1823. }
  1824. if (MatchAt.Match(nextLine).Success)
  1825. {
  1826. i++;
  1827. if (isFirstAt)
  1828. {
  1829. textBuilder.AppendLine();
  1830. isFirstAt = false;
  1831. }
  1832. textBuilder.AppendLine(nextLine);
  1833. }
  1834. else
  1835. {
  1836. break;
  1837. }
  1838. }
  1839. message.message = textBuilder.ToString();
  1840. messages.Add(message);
  1841. }
  1842. return messages;
  1843. }
  1844. protected override string GetErrorIdentifier()
  1845. {
  1846. throw new NotImplementedException(); // as we overriding the method Parse()
  1847. }
  1848. protected override Regex GetOutputRegex()
  1849. {
  1850. throw new NotImplementedException(); // as we overriding the method Parse()
  1851. }
  1852. }
  1853. #if UNITY_EDITOR_OSX && !ENABLE_GENERATE_NATIVE_PLUGINS_FOR_ASSEMBLIES_API
  1854. private class StaticLibraryPostProcessor
  1855. {
  1856. private const string TempSourceLibrary = @"Temp/StagingArea/StaticLibraries";
  1857. [PostProcessBuildAttribute(1)]
  1858. public static void OnPostProcessBuild(BuildTarget target, string path)
  1859. {
  1860. // Early out if we are building for the simulator, as we don't
  1861. //currently generate burst libraries that will work for that.
  1862. if (IsForSimulator(target))
  1863. {
  1864. return;
  1865. }
  1866. // We only support AOT compilation for ios from a macos host (we require xcrun and the apple tool chains)
  1867. //for other hosts, we simply act as if burst is not being used (an error will be generated by the build aot step)
  1868. //this keeps the behaviour consistent with how it was before static linkage was introduced
  1869. if (target == BuildTarget.iOS)
  1870. {
  1871. var aotSettingsForTarget = BurstPlatformAotSettings.GetOrCreateSettings(BuildTarget.iOS);
  1872. // Early exit if burst is not activated
  1873. if (!aotSettingsForTarget.EnableBurstCompilation)
  1874. {
  1875. return;
  1876. }
  1877. PostAddStaticLibraries(path);
  1878. }
  1879. if (target == BuildTarget.tvOS)
  1880. {
  1881. var aotSettingsForTarget = BurstPlatformAotSettings.GetOrCreateSettings(BuildTarget.tvOS);
  1882. // Early exit if burst is not activated
  1883. if (!aotSettingsForTarget.EnableBurstCompilation)
  1884. {
  1885. return;
  1886. }
  1887. PostAddStaticLibraries(path);
  1888. }
  1889. #if UNITY_2023_3_OR_NEWER
  1890. if (target == BuildTarget.visionOS)
  1891. {
  1892. var aotSettingsForTarget = BurstPlatformAotSettings.GetOrCreateSettings(BuildTarget.visionOS);
  1893. // Early exit if burst is not activated
  1894. if (!aotSettingsForTarget.EnableBurstCompilation)
  1895. {
  1896. return;
  1897. }
  1898. PostAddStaticLibraries(path);
  1899. }
  1900. #endif
  1901. }
  1902. private static void PostAddStaticLibraries(string path)
  1903. {
  1904. var assm = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(assembly =>
  1905. assembly.GetName().Name == "UnityEditor.iOS.Extensions.Xcode");
  1906. Type PBXType = assm?.GetType("UnityEditor.iOS.Xcode.PBXProject");
  1907. Type PBXSourceTree = assm?.GetType("UnityEditor.iOS.Xcode.PBXSourceTree");
  1908. if (PBXType != null && PBXSourceTree != null)
  1909. {
  1910. var project = Activator.CreateInstance(PBXType, null);
  1911. var _sGetPBXProjectPath = PBXType.GetMethod("GetPBXProjectPath");
  1912. var _ReadFromFile = PBXType.GetMethod("ReadFromFile");
  1913. var _sGetUnityTargetName = PBXType.GetMethod("GetUnityTargetName");
  1914. var _AddFileToBuild = PBXType.GetMethod("AddFileToBuild");
  1915. var _AddFile = PBXType.GetMethod("AddFile");
  1916. var _WriteToString = PBXType.GetMethod("WriteToString");
  1917. var sourcetree = new EnumConverter(PBXSourceTree).ConvertFromString("Source");
  1918. string sPath = (string)_sGetPBXProjectPath?.Invoke(null, new object[] { path });
  1919. _ReadFromFile?.Invoke(project, new object[] { sPath });
  1920. var _TargetGuidByName = PBXType.GetMethod("GetUnityFrameworkTargetGuid");
  1921. string g = (string) _TargetGuidByName?.Invoke(project, null);
  1922. var srcPath = TempSourceLibrary;
  1923. var dstPath = "Libraries";
  1924. var dstCopyPath = Path.Combine(path, dstPath);
  1925. var burstCppLinkFile = "lib_burst_generated.cpp";
  1926. var libName = $"{DefaultLibraryName}.a";
  1927. var libSrcPath = Path.Combine(srcPath, libName);
  1928. var libExists = File.Exists(libSrcPath);
  1929. if (!libExists)
  1930. {
  1931. return; // No libs, so don't write the cpp either
  1932. }
  1933. File.Copy(libSrcPath, Path.Combine(dstCopyPath, libName));
  1934. AddLibToProject(project, _AddFileToBuild, _AddFile, sourcetree, g, dstPath, libName);
  1935. // Additionally we need a small cpp file (weak symbols won't unfortunately override directly from the libs
  1936. //presumably due to link order?
  1937. WriteStaticLinkCppFile(dstCopyPath);
  1938. string cppPath = Path.Combine(dstPath, burstCppLinkFile);
  1939. string fileg = (string)_AddFile?.Invoke(project, new object[] { cppPath, cppPath, sourcetree });
  1940. _AddFileToBuild?.Invoke(project, new object[] { g, fileg });
  1941. string pstring = (string)_WriteToString?.Invoke(project, null);
  1942. File.WriteAllText(sPath, pstring);
  1943. }
  1944. }
  1945. private static void AddLibToProject(object project, System.Reflection.MethodInfo _AddFileToBuild, System.Reflection.MethodInfo _AddFile, object sourcetree, string g, string dstPath, string lib32Name)
  1946. {
  1947. string fg = (string)_AddFile?.Invoke(project,
  1948. new object[] { Path.Combine(dstPath, lib32Name), Path.Combine(dstPath, lib32Name), sourcetree });
  1949. _AddFileToBuild?.Invoke(project, new object[] { g, fg });
  1950. }
  1951. }
  1952. #endif
  1953. }
  1954. }
  1955. #endif