Files
FlaxEngine/Source/Tools/Flax.Build/Build/DotNet/DotNetSdk.cs
Wojciech Figat eebc4951de Merge branch '1.5' into dotnet7
# Conflicts:
#	Source/Platforms/DotNet/NUnit/agents/net40/nunit-agent.exe
#	Source/Platforms/DotNet/NUnit/agents/net40/nunit.engine.api.dll
#	Source/Platforms/DotNet/NUnit/agents/net40/nunit.engine.core.dll
#	Source/Platforms/DotNet/NUnit/agents/net7.0/nunit.agent.addins
#	Source/Platforms/DotNet/NUnit/nunit.engine.api.dll
#	Source/Platforms/DotNet/NUnit/nunit.engine.core.dll
#	Source/Platforms/DotNet/NUnit/nunit.engine.dll
#	Source/Platforms/DotNet/NUnit/nunit3-console.exe
#	Source/Platforms/DotNet/NUnit/nunit3-console.exe.config
#	Source/Platforms/DotNet/NUnit/testcentric.engine.metadata.dll
#	Source/Tools/Flax.Build/Deps/Downloader.cs
#	Source/Tools/Flax.Stats/CodeFrame.cs
#	Source/Tools/Flax.Stats/CodeFrameNode.cs
#	Source/Tools/Flax.Stats/Flax.Stats.Build.cs
#	Source/Tools/Flax.Stats/Languages.cs
#	Source/Tools/Flax.Stats/Program.cs
#	Source/Tools/Flax.Stats/TaskType.cs
#	Source/Tools/Flax.Stats/Tools.cs
#	Source/Tools/FlaxEngine.Tests/TestEditorUtils.cs
2023-01-10 15:49:44 +01:00

159 lines
6.0 KiB
C#

// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.IO;
using System.Linq;
using Microsoft.Win32;
namespace Flax.Build
{
/// <summary>
/// The DotNet SDK.
/// </summary>
public sealed class DotNetSdk : Sdk
{
/// <summary>
/// The singleton instance.
/// </summary>
public static readonly DotNetSdk Instance = new DotNetSdk();
/// <summary>
/// The minimum SDK version.
/// </summary>
public static Version MinimumVersion => new Version(7, 0);
/// <inheritdoc />
public override TargetPlatform[] Platforms
{
get
{
return new[]
{
TargetPlatform.Windows,
TargetPlatform.Linux,
};
}
}
/// <summary>
/// Dotnet SDK version (eg. 7.0).
/// </summary>
public readonly string VersionName;
/// <summary>
/// Dotnet Shared FX library version (eg. 7.0).
/// </summary>
public readonly string RuntimeVersionName;
/// <summary>
/// Dotnet hosting library files path (eg. <RootPath>/packs/Microsoft.NETCore.App.Host.<os>/<VersionName>/runtimes/<os>-<arcg>/native).
/// </summary>
public readonly string HostRootPath;
/// <summary>
/// Initializes a new instance of the <see cref="DotNetSdk"/> class.
/// </summary>
public DotNetSdk()
{
var platform = Platform.BuildTargetPlatform;
var architecture = Platform.BuildTargetArchitecture;
if (!Platforms.Contains(platform))
return;
// Find system-installed SDK
string dotnetPath;
string os, arch;
string[] dotnetSdkVersions, dotnetRuntimeVersions;
switch (architecture)
{
case TargetArchitecture.x86:
arch = "x86";
break;
case TargetArchitecture.x64:
arch = "x64";
break;
case TargetArchitecture.ARM:
arch = "arm";
break;
case TargetArchitecture.ARM64:
arch = "arm64";
break;
default:
throw new InvalidArchitectureException(architecture);
}
switch (platform)
{
case TargetPlatform.Windows:
{
os = $"win-{arch}";
#pragma warning disable CA1416
using RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
using RegistryKey sdkVersionsKey = baseKey.OpenSubKey($@"SOFTWARE\WOW6432Node\dotnet\Setup\InstalledVersions\{arch}\sdk");
using RegistryKey runtimeKey = baseKey.OpenSubKey(@$"SOFTWARE\WOW6432Node\dotnet\Setup\InstalledVersions\{arch}\sharedfx\Microsoft.NETCore.App");
using RegistryKey hostKey = baseKey.OpenSubKey(@$"SOFTWARE\dotnet\Setup\InstalledVersions\{arch}\sharedhost");
dotnetPath = (string)hostKey.GetValue("Path");
dotnetSdkVersions = sdkVersionsKey.GetValueNames();
dotnetRuntimeVersions = runtimeKey.GetValueNames();
#pragma warning restore CA1416
break;
}
case TargetPlatform.Linux:
{
// TODO: Support /etc/dotnet/install_location
os = $"linux-{arch}";
dotnetPath = "/usr/share/dotnet/";
dotnetSdkVersions = Directory.GetDirectories($"{dotnetPath}sdk/").Select(Path.GetFileName).ToArray();
dotnetRuntimeVersions = Directory.GetDirectories($"{dotnetPath}shared/Microsoft.NETCore.App/").Select(Path.GetFileName).ToArray();
break;
}
default:
throw new InvalidPlatformException(platform);
}
// Pick SDK version
string dotnetSdkVersion = dotnetSdkVersions.OrderByDescending(ParseVersion).FirstOrDefault();
string dotnetRuntimeVersion = dotnetRuntimeVersions.OrderByDescending(ParseVersion).FirstOrDefault();
if (string.IsNullOrEmpty(dotnetSdkVersion))
dotnetSdkVersion = Environment.GetEnvironmentVariable("DOTNET_ROOT");
if (string.IsNullOrEmpty(dotnetPath) || string.IsNullOrEmpty(dotnetSdkVersion) || string.IsNullOrEmpty(dotnetRuntimeVersion))
{
Log.Warning("Missing .NET SDK");
return;
}
int majorVersion = int.Parse(dotnetSdkVersion.Substring(0, dotnetSdkVersion.IndexOf(".")));
if (majorVersion < MinimumVersion.Major)
{
Log.Warning($"Unsupported .NET SDK {dotnetSdkVersion} version found. Minimum version required is .NET {MinimumVersion}.");
return;
}
HostRootPath = Path.Combine(dotnetPath, $"packs/Microsoft.NETCore.App.Host.{os}/{dotnetRuntimeVersion}/runtimes/{os}/native");
if (!Directory.Exists(HostRootPath))
{
Log.Warning($"Missing .NET SDK host runtime path in {HostRootPath}.");
return;
}
// Found
RootPath = dotnetPath;
IsValid = true;
Version = ParseVersion(dotnetSdkVersion);
VersionName = dotnetSdkVersion;
RuntimeVersionName = dotnetRuntimeVersion;
Log.Verbose($"Found .NET SDK {VersionName} (runtime {RuntimeVersionName}) at {RootPath}");
}
private static Version ParseVersion(string version)
{
// Give precedence to final releases over release candidate / beta releases
int rev = 9999;
if (version.Contains("-")) // e.g. 7.0.0-rc.2.22472.3
{
version = version.Substring(0, version.IndexOf("-"));
rev = 0;
}
Version ver = new Version(version);
return new Version(ver.Major, ver.Minor, ver.Build, rev);
}
}
}