Add .NET 7 support to Xbox

This commit is contained in:
Wojtek Figat
2023-04-04 14:46:08 +02:00
parent e48e15f02f
commit 0c4a608b65
7 changed files with 68 additions and 123 deletions

View File

@@ -42,21 +42,6 @@ DotNetAOTModes GDKPlatformTools::UseAOT() const
return DotNetAOTModes::MonoAOTDynamic;
}
bool GDKPlatformTools::OnScriptsStepDone(CookingData& data)
{
// Override Newtonsoft.Json.dll for some platforms (that don't support runtime code generation)
const String customBinPath = data.GetPlatformBinariesRoot() / TEXT("Newtonsoft.Json.dll");
const String assembliesPath = data.ManagedCodeOutputPath;
if (FileSystem::CopyFile(assembliesPath / TEXT("Newtonsoft.Json.dll"), customBinPath))
{
data.Error(TEXT("Failed to copy deloy custom assembly."));
return true;
}
FileSystem::DeleteFile(assembliesPath / TEXT("Newtonsoft.Json.pdb"));
return false;
}
bool GDKPlatformTools::OnDeployBinaries(CookingData& data)
{
// Copy binaries
@@ -83,87 +68,6 @@ bool GDKPlatformTools::OnDeployBinaries(CookingData& data)
return false;
}
void GDKPlatformTools::OnConfigureAOT(CookingData& data, AotConfig& config)
{
const auto platformDataPath = data.GetPlatformBinariesRoot();
const bool useInterpreter = true; // TODO: use Full AOT on GDK
const bool enableDebug = data.Configuration != BuildConfiguration::Release;
const Char* aotMode = useInterpreter ? TEXT("full,interp") : TEXT("full");
const Char* debugMode = enableDebug ? TEXT("soft-debug") : TEXT("nodebug");
config.AotCompilerArgs = String::Format(TEXT("--aot={0},verbose,stats,print-skipped,{1} -O=all"), aotMode, debugMode);
if (enableDebug)
config.AotCompilerArgs = TEXT("--debug ") + config.AotCompilerArgs;
config.AotCompilerPath = platformDataPath / TEXT("Tools/mono.exe");
}
bool GDKPlatformTools::OnPerformAOT(CookingData& data, AotConfig& config, const String& assemblyPath)
{
// Skip .dll.dll which could be a false result from the previous AOT which could fail
if (assemblyPath.EndsWith(TEXT(".dll.dll")))
{
LOG(Warning, "Skip AOT for file '{0}' as it can be a result from the previous task", assemblyPath);
return false;
}
// Check if skip this assembly (could be already processed)
const String filename = StringUtils::GetFileName(assemblyPath);
const String outputPath = config.AotCachePath / filename + TEXT(".dll");
if (FileSystem::FileExists(outputPath) && FileSystem::GetFileLastEditTime(assemblyPath) < FileSystem::GetFileLastEditTime(outputPath))
return false;
LOG(Info, "Calling AOT tool for \"{0}\"", assemblyPath);
// Cleanup temporary results (fromm the previous AT that fail or sth)
const String resultPath = assemblyPath + TEXT(".dll");
const String resultPathExp = resultPath + TEXT(".exp");
const String resultPathLib = resultPath + TEXT(".lib");
const String resultPathPdb = resultPath + TEXT(".pdb");
if (FileSystem::FileExists(resultPath))
FileSystem::DeleteFile(resultPath);
if (FileSystem::FileExists(resultPathExp))
FileSystem::DeleteFile(resultPathExp);
if (FileSystem::FileExists(resultPathLib))
FileSystem::DeleteFile(resultPathLib);
if (FileSystem::FileExists(resultPathPdb))
FileSystem::DeleteFile(resultPathPdb);
// Call tool
CreateProcessSettings procSettings;
procSettings.FileName = String::Format(TEXT("\"{0}\" {1} \"{2}\""), config.AotCompilerPath, config.AotCompilerArgs, assemblyPath);
procSettings.WorkingDirectory = StringUtils::GetDirectoryName(config.AotCompilerPath);
procSettings.Environment = config.EnvVars;
const int32 result = Platform::CreateProcess(procSettings);
if (result != 0)
{
data.Error(TEXT("AOT tool execution failed with result code {1} for assembly \"{0}\". See log for more info."), assemblyPath, result);
return true;
}
// Copy result
if (FileSystem::CopyFile(outputPath, resultPath))
{
data.Error(TEXT("Failed to copy the AOT tool result file. It can be missing."));
return true;
}
// Copy pdb file if exists
if (data.Configuration != BuildConfiguration::Release && FileSystem::FileExists(resultPathPdb))
{
FileSystem::CopyFile(config.AotCachePath / StringUtils::GetFileName(resultPathPdb), resultPathPdb);
}
// Clean intermediate results
if (FileSystem::DeleteFile(resultPath)
|| (FileSystem::FileExists(resultPathExp) && FileSystem::DeleteFile(resultPathExp))
|| (FileSystem::FileExists(resultPathLib) && FileSystem::DeleteFile(resultPathLib))
|| (FileSystem::FileExists(resultPathPdb) && FileSystem::DeleteFile(resultPathPdb))
)
{
LOG(Warning, "Failed to remove the AOT tool result file(s).");
}
return false;
}
bool GDKPlatformTools::OnPostProcess(CookingData& data, GDKPlatformSettings* platformSettings)
{
// Configuration

View File

@@ -27,10 +27,7 @@ public:
// [PlatformTools]
DotNetAOTModes UseAOT() const override;
bool OnScriptsStepDone(CookingData& data) override;
bool OnDeployBinaries(CookingData& data) override;
void OnConfigureAOT(CookingData& data, AotConfig& config) override;
bool OnPerformAOT(CookingData& data, AotConfig& config, const String& assemblyPath) override;
};
#endif

