Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[Proposal] Add Support for NativeAOT

Open TheCodeTraveler opened this issue 2 years ago • 4 comments

Feature name

Add Support for NativeAOT

Link to discussion

https://github.com/CommunityToolkit/Maui/issues/1460#issuecomment-1804383350

Progress tracker

  • [ ] Android Implementation
  • [ ] iOS Implementation
  • [ ] MacCatalyst Implementation
  • [ ] Windows Implementation
  • [ ] Tizen Implementation
  • [ ] Unit Tests
  • [ ] Samples
  • [ ] Documentation

Summary

This Proposal seeks to make all .NET MAUI CommunityToolkit libraries safe to use with NativeAOT.

This requires the library to be safe to use with the .NET Trimmer:

The IsTrimmable property defaults to true when configuring a project as AOT-compatible with <IsAotCompatible>true</IsAotCompatible>

https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming?pivots=dotnet-8-0#enable-project-specific-trimming

Motivation

In .NET 9, .NET MAUI will add NativeAOT support: https://github.com/dotnet/maui/issues/18658.

This give us approximately one year until .NET 9 releases in November 2024 to accomplish this Proposal.

Detailed Design

Directory.Build.props

We will make two changes to Directory.Build.proprs:

  1. Replace the <IsTrimmable>false property with <IsAotCompatible>true

The IsTrimmable property defaults to true when configuring a project as AOT-compatible with <IsAotCompatible>true</IsAotCompatible>

  1. Add <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<IsAotCompatible>true</IsAotCompatible>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>

Resolve Trim Warnings

Once <EnableTrimAnalyzer> has been enabled, we will then need to resolve every Trimmer warning. We can use the Microsoft docs to help us accomplish it: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming?pivots=dotnet-8-0#resolve-trim-warnings

Usage Syntax

To use .NET Trimming, the developer will add <TrimMode>full</TrimMode> to their CSProj. The .NET MAUI team will be adding <TrimMode>full</TrimMode> to the .NET MAUI Templates in .NET 9: https://github.com/dotnet/maui/issues/18658

Drawbacks

We may not fix all of the bugs introduced by the Trimmer in our first PR that adds NativeAOT Compatibility, and that's ok.

As new Trimmer-related bugs are reported, we will prioritize them and provide hot fixes.

Alternatives

There are no alternatives.

Now that .NET MAUI will be adding support for NativeAOT in .NET 9, every .NET MAUI developer is required to ensure their library is Trimmer safe.

Unresolved Questions

No response

Additional Information

How to make libraries compatible with native AOT: https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/

TheCodeTraveler avatar Nov 09 '23 19:11 TheCodeTraveler

@brminnick you can use this as reference too:

  • https://github.com/dotnet/maui/pull/3161

pictos avatar Nov 09 '23 21:11 pictos

Hello! What is the state of this proposal? Is somebody working on it already or could I help getting with this issue? I tried enabling the analyzers and seeing how much code needs fixing, and it seems that the codebase is mostly fine (https://github.com/CommunityToolkit/Maui/compare/main...simonrozsival:CommunityToolkit.Maui:mark-relevant-assemblies-aot-compatible).

I have some comments regarding this issue:

  • I'm not sure it is a good idea to place the <IsAotCompatible>true</IsAotCompatible> directly into Directory.Build.props. The trimming analyzers aren't compatible with netstandard2.* projects (all analyzers and source generators) and they also probably shouldn't be enabled for unit test projects. It might be better to add that property to only the projects where it should be really enabled.
  • When IsAotCompatible is set, the trim analyzers and all the other analyzers are enabled by default, there's no need to enable the analyzers manually.
  • The current default for TrimMode in MAUI apps is partial and so all assemblies that are marked as IsTrimmable will be trimmed. Once the community toolkit is trimmable, all apps will benefit from it, not just NativeAOT apps 😄 On the other hand, this might break code which accesses some internal types from the toolkit via reflection. This is why we're not marking MAUI itself as trim- or AOT-compatible, but we're just enabling the analyzers for now (see https://github.com/dotnet/maui/pull/21076#discussion_r1516064057). I'm not sure if this applies to the community toolkit though. NativeAOT doesn't care what the value of TrimMode is. It will trim any assembly, even if it is not marked as trimmable or AOT compatible.

simonrozsival avatar Apr 16 '24 13:04 simonrozsival

Thanks @simonrozsival! Great write up 💯

My plan was to begin working on this when the .NET 9 RC debuts since MAUI isn't adding AOT/Trimming until .NET 9. But, it sounds like you've already gotten a head start on it!

I'd love it if you could submit a draft PR with the work you've done so far! I think the best plan will be to keep your PR open (unmerged) until the .NET 9 RC so that we can continue to iterate on it as the MAUI team continues to iterate on their AOT/Trimming support.

TheCodeTraveler avatar Apr 16 '24 14:04 TheCodeTraveler

@brminnick sounds good, I'll open a draft PR

simonrozsival avatar Apr 16 '24 15:04 simonrozsival