Skip to content

Commit

Permalink
Update nuget targets (microsoft#688)
Browse files Browse the repository at this point in the history
* always generate authors targets file

* update targets to not use packagefiles

* Update authoring.md
  • Loading branch information
j0shuams authored Jan 27, 2021
1 parent 5351b4b commit e33691e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 61 deletions.
31 changes: 5 additions & 26 deletions docs/authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,21 @@ To use the component in a C# app, the authored component just needs to be added

For native (C++) apps, there are DLLs needed to host your authored component. When you use the automatic nuget packaging on-build support (in Visual Studio) to make a nupkg for your runtime component, the DLLs/WinMD are automatically added to your nupkg, before the ```GenerateNuspec``` MSBuild step.

You will need to create a targets file for your component, if you are not already, that imports a CsWinRT targets file. The imported targets file configures the native app to use the hosting dlls at runtime.
This means for your component ```MyAuthoredComponent```, you will need a targets file that has an import statment for ```MyAuthoredComponent.CsWinRT.targets```.
The targets file you need **must** be named ```MyAuthoredComponent.targets```, otherwise NuGet will ignore it.

For example, the simplest definition of ```MyAuthoredComponent.targets``` would be:
``` targets
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildThisDirectory)MyAuthoredComponent.CsWinRT.targets" />
</Project>
```

The ```MyAuthoredComponent.CsWinRT.targets``` is added to the package by CsWinRT, you'll just need to add your ```MyAuthoredComponent.targets``` file to the package as well.
Do this by adding the following to ``MyAuthoredComponent.csproj```

``` csproj
<ItemGroup>
<_PackageFiles Include="MyAuthoredComponent.targets" PackagePath="build;buildTransitive"/>
</ItemGroup>
```

**If you are going to write your own nuspec, i.e. not rely on automatic packaging** then the CsWinRT target that adds the hosting dlls to your package will not run, and you should make sure your nuspec contains the following ```file``` entries for ```MyAuthoredComponent``` (note: your TargetFramework may vary). If adding a file entry for ```Coords.targets``` does not work, then update the project file per the above example.

``` nuspec
<files>
<file src="$(TargetDir)MyAuthoredComponent.dll" target="lib\native\MyAuthoredComponent.dll" />
<file src="$(TargetDir)MyAuthoredComponent.winmd" target="winmd\MyAuthoredComponent.winmd" />
<file src="$(TargetDir)MyAuthoredComponent.dll" target="lib\native\MyAuthoredComponent.dll" />
<file src="$(TargetDir)MyAuthoredComponent.winmd" target="winmd\MyAuthoredComponent.winmd" />

<file src="$(TargetDir)Microsoft.Windows.SDK.NET.dll" target="lib\native\Microsoft.Windows.SDK.NET.dll" />
<file src="$(TargetDir)Microsoft.Windows.SDK.NET.dll" target="lib\native\Microsoft.Windows.SDK.NET.dll" />

<!-- Note: you must rename the CsWinRt.Authoring.Targets as follows -->
<file src="C:\Path\To\CsWinRT\NugetDir\buildTransitive\Microsoft.Windows.CsWinRT.Authoring.targets"
target="buildTransitive\MyAuthoredComponent.CsWinRT.targets" />
target="buildTransitive\MyAuthoredComponent.targets" />

<file src="C:\Path\To\CsWinRT\NugetDir\build\Microsoft.Windows.CsWinRT.Authoring.targets"
target="build\MyAuthoredComponent.CsWinRT.targets" />
target="build\MyAuthoredComponent.targets" />

<!-- Include the managed DLLs -->
<file src="C:\Path\To\CsWinRT\NugetDir\lib\net5.0\WinRT.Host.Shim.dll"
Expand Down
13 changes: 6 additions & 7 deletions nuget/Microsoft.Windows.CsWinRT.Authoring.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@
</PropertyGroup>

<!-- Happens before building, so C++/WinRT can make the corresponding header files -->
<Target Name="CsWinRTAddAuthoredWinMDReference"
Condition="'$(HostingSupport-IsNative)' == 'true'"
Outputs="@(Reference)">
<ItemGroup>
<Target Name="CsWinRTAddAuthoredWinMDReference" Condition="'$(HostingSupport-IsNative)' == 'true'" Outputs="@(Reference)">

<ItemGroup Label="Add the WinMD file as a reference of the native app">
<Reference Include="$(HostingSupport-MetadataDir)\*" />
</ItemGroup>

</Target>

<!-- Happens when resolving references, so the app can be hosted -->
<Target Name="CsWinRTCopyAuthoringDlls"
Condition="'$(HostingSupport-IsNative)' == 'true'"
Outputs="@(ReferenceCopyLocalPaths)">
<Target Name="CsWinRTCopyAuthoringDlls" Condition="'$(HostingSupport-IsNative)' == 'true'" Outputs="@(ReferenceCopyLocalPaths)">

<ItemGroup Label="Copy Dlls needed for hosting/using authored components">
<ReferenceCopyLocalPaths Include="$(HostingSupport-NativeDir)\*" />
Expand All @@ -40,6 +38,7 @@
<ReferenceCopyLocalPaths Include="$(HostingSupport-RuntimesDir)\win-x86\native\WinRT.Host.dll"
Condition="'$(Platform)' == 'Win32'"/>
</ItemGroup>

</Target>

</Project>
57 changes: 29 additions & 28 deletions nuget/Microsoft.Windows.CsWinRT.targets
Original file line number Diff line number Diff line change
Expand Up @@ -140,50 +140,51 @@ $(CsWinRTFilters)
<Import Project="$(MSBuildThisFileDirectory)Microsoft.Windows.CsWinRT.Prerelease.targets" Condition="Exists('$(MSBuildThisFileDirectory)Microsoft.Windows.CsWinRT.Prerelease.targets')"/>

<!-- Copy Authored winmd to output folder -->
<Target Name="CsWinRTPlaceWinMDInOutputFolder"
Condition="'$(CsWinRTComponent)' == 'true'"
BeforeTargets="AfterBuild">
<Target Name="CsWinRTPlaceWinMDInOutputFolder" Condition="'$(CsWinRTComponent)' == 'true'" BeforeTargets="AfterBuild">
<Copy SourceFiles="$(GeneratedFilesDir)\$(AssemblyName).winmd" DestinationFolder="$(TargetDir)" UseHardlinksIfPossible="false" SkipUnchangedFiles="true" />
</Target>

<!-- When an authored component packs (nuget), add the necessary hosting assets to the package -->
<Target Name="CsWinRTIncludeHostDlls"
Condition="'$(CsWinRTComponent)' == 'true'"
BeforeTargets="GenerateNuspec"
Outputs="@(_PackageFiles)">
<!-- When an authored component makes a nupkg, add the necessary hosting assets to the package -->
<Target Name="CsWinRTIncludeHostDlls" Condition="'$(CsWinRTComponent)' == 'true'" BeforeTargets="AfterBuild" Outputs="@(Content)">
<ItemGroup>
<_PackageFiles Include="$(TargetDir)$(AssemblyName).winmd"
PackagePath="winmd\"/>
<Content Include="$(TargetDir)$(AssemblyName).winmd"
Pack="true"
PackagePath="winmd" />

<!-- The authored component gets the WinRT.Runtime and NET.SDK dlls from ReferenceCopyLocalPaths
(because CopyLocalLockFileAssemblies == true); $(TargetPath) has the assembly (e.g. PackageId.dll) -->
<_PackageFiles Include="$(CsWinRTPath)lib\net5.0\WinRT.Host.Shim.dll;@(ReferenceCopyLocalPaths);$(TargetPath)"
PackagePath="lib\native"/>
(because CopyLocalLockFileAssemblies == true) and $(TargetPath) has the assembly (e.g. PackageId.dll) -->
<Content Include="$(CsWinRTPath)lib\net5.0\WinRT.Host.Shim.dll;@(ReferenceCopyLocalPaths);$(TargetPath)"
Pack="true"
PackagePath="lib\native" />

<!-- Custom targets that copy dlls for consumers of the authored component.
`buildTransitive\` is for PackageReference, `build\` is for packages.config -->
<_PackageFiles Include="$(CsWinRTPath)buildTransitive\Microsoft.Windows.CsWinRT.Authoring.targets"
PackagePath="buildTransitive\$(AssemblyName).CsWinRT.targets;build\$(AssemblyName).CsWinRT.targets"/>
<Content Include="$(CsWinRTPath)buildTransitive\Microsoft.Windows.CsWinRT.Authoring.targets"
Pack="true"
PackagePath="buildTransitive\$(AssemblyName).targets;build\$(AssemblyName).targets"/>

<!-- WinRT.Host.dll is architecture specific -->
<!-- x64 -->
<_PackageFiles Condition="Exists('$(CsWinRTPath)runtimes\win-x64\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-x64\native\WinRT.Host.dll"
PackagePath="runtimes\win-x64\native"/>
<Content Condition="Exists('$(CsWinRTPath)runtimes\win-x64\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-x64\native\WinRT.Host.dll"
Pack="true"
PackagePath="runtimes\win-x64\native"/>
<!-- x86 -->
<_PackageFiles Condition="Exists('$(CsWinRTPath)runtimes\win-x86\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-x86\native\WinRT.Host.dll"
PackagePath="runtimes\win-x86\native"/>
<Content Condition="Exists('$(CsWinRTPath)runtimes\win-x86\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-x86\native\WinRT.Host.dll"
Pack="true"
PackagePath="runtimes\win-x86\native"/>
<!-- arm -->
<_PackageFiles Condition="Exists('$(CsWinRTPath)runtimes\win-arm\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-arm\native\WinRT.Host.dll"
PackagePath="runtimes\win-arm\native"/>
<Content Condition="Exists('$(CsWinRTPath)runtimes\win-arm\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-arm\native\WinRT.Host.dll"
Pack="true"
PackagePath="runtimes\win-arm\native"/>
<!-- arm64 -->
<_PackageFiles Condition="Exists('$(CsWinRTPath)runtimes\win-arm64\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-arm64\native\WinRT.Host.dll"
PackagePath="runtimes\win-arm64\native"/>
<Content Condition="Exists('$(CsWinRTPath)runtimes\win-arm64\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-arm64\native\WinRT.Host.dll"
Pack="true"
PackagePath="runtimes\win-arm64\native"/>
</ItemGroup>
</Target>


</Project>

0 comments on commit e33691e

Please sign in to comment.