Skip to content

Commit

Permalink
Allow dropping native host for NativeAOT WinRT components (#1395)
Browse files Browse the repository at this point in the history
* Add property/logic to generate WinRT native exports

* Make CsWinRTAotExportsEnabled compiler visible

* Automatically set CsWinRTAotExportsEnabled if needed

* Update authoring targets for NativeAOT scenario

* Refactor items in authoring .targets for clarity

* Fix two typos in generated native exports

* Return REGDB_E_CLASSNOTREG for class not registered

* Enable testing authoring scenario with NativeAOT

* Return CLASS_E_CLASSNOTAVAILABLE instead

* Update WinRT.Host.runtimeconfig.json for .NET 8

* Update .NET 8 SDK to 8.0.100 stable

* Update AuthoringConsumptionTest.vcxproj

* Skip native host copy target for NAOT

* Fix AuthoringTest .NET TFM in AuthoringWinUITest projects

* Update unit test for class not registered

* Update CsWinRTExeTFM for .NET 7 and .NET 8

* Handle .NET 8 in generator

* Fix version causing debug build failures

* Add target to publish AuthoringTest from consuming test

* Publish AuthoringTest with NAOT in build.cmd

* Fix publishing for AOT (some tests still failing).

* Fix gaps found from AuthoringTest being built for AOT

* Add property/logic to generate WinRT native exports

* Make CsWinRTAotExportsEnabled compiler visible

* Automatically set CsWinRTAotExportsEnabled if needed

* Update authoring targets for NativeAOT scenario

* Refactor items in authoring .targets for clarity

* Fix two typos in generated native exports

* Return REGDB_E_CLASSNOTREG for class not registered

* Enable testing authoring scenario with NativeAOT

* Return CLASS_E_CLASSNOTAVAILABLE instead

* Update WinRT.Host.runtimeconfig.json for .NET 8

* Update .NET 8 SDK to 8.0.100 stable

* Update AuthoringConsumptionTest.vcxproj

* Skip native host copy target for NAOT

* Fix AuthoringTest .NET TFM in AuthoringWinUITest projects

* Update unit test for class not registered

* Update CsWinRTExeTFM for .NET 7 and .NET 8

* Handle .NET 8 in generator

* Fix version causing debug build failures

* Add target to publish AuthoringTest from consuming test

* Publish AuthoringTest with NAOT in build.cmd

* Fix publishing for AOT (some tests still failing).

* Fix issue with IID optimizer for certain scenarios and fix missing vtable entries for some custom mapped types

* Fix bug where generics in class names caused issues

* Add property/logic to generate WinRT native exports

* Make CsWinRTAotExportsEnabled compiler visible

* Automatically set CsWinRTAotExportsEnabled if needed

* Update authoring targets for NativeAOT scenario

* Refactor items in authoring .targets for clarity

* Fix two typos in generated native exports

* Return REGDB_E_CLASSNOTREG for class not registered

* Enable testing authoring scenario with NativeAOT

* Return CLASS_E_CLASSNOTAVAILABLE instead

* Update WinRT.Host.runtimeconfig.json for .NET 8

* Update .NET 8 SDK to 8.0.100 stable

* Update AuthoringConsumptionTest.vcxproj

* Skip native host copy target for NAOT

* Fix AuthoringTest .NET TFM in AuthoringWinUITest projects

* Update unit test for class not registered

* Update CsWinRTExeTFM for .NET 7 and .NET 8

* Handle .NET 8 in generator

* Fix version causing debug build failures

* Add target to publish AuthoringTest from consuming test

* Publish AuthoringTest with NAOT in build.cmd

* Fix publishing for AOT (some tests still failing).

* Address PR feedback

* PR feedback from other PR

---------

Co-authored-by: Manodasan Wignarajah <[email protected]>
  • Loading branch information
Sergio0694 and manodasanW authored Jan 10, 2024
1 parent 8bb64f5 commit f01956b
Show file tree
Hide file tree
Showing 49 changed files with 1,486 additions and 817 deletions.
79 changes: 48 additions & 31 deletions nuget/Microsoft.Windows.CsWinRT.Authoring.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Copyright (C) Microsoft Corporation. All rights reserved.

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- Generate a RuntimeConfig -->
<EnableDynamicLoading>true</EnableDynamicLoading>
<!-- Generate a RuntimeConfig (only for the managed, hosted scenario) -->
<EnableDynamicLoading Condition="'$(CsWinRTAotExportsEnabled)' != 'true'">true</EnableDynamicLoading>
<!-- Name the RuntimeConfig properly -->
<ProjectRuntimeConfigFileName>WinRT.Host.runtimeconfig.json</ProjectRuntimeConfigFileName>
<ProjectRuntimeConfigFileName Condition="'$(CsWinRTAotExportsEnabled)' != 'true'">WinRT.Host.runtimeconfig.json</ProjectRuntimeConfigFileName>
<CsWinRTDetectDependentAuthoringWinMDs Condition="'$(CsWinRTDetectDependentAuthoringWinMDs)'==''">true</CsWinRTDetectDependentAuthoringWinMDs>
<GetCopyToOutputDirectoryItemsDependsOn>$(GetCopyToOutputDirectoryItemsDependsOn);CsWinRTAuthoring_AddManagedDependencies</GetCopyToOutputDirectoryItemsDependsOn>
</PropertyGroup>
Expand Down Expand Up @@ -55,8 +55,11 @@ Copyright (C) Microsoft Corporation. All rights reserved.

</Target>

<!-- For Project Reference consumers, copy the necessary WinRT DLLs to output directory -->
<ItemGroup>
<!--
For Project Reference consumers, copy the necessary WinRT DLLs to output directory.
Only include these if we are not generating native exports for NativeAOT.
-->
<ItemGroup Condition="'$(CsWinRTAotExportsEnabled)' != 'true'">

<None Condition="Exists('$(CsWinRTPath)lib\net6.0\WinRT.Host.Shim.dll')" Include="$(CsWinRTPath)lib\net6.0\WinRT.Host.Shim.dll">
<TargetPath>WinRT.Host.Shim.dll</TargetPath>
Expand All @@ -77,13 +80,16 @@ Copyright (C) Microsoft Corporation. All rights reserved.

</ItemGroup>

<!-- Any managed dependencies of the WinRT component won't automatically be brought
in by a referencing C++ app, so we manually set them to be copied here -->
<!--
Any managed dependencies of the WinRT component won't automatically be brought
in by a referencing C++ app, so we manually set them to be copied here. Only
needed when not using NativeAOT (as there's no additional managed .dll-s there).
-->
<Target Name="CsWinRTAuthoring_AddManagedDependencies"
BeforeTargets="GetCopyToOutputDirectoryItems"
Returns="@(AllItemsFullPathWithTargetPath)">

<ItemGroup>
<ItemGroup Condition="'$(CsWinRTAotExportsEnabled)' != 'true'">

<!-- Make sure all managed binaries/projections are shared across project references made by native apps -->
<AllItemsFullPathWithTargetPath Include="@(ReferenceCopyLocalPaths)"
Expand All @@ -101,7 +107,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
AfterTargets="GenerateBuildRuntimeConfigurationFiles"
BeforeTargets="GetCopyToOutputDirectoryItems;_GetCopyToOutputDirectoryItemsFromThisProject">

<ItemGroup>
<ItemGroup Condition="'$(CsWinRTAotExportsEnabled)' != 'true'">

<AllItemsFullPathWithTargetPath Include="$(ProjectRuntimeConfigFilePath)">
<TargetPath>$([System.IO.Path]::GetFileName($(ProjectRuntimeConfigFilePath)))</TargetPath>
Expand Down Expand Up @@ -148,8 +154,9 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<TargetPathWithTargetPlatformMoniker Remove="@(TargetPathWithTargetPlatformMoniker)" />
<!-- Add the .winmd -->
<TargetPathWithTargetPlatformMoniker Include="$(TargetDir)$(AssemblyName).winmd">
<!-- implementation is NOT $(AssemblyName).dll -->
<Implementation>WinRT.Host.dll</Implementation>
<!-- implementation is NOT $(AssemblyName).dll (unless NativeAOT publishing is used) -->
<Implementation Condition="'$(CsWinRTAotExportsEnabled)' != 'true'">WinRT.Host.dll</Implementation>
<Implementation Condition="'$(CsWinRTAotExportsEnabled)' == 'true'">$(AssemblyName).dll</Implementation>
<!-- show to c++ compiler (native case) and dotnet sdk (managed case) -->
<ResolveableAssembly>true</ResolveableAssembly>
<!-- Used by the dotnet sdk -->
Expand Down Expand Up @@ -250,10 +257,38 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<!-- When an authored component makes a nupkg, add the necessary hosting assets to the package -->
<Target Name="CsWinRTIncludeHostDlls" DependsOnTargets="CsWinRTGenerateTransitiveTargets" BeforeTargets="AfterBuild" Outputs="@(Content)">

<!-- When packing, include all necessary DLLs and the targets file for DLL copying on the native side -->
<!--
Include all assets that are shared across all configurations.
For instance, the .winmd file is always needed.
-->
<ItemGroup>

<Content Include="$(CsWinRTPath)lib\net6.0\WinRT.Host.Shim.dll">
<!-- Pack any XAML resource PRI files next to the .winmd -->
<Content Include="$(ProjectPriFullPath)">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)\winmd</PackagePath>
</Content>

<!-- Pack the WinMD we generated in its own folder, under the managed TFM folder -->
<Content Include="$(TargetDir)$(AssemblyName).winmd">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)\winmd</PackagePath>
</Content>

