Skip to content

Commit bb62425

Browse files
Copilotrolfbjarne
andcommitted
Add RuntimeIdentifier input to ComputeInstructionSet task for macOS/Mac Catalyst
Co-authored-by: rolfbjarne <249268+rolfbjarne@users.noreply.github.com>
1 parent bb17758 commit bb62425

File tree

4 files changed

+65
-43
lines changed

4 files changed

+65
-43
lines changed

dotnet/targets/Microsoft.Sdk.R2R.targets

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<UsingTask TaskName="Xamarin.MacDev.Tasks.ComputeInstructionSet" AssemblyFile="$(_XamarinTaskAssembly)" />
4+
5+
<!--
6+
Compute the instruction set for R2R/NAOT compilation based on the SupportedOSPlatformVersion and RuntimeIdentifier.
7+
This target runs early in the build to set PublishReadyToRunCrossgen2ExtraArgs if needed.
8+
-->
9+
<Target Name="_ComputeInstructionSetForCrossgen2"
10+
Condition="'$(PublishReadyToRun)' == 'true'"
11+
BeforeTargets="ResolveReadyToRunCompilers">
12+
<ComputeInstructionSet
13+
SupportedOSPlatformVersion="$(SupportedOSPlatformVersion)"
14+
RuntimeIdentifier="$(RuntimeIdentifier)"
15+
SessionId="$(SessionId)"
16+
TargetFrameworkMoniker="$(TargetFramework)"
17+
SdkDevPath="$(_SdkDevPath)"
18+
>
19+
<Output TaskParameter="InstructionSet" PropertyName="_ComputedInstructionSet" />
20+
</ComputeInstructionSet>
21+
22+
<PropertyGroup Condition="'$(_ComputedInstructionSet)' != ''">
23+
<!-- Append the instruction set to the existing PublishReadyToRunCrossgen2ExtraArgs -->
24+
<PublishReadyToRunCrossgen2ExtraArgs Condition="'$(PublishReadyToRunCrossgen2ExtraArgs)' == ''">--instruction-set:$(_ComputedInstructionSet)</PublishReadyToRunCrossgen2ExtraArgs>
25+
<PublishReadyToRunCrossgen2ExtraArgs Condition="'$(PublishReadyToRunCrossgen2ExtraArgs)' != ''">$(PublishReadyToRunCrossgen2ExtraArgs) --instruction-set:$(_ComputedInstructionSet)</PublishReadyToRunCrossgen2ExtraArgs>
26+
</PropertyGroup>
27+
</Target>
28+
329
<!-- Workaround for https://github.com/dotnet/runtime/issues/79902 -->
430
<Target Name="_FixR2ROutputPaths" AfterTargets="_PrepareForReadyToRunCompilation">
531
<ItemGroup>

dotnet/targets/Xamarin.Shared.Sdk.targets

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
</PropertyGroup>
99

1010
<UsingTask TaskName="Xamarin.MacDev.Tasks.CompileNativeCode" AssemblyFile="$(_XamarinTaskAssembly)" />
11-
<UsingTask TaskName="Xamarin.MacDev.Tasks.ComputeInstructionSet" AssemblyFile="$(_XamarinTaskAssembly)" />
1211
<UsingTask TaskName="Xamarin.MacDev.Tasks.FindAotCompiler" AssemblyFile="$(_XamarinTaskAssembly)" />
1312
<UsingTask TaskName="Xamarin.MacDev.Tasks.GetFullPaths" AssemblyFile="$(_XamarinTaskAssembly)" />
1413
<UsingTask TaskName="Xamarin.MacDev.Tasks.InstallNameTool" AssemblyFile="$(_XamarinTaskAssembly)" />
@@ -532,29 +531,6 @@
532531
</PropertyGroup>
533532
</Target>
534533

535-
<!--
536-
Compute the instruction set for R2R/NAOT compilation based on the SupportedOSPlatformVersion.
537-
This target runs early in the build to set PublishReadyToRunCrossgen2ExtraArgs if needed.
538-
-->
539-
<Target Name="_ComputeInstructionSetForCrossgen2"
540-
Condition="'$(PublishReadyToRun)' == 'true'"
541-
BeforeTargets="PrepareForILLink">
542-
<ComputeInstructionSet
543-
SupportedOSPlatformVersion="$(SupportedOSPlatformVersion)"
544-
SessionId="$(SessionId)"
545-
TargetFrameworkMoniker="$(TargetFramework)"
546-
SdkDevPath="$(_SdkDevPath)"
547-
>
548-
<Output TaskParameter="InstructionSet" PropertyName="_ComputedInstructionSet" />
549-
</ComputeInstructionSet>
550-
551-
<PropertyGroup Condition="'$(_ComputedInstructionSet)' != ''">
552-
<!-- Append the instruction set to the existing PublishReadyToRunCrossgen2ExtraArgs -->
553-
<PublishReadyToRunCrossgen2ExtraArgs Condition="'$(PublishReadyToRunCrossgen2ExtraArgs)' == ''">--instruction-set:$(_ComputedInstructionSet)</PublishReadyToRunCrossgen2ExtraArgs>
554-
<PublishReadyToRunCrossgen2ExtraArgs Condition="'$(PublishReadyToRunCrossgen2ExtraArgs)' != ''">$(PublishReadyToRunCrossgen2ExtraArgs) --instruction-set:$(_ComputedInstructionSet)</PublishReadyToRunCrossgen2ExtraArgs>
555-
</PropertyGroup>
556-
</Target>
557-
558534
<Target Name="_ComputeLinkerArguments" DependsOnTargets="$(_ComputeLinkerArgumentsDependsOn)" />
559535

