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

Conversation

rolfbjarne
Copy link
Member

@rolfbjarne rolfbjarne commented Jan 25, 2023

Add support for using NativeAOT on all our platforms.

This contains numerous changes in a lot of places to add support for NativeAOT:

  • build logic
  • runtime
  • managed code
  • tests

And it all pretty much consists of special-casing NativeAOT everywhere we need to.

Note: NativeAOT doesn't work on macOS yet, because a dotnet/runtime fix is required, and thus the corresponding test variations for monotouch-test have been commented out.

This PR is best reviewed commit-by-commit.

This contributes towards #17339.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne changed the base branch from main to net8.0 April 28, 2023 09:59
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@ivanpovazan
Copy link
Contributor

FWIW here are adaptations that work for building a MAUI template app with NativeAOT on a iOS device
https://github.com/ivanpovazan/xamarin-macios/tree/net8.0-msr-naot combined with dotnet/runtime#85996
(credits to @filipnavara 🚀 )

@ivanpovazan
Copy link
Contributor

@rolfbjarne we should consider adding something like this in the native linker configuration: dotnet/runtime#86707 (comment)
It shaves off 20Mb of zeroes from the binary for NativeAOT builds.

@rolfbjarne
Copy link
Member Author

@rolfbjarne we should consider adding something like this in the native linker configuration: dotnet/runtime#86707 (comment) It shaves off 20Mb of zeroes from the binary for NativeAOT builds.

I've filed #18332 to look into this.

@github-actions

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@github-actions

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

…hen using NativeAOT (and warn if someone asks otherwise).
This is because NativeAOT contains swift code, and we'd have to add code to
embed the Swift libraries in any apps that target early OS versions. We could
eventually implement this, but let's wait and see if there's a demand first.
This matches how NativeAOT works for other platforms.
…7 tests.

We had to bump NUnitLite and Touch.Unit's TargetFramework properties to
net8.0-* (otherwise projects using NativeAOT fails if these projects are
referenced), which means they can't be used from .NET 7 (aka our .NET 7
tests).

So just don't reference NUnitLite and Touch.Unit in this test.
We can fix this better once this fix reaches us:

    dotnet/runtime#87813

because then we can set the ICU data file at build time (to a relative path).
@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: b4e3361a10ce6856d77d6ee9c9df7460489eefe6 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

📚 [PR Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

❌ [PR Build] Tests on macOS M1 - Mac Ventura (13.0) failed ❌

Failed tests are:

  • linksdk
  • monotouch-test

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

❌ [PR Build] Tests on macOS M1 - Mac Big Sur (11.5) failed ❌

Failed tests are:

  • linksdk
  • monotouch-test

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

✅ API diff vs stable

.NET (No breaking changes)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: b4e3361a10ce6856d77d6ee9c9df7460489eefe6 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [CI Build] Test results 🔥

Test results

❌ Tests failed on VSTS: simulator tests

0 tests crashed, 1 tests failed, 91 tests passed.

Failures

❌ dotnettests tests [attempt 2]

1 tests failed, 0 tests passed.
  • DotNet tests: Failed (Execution failed with exit code 1)

Html Report (VSDrops) Download

Successes

⚠️ bcl: No tests selected. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ framework: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 1 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 4 tests passed. Html Report (VSDrops) Download
⚠️ install_source: No tests selected. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker: All 40 tests passed. Html Report (VSDrops) Download
⚠️ mac_binding_project: No tests selected. Html Report (VSDrops) Download
⚠️ mmp: No tests selected. Html Report (VSDrops) Download
⚠️ mononative: No tests selected. Html Report (VSDrops) Download
✅ monotouch: All 26 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
⚠️ mtouch: No tests selected. Html Report (VSDrops) Download
⚠️ xammac: No tests selected. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: b4e3361a10ce6856d77d6ee9c9df7460489eefe6 [PR build]

return IntPtr.Zero;

string location;
if (IsNativeAOT) {
Copy link
Contributor

Choose a reason for hiding this comment

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

When I read this, it raises some questions to me: what is wrong with asm?.Location in the IsNativeAOT case?
Docs say that this is a path unless it was loaded from a byte array in which case it will be "". Is that the case here? Because if it is, maybe the test should be if it's empty string which would catch more cases maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

Assembly.Location doesn't return anything meaningful when using NativeAOT, because there's no assembly on disk.

Additionally, the NativeAOT toolchain will show a warning if Assembly.Location is called, so we have to avoid it if we want to be warning-free (these warnings will show up for every customer as well, so it's much better to just avoid the warning instead of having the code path that copes with whatever it returns + the warning.

Copy link
Contributor

Choose a reason for hiding this comment

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

NativeAOT doesn't have the assemblies as files, similar to the single-file CoreCLR deployment mode. They are all linked together into the same executable.

Here's one place where it is documented: https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli#api-incompatibility

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, here is another related workaround for NativeAOT which explains the limitations in the PR description: xamarin/Touch.Unit#116 (comment)

@rolfbjarne
Copy link
Member Author

Test failures are unrelated.

@rolfbjarne rolfbjarne merged commit 9b72a75 into xamarin:net8.0 Jun 22, 2023
@rolfbjarne rolfbjarne deleted the nativeaot-bridge branch June 22, 2023 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants