[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 <alex@alexsoto.me>
This commit is contained in:
Rolf Bjarne Kvinge 2023-08-07 17:51:37 +02:00 коммит произвёл GitHub
Родитель d9150f63ea
Коммит a33c80d6c7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 57 добавлений и 23 удалений

Просмотреть файл

@ -10,14 +10,6 @@
<PkgPackageDir Condition="'$(PkgPackageDir)' == '' And '$(PkgPackagePath)' == ''">$(PublishDir)</PkgPackageDir>
</PropertyGroup>
<!-- Unfortunately we can't set a default runtime identifier when publishing, because by the time we know we're publishing,
it's too late to change the runtime identifier. This means that we'll have to make it mandatory to specify a runtime
identifier when publishing for a mobile platform (iOS, tvOS), because the default runtime identifier is for the simulator. -->
<Error
Text="A runtime identifier must be specified in order to publish this project."
Condition="'$(_XamarinUsingDefaultRuntimeIdentifier)' == 'true' And ('$(_PlatformName)' == 'iOS' Or '$(_PlatformName)' == 'tvOS')"
/>
<!-- It's mandatory to specify a runtime identifier for device when publishing for a mobile platform (iOS, tvOS). -->
<Error
Text="A runtime identifier for a device architecture must be specified in order to publish this project. '$(RuntimeIdentifier)' is a simulator architecture."

Просмотреть файл

@ -77,12 +77,26 @@
<_XamarinUsingDefaultRuntimeIdentifier Condition="'$(RuntimeIdentifier)' == ''">true</_XamarinUsingDefaultRuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(_PlatformName)' == 'iOS'">iossimulator-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(_PlatformName)' == 'tvOS'">tvossimulator-x64</RuntimeIdentifier>
<!-- Figure out which architecture we're running on -->
<_IsArm64Machine Condition="'$(_IsArm64Machine)' == '' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'arm64'">true</_IsArm64Machine>
<_IsArm64Machine Condition="'$(_IsArm64Machine)' == ''">false</_IsArm64Machine>
<!-- for mobile non-publish builds we default to the simulator (and the host mac's architecture) -->
<!-- for mobile publish builds we default to the device architecture -->
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(_PlatformName)' == 'iOS' And '$(_IsPublishing)' == 'true'">ios-arm64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(_PlatformName)' == 'iOS' And '$(_IsPublishing)' != 'true' And '$(_IsArm64Machine)' == 'true'">iossimulator-arm64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(_PlatformName)' == 'iOS' And '$(_IsPublishing)' != 'true' And '$(_IsArm64Machine)' != 'true'">iossimulator-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(_PlatformName)' == 'tvOS' And '$(_IsPublishing)' == 'true'">tvos-arm64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(_PlatformName)' == 'tvOS' And '$(_IsPublishing)' != 'true' And '$(_IsArm64Machine)' == 'true'">tvossimulator-arm64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(_PlatformName)' == 'tvOS' And '$(_IsPublishing)' != 'true' And '$(_IsArm64Machine)' != 'true'">tvossimulator-x64</RuntimeIdentifier>
<!-- For debug desktop builds we default to the host architecture -->
<!-- For release desktop builds we default to universal apps in .NET 7+ -->
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(Configuration)' != 'Release' And '$(_PlatformName)' == 'macOS'">osx-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(Configuration)' != 'Release' And '$(_PlatformName)' == 'MacCatalyst'">maccatalyst-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(Configuration)' != 'Release' And '$(_PlatformName)' == 'macOS' And '$(_IsArm64Machine)' == 'true'">osx-arm64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(Configuration)' != 'Release' And '$(_PlatformName)' == 'macOS' And '$(_IsArm64Machine)' != 'true'">osx-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(Configuration)' != 'Release' And '$(_PlatformName)' == 'MacCatalyst' And '$(_IsArm64Machine)' == 'true'">maccatalyst-arm64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' And '$(Configuration)' != 'Release' And '$(_PlatformName)' == 'MacCatalyst' And '$(_IsArm64Machine)' != 'true'">maccatalyst-x64</RuntimeIdentifier>
<RuntimeIdentifiers Condition="'$(RuntimeIdentifier)' == '' And '$(Configuration)' == 'Release' And '$(_PlatformName)' == 'macOS'">osx-x64;osx-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers Condition="'$(RuntimeIdentifier)' == '' And '$(Configuration)' == 'Release' And '$(_PlatformName)' == 'MacCatalyst'">maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers>

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -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.";

Просмотреть файл

@ -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}");
}

Просмотреть файл

@ -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) };