Skip to content

Conversation

@Sergio0694
Copy link
Contributor

Closes #103451

This PR changes the NativeAOT .targets on Windows to publish a .dll for executables using CustomNativeMain.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 15, 2024
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@MichalPetryka
Copy link
Contributor

This PR changes the NativeAOT .targets on Windows to publish a .dll for executables using CustomNativeMain.

Would it make sense to do this on all platforms with shared libraries? I think this mode might be used by Unity when they add NativeAOT support so it'd be nice if getting a shared library from it wasn't Windows only.

@jkotas
Copy link
Member

jkotas commented Jun 15, 2024

Would it make sense to do this on all platforms with shared libraries?

Sounds good to me.

@Sergio0694 Sergio0694 marked this pull request as ready for review June 15, 2024 20:24
<FullRuntimeName Condition="'$(ServerGarbageCollection)' == 'true' or '$(IlcLinkServerGC)' == 'true' or '$(ControlFlowGuard)' == 'Guard'">Runtime.ServerGC</FullRuntimeName>
<BootstrapperName>bootstrapper</BootstrapperName>
<BootstrapperName Condition="'$(NativeLib)' != '' or '$(CustomNativeMain)' == 'true'">bootstrapperdll</BootstrapperName>
<BootstrapperName Condition="'$(NativeLib)' != ''">bootstrapperdll</BootstrapperName>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas should we also keep this condition like in the Unix file? As in, is the bootstrapper .dll always going to be used when a custom main is used, regardless of whether the native binary being produced is actually a .dll or an .exe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the Windows and Unix should be in sync.

@Sergio0694 Sergio0694 changed the title Build a .dll if 'CustomNativeMain' is set Enable building .dll if both 'CustomNativeMain' and 'NativeLib' are set Jun 15, 2024
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, assuming that this works for CsWinRT E2E.

@Sergio0694
Copy link
Contributor Author

Thank you! If we want to wait to merge this until we can confirm that, is there a guide on how to generate a local working SDK we can use to try things out, or something? I've only ever tried using nightly SDKs downloaded from dotnet/sdk before 😅

@jkotas
Copy link
Member

jkotas commented Jun 21, 2024

https://github.com/dotnet/runtime/blob/main/docs/workflow/building/coreclr/nativeaot.md#building-packages has the steps for building private version of the naot package and referencing it in your app.

@Sergio0694
Copy link
Contributor Author

@jkotas so I'm trying to test this to get it merged before the RC1 snap, but something seems off. I've pushed a small tweak that now correctly makes NAOT produce a native .dll instead of an .exe. But, if I do `dumpbin /exports', I don't see any exports 🤔

D:\source\CustomMainTest\CustomMainTest\bin\Release\net9.0\win-x64\publish>dumpbin CustomMainTest.dll /exports
Microsoft (R) COFF/PE Dumper Version 14.40.33812.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file CustomMainTest.dll

File Type: DLL

  Section contains the following exports for CustomMainTest.dll

    00000000 characteristics
    FFFFFFFF time date stamp
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 00121420 DotNetRuntimeDebugHeader = DotNetRuntimeDebugHeader

  Summary

        B000 .data
       53000 .managed
        A000 .pdata
       4B000 .rdata
        1000 .reloc
        1000 .rsrc
       66000 .text
       1C000 hydrated

Is there something else we forgot to tweak? 😅

@Sergio0694
Copy link
Contributor Author

Nevermind, all good! Special thanks to @MichalStrehovsky 😄

image

For the custom main, I had to add:

<ItemGroup>
  <IlcArg Include="--export-dynamic-symbol:__managed__Main" />
</ItemGroup>

To the .csproj. For the export, I had just forgotten to set EntryPoint on the attribute.

@jkotas we should be good to merge this and hopefully we're still in time for RC1? 🤞

@jkotas
Copy link
Member

jkotas commented Jul 20, 2024

Note that the EventPipe is not able to shutdown cleanly in NativeAOT published libraries (#89346 has detailed). You may run into this issue if you ever try to collect EventPipe-based trace with this setup. The workaround is to end the trace session manually before the process shutdown.

@jkotas jkotas merged commit 7f63184 into dotnet:main Jul 20, 2024
@MichalStrehovsky
Copy link
Member

For the custom main, I had to add:

<ItemGroup>
  <IlcArg Include="--export-dynamic-symbol:__managed__Main" />
</ItemGroup>

@Sergio0694 Might want to consider adding this (under the right conditions) to ILCompiler targets as well. IlcArg and --export-dynamic-symbol are not considered documented.

Might want to also consider adding a test for this, similar to src\tests\nativeaot\CustomMain. Just to make sure we don't break this. If there's a test, it won't be broken. No guarantees without a test.

@Sergio0694
Copy link
Contributor Author

"IlcArg and --export-dynamic-symbol are not considered documented."

Oh. Yeah that's not good, we don't want to take dependencies on undocumented stuff 😅
I'll open a new PR for that, thank you for pointing this out!

@github-actions github-actions bot locked and limited conversation to collaborators Aug 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to output a shim .exe + .dll for an application (for WinRT activation)

4 participants