View File

@@ -54,7 +54,7 @@ public class nethost : ThirdPartyModule
options.OutputFiles.Add(Path.Combine(hostRuntime.Path, "nethost.lib"));
options.DependencyFiles.Add(Path.Combine(hostRuntime.Path, "nethost.dll"));
}
else
else if (hostRuntime.Type == DotNetSdk.HostType.Mono)
{
options.PublicDefinitions.Add("USE_MONO_DYNAMIC_LIB");
options.OutputFiles.Add(Path.Combine(hostRuntime.Path, "coreclr.import.lib"));

View File

@@ -304,14 +304,9 @@ namespace Flax.Build
}
else if (aotMode == DotNetAOTModes.MonoAOTDynamic || aotMode == DotNetAOTModes.MonoAOTStatic)
{
var platformToolsRoot = Path.Combine(Globals.EngineRoot, "Source/Platforms", platform.ToString(), "Binaries/Tools");
if (!Directory.Exists(platformToolsRoot))
throw new Exception("Missing platform tools " + platformToolsRoot);
var dotnetLibPath = Path.Combine(aotAssembliesPath, "lib/net7.0");
var monoAssembliesOutputPath = aotMode == DotNetAOTModes.MonoAOTDynamic ? dotnetOutputPath : null;
// TODO: impl Mono AOT more generic way, not just Windows-only case
// Build list of assemblies to process (use game assemblies as root to walk over used references from stdlib)
var assembliesPaths = new List<string>();
if (Configuration.SkipUnusedDotnetLibsPackaging)
@@ -371,25 +366,16 @@ namespace Flax.Build
Log.Info("");
Log.Info("");
}
// Setup options
var aotCompilerPath = Path.Combine(platformToolsRoot, "mono-aot-cross.exe");
var monoAotMode = "full";
var debugMode = useDebug ? "soft-debug" : "nodebug";
var aotCompilerArgs = $"--aot={monoAotMode},verbose,stats,print-skipped,{debugMode} -O=all";
if (useDebug || dotnetAotDebug)
aotCompilerArgs = "--debug " + aotCompilerArgs;
var envVars = new Dictionary<string, string>();
envVars["MONO_PATH"] = aotAssembliesPath + ";" + dotnetLibPath;
if (dotnetAotDebug)
{
envVars["MONO_LOG_LEVEL"] = "debug";
}
// Run cross-compiler compiler
Log.Info(" * " + assemblyPath);
int result = Utilities.Run(aotCompilerPath, $"{aotCompilerArgs} \"{assemblyPath}\"", null, platformToolsRoot, Utilities.RunOptions.AppMustExist | Utilities.RunOptions.ConsoleLogOutput, envVars);
if (result != 0)
var options = new Toolchain.CSharpOptions
{
InputFile = assemblyPath,
AssembliesFolder = aotAssembliesPath,
ClassLibraryPath = dotnetLibPath,
EnableDebugSymbols = useDebug,
EnableToolDebug = dotnetAotDebug,
};
if (buildToolchain.CompileCSharp(options))
{
Log.Error("Failed to run AOT on assembly " + assemblyPath);
failed = true;

View File

@@ -274,6 +274,14 @@ namespace Flax.Build
Log.Verbose($" - Host Runtime for {e.Key.Key} {e.Key.Value}");
}
/// <summary>
/// Initializes platforms which can register custom host runtime location
/// </summary>
private static void InitPlatforms()
{
Platform.GetPlatform(TargetPlatform.Windows, true);
}
/// <summary>
/// Gets the host runtime identifier for a given platform (eg. linux-arm64).
/// </summary>
@@ -338,6 +346,7 @@ namespace Flax.Build
/// </summary>
public void PrintRuntimes()
{
InitPlatforms();
foreach (var e in _hostRuntimes)
{
// Filter with input commandline
@@ -357,6 +366,7 @@ namespace Flax.Build
/// </summary>
public bool GetHostRuntime(TargetPlatform platform, TargetArchitecture arch, out HostRuntime hostRuntime)
{
InitPlatforms();
return _hostRuntimes.TryGetValue(new KeyValuePair<TargetPlatform, TargetArchitecture>(platform, arch), out hostRuntime);
}

View File

@@ -133,5 +133,27 @@ namespace Flax.Build
/// <param name="options">The build options with linking environment.</param>
/// <param name="outputFilePath">The output file path (result linked file).</param>
public abstract void LinkFiles(TaskGraph graph, BuildOptions options, string outputFilePath);
/// <summary>
/// C# compilation options container.
/// </summary>
public struct CSharpOptions
{
public string InputFile;
public string AssembliesFolder;
public string ClassLibraryPath;
public bool EnableDebugSymbols;
public bool EnableToolDebug;
}
/// <summary>
/// Compiles the C# assembly with AOT cross-compiler.
/// </summary>
/// <param name="options">The options.</param>
/// <returns>True if failed, or not supported.</returns>
public virtual bool CompileCSharp(CSharpOptions options)
{
return true;
}
}
}

