|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Net; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace Xamarin.Android.Prepare |
| 7 | +{ |
| 8 | + partial class Step_InstallGNUBinutils : StepWithDownloadProgress |
| 9 | + { |
| 10 | + static readonly string ProductName = $"GNU Binutils {Configurables.Defaults.BinutilsVersion}"; |
| 11 | + |
| 12 | + public Step_InstallGNUBinutils () |
| 13 | + : base ("Install GNU Binutils") |
| 14 | + {} |
| 15 | + |
| 16 | + protected override async Task<bool> Execute (Context context) |
| 17 | + { |
| 18 | + string hostDestinationDirectory = Configurables.Paths.HostBinutilsInstallDir; |
| 19 | + string windowsDestinationDirectory = Configurables.Paths.WindowsBinutilsInstallDir; |
| 20 | + |
| 21 | + bool hostHaveAll = HaveAllBinutils (hostDestinationDirectory); |
| 22 | + bool windowsHaveAll = HaveAllBinutils (windowsDestinationDirectory, ".exe"); |
| 23 | + |
| 24 | + if (hostHaveAll && windowsHaveAll) { |
| 25 | + Log.StatusLine ("All Binutils are already installed"); |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + string packageName = Path.GetFileName (Configurables.Urls.BinutilsArchive.LocalPath); |
| 30 | + string localArchivePath = Path.Combine (Configurables.Paths.BinutilsCacheDir, packageName); |
| 31 | + |
| 32 | + if (!await DownloadBinutils (context, localArchivePath, Configurables.Urls.BinutilsArchive)) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + string tempDir = Path.Combine (Path.GetTempPath (), "xaprepare-binutils"); |
| 37 | + Utilities.DeleteDirectorySilent (tempDir); |
| 38 | + Utilities.CreateDirectory (tempDir); |
| 39 | + |
| 40 | + Log.DebugLine ($"Unpacking {ProductName} archive {localArchivePath} into {tempDir}"); |
| 41 | + if (!await Utilities.Unpack (localArchivePath, tempDir, cleanDestinatioBeforeUnpacking: true)) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + if (!hostHaveAll) { |
| 46 | + CopyToDestination (context, "Host", tempDir, hostDestinationDirectory, executableExtension: ExecutableExtension); |
| 47 | + } |
| 48 | + |
| 49 | + if (!windowsHaveAll) { |
| 50 | + CopyToDestination (context, "Windows", tempDir, windowsDestinationDirectory, "windows", ".exe"); |
| 51 | + } |
| 52 | + |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + bool CopyToDestination (Context context, string label, string sourceDir, string destinationDir, string osName = HostName, string? executableExtension = null) |
| 57 | + { |
| 58 | + Log.StatusLine (); |
| 59 | + Log.StatusLine ($"Installing for {label}:"); |
| 60 | + |
| 61 | + string sourcePath = Path.Combine (sourceDir, osName); |
| 62 | + foreach (var kvp in Configurables.Defaults.AndroidToolchainPrefixes) { |
| 63 | + string prefix = kvp.Value; |
| 64 | + |
| 65 | + foreach (NDKTool tool in Configurables.Defaults.NDKTools) { |
| 66 | + string toolName = GetToolName (prefix, tool, executableExtension); |
| 67 | + string toolSourcePath = Path.Combine (sourcePath, toolName); |
| 68 | + string toolDestinationPath = Path.Combine (destinationDir, toolName); |
| 69 | + string versionMarkerPath = GetVersionMarker (toolDestinationPath); |
| 70 | + |
| 71 | + Log.StatusLine ($" {context.Characters.Bullet} Installing ", toolName, tailColor: ConsoleColor.White); |
| 72 | + Utilities.CopyFile (toolSourcePath, toolDestinationPath); |
| 73 | + File.WriteAllText (versionMarkerPath, DateTime.UtcNow.ToString ()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + async Task<bool> DownloadBinutils (Context context, string localPackagePath, Uri url) |
| 81 | + { |
| 82 | + if (Utilities.FileExists (localPackagePath)) { |
| 83 | + Log.StatusLine ($"{ProductName} archive already downloaded"); |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + Log.StatusLine ($"Downloading {ProductName} from ", url.ToString (), tailColor: ConsoleColor.White); |
| 88 | + (bool success, ulong size, HttpStatusCode status) = await Utilities.GetDownloadSizeWithStatus (url); |
| 89 | + if (!success) { |
| 90 | + if (status == HttpStatusCode.NotFound) { |
| 91 | + Log.ErrorLine ($"{ProductName} archive URL not found"); |
| 92 | + return false; |
| 93 | + } |
| 94 | + Log.WarningLine ($"Failed to obtain {ProductName} size. HTTP status code: {status} ({(int)status})"); |
| 95 | + } |
| 96 | + |
| 97 | + DownloadStatus downloadStatus = Utilities.SetupDownloadStatus (context, size, context.InteractiveSession); |
| 98 | + Log.StatusLine ($" {context.Characters.Link} {url}", ConsoleColor.White); |
| 99 | + await Download (context, url, localPackagePath, ProductName, Path.GetFileName (localPackagePath), downloadStatus); |
| 100 | + |
| 101 | + if (!File.Exists (localPackagePath)) { |
| 102 | + Log.ErrorLine ($"Download of {ProductName} from {url} failed."); |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + bool HaveAllBinutils (string dir, string? executableExtension = null) |
| 110 | + { |
| 111 | + Log.DebugLine ("Checking if all binutils are installed in {dir}"); |
| 112 | + string extension = executableExtension ?? String.Empty; |
| 113 | + foreach (var kvp in Configurables.Defaults.AndroidToolchainPrefixes) { |
| 114 | + string prefix = kvp.Value; |
| 115 | + |
| 116 | + foreach (NDKTool tool in Configurables.Defaults.NDKTools) { |
| 117 | + string toolName = GetToolName (prefix, tool, executableExtension); |
| 118 | + string toolPath = Path.Combine (dir, toolName); |
| 119 | + string versionMarkerPath = GetVersionMarker (toolPath); |
| 120 | + |
| 121 | + Log.DebugLine ($"Checking {toolName}"); |
| 122 | + if (!Utilities.FileExists (toolPath)) { |
| 123 | + Log.DebugLine ($"Binutils tool {toolPath} does not exist"); |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + if (!Utilities.FileExists (versionMarkerPath)) { |
| 128 | + Log.DebugLine ($"Binutils tool {toolPath} exists, but its version is incorrect"); |
| 129 | + return false; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + string GetToolName (string prefix, NDKTool tool, string? executableExtension = null) |
| 138 | + { |
| 139 | + return $"{prefix}-{(String.IsNullOrEmpty (tool.DestinationName) ? tool.Name : tool.DestinationName)}{executableExtension}"; |
| 140 | + } |
| 141 | + |
| 142 | + string GetVersionMarker (string toolPath) |
| 143 | + { |
| 144 | + return $"{toolPath}.{Configurables.Defaults.BinutilsVersion}"; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments