Add -dotnet=ver command arg to Flax.Build to specify .NET SDK version to use for build

This commit is contained in:
Wojtek Figat
2023-11-25 12:16:13 +01:00
parent 2cef368282
commit 40d6e18e7e
8 changed files with 91 additions and 20 deletions

View File

@@ -32,7 +32,7 @@ public class nethost : ThirdPartyModule
// Get .NET SDK runtime host
var dotnetSdk = DotNetSdk.Instance;
if (!dotnetSdk.IsValid)
throw new Exception($"Missing NET SDK {DotNetSdk.MinimumVersion}.");
throw new DotNetSdk.MissingException();
if (!dotnetSdk.GetHostRuntime(options.Platform.Target, options.Architecture, out var hostRuntime))
{
if (options.Target.IsPreBuilt)

View File

@@ -164,7 +164,7 @@ namespace Flax.Build
#if USE_NETCORE
var dotnetSdk = DotNetSdk.Instance;
if (!dotnetSdk.IsValid)
throw new Exception("Cannot compile C# without .NET SDK");
throw new DotNetSdk.MissingException();
string dotnetPath = "dotnet", referenceAnalyzers;
string[] runtimeVersionNameParts = dotnetSdk.RuntimeVersionName.Split('.');
string runtimeVersionShort = runtimeVersionNameParts[0] + '.' + runtimeVersionNameParts[1];

View File

@@ -106,6 +106,20 @@ namespace Flax.Build
}
}
/// <summary>
/// Exception when .NET SDK is missing.
/// </summary>
public sealed class MissingException : Exception
{
/// <summary>
/// Init with a proper message.
/// </summary>
public MissingException()
: base(string.IsNullOrEmpty(Configuration.Dotnet) ? $"Missing .NET SDK {MinimumVersion} (or higher)." : $"Missing .NET SDK {Configuration.Dotnet}.")
{
}
}
private Dictionary<KeyValuePair<TargetPlatform, TargetArchitecture>, HostRuntime> _hostRuntimes = new();
/// <summary>
@@ -223,7 +237,7 @@ namespace Flax.Build
// We need to support two paths here:
// 1. We are running an x64 binary and we are running on an arm64 host machine
// 2. We are running an Arm64 binary and we are targeting an x64 host machine
if (Flax.Build.Platforms.MacPlatform.GetProcessIsTranslated() || isRunningOnArm64Targetx64)
if (Flax.Build.Platforms.MacPlatform.GetProcessIsTranslated() || isRunningOnArm64Targetx64)
{
rid = "osx-x64";
dotnetPath = Path.Combine(dotnetPath, "x64");
@@ -260,18 +274,23 @@ namespace Flax.Build
Log.Verbose($"Found the following .NET SDK versions: {string.Join(", ", dotnetSdkVersions)}");
Log.Verbose($"Found the following .NET runtime versions: {string.Join(", ", dotnetRuntimeVersions)}");
string dotnetSdkVersion = dotnetSdkVersions.FirstOrDefault(x => ParseVersion(x).Major >= MinimumVersion.Major && ParseVersion(x).Major <= MaximumVersion.Major);
string dotnetRuntimeVersion = dotnetRuntimeVersions.FirstOrDefault(x => ParseVersion(x).Major >= MinimumVersion.Major && ParseVersion(x).Major <= MaximumVersion.Major);
var dotnetSdkVersion = GetVersion(dotnetSdkVersions);
var dotnetRuntimeVersion = GetVersion(dotnetRuntimeVersions);
var minVer = string.IsNullOrEmpty(Configuration.Dotnet) ? MinimumVersion.ToString() : Configuration.Dotnet;
if (string.IsNullOrEmpty(dotnetSdkVersion))
dotnetSdkVersion = dotnetPath;
if (dotnetSdkVersion == null && dotnetSdkVersions.Count() > 0)
{
Log.Warning($"Unsupported .NET SDK {dotnetSdkVersions.First()} version found. Minimum version required is .NET {MinimumVersion}.");
if (dotnetSdkVersions.Any())
Log.Warning($"Unsupported .NET SDK versions found: {string.Join(", ", dotnetSdkVersions)}. Minimum version required is .NET {minVer}.");
else
Log.Warning($"Missing .NET SDK. Minimum version required is .NET {minVer}.");
return;
}
if (string.IsNullOrEmpty(dotnetSdkVersion) || string.IsNullOrEmpty(dotnetRuntimeVersion))
if (string.IsNullOrEmpty(dotnetRuntimeVersion))
{
Log.Warning("Missing .NET SDK");
if (dotnetRuntimeVersions.Any())
Log.Warning($"Unsupported .NET runtime versions found: {string.Join(", ", dotnetRuntimeVersions)}. Minimum version required is .NET {minVer}.");
else
Log.Warning($"Missing .NET runtime. Minimum version required is .NET {minVer}.");
return;
}
RootPath = dotnetPath;
@@ -434,7 +453,7 @@ namespace Flax.Build
exists = Directory.Exists(path);
if (exists)
_hostRuntimes[new KeyValuePair<TargetPlatform, TargetArchitecture>(platform, arch)] = new HostRuntime(platform, path);
_hostRuntimes[new KeyValuePair<TargetPlatform, TargetArchitecture>(platform, arch)] = new HostRuntime(platform, Utilities.NormalizePath(path));
return exists;
}
@@ -456,6 +475,8 @@ namespace Flax.Build
}
if (!Version.TryParse(version, out var ver))
return null;
if (ver.Build == -1)
return new Version(ver.Major, ver.Minor);
return new Version(ver.Major, ver.Minor, ver.Build, rev);
}
@@ -466,9 +487,42 @@ namespace Flax.Build
private static string GetVersion(IEnumerable<string> versions)
{
return versions.OrderByDescending(ParseVersion)
.Where(x => ParseVersion(x).Major >= MinimumVersion.Major && ParseVersion(x).Major <= MaximumVersion.Major)
.FirstOrDefault();
Version dotnetVer = null;
int dotnetVerNum = -1;
if (!string.IsNullOrEmpty(Configuration.Dotnet))
{
dotnetVer = ParseVersion(Configuration.Dotnet);
if (int.TryParse(Configuration.Dotnet, out var tmp) && tmp >= MinimumVersion.Major)
dotnetVerNum = tmp;
}
var sorted = versions.OrderByDescending(ParseVersion);
foreach (var version in sorted)
{
var v = ParseVersion(version);
// Filter by version specified in command line
if (dotnetVer != null)
{
if (dotnetVer.Major != v.Major)
continue;
if (dotnetVer.Minor != v.Minor)
continue;
if (dotnetVer.Revision != -1 && dotnetVer.Revision != v.Revision)
continue;
if (dotnetVer.Build != -1 && dotnetVer.Build != v.Build)
continue;
}
else if (dotnetVerNum != -1)
{
if (dotnetVerNum != v.Major)
continue;
}
// Filter by min/max versions supported by Flax.Build
if (v.Major >= MinimumVersion.Major && v.Major <= MaximumVersion.Major)
return version;
}
return null;
}
private static bool IsValidVersion(string versionPath)

View File

@@ -225,10 +225,24 @@ namespace Flax.Build
[CommandLine("compiler", "<name>", "Overrides the compiler to use for building. Eg. v140 overrides the toolset when building for Windows.")]
public static string Compiler = null;
/// <summary>
/// Specifies the dotnet SDK version to use for the build. Eg. set to '7' to use .NET 7 even if .NET 8 is installed.
/// </summary>
[CommandLine("dotnet", "<ver>", "Specifies the dotnet SDK version to use for the build. Eg. set to '7' to use .NET 7 even if .NET 8 is installed.")]
public static string Dotnet = null;
/// <summary>
/// Custom configuration defines provided via command line for the build tool.
/// </summary>
public static List<string> CustomDefines = new List<string>();
internal static void PassArgs(ref string cmdLine)
{
if (!string.IsNullOrEmpty(Compiler))
cmdLine += " -compiler=" + Compiler;
if (!string.IsNullOrEmpty(Dotnet))
cmdLine += " -dotnet=" + Dotnet;
}
}
/// <summary>

View File

@@ -17,8 +17,7 @@ namespace Flax.Deploy
var flaxBuildTool = Path.Combine(Globals.EngineRoot, buildPlatform == TargetPlatform.Windows ? "Binaries/Tools/Flax.Build.exe" : "Binaries/Tools/Flax.Build");
var format = "-build -buildtargets={0} -log -logfile= -perf -platform={1} -arch={2} -configuration={3}";
var cmdLine = string.Format(format, target, platform, architecture, configuration);
if (!string.IsNullOrEmpty(Configuration.Compiler))
cmdLine += " -compiler=" + Configuration.Compiler;
Configuration.PassArgs(ref cmdLine);
Log.Info($"Building {target} for {platform} {architecture} {configuration}...");
int result = Utilities.Run(flaxBuildTool, cmdLine, null, root);

View File

@@ -172,8 +172,7 @@ namespace Flax.Build.Projects.VisualStudio
configuration.Configuration,
configuration.Platform,
target.Name);
if (!string.IsNullOrEmpty(Configuration.Compiler))
cmdLine += " -compiler=" + Configuration.Compiler;
Configuration.PassArgs(ref cmdLine);
vcProjectFileContent.AppendLine(string.Format(" <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='{0}'\">", configuration.Name));
if (platform is IVisualStudioProjectCustomizer customizer)

View File

@@ -731,8 +731,7 @@ namespace Flax.Build.Projects.VisualStudio
configuration.Configuration,
configuration.Platform,
configuration.Target);
if (!string.IsNullOrEmpty(Configuration.Compiler))
cmdLine += " -compiler=" + Configuration.Compiler;
Configuration.PassArgs(ref cmdLine);
str.AppendLine(string.Format(" <Exec Command=\"{0} {1}\" Condition=\"'$(Configuration)|$(Platform)'=='{2}'\"/>", cmdLine, extraArgs, configuration.Name));
}

View File

@@ -201,6 +201,8 @@ namespace Flax.Build.Projects.VisualStudioCode
json.AddUnnamedField(string.Format("-buildTargets={0}", target.Name));
if (!string.IsNullOrEmpty(Configuration.Compiler))
json.AddUnnamedField(string.Format("-compiler={0}", Configuration.Compiler));
if (!string.IsNullOrEmpty(Configuration.Dotnet))
json.AddUnnamedField(string.Format("-dotnet={0}", Configuration.Dotnet));
}
json.EndArray();
@@ -228,6 +230,8 @@ namespace Flax.Build.Projects.VisualStudioCode
json.AddUnnamedField(string.Format("--buildTargets={0}", target.Name));
if (!string.IsNullOrEmpty(Configuration.Compiler))
json.AddUnnamedField(string.Format("--compiler={0}", Configuration.Compiler));
if (!string.IsNullOrEmpty(Configuration.Dotnet))
json.AddUnnamedField(string.Format("-dotnet={0}", Configuration.Dotnet));
}
json.EndArray();
@@ -255,6 +259,8 @@ namespace Flax.Build.Projects.VisualStudioCode
json.AddUnnamedField(string.Format("--buildTargets={0}", target.Name));
if (!string.IsNullOrEmpty(Configuration.Compiler))
json.AddUnnamedField(string.Format("--compiler={0}", Configuration.Compiler));
if (!string.IsNullOrEmpty(Configuration.Dotnet))
json.AddUnnamedField(string.Format("-dotnet={0}", Configuration.Dotnet));
}
json.EndArray();