560536
<Target Name="_ComputeLinkerInputs">

msbuild/Xamarin.MacDev.Tasks/Tasks/ComputeInstructionSet.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class ComputeInstructionSet : XamarinTask {
1818
[Required]
1919
public string SupportedOSPlatformVersion { get; set; } = "";
2020

21+
public string RuntimeIdentifier { get; set; } = "";
22+
2123
#endregion
2224

2325
#region Outputs
@@ -337,19 +339,32 @@ public override bool Execute ()
337339

338340
string? ComputeMacInstructionSet (ApplePlatform platform, Version targetVersion)
339341
{
340-
// For macOS, we need to consider both Intel and Apple Silicon
341-
// For now, we'll use a conservative approach:
342-
// - macOS 11.0+ (Big Sur): First version to support Apple Silicon
343-
// - macOS < 11.0: Intel only
344-
345-
if (targetVersion.Major >= 11) {
346-
// Supports Apple Silicon, but we need to support Intel too
347-
// For cross-platform support, we'll return the Intel instruction set
348-
// The actual instruction set will depend on the RuntimeIdentifier
349-
return "x86-64-v2"; // Conservative Intel support
350-
} else {
351-
// Intel only
342+
// For macOS and Mac Catalyst, we need to determine the instruction set based on RuntimeIdentifier
343+
// RuntimeIdentifier format: <os>-<arch> (e.g., "osx-x64", "osx-arm64", "maccatalyst-x64", "maccatalyst-arm64")
344+
345+
if (string.IsNullOrEmpty (RuntimeIdentifier)) {
346+
Log.LogMessage (MessageImportance.Low, $"RuntimeIdentifier is not set, cannot determine instruction set for {platform}");
347+
return null;
348+
}
349+
350+
var parts = RuntimeIdentifier.Split ('-');
351+
if (parts.Length < 2) {
352+
Log.LogMessage (MessageImportance.Low, $"RuntimeIdentifier '{RuntimeIdentifier}' has unexpected format");
353+
return null;
354+
}
355+
356+
var arch = parts [1];
357+
358+
// Determine instruction set based on architecture
359+
if (arch == "x64") {
360+
// Intel/AMD x64 architecture
352361
return "x86-64-v2";
362+
} else if (arch == "arm64") {
363+
// Apple Silicon
364+
return "apple-m1";
365+
} else {
366+
Log.LogMessage (MessageImportance.Low, $"Unknown architecture '{arch}' in RuntimeIdentifier '{RuntimeIdentifier}'");
367+
return null;
353368
}
354369
}
355370

tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/ComputeInstructionSetTaskTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,24 @@ public void TesttvOSInstructionSets (ApplePlatform platform, string osVersion, s
5050
}
5151

5252
[Test]
53-
[TestCase (ApplePlatform.MacOSX, "10.15", "x86-64-v2")]
54-
[TestCase (ApplePlatform.MacOSX, "11.0", "x86-64-v2")]
55-
[TestCase (ApplePlatform.MacOSX, "12.0", "x86-64-v2")]
56-
[TestCase (ApplePlatform.MacCatalyst, "14.0", "x86-64-v2")]
57-
[TestCase (ApplePlatform.MacCatalyst, "15.0", "x86-64-v2")]
58-
public void TestmacOSInstructionSets (ApplePlatform platform, string osVersion, string expectedInstructionSet)
53+
[TestCase (ApplePlatform.MacOSX, "10.15", "osx-x64", "x86-64-v2")]
54+
[TestCase (ApplePlatform.MacOSX, "11.0", "osx-x64", "x86-64-v2")]
55+
[TestCase (ApplePlatform.MacOSX, "11.0", "osx-arm64", "apple-m1")]
56+
[TestCase (ApplePlatform.MacOSX, "12.0", "osx-x64", "x86-64-v2")]
57+
[TestCase (ApplePlatform.MacOSX, "12.0", "osx-arm64", "apple-m1")]
58+
[TestCase (ApplePlatform.MacCatalyst, "14.0", "maccatalyst-x64", "x86-64-v2")]
59+
[TestCase (ApplePlatform.MacCatalyst, "14.0", "maccatalyst-arm64", "apple-m1")]
60+
[TestCase (ApplePlatform.MacCatalyst, "15.0", "maccatalyst-x64", "x86-64-v2")]
61+
[TestCase (ApplePlatform.MacCatalyst, "15.0", "maccatalyst-arm64", "apple-m1")]
62+
public void TestmacOSInstructionSets (ApplePlatform platform, string osVersion, string runtimeIdentifier, string expectedInstructionSet)
5963
{
6064
var task = CreateTask (platform);
6165
task.SupportedOSPlatformVersion = osVersion;
66+
task.RuntimeIdentifier = runtimeIdentifier;
6267

6368
ExecuteTask (task);
6469

65-
Assert.AreEqual (expectedInstructionSet, task.InstructionSet, $"Instruction set for {platform} {osVersion}");
70+
Assert.AreEqual (expectedInstructionSet, task.InstructionSet, $"Instruction set for {platform} {osVersion} with {runtimeIdentifier}");
6671
}
6772

6873
[Test]

0 commit comments

Comments
 (0)