View File

@@ -1024,5 +1024,31 @@ namespace Flax.Build.Platforms
priNewFile.ProducedFiles.Add(priFile);
}
}
/// <inheritdoc />
public override bool CompileCSharp(CSharpOptions options)
{
var platformToolsRoot = Path.Combine(Globals.EngineRoot, "Source/Platforms", Platform.Target.ToString(), "Binaries/Tools");
if (!Directory.Exists(platformToolsRoot))
throw new Exception("Missing platform tools " + platformToolsRoot);
var aotCompilerPath = Path.Combine(platformToolsRoot, "mono-aot-cross.exe");
// Setup options
var monoAotMode = "full";
var debugMode = options.EnableDebugSymbols ? "soft-debug" : "nodebug";
var aotCompilerArgs = $"--aot={monoAotMode},verbose,stats,print-skipped,{debugMode} -O=all";
if (options.EnableDebugSymbols || options.EnableToolDebug)
aotCompilerArgs = "--debug " + aotCompilerArgs;
var envVars = new Dictionary<string, string>();
envVars["MONO_PATH"] = options.AssembliesFolder + ";" + options.ClassLibraryPath;
if (options.EnableToolDebug)
{
envVars["MONO_LOG_LEVEL"] = "debug";
}
// Run cross-compiler compiler
int result = Utilities.Run(aotCompilerPath, $"{aotCompilerArgs} \"{options.InputFile}\"", null, platformToolsRoot, Utilities.RunOptions.AppMustExist | Utilities.RunOptions.ConsoleLogOutput, envVars);
return result != 0;
}
}
}