Skip to content

Commit d0c6a17

Browse files
authored
Select the NativeAOT runtime pack as the target package when it's provided as part of the KnownILCompilerPack metadata (#46611)
1 parent d11ee4c commit d0c6a17

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

src/Layout/redist/targets/GenerateBundledVersions.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ Copyright (c) .NET Foundation. All rights reserved.
588588
<KnownILCompilerPack Include="Microsoft.DotNet.ILCompiler"
589589
TargetFramework="net10.0"
590590
ILCompilerPackNamePattern="runtime.**RID**.Microsoft.DotNet.ILCompiler"
591+
ILCompilerRuntimePackNamePattern="Microsoft.NETCore.App.Runtime.NativeAOT.**RID**"
591592
ILCompilerPackVersion="$(MicrosoftNETCoreAppRuntimePackageVersion)"
592593
ILCompilerRuntimeIdentifiers="@(ILCompilerSupportedRids, '%3B')"
593594
/>

src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,13 +847,36 @@ private ToolPackSupport AddToolPack(
847847
{
848848
return ToolPackSupport.UnsupportedForTargetRuntimeIdentifier;
849849
}
850-
if (!hostRuntimeIdentifier.Equals(targetRuntimeIdentifier))
850+
851+
// If there's an available runtime pack, use it instead of the ILCompiler package for target-specific bits.
852+
bool useRuntimePackForAllTargets = false;
853+
string targetPackNamePattern = packNamePattern;
854+
if (knownPack.GetMetadata("ILCompilerRuntimePackNamePattern") is string runtimePackNamePattern && runtimePackNamePattern != string.Empty)
851855
{
852-
var targetIlcPackName = packNamePattern.Replace("**RID**", targetRuntimeIdentifier);
856+
targetPackNamePattern = runtimePackNamePattern;
857+
useRuntimePackForAllTargets = true;
858+
}
859+
860+
if (useRuntimePackForAllTargets || !hostRuntimeIdentifier.Equals(targetRuntimeIdentifier))
861+
{
862+
var targetIlcPackName = targetPackNamePattern.Replace("**RID**", targetRuntimeIdentifier);
853863
var targetIlcPack = new TaskItem(targetIlcPackName);
854864
targetIlcPack.SetMetadata(MetadataKeys.NuGetPackageId, targetIlcPackName);
855865
targetIlcPack.SetMetadata(MetadataKeys.NuGetPackageVersion, packVersion);
856866
TargetILCompilerPacks = new[] { targetIlcPack };
867+
868+
string targetILCompilerPackPath = GetPackPath(targetIlcPackName, packVersion);
869+
if (targetILCompilerPackPath != null)
870+
{
871+
targetIlcPack.SetMetadata(MetadataKeys.PackageDirectory, targetILCompilerPackPath);
872+
}
873+
else if (EnableRuntimePackDownload)
874+
{
875+
// We need to download the runtime pack
876+
var targetIlcPackToDownload = new TaskItem(targetIlcPackName);
877+
targetIlcPackToDownload.SetMetadata(MetadataKeys.Version, packVersion);
878+
packagesToDownload.Add(targetIlcPackToDownload);
879+
}
857880
}
858881
}
859882

