Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using NativeAOT. #17374

Merged
merged 46 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d605221
Bump MonoTouch.Dialog and Touch.Unit.
rolfbjarne Jun 21, 2023
f964b5a
[runtime] Create a version of our runtime that can be used with Nativ…
rolfbjarne Jun 21, 2023
a766901
[dotnet] Choose the correct runtime library depending on which runtim…
rolfbjarne Jun 21, 2023
7f15d86
[xharness] Add a NativeAOT variation of monotouch-test.
rolfbjarne Jun 21, 2023
d584840
[dotnet] Force the managed static registrar when using NativeAot.
rolfbjarne Jun 21, 2023
607cc78
[dotnet] Use the correct package infix for NativeAOT
rolfbjarne Jun 21, 2023
ca6f2e4
[Foundation] Use the correct toggle ref implementation on !macOS sinc…
rolfbjarne Jun 21, 2023
94c96ec
[dotnet] Don't run Mono's AOT compiler if we're using NativeAOT.
rolfbjarne Jun 21, 2023
a3b466b
[tools] Don't allow keeping the dynamic registrar when using NativeAOT.
rolfbjarne Jun 21, 2023
ab8f928
[tools] Don't call mono_jit_set_aot_mode from main when using NativeAOT
rolfbjarne Jun 21, 2023
60e4936
[tools] Don't generate calls to mono_aot_register_module in main when…
rolfbjarne Jun 21, 2023
9b24c5d
[runtime] NativeAOT does not support dynamic registration
rolfbjarne Jun 21, 2023
d5a53d6
[runtime] Add a native IsNativeAOT flag.
rolfbjarne Jun 21, 2023
b4a67c6
[src] Make the Runtime.IsNativeAOT property a constant in the linker.
rolfbjarne Jun 21, 2023
e0e4b77
[dotnet] Set AutoreleasePoolSupport earlier so that the linker doesn'…
rolfbjarne Jun 21, 2023
901be39
[ObjCRuntime] Avoid using Assembly.Location for NativeAOT, use an alt…
rolfbjarne Jun 21, 2023
3105214
[tests] Detect NativeAOT correctly in the Symbols test
rolfbjarne Jun 21, 2023
5b8d60b
[tests] Adjust RegistrarTest to always expected the dynamic registrar…
rolfbjarne Jun 21, 2023
890d3ca
[tests] Adjust asserts since some collection assert doesn't work with…
rolfbjarne Jun 21, 2023
0db2c5c
[dotnet] Rework feature detection and notification a bit.
rolfbjarne Jun 21, 2023
595bcdb
[dotnet] Don't call ComputeResolvedFilesToPublishList when using Nati…
rolfbjarne Jun 21, 2023
13fa769
[dotnet] Disable our own IL stripping when using NativeAOT, because N…
rolfbjarne Jun 21, 2023
b36db05
[dotnet] Call ILC, the NativeAOT compiler, before we link the native …
rolfbjarne Jun 21, 2023
00b2865
[dotnet] Adjust ILC's build dependencies to cope with the fact that I…
rolfbjarne Jun 21, 2023
8cfee8b
[dotnet] Ask ILC to treat all P/Invokes to __Internal as direct P/Inv…
rolfbjarne Jun 21, 2023
6bd8915
[dotnet] Rearrange items in item groups so that ILC gets the output f…
rolfbjarne Jun 21, 2023
3977493
[dotnet] Ask ILC to process all unmanaged callers only methods from a…
rolfbjarne Jun 21, 2023
79b7657
[dotnet] Ask ILC to only trim assemblies if trimming is explicitly en…
rolfbjarne Jun 21, 2023
c48d583
[dotnet] Link the output from ILC into the native executable
rolfbjarne Jun 21, 2023
2ee5326
[dotnet] Collect all the native linker arguments ILC would have used …
rolfbjarne Jun 21, 2023
617f64e
[dotnet] Enforce that the linker must be enabled for all assemblies w…
rolfbjarne Jun 21, 2023
d601e51
[dotnet] Set PublishAotUsingRuntimePack=true when using NativeAOT to …
rolfbjarne Jun 21, 2023
183c76a
[dotnet] Use a different min OS version when building with NativeAOT.
rolfbjarne Jun 21, 2023
e3228d0
[dotnet] Turn off some publishing behavior we don't need
rolfbjarne Jun 21, 2023
a1d707c
[dotnet] Fix items in TrimmerRootAssembly to follow docs and what ILC…
rolfbjarne Jun 21, 2023
0adbf77
[tests] Fix computing when the dynamic registrar is used.
rolfbjarne Jun 21, 2023
7d65804
[dotnet/tests] Only use NativeAOT when publishing.
rolfbjarne Jun 21, 2023
97b91f1
[dotnet] Remove libSystem.Net.Security.Native from tvOS builds when u…
rolfbjarne Jun 21, 2023
c49888b
[tests] Adjust ConfigTest to cope with different behavior when using …
rolfbjarne Jun 21, 2023
264be70
[dotnet] Disable SelfContained for the outer build of a universal app…
rolfbjarne Jun 21, 2023
8ac35ba
[tests] Disable the NUnitLite and Touch.Unit references for the .NET …
rolfbjarne Jun 21, 2023
e31b0a6
[xharness] Disable NativeAOT test variations for macOS for now, we're…
rolfbjarne Jun 21, 2023
94cdec2
[xharness] Disable NativeAOT test variations for Mac Catalyst for now…
rolfbjarne Jun 21, 2023
42d6121
[dotnet] Set a flag to avoid a bug with the NativeAOT compiler
rolfbjarne Jun 21, 2023
3c912d7
[dotnet] Treat the ICU data file as a resource instead of an assembly.
rolfbjarne Jun 21, 2023
b4e3361
[dotnet] Tell ILC about any satellite assemblies.
rolfbjarne Jun 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
[submodule "external/Touch.Unit"]
path = external/Touch.Unit
url = ../../spouliot/Touch.Unit.git
branch = main
branch = net8.0
[submodule "external/Xamarin.MacDev"]
path = external/Xamarin.MacDev
url = ../../xamarin/Xamarin.MacDev
branch = main
[submodule "external/MonoTouch.Dialog"]
path = external/MonoTouch.Dialog
url = ../../migueldeicaza/MonoTouch.Dialog
branch = dotnet
branch = net8.0
[submodule "api-tools"]
path = external/api-tools
url = ../../rolfbjarne/api-tools
Expand Down
10 changes: 10 additions & 0 deletions Make.config
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ DOTNET_MIN_TVOS_SDK_VERSION=11.0
DOTNET_MIN_MACCATALYST_SDK_VERSION=13.1
DOTNET_MIN_MACOS_SDK_VERSION=10.15

