Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/6.0.4xx-xcode14] Ensure we restore the temp csproj to compute AOT compiler path #16282

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 74 additions & 22 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/FindAotCompilerTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Microsoft.Build.Framework;

using Xamarin.Localization.MSBuild;
using Threading = System.Threading.Tasks;

namespace Xamarin.MacDev.Tasks {
public abstract class FindAotCompilerTaskBase : XamarinTask {
Expand Down Expand Up @@ -46,13 +45,10 @@ public override bool Execute ()
string ComputeAotCompilerPath ()
{
var projectPath = Path.GetTempFileName ();
var outputFile = Path.GetTempFileName ();
var binlog = Path.GetTempFileName ();

File.Delete (projectPath);
projectPath += ".csproj";
File.Delete (binlog);
binlog += ".binlog";

var csproj = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
Expand All @@ -68,17 +64,11 @@ string ComputeAotCompilerPath ()
";
File.WriteAllText (projectPath, csproj);

var executable = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
if (string.IsNullOrEmpty (executable))
executable = "dotnet";
var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");

var arguments = new List<string> ();
arguments.Add ("build");
arguments.Add ("/p:OutputFilePath=" + outputFile);
arguments.Add ("/p:RuntimeIdentifier=" + RuntimeIdentifier);
arguments.Add ("/t:ComputeAotCompilerPath");
arguments.Add ("/bl:" + binlog);
arguments.Add (projectPath);
if (string.IsNullOrEmpty (dotnetPath)) {
dotnetPath = "dotnet";
}

var environment = default (Dictionary<string, string>);
var customHome = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_HOME");
Expand All @@ -88,21 +78,83 @@ string ComputeAotCompilerPath ()
}

try {
ExecuteAsync (executable, arguments, environment: environment).Wait ();
return File.ReadAllText (outputFile).Trim ();
ExecuteRestoreAsync (dotnetPath, projectPath, environment).Wait ();

return ExecuteBuildAsync (dotnetPath, projectPath, environment).Result;
} finally {
if (KeepTemporaryOutput) {
Log.LogMessage (MessageImportance.Normal, "Temporary files for the FindAotCompiler task:");
Log.LogMessage (MessageImportance.Normal, $" Project file: {projectPath}");
Log.LogMessage (MessageImportance.Normal, $" Output file: {outputFile}");
Log.LogMessage (MessageImportance.Normal, $" Binary log: {binlog}");
Log.LogMessage (MessageImportance.Normal, $"Temporary project for the FindAotCompiler task: {projectPath}");
} else {
File.Delete (projectPath);
}
}
}

async Threading.Task ExecuteRestoreAsync (string dotnetPath, string projectPath, Dictionary<string, string> environment)
{
var binlog = GetTempBinLog ();
var arguments = new List<string> ();

arguments.Add ("restore");

var dotnetDir = Path.GetDirectoryName (dotnetPath);
var configFile = Path.Combine (dotnetDir, "NuGet.config");

if (File.Exists (configFile)) {
arguments.Add ("/p:RestoreConfigFile=" + configFile);
}

arguments.Add ("/bl:" + binlog);
arguments.Add (projectPath);

try {
await ExecuteAsync (dotnetPath, arguments, environment: environment);
} finally {
if (KeepTemporaryOutput) {
Log.LogMessage (MessageImportance.Normal, $"Temporary restore log for the FindAotCompiler task: {binlog}");
} else {
File.Delete (binlog);
}
}
}

async Threading.Task<string> ExecuteBuildAsync (string dotnetPath, string projectPath, Dictionary<string, string> environment)
{
var outputFile = Path.GetTempFileName ();
var binlog = GetTempBinLog ();
var arguments = new List<string> ();

arguments.Add ("build");
arguments.Add ("/p:OutputFilePath=" + outputFile);
arguments.Add ("/p:RuntimeIdentifier=" + RuntimeIdentifier);
arguments.Add ("/t:ComputeAotCompilerPath");
arguments.Add ("/bl:" + binlog);
arguments.Add (projectPath);

try {
await ExecuteAsync (dotnetPath, arguments, environment: environment);

return File.ReadAllText (outputFile).Trim ();
} finally {
if (KeepTemporaryOutput) {
Log.LogMessage (MessageImportance.Normal, $"Temporary output for the FindAotCompiler task: {outputFile}");
Log.LogMessage (MessageImportance.Normal, $"Temporary build log for the FindAotCompiler task: {binlog}");
} else {
File.Delete (outputFile);
File.Delete (binlog);
}
}
}

string GetTempBinLog ()
{
var binlog = Path.GetTempFileName ();

File.Delete (binlog);
binlog += ".binlog";

return binlog;
}
}
}