godotengine / godot-docs

Godot Engine official documentation
https://docs.godotengine.org
Other
3.92k stars 3.21k forks source link

The information about NativeAOT needs to be added to the C# platform support section. #9239

Closed Foxchandaisuki closed 6 months ago

Foxchandaisuki commented 6 months ago

Your Godot version: 4.2

Issue description: It is necessary to add the text from the Desktop paragraph and other sections of this article to the document's page to inform users that they can AOT compile C# code to native code and how to do so.

The items listed below are the exact content that needs to be included.

Before .NET unification there was the Windows-only .NET Framework and the cross-platform Mono and .NET Core. Unification means there will be just one .NET going forward, so the next release after .NET Core 3.0 was named .NET 5, and Mono and .NET Framework won’t have any new major releases.

The term Mono can be used to refer to a collection of technologies. With .NET unification, the Mono framework is deprecated, but the runtime is still supported. Unified .NET uses both the CoreCLR and the Mono runtimes, but Mono is the only runtime that supports mobile and web platforms.

However, in .NET 7.0 a new runtime became available, NativeAOT, which allows publishing native binaries built for a specific platform, instead of the JIT-based portable binaries. In .NET 7.0, NativeAOT only supports Windows and Linux, but in .NET 8.0 it adds support for macOS and experimental support for Android and iOS. This means in .NET 8.0 NativeAOT can be used as an alternative to Mono for mobile platforms.

Desktop platforms have been supported since 4.0, but what some users may not know is that those platforms support all .NET runtimes. This is not new to 4.2 and, in fact, has been supported since 4.0. Godot’s C# integration uses standard .NET so everything that works in a normal C# project should work in Godot too. To use a different runtime from the default (which is CoreCLR), users have to modify the C# project file (.csproj) as documented in the Microsoft documentation.

To enable NativeAOT in desktop platforms, make sure to target .NET 7.0 or higher and set the property to true. Also, since Godot uses reflection, make sure to root the assemblies (this is done automatically for you when exporting to iOS because NativeAOT is currently the only supported runtime). Here’s an example C# project:

<Project Sdk="Godot.NET.Sdk/4.2.0">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <EnableDynamicLoading>true</EnableDynamicLoading>
    <!-- Use NativeAOT. -->
    <PublishAOT>true</PublishAOT>
  </PropertyGroup>
  <ItemGroup>
    <!-- Root the assemblies to avoid trimming. -->
    <TrimmerRootAssembly Include="GodotSharp" />
    <TrimmerRootAssembly Include="$(TargetName)" />
  </ItemGroup>
</Project>

URL to the documentation page (if already existing): https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/index.html#c-platform-support

raulsntos commented 6 months ago

In that article I wanted to let users know that using NativeAOT was possible, but it's an advanced use case that most users shouldn't use at this time because it has some caveats.

As mentioned in the article, NativeAOT requires trimming and that is incompatible with some of the reflection APIs we use in the Godot libraries. This means that you have to explicitly disable trimming by rooting the assemblies and that's not something we want to recommend.

The goal is to eventually make the Godot libraries fully NativeAOT compatible so disabling trimming is no longer necessary. Until then I don't think it should be documented, advanced users should be able to figure out how to make it work and don't need the documentation or can just rely on Microsoft's documentation.

skyace65 commented 6 months ago

Closing this per raulsntos' comment.