Geen omschrijving
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.

Unity.SysrootPackage.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. using UnityEngine;
  8. using UnityEditorInternal;
  9. using UnityEditor;
  10. using NiceIO.Sysroot;
  11. #if UNITY_STANDALONE_LINUX_API
  12. using UnityEditor.LinuxStandalone;
  13. #endif
  14. #if UNITY_EMBEDDED_LINUX_API
  15. using UnityEditor.EmbeddedLinux;
  16. #endif
  17. namespace UnityEditor.Il2Cpp
  18. {
  19. /// <summary>
  20. /// Describe a payload location and destination
  21. /// </summary>
  22. public struct PayloadDescriptor
  23. {
  24. /// <summary>
  25. /// Path of payload tarball
  26. /// </summary>
  27. internal NPath path;
  28. /// <summary>
  29. /// Path directory where payload is to be installed
  30. /// </summary>
  31. internal NPath dir;
  32. }
  33. /// <summary>
  34. /// Initialization status
  35. /// </summary>
  36. enum InitializationStatus
  37. {
  38. Uninitialized,
  39. Failed,
  40. Succeeded
  41. }
  42. /// <summary>
  43. /// Base class for sysroot and toolchain packages
  44. /// </summary>
  45. public class SysrootPackage
  46. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  47. : Sysroot
  48. #endif
  49. {
  50. private static bool IsLinuxIL2CPPPresent()
  51. {
  52. string targetDir = $"{BuildPipeline.GetPlaybackEngineDirectory(BuildTargetGroup.Standalone, BuildTarget.StandaloneLinux64, BuildOptions.None)}/Variations/il2cpp";
  53. if (Directory.Exists(targetDir))
  54. return true;
  55. return false;
  56. }
  57. public static bool ShouldLogDebugMessage()
  58. {
  59. return !String.IsNullOrEmpty(Environment.GetEnvironmentVariable("UNITY_SYSROOT_DEBUG"));
  60. }
  61. [InitializeOnLoadMethod]
  62. private static void IssueWarningIfLinuxIL2CPPNotPresent()
  63. {
  64. if (ShouldLogDebugMessage() && !IsLinuxIL2CPPPresent())
  65. {
  66. UnityEngine.Debug.LogWarning($"Linux Compiler Toolchain package(s) present, but required Linux-IL2CPP is missing");
  67. }
  68. }
  69. /// <summary>
  70. /// Name of package
  71. /// </summary>
  72. public
  73. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  74. override
  75. #else
  76. virtual
  77. #endif
  78. string Name => "com.unity.sysroot";
  79. /// <summary>
  80. /// Name of host platform (linux, win, macos)
  81. /// </summary>
  82. public
  83. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  84. override
  85. #else
  86. virtual
  87. #endif
  88. string HostPlatform => "";
  89. /// <summary>
  90. /// Name of host architecture
  91. /// </summary>
  92. public
  93. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  94. override
  95. #else
  96. virtual
  97. #endif
  98. string HostArch => "";
  99. /// <summary>
  100. /// Name of target platform (linux, win, macos)
  101. /// </summary>
  102. public
  103. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  104. override
  105. #else
  106. virtual
  107. #endif
  108. string TargetPlatform => "";
  109. /// <summary>
  110. /// Name of target architecture
  111. /// </summary>
  112. public
  113. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  114. override
  115. #else
  116. virtual
  117. #endif
  118. string TargetArch => "";
  119. /// <summary>
  120. /// Supplies arguments to il2cpp.exe
  121. /// </summary>
  122. /// <returns>Next argument to il2cpp.exe</returns>
  123. public
  124. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  125. override
  126. #else
  127. virtual
  128. #endif
  129. IEnumerable<string> GetIl2CppArguments() { return null; }
  130. #if !IL2CPP_LEGACY_API_PRESENT
  131. /// <summary>
  132. /// Supplies sysroot argument to il2cpp.exe
  133. /// </summary>
  134. /// <returns>Next argument to il2cpp.exe</returns>
  135. public
  136. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  137. override
  138. #else
  139. virtual
  140. #endif
  141. string GetSysrootPath() { return null; }
  142. /// <summary>
  143. /// Supplies toolchain path argument to il2cpp.exe
  144. /// </summary>
  145. /// <returns>Next argument to il2cpp.exe</returns>
  146. public
  147. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  148. override
  149. #else
  150. virtual
  151. #endif
  152. string GetToolchainPath() { return null; }
  153. /// <summary>
  154. /// Supplies compiler flags argument to il2cpp.exe
  155. /// </summary>
  156. /// <returns>Next argument to il2cpp.exe</returns>
  157. public
  158. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  159. override
  160. #else
  161. virtual
  162. #endif
  163. string GetIl2CppCompilerFlags() { return null; }
  164. /// <summary>
  165. /// Supplies linker flags argument to il2cpp.exe
  166. /// </summary>
  167. /// <returns>Next argument to il2cpp.exe</returns>
  168. public
  169. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  170. override
  171. #else
  172. virtual
  173. #endif
  174. string GetIl2CppLinkerFlags() { return null; }
  175. #endif
  176. /// <summary>
  177. /// Name of payload tarball
  178. /// </summary>
  179. protected string Payload => "payload.tar.7z";
  180. private List<PayloadDescriptor> _payloads = new List<PayloadDescriptor>();
  181. private InitializationStatus _initStatus = InitializationStatus.Uninitialized;
  182. /// <summary>
  183. /// Initialize package
  184. /// </summary>
  185. /// <returns>Success or failure of initialization</returns>
  186. public
  187. #if UNITY_STANDALONE_LINUX_API || UNITY_WEBGL_API || UNITY_EMBEDDED_LINUX_API
  188. override
  189. #else
  190. virtual
  191. #endif
  192. bool Initialize()
  193. {
  194. if (_initStatus != InitializationStatus.Uninitialized)
  195. return _initStatus == InitializationStatus.Succeeded;
  196. foreach (PayloadDescriptor pd in _payloads)
  197. {
  198. if (!Directory.Exists(pd.dir.ToString(SlashMode.Native)) && !InstallPayload(pd))
  199. {
  200. UnityEngine.Debug.LogError($"Failed to initialize package: {Name}");
  201. _initStatus = InitializationStatus.Failed;
  202. return false;
  203. }
  204. }
  205. _initStatus = InitializationStatus.Succeeded;
  206. return true;
  207. }
  208. /// <summary>
  209. /// Compute path of payload tarball
  210. /// </summary>
  211. /// <param name="packageName">The name of the package</param>
  212. /// <returns>Path of payload tarball</returns>
  213. internal NPath PayloadPath(string packageName)
  214. {
  215. return new NPath(Path.GetFullPath($"Packages/{packageName}")).Combine("data~/payload.tar.7z");
  216. }
  217. /// <summary>
  218. /// Register payload tarball and destination (installed location)
  219. /// </summary>
  220. /// <param name="packageName">The name of the package</param>
  221. /// <param name="payloadDir">The directory to install the payload in relative to sysroot cache</param>
  222. public void RegisterPayload(string packageName, string payloadDir)
  223. {
  224. _payloads.Add(new PayloadDescriptor{path = PayloadPath(packageName).ToString(SlashMode.Native), dir = PayloadInstallDirectory(payloadDir).ToString(SlashMode.Native)});
  225. }
  226. private bool RunShellCommand(string command, string workDir = null)
  227. {
  228. var p = new Process();
  229. p.StartInfo.UseShellExecute = false;
  230. #if UNITY_EDITOR_WIN
  231. p.StartInfo.CreateNoWindow = true;
  232. p.StartInfo.FileName = "cmd";
  233. p.StartInfo.Arguments = $"/c \"{command}\"";
  234. #else
  235. p.StartInfo.FileName = "/bin/sh";
  236. p.StartInfo.Arguments = $"-c \'{command}\'";
  237. #endif
  238. p.StartInfo.WorkingDirectory = string.IsNullOrEmpty(workDir) ? Environment.CurrentDirectory : workDir;
  239. p.Start();
  240. p.WaitForExit();
  241. bool result = p.ExitCode == 0;
  242. if (!result && ShouldLogDebugMessage())
  243. UnityEngine.Debug.LogError($"Failed to execute command command=\"{p.StartInfo.FileName}\" arguments=\"{p.StartInfo.Arguments}\"");
  244. return result;
  245. }
  246. private bool DecompressSysroot(NPath payload, NPath workDir)
  247. {
  248. if (!RunShellCommand(CommandCreateDirectory(workDir)))
  249. {
  250. UnityEngine.Debug.LogError($"Failed to create directory {workDir}");
  251. return false;
  252. }
  253. if (!RunShellCommand(CommandUncompressTarball(payload, workDir), workDir.ToString(SlashMode.Native)))
  254. {
  255. UnityEngine.Debug.LogError($"Failed to uncompress payload");
  256. RunShellCommand(CommandRemoveDirectoryTree(workDir));
  257. return false;
  258. }
  259. return PostDecompressActions(workDir);
  260. }
  261. private bool InstallPayload(PayloadDescriptor pd)
  262. {
  263. return DecompressSysroot(pd.path, pd.dir);
  264. }
  265. private string CommandCreateDirectory(NPath dir)
  266. {
  267. _initStatus = InitializationStatus.Failed;
  268. #if UNITY_EDITOR_WIN
  269. return $"mkdir {dir.InQuotes(SlashMode.Native)}";
  270. #else
  271. return $"mkdir -p {dir.InQuotes()}";
  272. #endif
  273. }
  274. private string Get7zPath()
  275. {
  276. #if UNITY_EDITOR_WIN
  277. string command = "7z";
  278. #else
  279. string command = "7za";
  280. #endif
  281. return new NPath($"{EditorApplication.applicationContentsPath}/Tools/{command}").InQuotes(SlashMode.Native);
  282. }
  283. private string CommandUncompressTarball(NPath tarball, NPath destDir)
  284. {
  285. #if UNITY_EDITOR_WIN
  286. return $"{Get7zPath()} x -y {tarball.InQuotes(SlashMode.Native)} -so | {Get7zPath()} x -y -aoa -ttar -si";
  287. #else
  288. return $"{Get7zPath()} x -y {tarball.InQuotes()} -so | tar xf - --directory={destDir.InQuotes()}";
  289. #endif
  290. }
  291. private string CommandRemoveDirectoryTree(NPath dir)
  292. {
  293. #if UNITY_EDITOR_WIN
  294. return $"rd /s /q {dir.InQuotes(SlashMode.Native)}";
  295. #else
  296. return $"rm -rf {dir.InQuotes()}";
  297. #endif
  298. }
  299. private bool PostDecompressActions(NPath workDir)
  300. {
  301. return true;
  302. }
  303. private string UserAppDataFolder()
  304. {
  305. return
  306. #if UNITY_EDITOR_OSX
  307. $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/Library/Unity";
  308. #else
  309. $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}/unity3d";
  310. #endif
  311. }
  312. /// <summary>
  313. /// Returns path of installed payload
  314. /// </summary>
  315. /// <param name="payloadDir">The directory to install the payload in relative to sysroot cache</param>
  316. /// <returns>Fully-qualified path of install directory</returns>
  317. internal NPath PayloadInstallDirectory(string payloadDir)
  318. {
  319. string cacheDir = Environment.GetEnvironmentVariable("UNITY_SYSROOT_CACHE");
  320. if (string.IsNullOrEmpty(cacheDir))
  321. cacheDir = $"{UserAppDataFolder()}/cache/sysroots";
  322. return new NPath($"{cacheDir}/{payloadDir}");
  323. }
  324. }
  325. }