<!-- Use a targets file generated for the component -->
<Content Include="$(AuthoringTransitiveTargets)">
<Pack>true</Pack>
<PackagePath>buildTransitive\$(AssemblyName).targets;build\$(AssemblyName).targets</PackagePath>
</Content>
</ItemGroup>

<!--
Include all necessary DLLs and the targets file for DLL copying on the native side.
These additional assets are only used when the WinRT component is a managed assembly.
-->
<ItemGroup Condition="'$(CsWinRTAotExportsEnabled)' != 'true'">

<Content Include="$(CsWinRTPath)lib\net6.0\WinRT.Host.Shim.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
Expand Down Expand Up @@ -281,24 +316,6 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>

<!-- Pack any XAML resource PRI files next to the .winmd -->
<Content Include="$(ProjectPriFullPath)">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)\winmd</PackagePath>
</Content>

<!-- Pack the WinMD we generated in its own folder, under the managed TFM folder -->
<Content Include="$(TargetDir)$(AssemblyName).winmd">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)\winmd</PackagePath>
</Content>

<!-- Use a targets file generated for the component -->
<Content Include="$(AuthoringTransitiveTargets)">
<Pack>true</Pack>
<PackagePath>buildTransitive\$(AssemblyName).targets;build\$(AssemblyName).targets</PackagePath>
</Content>

