From a33c80d6c7c6e752b02e19dab9f02b6bfb11173a Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 7 Aug 2023 17:51:37 +0200 Subject: [PATCH] [net8.0] Change the default RuntimeIdentifier. (#18495) * When publishing a mobile app, choose a device architecture (as opposed to showing an error if none was specified by the developer) * When building for the simulator, choose the arm64 runtime identifier if we're running on an arm64 machine. * When building a Debug build for desktop, choose the arm64 runtime identifier if we're running on an arm64 machine. Fixes https://github.com/xamarin/xamarin-macios/issues/18455. Fixes https://github.com/xamarin/xamarin-macios/issues/16152. Fixes https://github.com/xamarin/xamarin-macios/issues/17841. Section in the release notes: https://github.com/xamarin/xamarin-macios/wiki/.NET-8-release-notes#default-runtimeidentifiers --------- Co-authored-by: Alex Soto --- .../Xamarin.Shared.Sdk.Publish.targets | 8 ------- dotnet/targets/Xamarin.Shared.Sdk.props | 22 +++++++++++++++---- tests/common/Configuration.cs | 20 +++++++++++++++++ tests/dotnet/UnitTests/PostBuildTest.cs | 6 +---- tests/dotnet/UnitTests/TestBaseClass.cs | 13 +++++++---- .../xharness/Jenkins/TestVariationsFactory.cs | 11 ++++++++-- 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/dotnet/targets/Xamarin.Shared.Sdk.Publish.targets b/dotnet/targets/Xamarin.Shared.Sdk.Publish.targets index 07d03e1878..44d97039ff 100644 --- a/dotnet/targets/Xamarin.Shared.Sdk.Publish.targets +++ b/dotnet/targets/Xamarin.Shared.Sdk.Publish.targets @@ -10,14 +10,6 @@ $(PublishDir) - - - true - iossimulator-x64 - tvossimulator-x64 + + <_IsArm64Machine Condition="'$(_IsArm64Machine)' == '' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'arm64'">true + <_IsArm64Machine Condition="'$(_IsArm64Machine)' == ''">false + + + ios-arm64 + iossimulator-arm64 + iossimulator-x64 + + tvos-arm64 + tvossimulator-arm64 + tvossimulator-x64 + + - osx-x64 - maccatalyst-x64 + osx-arm64 + osx-x64 + maccatalyst-arm64 + maccatalyst-x64 osx-x64;osx-arm64 maccatalyst-x64;maccatalyst-arm64 diff --git a/tests/common/Configuration.cs b/tests/common/Configuration.cs index fef4fc9dee..b9eeed7d6f 100644 --- a/tests/common/Configuration.cs +++ b/tests/common/Configuration.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading; @@ -1193,5 +1194,24 @@ namespace Xamarin.Tests { Thread.Sleep (1000); } + // Return true if the current machine can run ARM64 binaries. + static bool? canRunArm64; + public static bool CanRunArm64 { + get { + if (!canRunArm64.HasValue) { + int rv = 0; + IntPtr size = (IntPtr) sizeof (int); + if (sysctlbyname ("hw.optional.arm64", ref rv, ref size, IntPtr.Zero, IntPtr.Zero) == 0) { + canRunArm64 = rv == 1; + } else { + canRunArm64 = false; + } + } + return canRunArm64.Value; + } + } + + [DllImport ("libc")] + static extern int sysctlbyname (string name, ref int value, ref IntPtr size, IntPtr zero, IntPtr zeroAgain); } } diff --git a/tests/dotnet/UnitTests/PostBuildTest.cs b/tests/dotnet/UnitTests/PostBuildTest.cs index f436c3d29c..93c83066d9 100644 --- a/tests/dotnet/UnitTests/PostBuildTest.cs +++ b/tests/dotnet/UnitTests/PostBuildTest.cs @@ -159,9 +159,7 @@ namespace Xamarin.Tests { [TestCase (ApplePlatform.iOS, "iossimulator-x64")] [TestCase (ApplePlatform.iOS, "iossimulator-x86")] [TestCase (ApplePlatform.iOS, "iossimulator-x64;iossimulator-x64")] - [TestCase (ApplePlatform.iOS, "")] [TestCase (ApplePlatform.TVOS, "tvossimulator-x64")] - [TestCase (ApplePlatform.TVOS, "")] public void PublishFailureTest (ApplePlatform platform, string runtimeIdentifiers) { var project = "MySimpleApp"; @@ -197,9 +195,7 @@ namespace Xamarin.Tests { var errors = BinLog.GetBuildLogErrors (rv.BinLogPath).ToArray (); Assert.AreEqual (1, errors.Length, "Error Count"); string expectedErrorMessage; - if (string.IsNullOrEmpty (runtimeIdentifiers)) { - expectedErrorMessage = $"A runtime identifier must be specified in order to publish this project."; - } else if (runtimeIdentifiers.IndexOf (';') >= 0) { + if (runtimeIdentifiers.IndexOf (';') >= 0) { expectedErrorMessage = $"A runtime identifier for a device architecture must be specified in order to publish this project. '{runtimeIdentifiers}' are simulator architectures."; } else { expectedErrorMessage = $"A runtime identifier for a device architecture must be specified in order to publish this project. '{runtimeIdentifiers}' is a simulator architecture."; diff --git a/tests/dotnet/UnitTests/TestBaseClass.cs b/tests/dotnet/UnitTests/TestBaseClass.cs index 6c7346be33..d63a446474 100644 --- a/tests/dotnet/UnitTests/TestBaseClass.cs +++ b/tests/dotnet/UnitTests/TestBaseClass.cs @@ -73,15 +73,20 @@ namespace Xamarin.Tests { protected string GetDefaultRuntimeIdentifier (ApplePlatform platform, string configuration = "Debug") { + var arch = Configuration.CanRunArm64 ? "arm64" : "x64"; switch (platform) { case ApplePlatform.iOS: - return "iossimulator-x64"; + return $"iossimulator-{arch}"; case ApplePlatform.TVOS: - return "tvossimulator-x64"; + return $"tvossimulator-{arch}"; case ApplePlatform.MacOSX: - return "Release".Equals (configuration, StringComparison.OrdinalIgnoreCase) ? "osx-x64;osx-arm64" : "osx-x64"; + if ("Release".Equals (configuration, StringComparison.OrdinalIgnoreCase)) + return "osx-x64;osx-arm64"; + return $"osx-{arch}"; case ApplePlatform.MacCatalyst: - return "Release".Equals (configuration, StringComparison.OrdinalIgnoreCase) ? "maccatalyst-x64;maccatalyst-arm64" : "maccatalyst-x64"; + if ("Release".Equals (configuration, StringComparison.OrdinalIgnoreCase)) + return "maccatalyst-x64;maccatalyst-arm64"; + return $"maccatalyst-{arch}"; default: throw new ArgumentOutOfRangeException ($"Unknown platform: {platform}"); } diff --git a/tests/xharness/Jenkins/TestVariationsFactory.cs b/tests/xharness/Jenkins/TestVariationsFactory.cs index 3ef390bdf8..9954bb966a 100644 --- a/tests/xharness/Jenkins/TestVariationsFactory.cs +++ b/tests/xharness/Jenkins/TestVariationsFactory.cs @@ -35,21 +35,28 @@ namespace Xharness.Jenkins { var ignore = test.TestProject.Ignore; var mac_supports_arm64 = Harness.CanRunArm64; var arm64_runtime_identifier = string.Empty; + var x64_runtime_identifier = string.Empty; var arm64_sim_runtime_identifier = string.Empty; + var x64_sim_runtime_identifier = string.Empty; switch (test.Platform) { case TestPlatform.Mac: arm64_runtime_identifier = "osx-arm64"; + x64_runtime_identifier = "osx-x64"; break; case TestPlatform.MacCatalyst: arm64_runtime_identifier = "maccatalyst-arm64"; + x64_runtime_identifier = "maccatalyst-x64"; break; case TestPlatform.iOS: case TestPlatform.iOS_Unified: + case TestPlatform.iOS_Unified64: arm64_sim_runtime_identifier = "iossimulator-arm64"; + x64_sim_runtime_identifier = "iossimulator-x64"; break; case TestPlatform.tvOS: arm64_sim_runtime_identifier = "tvossimulator-arm64"; + x64_sim_runtime_identifier = "tvossimulator-x64"; break; } @@ -136,7 +143,7 @@ namespace Xharness.Jenkins { if (test.TestProject.IsDotNetProject) { yield return new TestData { Variation = "Debug (managed static registrar)", Registrar = "managed-static", Debug = true, Profiling = false, Ignored = ignore }; yield return new TestData { Variation = "Release (managed static registrar, all optimizations)", BundlerArguments = "--optimize:all", Registrar = "managed-static", Debug = false, Profiling = false, LinkMode = "Full", Defines = "OPTIMIZEALL", Ignored = ignore }; - yield return new TestData { Variation = "Release (NativeAOT)", Debug = false, PublishAot = true, Ignored = ignore, Defines = "NATIVEAOT", LinkMode = "Full" }; + yield return new TestData { Variation = "Release (NativeAOT, x64)", Debug = false, PublishAot = true, Ignored = ignore, Defines = "NATIVEAOT", RuntimeIdentifier = x64_sim_runtime_identifier, LinkMode = "Full" }; } break; case "introspection": @@ -176,7 +183,7 @@ namespace Xharness.Jenkins { // yield return new TestData { Variation = "Release (NativeAOT)", Debug = false, PublishAot = true, Ignored = !jenkins.TestSelection.IsEnabled (TestLabel.Monotouch) || !jenkins.TestSelection.IsEnabled (PlatformLabel.MacCatalyst), Defines = "NATIVEAOT", LinkMode = "Full" }; yield return new TestData { Variation = "Release (NativeAOT, ARM64)", Debug = false, PublishAot = true, Ignored = !jenkins.TestSelection.IsEnabled (TestLabel.Monotouch) || !jenkins.TestSelection.IsEnabled (PlatformLabel.MacCatalyst) || !mac_supports_arm64, Defines = "NATIVEAOT", RuntimeIdentifier = arm64_runtime_identifier, LinkMode = "Full" }; - yield return new TestData { Variation = "Release (NativeAOT, x64)", Debug = false, PublishAot = true, Ignored = !jenkins.TestSelection.IsEnabled (TestLabel.Monotouch) || !jenkins.TestSelection.IsEnabled (PlatformLabel.MacCatalyst), Defines = "NATIVEAOT", LinkMode = "Full", RuntimeIdentifier = "maccatalyst-x64" }; + yield return new TestData { Variation = "Release (NativeAOT, x64)", Debug = false, PublishAot = true, Ignored = !jenkins.TestSelection.IsEnabled (TestLabel.Monotouch) || !jenkins.TestSelection.IsEnabled (PlatformLabel.MacCatalyst), Defines = "NATIVEAOT", LinkMode = "Full", RuntimeIdentifier = x64_runtime_identifier }; } if (test.Platform == TestPlatform.Mac) { yield return new TestData { Variation = "Release", Debug = false, Ignored = !jenkins.TestSelection.IsEnabled (TestLabel.Monotouch) || !jenkins.TestSelection.IsEnabled (PlatformLabel.Mac) };