I'm trying to add full .NET8 Native AOT support for Flax but in very prototype stage. Let's disscuss!

According to FlaxEngine/Source/Tools/Flax.Build/Build/DotNet/DotNetAOT.cs at master · FlaxEngine/FlaxEngine (github.com)
FlaxEngine build system has already supported native AOT mode. The remaining job is to modify original hostfxr implementation by referring to mono aot implementation.

Thanks to the good design of FlaxEngine, it seems to only need to rewrite InitHostfxr, ShutdownHostfxr, GetStaticMethodPointer those three fuctions in Source/Engine/Scripting/runtime/DotNet.cpp.

And then we just need to checkout all the code that has differences between JIT and AOT mode. I believe this could be done by searching all places used USE_MONO_AOT macros and to add our own macors to make it AOT-compatiable.

Please correct me if I’m wrong at any point.

According to this document Native code interop with Native AOT - .NET | Microsoft Learn

We have to add [UnmanagedCallersOnly(EntryPoint="xxxxx")] attribute to all C#-side method called by C++. So we just need to add EntryPoint parameter for each because Flax already marked them with [UnmanagedCallersOnly]

All internal calls from C++ to C# already use that UnmanagedCallersOnly attribute: FlaxEngine/Source/Engine/Engine/NativeInterop.Unmanaged.cs at master · FlaxEngine/FlaxEngine · GitHub and it works great on CoreCRL + Mono + Mono AOT backend. That ILC prototype code you’ve found was used to implement Native AOT on Windows (and other platforms in suture) at the time of .NET 7 but it was unfinished. Also, dotnet team made lots of progress in this area which should;d make it easier to do. Keep in mind that some of that code could be already obsolete as they might change some cmd line used by ILC compiler.

To work on it you can start by adding UseAOT override to WindowsPlatformTools FlaxEngine/Source/Editor/Cooker/Platform/Windows/WindowsPlatformTools.h at master · FlaxEngine/FlaxEngine · GitHub that would return DotNetAOTModes::ILC which will run AOT compilation code you’ve mentioned. I would copy the command line from Editor output and use it to run Flax.Build from Visual Studio with debugger to implement C# assemblies compilation.

Good luck!