test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class GivenThatWeWantToPublishAnAotApp : SdkTest
1515
{
1616
private readonly string RuntimeIdentifier = $"/p:RuntimeIdentifier={RuntimeInformation.RuntimeIdentifier}";
1717

18-
private readonly string ExplicitPackageVersion = "7.0.0-rc.2.22456.11";
18+
private const string NetCurrentExplicitPackageVersion = "10.0.0-preview.6.25316.103";
1919

2020
public GivenThatWeWantToPublishAnAotApp(ITestOutputHelper log) : base(log)
2121
{
@@ -67,8 +67,10 @@ public void NativeAot_hw_runs_with_no_warnings_when_PublishAot_is_enabled(string
6767
DoSymbolsExist(publishDirectory, testProject.Name).Should().BeTrue($"{publishDirectory} should contain {testProject.Name} symbol");
6868
IsNativeImage(publishedExe).Should().BeTrue();
6969

70+
bool useRuntimePackLayout = targetFramework is not ("net7.0" or "net8.0" or "net9.0");
71+
7072
GetKnownILCompilerPackVersion(testAsset, targetFramework, out string expectedVersion);
71-
CheckIlcVersions(testAsset, targetFramework, rid, expectedVersion);
73+
CheckIlcVersions(testAsset, targetFramework, rid, expectedVersion, useRuntimePackLayout);
7274

7375
var command = new RunExeCommand(Log, publishedExe)
7476
.Execute().Should().Pass()
@@ -249,8 +251,10 @@ public void NativeAot_app_builds_with_config_when_PublishAot_is_enabled(string t
249251
File.Exists(depsPath).Should().BeTrue();
250252
}
251253

254+
private const string Net7ExplicitPackageVersion = "7.0.0";
255+
252256
[RequiresMSBuildVersionTheory("17.0.0.32901")]
253-
[InlineData(ToolsetInfo.CurrentTargetFramework)]
257+
[InlineData("net7.0")]
254258
public void NativeAot_hw_runs_with_PackageReference_PublishAot_is_enabled(string targetFramework)
255259
{
256260
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
@@ -266,7 +270,7 @@ public void NativeAot_hw_runs_with_PackageReference_PublishAot_is_enabled(string
266270
testProject.AdditionalProperties["PublishAot"] = "true";
267271

268272
// This will add a reference to a package that will also be automatically imported by the SDK
269-
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", ExplicitPackageVersion));
273+
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", Net7ExplicitPackageVersion));
270274

271275
// Linux symbol files are embedded and require additional steps to be stripped to a separate file
272276
// assumes /bin (or /usr/bin) are in the PATH
@@ -302,7 +306,7 @@ public void NativeAot_hw_runs_with_PackageReference_PublishAot_is_enabled(string
302306
.Execute().Should().Pass()
303307
.And.HaveStdOutContaining("Hello World");
304308

305-
CheckIlcVersions(testAsset, targetFramework, rid, ExplicitPackageVersion);
309+
CheckIlcVersions(testAsset, targetFramework, rid, Net7ExplicitPackageVersion, useRuntimePackLayout: false);
306310
}
307311

308312
[RequiresMSBuildVersionTheory("17.0.0.32901")]
@@ -315,7 +319,7 @@ public void NativeAot_hw_runs_with_PackageReference_PublishAot_is_empty(string t
315319
var testProject = CreateHelloWorldTestProject(targetFramework, projectName, true);
316320

317321
// This will add a reference to a package that will also be automatically imported by the SDK
318-
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", ExplicitPackageVersion));
322+
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", NetCurrentExplicitPackageVersion));
319323

320324
// Linux symbol files are embedded and require additional steps to be stripped to a separate file
321325
// assumes /bin (or /usr/bin) are in the PATH
@@ -343,7 +347,7 @@ public void NativeAot_hw_runs_with_PackageReference_PublishAot_is_empty(string t
343347
}
344348

345349
[RequiresMSBuildVersionTheory("17.0.0.32901")]
346-
[MemberData(nameof(Net7Plus), MemberType = typeof(PublishTestUtils))]
350+
[InlineData(ToolsetInfo.CurrentTargetFramework)]
347351
public void NativeAot_hw_runs_with_cross_target_PublishAot_is_enabled(string targetFramework)
348352
{
349353
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && (RuntimeInformation.OSArchitecture == Architecture.X64))
@@ -367,13 +371,13 @@ public void NativeAot_hw_runs_with_cross_target_PublishAot_is_enabled(string tar
367371
File.Exists(publishedExe).Should().BeTrue();
368372

369373
GetKnownILCompilerPackVersion(testAsset, targetFramework, out string expectedVersion);
370-
CheckIlcVersions(testAsset, targetFramework, rid, expectedVersion);
374+
CheckIlcVersions(testAsset, targetFramework, rid, expectedVersion, useRuntimePackLayout: true);
371375
}
372376
}
373377

374378

375379
[RequiresMSBuildVersionTheory("17.0.0.32901")]
376-
[MemberData(nameof(Net7Plus), MemberType = typeof(PublishTestUtils))]
380+
[InlineData(ToolsetInfo.CurrentTargetFramework)]
377381
public void NativeAot_hw_runs_with_cross_PackageReference_PublishAot_is_enabled(string targetFramework)
378382
{
379383
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && (RuntimeInformation.OSArchitecture == Architecture.X64))
@@ -382,11 +386,16 @@ public void NativeAot_hw_runs_with_cross_PackageReference_PublishAot_is_enabled(
382386
var rid = "win-arm64";
383387

384388
var testProject = CreateHelloWorldTestProject(targetFramework, projectName, true);
389+
testProject.RecordProperties("BundledNETCoreAppPackageVersion");
385390
testProject.AdditionalProperties["PublishAot"] = "true";
386391

387392
// This will add a reference to a package that will also be automatically imported by the SDK
388-
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", ExplicitPackageVersion));
389-
testProject.PackageReferences.Add(new TestPackageReference("runtime.win-x64.Microsoft.DotNet.ILCompiler", ExplicitPackageVersion));
393+
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", "$(BundledNETCoreAppPackageVersion)"));
394+
testProject.AddItem("PackageDownload", new Dictionary<string, string>
395+
{
396+
{ "Include", "Microsoft.NETCore.App.Runtime.NativeAOT.win-arm64" },
397+
{ "Version", $"[$(BundledNETCoreAppPackageVersion)]" }
398+
});
390399

391400
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);
392401

@@ -398,13 +407,16 @@ public void NativeAot_hw_runs_with_cross_PackageReference_PublishAot_is_enabled(
398407
.And.HaveStdOutContaining("warning")
399408
.And.HaveStdOutContaining("Microsoft.DotNet.ILCompiler");
400409

410+
var buildProperties = testProject.GetPropertyValues(testAsset.TestRoot, targetFramework);
411+
var targetVersion = buildProperties["BundledNETCoreAppPackageVersion"];
412+
401413
var publishDirectory = publishCommand.GetOutputDirectory(targetFramework: targetFramework, runtimeIdentifier: rid).FullName;
402414
var publishedDll = Path.Combine(publishDirectory, $"{projectName}.dll");
403415
var publishedExe = Path.Combine(publishDirectory, $"{testProject.Name}{Constants.ExeSuffix}");
404416
File.Exists(publishedDll).Should().BeFalse();
405417
File.Exists(publishedExe).Should().BeTrue();
406418

407-
CheckIlcVersions(testAsset, targetFramework, rid, ExplicitPackageVersion);
419+
CheckIlcVersions(testAsset, targetFramework, rid, targetVersion, useRuntimePackLayout: true);
408420
}
409421
}
410422

@@ -420,8 +432,8 @@ public void NativeAot_hw_runs_with_cross_PackageReference_PublishAot_is_empty(st
420432
var testProject = CreateHelloWorldTestProject(targetFramework, projectName, true);
421433

422434
// This will add a reference to a package that will also be automatically imported by the SDK
423-
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", ExplicitPackageVersion));
424-
testProject.PackageReferences.Add(new TestPackageReference("runtime.win-x64.Microsoft.DotNet.ILCompiler", ExplicitPackageVersion));
435+
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", NetCurrentExplicitPackageVersion));
436+
testProject.PackageReferences.Add(new TestPackageReference("runtime.win-x64.Microsoft.DotNet.ILCompiler", NetCurrentExplicitPackageVersion));
425437

426438
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);
427439

@@ -469,7 +481,7 @@ public void NativeAot_hw_fails_with_sdk6_PackageReference_PublishAot_is_enabled(
469481
var testProject = CreateHelloWorldTestProject("net6.0", projectName, true);
470482
testProject.AdditionalProperties["PublishAot"] = "true";
471483

472-
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", ExplicitPackageVersion));
484+
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DotNet.ILCompiler", NetCurrentExplicitPackageVersion));
473485

474486
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);
475487

@@ -1041,7 +1053,7 @@ private void GetKnownILCompilerPackVersion(TestAsset testAsset, string targetFra
10411053
.Single();
10421054
}
10431055

1044-
private void CheckIlcVersions(TestAsset testAsset, string targetFramework, string rid, string expectedVersion)
1056+
private void CheckIlcVersions(TestAsset testAsset, string targetFramework, string rid, string expectedVersion, bool useRuntimePackLayout)
10451057
{
10461058
// Compiler version matches expected version
10471059
var ilcToolsPathCommand = new GetValuesCommand(testAsset, "IlcToolsPath", targetFramework: targetFramework)
@@ -1061,7 +1073,17 @@ private void CheckIlcVersions(TestAsset testAsset, string targetFramework, strin
10611073
ilcReferenceCommand.Execute($"/p:RuntimeIdentifier={rid}", "/p:SelfContained=true").Should().Pass();
10621074
var ilcReference = ilcReferenceCommand.GetValues();
10631075
var corelibReference = ilcReference.Where(r => Path.GetFileName(r).Equals("System.Private.CoreLib.dll")).Single();
1064-
var ilcReferenceVersion = Path.GetFileName(Path.GetDirectoryName(Path.GetDirectoryName(corelibReference)));
1076+
string ilcReferenceVersion;
1077+
if (useRuntimePackLayout)
1078+
{
1079+
// In the runtime pack layout, System.Private.CoreLib.dll is in the runtimes/<rid>/native directory
1080+
ilcReferenceVersion = Path.GetFileName(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(corelibReference)))));
1081+
}
1082+
else
1083+
{
1084+
// In the old layout, System.Private.CoreLib.dll is in the framework directory
1085+
ilcReferenceVersion = Path.GetFileName(Path.GetDirectoryName(Path.GetDirectoryName(corelibReference)));
1086+
}
10651087
ilcReferenceVersion.Should().Be(expectedVersion);
10661088
}
10671089

0 commit comments

Comments
 (0)