Skip to content

Commit

Permalink
Ensure we restore the temp csproj to compute AOT compiler path (#16280)
Browse files Browse the repository at this point in the history
The build command doesn't support a parameter to specify a NuGet.config, so we need to restore the temp project first, so we pass the NuGet.config file to use the sources from, in case it exists (e.g: the NuGet.config from the XMA dotnet path).

This is useful to support auhtorized NuGet feeds

Fixes Bug #1611102 - [XVS][MAUI] Failed to build .NET MAUI (net6.0) with iOS Simulator: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1611102

Co-authored-by: GitHub Actions Autoformatter <github-actions-autoformatter@xamarin.com>
  • Loading branch information
mauroa and GitHub Actions Autoformatter authored Oct 7, 2022
1 parent 9248e37 commit 6bc66bf
Showing 1 changed file with 74 additions and 22 deletions.
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;
}
}
}

4 comments on commit 6bc66bf

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.