-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[runtime] Infrastructure to build runtime components using NativeAOT #98565
Conversation
was missing dependency resolution for the native projects
GNU ld and clang's lld support a '-r' options for linking a bunch of object files together into another one. Maybe we can use this together with the exclude-libs/hidden-lx options to combine just the native lib and the runtime support libs before doing the final link. unclear if there's anything similar on Windows.
Tagging subscribers to this area: @dotnet/area-infrastructure-libraries Issue DetailsMotivation: While the .NET runtime itself is perfectly capable of running .NET code, we also provide other components that currently have to be implemented in native code. For example the DAC - its job is to introspect a live CoreCLR process or a memory dump and provide the information to diagnostic tools. The DAC is not a .NET runtime and it exposes a native interface to other tooling. But as we evolve the DAC it might make sense to implement parts of it in C#. This PR: we provide two things: an example, and the CMake and MSBuild infrastructure for defining Ideally we'd like to be able to produce shared or static libraries. Currently only shared libraries really work robustly (in that only the exported native symbols of the C# library are exposed and the NativeAOT implementation details are completely hidden). There's an attempt at doing static linking too, but it needs a bit more work on Linux/Mac (using The example is just a library that writes a message to the console when mscordac is loaded. We shouldn't merge that bit. Possible improvements before this is ready to merge:
|
52169b5
to
b94c67a
Compare
This comment has been minimized.
This comment has been minimized.
SharedLibraryInstallName only works on mobile apple platforms
I'm going to remove the example now. I've seen it build with the latest changes and there's nothing interesting in the example itself anymore. I think I'm ready for another round of reviews |
I think I want to remove the |
I think I agree here. If we exclusively reference the exports lazily through dlopen/LoadLibrary, then we don't need to have the header target. If we were to link (dynamically or statically) against the lib, then I think we should expose the header as part of the same target that has the lib so linking against one CMake target would pull everything in needed to use that target. In fact, if we are going to reference at runtime with dlopen/LoadLibrary, we don't need the full CMake integration at all. We only need one (imported?) target that adds the include directory for the header that has the function pointer types for dynamic interaction. |
yea, that's true. I'll move the cmake stuff to a separate branch/PR and keep it on the back burner until we have a need for it.
I'm not sure I totally agree. Particularly for static linking it's sometimes helpful to only have a single target that depends on the libs, while multiple targets depend on the headers. In any case, that can be hashed out later - when we have a need for the cmake stuff. |
Removed cmake integration. Verified that the cdac stuff still builds. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments to reduce the MSBuild logic and reduce the number of steps to set up a new project.
for IsSourceProject C# projects under src/native/managed
Co-Authored-By: Jeremy Koritzinsky <jekoritz@microsoft.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
…or copying to runtime output (#100782) The InstallRuntimeComponentToFinalDestination target from infrastructure for NativeAOT-ed runtime components (#98565) copies the component and symbol files to the runtime output directory. Make the target use NativeOutputPath for the for the library and symbol path instead of the ones copied to the publish directory. They should be available after LinkNative. This was working in the runtime repo because the projects under src/native/managed are currently using the ILCompiler package corresponding to the SDK being used to build the repo. That version of the package doesn't have #98342 - the native-library.targets were relying the symbols already being copied before _CopyAotSymbols was run.
Motivation: While the .NET runtime itself is perfectly capable of running .NET code, we also provide other components that currently have to be implemented in native code. For example the DAC - its job is to introspect a live CoreCLR process or a memory dump and provide the information to diagnostic tools. The DAC is not a .NET runtime and it exposes a native interface to other tooling. But as we evolve the DAC it might make sense to implement parts of it in C#.
Native runtime component libraries using NativeAOT
This PR allows managed libraries to be compiled using NativeAOT and to be used in the runtime using normal CMake idioms.
Adding a new managed library
Add a new subdirectory to
src/native/managed
for your library with asrc
andinc
subdirectories:In
src/native/managed/compile-native.proj
, addsrc/native/managed/libMyNewLibrary/src/libMyNewLibrary.csproj
to theNativeLibsProjectsToBuild
item group.
In
src/native/managed/libMyNewLibrary/src/libMyNewLibrary.csproj
:<Import Project="..\..\native-library.props" />
<Import Project="..\..\native-library.targets" />
@(InstallRuntimeComponentDest)
that has directory names relative toartifacts/bin/<runtimeFlavor>/<os.arch.config>/
where the shared library should be installed. It's a good idea to have at least.
:Limitations:
libXXXX
- currently the infrastructure expects alib
prefix on all platforms.infrastructure is not finished yet. Additionally, mixing Debug/Release configurations with static
linking will not be supported on Windows.
Ideally we'd like to be able to produce shared or static libraries. Currently only shared libraries really work robustly (in that only the exported native symbols of the C# library are exposed and the NativeAOT implementation details are completely hidden).
There was an attempt at doing static linking too, but it needs a bit more work on Linux/Mac (using
-r
to create relocatable objects in conjunction with-Wl,--exclude-libs
or-Wl,-hidden-l
to hide the symbols of the NativeAOT runtime). Doing naïve static linking results in duplicate GC and eventpipe symbols (since CoreCLR and NativeAOT both define them). Static linking on Windows probably isn't worthwhile due to #98356 and because I'm not sure if hiding symbols will be possible.There was some work to have cmake integration: runtime could do
find_nativeaot_library(libWhatever [REQUIRED])
and get two cmake targets:libWhatever::libs
andlibWhatever::headers
that could be used for linking with other targets. The current usecase doesn't need this level of integration however, so the cmake stuff was moved to this branch: https://github.com/lambdageek/runtime/tree/naot-runtime-lib-with-cmake