Description
Is your feature request related to a problem? Please describe.
There are plenty of reasons why someone might want to compile a DLL to machine code, but then also still be able to use it in a .NET assembly. Currently the only way to do this is to treat the native DLL as if it was a DLL compiled by any other AOT language, exporting the function on one end and then importing it on the other.
It would be nice if in the event of a .NET to .NET linking, there was additional support to streamline and simplify the process, as well as be able to make certain assumptions that you can't make when you only know that the library is a native library and not a .NET AOT library.
Describe the solution you'd like
A new compilation target for AOT libraries designed to be used with other .NET projects, for the purposes of this ticket I will just call it a .netlib
. The goal of a .netlib
would be that someone (1st part/3rd party/etc.) compiles it, then the consumer of the library adds a reference it it in their .NET project. Whether it is a library or an executable referencing the .netlib
shouldn't matter, and once the reference is made it should function exactly the same as if you were linking to a JIT assembly.
My assumption is that something like that could be achieved by taking the source code of the library you are compiling to a .netlib
and creating a secondary non-AOT bridging library. The bridging library would only contain wrapper classes/functions that wrap around the publicly accessible classes/functions from the source code. Then when compiling the consumer executable/library the wrapper functions would be embedded in the consumer calling directly into the AOT library. During publishing of an application like this it would copy the executable and the DLL from the .netlib
into the output folder.
The contents of a .netlib
might contain something along the lines of a:
- Header: the section fo the
.netlib
that tells the compiler all the relevant information it needs to know, os-arch target. Size of the bridging library/aot library for copying the data later, etc. And other features, like what version of C# the code was written in to know what features are available when consuming the library. - Bridging Library: The bridging library would just be a standard .NET assembly that would be linked to the consumer.
- The AOT library: the fully compiled machine code to be linked to at runtime..
Additional context
I want to stress I am not a compiler engineer, I am just a regular programmer. Solving (or otherwise simplifying) the AOT barrier within .NET would go a long way to making me want to write more code in .NET. Additionally, I would assume that when you can make the assumption that the DLL you are linking to was written in .NET you can call the DLL functions as if they are managed functions rather than unmanaged types (maybe I am wrong on this, but my current understanding is that AOT code is still somewhat managed)
As this feature would be intended to be used with other AOT targets, I also wonder if the overhead associated with calling DLL functions can be minimised. Currently, if you want to pass data to and from a native library there is a high chance that you have to go through a marshalling layer of some kind. Instead of you could make the assumption that the string type in the library is the same string type in the executable all of the marshalling can be removed and the data can be directly accessed.
I would love feedback on the proposal, if it's even possible and if so what the state of .NET AOT is in and if it would be close to being able to support such a feature. Thanks