# Minimum OS versions when using NativeOAT - these are at least the general min versions above (but may be higher).
DOTNET_MIN_NATIVEAOT_IOS_SDK_VERSION=12.2
DOTNET_MIN_NATIVEAOT_TVOS_SDK_VERSION=12.2
DOTNET_MIN_NATIVEAOT_MACCATALYST_SDK_VERSION=13.1
DOTNET_MIN_NATIVEAOT_MACOS_SDK_VERSION=10.15
Comment on lines +276 to +280
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this? (Not sure which dotnet/runtime -> dotnet/sdk is ingested at the moment but the Swift workaround should not be necessary anymore.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filipnavara do you know the PR/commit that fixed it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed to look at it after this PR is in: #18477


# The min simulator version available in the Xcode we're using
MIN_IOS_SIMULATOR_VERSION=13.7
MIN_WATCHOS_SIMULATOR_VERSION=7.0
Expand Down Expand Up @@ -682,6 +688,7 @@ DOTNET_PLATFORMS=
ifdef INCLUDE_IOS
DOTNET_PLATFORMS+=iOS
DOTNET_IOS_BITNESSES+=64
DOTNET_NATIVEAOT_PLATFORMS+=iOS

# 32-bit architectures
ifdef IOS_SUPPORTS_32BIT_ARCHITECTURES
Expand All @@ -705,6 +712,7 @@ endif # INCLUDE_IOS
ifdef INCLUDE_TVOS
DOTNET_PLATFORMS+=tvOS
DOTNET_TVOS_BITNESSES+=64
DOTNET_NATIVEAOT_PLATFORMS+=tvOS
ifdef INCLUDE_DEVICE
DOTNET_TVOS_RUNTIME_IDENTIFIERS=tvos-arm64 tvossimulator-x64 tvossimulator-arm64
else
Expand All @@ -728,6 +736,7 @@ endif
ifdef INCLUDE_MACCATALYST
DOTNET_PLATFORMS+=MacCatalyst
DOTNET_MACCATALYST_BITNESSES+=64
DOTNET_NATIVEAOT_PLATFORMS+=MacCatalyst
DOTNET_MACCATALYST_RUNTIME_IDENTIFIERS=maccatalyst-x64 maccatalyst-arm64
DOTNET_MACCATALYST_RUNTIME_IDENTIFIERS_64+=$(DOTNET_MACCATALYST_RUNTIME_IDENTIFIERS)
endif
Expand All @@ -736,6 +745,7 @@ ifdef INCLUDE_MAC
DOTNET_PLATFORMS+=macOS
DOTNET_CORECLR_PLATFORMS+=macOS
DOTNET_MACOS_BITNESSES+=64
DOTNET_NATIVEAOT_PLATFORMS+=macOS
DOTNET_MACOS_RUNTIME_IDENTIFIERS=osx-x64 osx-arm64
DOTNET_MACOS_RUNTIME_IDENTIFIERS_64=$(DOTNET_MACOS_RUNTIME_IDENTIFIERS)
endif
Expand Down
20 changes: 16 additions & 4 deletions dotnet/generate-target-platforms.csharp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ var doc = new XmlDocument ();
doc.Load (plistPath);
var nodes = doc.SelectNodes ($"/plist/dict/key[text()='KnownVersions']/following-sibling::dict[1]/key[text()='{platform}']/following-sibling::array[1]/string");

var allLines = File.ReadAllLines ("../Make.config");

var minSdkVersionName = $"DOTNET_MIN_{platform.ToUpper ()}_SDK_VERSION";
var minSdkVersionString = File.ReadAllLines ("../Make.config").Single (v => v.StartsWith (minSdkVersionName + "=", StringComparison.Ordinal)).Substring (minSdkVersionName.Length + 1);
var minSdkVersionString = allLines.Single (v => v.StartsWith (minSdkVersionName + "=", StringComparison.Ordinal)).Substring (minSdkVersionName.Length + 1);
var minSdkVersion = Version.Parse (minSdkVersionString);

var minNativeAotSdkVersionName = $"DOTNET_MIN_NATIVEAOT_{platform.ToUpper ()}_SDK_VERSION";
var minNativeAotSdkVersionString = allLines.Single (v => v.StartsWith (minNativeAotSdkVersionName + "=", StringComparison.Ordinal)).Substring (minNativeAotSdkVersionName.Length + 1);
var minNativeAotSdkVersion = Version.Parse (minNativeAotSdkVersionString);

using (TextWriter writer = new StreamWriter (outputPath)) {
writer.WriteLine ($"<!-- This file contains a generated list of the {platform} platform versions that are supported for this SDK -->");
writer.WriteLine ($"<!-- Generation script: https://github.com/xamarin/xamarin-macios/blob/main/dotnet/generate-target-platforms.csharp -->");
Expand All @@ -36,17 +42,23 @@ using (TextWriter writer = new StreamWriter (outputPath)) {

foreach (XmlNode n in nodes) {
var version = n.InnerText;
if (Version.Parse (version) < minSdkVersion)
var parsedVersion = Version.Parse (version);
if (parsedVersion < minSdkVersion)
continue;
writer.WriteLine ($"\t\t<{platform}SdkSupportedTargetPlatformVersion Include=\"{n.InnerText}\" />");
if (parsedVersion < minNativeAotSdkVersion) {
writer.WriteLine ($"\t\t<{platform}SdkSupportedTargetPlatformVersion Include=\"{n.InnerText}\" Condition=\"!('$(PublishAot)' == 'true' And '$(_IsPublishing)' == 'true')\" />");
} else {
writer.WriteLine ($"\t\t<{platform}SdkSupportedTargetPlatformVersion Include=\"{n.InnerText}\" />");
}
}

writer.WriteLine ("\t</ItemGroup>");
writer.WriteLine ("\t<ItemGroup>");
writer.WriteLine ($"\t\t<SdkSupportedTargetPlatformVersion Condition=\"'$(TargetPlatformIdentifier)' == '{platform}'\" Include=\"@({platform}SdkSupportedTargetPlatformVersion)\" />");
writer.WriteLine ("\t</ItemGroup>");
writer.WriteLine ("\t<PropertyGroup>");
writer.WriteLine ($"\t\t<{platform}MinSupportedOSPlatformVersion>{minSdkVersionString}</{platform}MinSupportedOSPlatformVersion>");
writer.WriteLine ($"\t\t<{platform}MinSupportedOSPlatformVersion Condition=\"!('$(PublishAot)' == 'true' And '$(_IsPublishing)' == 'true')\">{minSdkVersionString}</{platform}MinSupportedOSPlatformVersion>");
writer.WriteLine ($"\t\t<{platform}MinSupportedOSPlatformVersion Condition=\"'$(PublishAot)' == 'true' And '$(_IsPublishing)' == 'true'\">{minNativeAotSdkVersionString}</{platform}MinSupportedOSPlatformVersion>");
writer.WriteLine ("\t</PropertyGroup>");
writer.WriteLine ("</Project>");
}
Expand Down
11 changes: 10 additions & 1 deletion dotnet/targets/Xamarin.Shared.Sdk.DefaultItems.targets
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@
<EnableDefaultmacOSItems Condition=" '$(_PlatformName)' == 'macOS' And '$(EnableDefaultmacOSItems)' == '' ">$(EnableDefaultItems)</EnableDefaultmacOSItems>
<EnableDefaultMacCatalystItems Condition=" '$(_PlatformName)' == 'MacCatalyst' And '$(EnableDefaultMacCatalystItems)' == '' ">$(EnableDefaultItems)</EnableDefaultMacCatalystItems>

<UseMonoRuntime Condition=" '$(UseMonoRuntime)' == '' And '$(_PlatformName)' != 'macOS'">true</UseMonoRuntime>
<!--
PublishAot should only take effect when doing 'dotnet publish', not when doing 'dotnet build'. We distinguish these cases using the '_IsPublishing' property,
but it's rather annoying to always have to check both PublishAot and _IsPublishing to see if we're using NativeAOT, so introduce a third property that's
only set to true if both PublishAot=true and _IsPublishing=true
-->
<_UseNativeAot Condition="'$(PublishAot)' == 'true' And '$(_IsPublishing)' == 'true'">true</_UseNativeAot>

<UseMonoRuntime Condition=" '$(UseMonoRuntime)' == '' And '$(_PlatformName)' == 'macOS'">false</UseMonoRuntime>
<UseMonoRuntime Condition=" '$(UseMonoRuntime)' == '' And '$(_UseNativeAot)' == 'true'">false</UseMonoRuntime>
<UseMonoRuntime Condition=" '$(UseMonoRuntime)' == ''">true</UseMonoRuntime>

</PropertyGroup>

<ItemGroup>
Expand Down
39 changes: 39 additions & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@

<!-- Explicitly export symbols using clang command-line option "-exported_symbols_list export_list" -->
<_ExportSymbolsExplicitly Condition="'$(_ExportSymbolsExplicitly)' == ''">true</_ExportSymbolsExplicitly>

<!--
Some runtime libraries feature switches defaults that need to be set early
Available feature switches: https://github.com/dotnet/runtime/blob/master/docs/workflow/trimming/feature-switches.md
-->
<!-- AutoreleasePoolSupport needs to be set earlier than other switches, so that illink doesn't override it - https://github.com/dotnet/runtime/pull/86753 - so it's set here, instead of in Xamarin.Shared.Sdk.targets -->
<AutoreleasePoolSupport Condition="'$(AutoreleasePoolSupport)' == ''">true</AutoreleasePoolSupport>
</PropertyGroup>

<!-- Set the default RuntimeIdentifier if not already specified. -->
Expand Down Expand Up @@ -95,6 +102,20 @@
<SelfContained>true</SelfContained>
</PropertyGroup>

<!--

SelfContained is automatically enabled if PublishAot is true, and that
doesn't work properly (restore fails because RuntimeIdentifier is not
set) when doing the outer build of a universal apps (when
RuntimeIdentifier=''), so manually disable SelfContained in that case.

This might not be necessary after: https://github.com/dotnet/sdk/pull/33229

-->
<PropertyGroup Condition="'$(PublishAot)' == 'true' And '$(_IsPublishing)' == 'true' And '$(RuntimeIdentifiers)' != '' And '$(RuntimeIdentifier)' == '' And '$(SelfContained)' == ''">
<SelfContained>false</SelfContained>
</PropertyGroup>

<!--
Enable LLVM by default for mobile release builds.

Expand All @@ -105,4 +126,22 @@
<PropertyGroup Condition="'$(MtouchUseLlvm)' == '' And '$(Configuration)' == 'Release' And ('$(_PlatformName)' == 'iOS' Or '$(_PlatformName)' == 'tvOS')">
<MtouchUseLlvm>true</MtouchUseLlvm>
</PropertyGroup>

<!-- Various options when using NativeAOT -->
<PropertyGroup Condition="'$(PublishAot)' == 'true' And '$(_IsPublishing)' == 'true'">
<!-- Disable our own assembly IL stripping logic, because ILC does that already -->
<EnableAssemblyILStripping>false</EnableAssemblyILStripping>

<!-- We're using our own native main function when using NativeAOT -->
<CustomNativeMain>true</CustomNativeMain>

<!-- We must find the BCL libraries using the runtime pack instead of using the built-in NativeAOT BCL -->
<PublishAotUsingRuntimePack>true</PublishAotUsingRuntimePack>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that to deploy an application with NativeAOT one should only include -p:PublishAot=true when publishing:

  • via .NET CLI:
    dotnet publish -f net8.0-ios -c Release -r ios-arm64 --self-contained -p:PublishAot=true
  • via .csproj
    <PropertyGroup Condition="'$(Configuration)' == 'Release'">
        <PublishAot>true</PublishAot>
    </PropertyGroup>

in order to switch between Mono and NativeAOT deployments?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a plan to use PublishAot for Mono as well (dotnet/sdk#25392) so there'd be another property to choose between Mono/NativeAOT

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you can set PublishAot always, but it'll only take effect when using dotnet publish (to match how PublishAot works on other platforms).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you can set PublishAot always, but it'll only take effect when using dotnet publish (to match how PublishAot works on other platforms).

Does that mean that if the project file is configured with:

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <PublishAot>true</PublishAot>
</PropertyGroup>

doing dotnet build -c Release will use MonoAOT, while dotnet publish -c Release will use NativeAOT, or I misunderstood this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and you don't even need the condition:

<PropertyGroup>
    <PublishAot>true</PublishAot>
</PropertyGroup>

This will use MonoAOT:

dotnet build

While this will use NativeAOT:

dotnet publish


<!-- This turns off some NativeAOT logic we don't want nor need -->
<NativeCompilationDuringPublish>false</NativeCompilationDuringPublish>

<!-- This works around an issue in NativeAOT: https://github.com/dotnet/runtime/issues/86186 -->
<IlcKeepManagedDebuggerSupport>true</IlcKeepManagedDebuggerSupport>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IlcKeepManagedDebuggerSupport should be fixed in latest SDK, and this should be unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried earlier today, and things were still broken, so I put the workaround back in for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotnet/runtime@652433b (for reference). There is some arcade blockage in dotnet/runtime (still not fully resolved) so I imagine it may not have flown through yet...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed to look at it after this PR is in: #18478

</PropertyGroup>
</Project>
Loading