<!-- We package a version of WinRT.Host.dll for each possible architecture -->
<Content Condition="Exists('$(CsWinRTPath)hosting\x64\native\WinRT.Host.dll')" Include="$(CsWinRTPath)hosting\x64\native\WinRT.Host.dll">
<Pack>true</Pack>
Expand Down
12 changes: 11 additions & 1 deletion nuget/Microsoft.Windows.CsWinRT.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<TrackFileAccess Condition="'$(CsWinRTComponent)' != 'true'">false</TrackFileAccess>
<CsWinRTAotOptimizerEnabled Condition="'$(CsWinRTAotOptimizerEnabled)' == '' and $([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) >= 6">true</CsWinRTAotOptimizerEnabled>
<CsWinRTAotOptimizerEnabled Condition="'$(CsWinRTAotOptimizerEnabled)' != 'true'">false</CsWinRTAotOptimizerEnabled>

<!--
If the AOT optimizer is enabled, and we're publishing with NativeAOT, automatically set CsWinRTAotExportsEnabled as well.
Only do this if the property is not already set by the user, so we respect any existing preference.
-->
<CsWinRTAotExportsEnabled Condition="'$(CsWinRTAotExportsEnabled)' == '' and '$(CsWinRTAotOptimizerEnabled)' == 'true' and '$(PublishAot)' == 'true'">true</CsWinRTAotExportsEnabled>
<CsWinRTAotExportsEnabled Condition="'$(CsWinRTAotExportsEnabled)' != 'true'">false</CsWinRTAotExportsEnabled>
</PropertyGroup>

<ItemGroup>
<CompilerVisibleProperty Include="CsWinRTAotOptimizerEnabled" />
<CompilerVisibleProperty Include="CsWinRTAotExportsEnabled" />
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)Microsoft.Windows.CsWinRT.Embedded.targets" Condition="'$(CsWinRTEmbedded)' == 'true'"/>
Expand Down Expand Up @@ -152,7 +160,9 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<CsWinRTEmbeddedProjection Condition="'$(CsWinRTEmbedded)' == 'true'">-embedded</CsWinRTEmbeddedProjection>
<CsWinRTEmbeddedEnums Condition="'$(CsWinRTEmbeddedPublicEnums)' == 'true'">-public_enums</CsWinRTEmbeddedEnums>
<CsWinRTExeTFM Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) == 5">net5.0</CsWinRTExeTFM>
<CsWinRTExeTFM Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) > 5">net6.0</CsWinRTExeTFM>
<CsWinRTExeTFM Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) == 6">net6.0</CsWinRTExeTFM>
<CsWinRTExeTFM Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) == 7">net7.0</CsWinRTExeTFM>
<CsWinRTExeTFM Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) >= 8">net8.0</CsWinRTExeTFM>
<CsWinRTExeTFM Condition="'$(CsWinRTExeTFM)' == ''">netstandard2.0</CsWinRTExeTFM>

<CsWinRTParams Condition="'$(CsWinRTParams)' == ''">
Expand Down
Loading

0 comments on commit f01956b

Please sign in to comment.