CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.18k stars 374 forks source link

[Proposal] Add Support for NativeAOT #1509

Open brminnick opened 9 months ago

brminnick commented 9 months ago

Feature name

Add Support for NativeAOT

Link to discussion

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

Progress tracker

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 true

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 true

  2. 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/

pictos commented 9 months ago

@brminnick you can use this as reference too:

simonrozsival commented 4 months ago

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:

brminnick commented 4 months ago

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.

simonrozsival commented 4 months ago

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