Add NuGet.BuildTasks
[platform/core/dotnet/build-tools.git] / tasks / NuGet / Microsoft.NuGet.targets
1 <!--
2 ***********************************************************************************************
3 Microsoft.NuGet.targets
4
5 WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
6           created a backup copy.  Incorrect changes to this file will make it
7           impossible to load or build your projects from the command-line or the IDE.
8
9 Copyright (c) .NET Foundation. All rights reserved. 
10 ***********************************************************************************************
11 -->
12 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
13   <UsingTask TaskName="Microsoft.NuGet.Build.Tasks.ResolveNuGetPackageAssets" AssemblyFile="Microsoft.NuGet.Build.Tasks.dll" />
14
15   <PropertyGroup Condition="'$(ProjectLockFile)' == ''">
16     <_ProjectSpecificProjectJsonFile>$(MSBuildProjectName).project.json</_ProjectSpecificProjectJsonFile>
17     <ProjectLockFile Condition="Exists('$(_ProjectSpecificProjectJsonFile)')">$(MSBuildProjectName).project.lock.json</ProjectLockFile>
18     <ProjectLockFile Condition="!Exists('$(_ProjectSpecificProjectJsonFile)')">project.lock.json</ProjectLockFile>
19   </PropertyGroup>
20
21   <PropertyGroup>
22     <ResolveNuGetPackages Condition="'$(ResolveNuGetPackages)' == '' and '$(MSBuildProjectExtension)' != '.xproj'">true</ResolveNuGetPackages>
23
24     <BaseNuGetRuntimeIdentifier Condition="'$(BaseNuGetRuntimeIdentifier)' == '' and '$(TargetPlatformIdentifier)' == 'UAP'">win10</BaseNuGetRuntimeIdentifier>
25     <BaseNuGetRuntimeIdentifier Condition="'$(BaseNuGetRuntimeIdentifier)' == ''">win</BaseNuGetRuntimeIdentifier>
26
27     <UseTargetPlatformAsNuGetTargetMoniker Condition="'$(UseTargetPlatformAsNuGetTargetMoniker)' == '' AND '$(TargetFrameworkMoniker)' == '.NETCore,Version=v5.0'">true</UseTargetPlatformAsNuGetTargetMoniker>
28     <NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == '' AND '$(UseTargetPlatformAsNuGetTargetMoniker)' == 'true'">$(TargetPlatformIdentifier),Version=v$([System.Version]::Parse('$(TargetPlatformMinVersion)').ToString(3))</NuGetTargetMoniker>
29     <NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == '' AND '$(UseTargetPlatformAsNuGetTargetMoniker)' != 'true'">$(TargetFrameworkMoniker)</NuGetTargetMoniker>
30
31     <CopyNuGetImplementations Condition="'$(CopyNuGetImplementations)' == '' and (('$(OutputType)' != 'library' and ('$(OutputType)' != 'winmdobj' or '$(AppxPackage)' == 'true')) or '$(TargetFrameworkIdentifier)' == '.NETFramework')">true</CopyNuGetImplementations>
32     <IncludeFrameworkReferencesFromNuGet Condition="'$(IncludeFrameworkReferencesFromNuGet)' == ''">true</IncludeFrameworkReferencesFromNuGet>
33
34     <_NuGetRuntimeIdentifierPlatformTargetSuffix Condition="'$(PlatformTarget)' != '' and '$(PlatformTarget)' != 'AnyCPU'">-$(PlatformTarget.ToLower())</_NuGetRuntimeIdentifierPlatformTargetSuffix>
35     <_NuGetRuntimeIdentifierWithoutAot>$(BaseNuGetRuntimeIdentifier)$(_NuGetRuntimeIdentifierPlatformTargetSuffix)</_NuGetRuntimeIdentifierWithoutAot>
36   </PropertyGroup>
37
38   <!-- If a NuGetRuntimeIdentifier wasn't already specified, let's go generate it -->
39   <PropertyGroup Condition="'$(NuGetRuntimeIdentifier)' == '' and '$(CopyNuGetImplementations)' == 'true'">
40     <NuGetRuntimeIdentifier>$(_NuGetRuntimeIdentifierWithoutAot)</NuGetRuntimeIdentifier>
41     <NuGetRuntimeIdentifier Condition="'$(UseDotNetNativeToolchain)' == 'true'">$(_NuGetRuntimeIdentifierWithoutAot)-aot</NuGetRuntimeIdentifier>
42   </PropertyGroup>
43
44   <ItemGroup>
45     <!-- If we are resolving from project.lock.json, we need to consider any edit to it as something that forces a rebuild -->
46     <CustomAdditionalCompileInputs Include="$(ProjectLockFile)" Condition="'$(ResolveNuGetPackages)' == 'true' and Exists('$(ProjectLockFile)')" />
47   </ItemGroup>
48
49   <PropertyGroup Condition="'$(TargetPlatformIdentifier)' == 'UAP'">
50     <_NuGetTargetFallbackMoniker>UAP,Version=v10.0</_NuGetTargetFallbackMoniker>
51     <ImplicitlyExpandTargetFramework>false</ImplicitlyExpandTargetFramework>
52     <EnableAppLocalFXWorkaround>false</EnableAppLocalFXWorkaround>
53     <UseNetNativeCustomFramework>true</UseNetNativeCustomFramework>
54   </PropertyGroup>
55
56   <!--
57     ============================================================
58                                         GetProjectsReferencingProjectJsonFiles
59     ============================================================
60   -->
61   <Target Name="GetProjectsReferencingProjectJson" DependsOnTargets="_SplitProjectReferencesByFileExistence" Returns="@(_ProjectReferencingProjectJsonFile)">
62     <ItemGroup Condition="'$(ResolveNuGetPackages)' == 'true'">
63       <_ProjectReferencingProjectJsonFile Include="$(MSBuildProjectFullPath)">
64         <ProjectReferences>@(ProjectReference)</ProjectReferences>
65         <ProjectJson>$(MSBuildProjectDirectory)\project.json</ProjectJson>
66       </_ProjectReferencingProjectJsonFile>
67     </ItemGroup>
68
69     <MSBuild
70       Projects="@(_MSBuildProjectReferenceExistent)"
71       Targets="GetProjectsReferencingProjectJson"
72       BuildInParallel="$(BuildInParallel)"
73       Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration); %(_MSBuildProjectReferenceExistent.SetPlatform)"
74       RemoveProperties="%(_MSBuildProjectReferenceExistent.GlobalPropertiesToRemove)">
75
76       <Output TaskParameter="TargetOutputs" ItemName="_ProjectReferencingProjectJsonFile" />
77     </MSBuild>
78   </Target>
79
80   <ItemGroup>
81     <NuGetPreprocessorValue Include="rootnamespace">
82       <Value>$(RootNamespace)</Value>
83     </NuGetPreprocessorValue>
84     <NuGetPreprocessorValue Include="assemblyname">
85       <Value>$(AssemblyName)</Value>
86     </NuGetPreprocessorValue>
87     <NuGetPreprocessorValue Include="fullpath">
88       <Value>$(MSBuildProjectDirectory)</Value>
89     </NuGetPreprocessorValue>
90     <NuGetPreprocessorValue Include="outputfilename">
91       <Value>$(TargetFileName)</Value>
92     </NuGetPreprocessorValue>
93     <NuGetPreprocessorValue Include="filename">
94       <Value>$(MSBuildProjectFile)</Value>
95     </NuGetPreprocessorValue>
96   </ItemGroup>
97
98   <!--
99     ============================================================
100                                         ResolveNuGetPackageAssets
101
102     Resolve assets from consumed NuGet packages listed in the project.lock.json
103
104         [OUT]
105         @(Analyzer) - Paths to build-time diagnostic analyzers
106         @(Reference) - Paths to build-time NuGet dependencies
107         @(ReferenceCopyLocalPaths) - Paths to run-time dependencies to copy
108     ============================================================
109   -->
110   <PropertyGroup>
111     <ResolveAssemblyReferencesDependsOn>$(ResolveAssemblyReferencesDependsOn);ResolveNuGetPackageAssets</ResolveAssemblyReferencesDependsOn>
112     <PrepareResourcesDependsOn>ResolveNuGetPackageAssets;$(PrepareResourcesDependsOn)</PrepareResourcesDependsOn>
113   </PropertyGroup>
114
115   <PropertyGroup>
116     <ResolveNuGetPackageAssetsDependsOn>ResolveProjectReferences</ResolveNuGetPackageAssetsDependsOn>
117     <ResolveNuGetPackageAssetsDependsOn Condition="'$(ImplicitlyExpandTargetFramework)' == 'true'">$(ResolveNuGetPackageAssetsDependsOn);ImplicitlyExpandTargetFramework</ResolveNuGetPackageAssetsDependsOn>
118   </PropertyGroup>
119
120   <Target Name="ResolveNuGetPackageAssets" DependsOnTargets="$(ResolveNuGetPackageAssetsDependsOn)" Condition="'$(ResolveNuGetPackages)' == 'true' and exists('$(ProjectLockFile)')">
121     <!-- We need to figure out the output path of any dependent xproj projects -->
122     <MSBuild
123       Projects="@(_MSBuildProjectReferenceExistent)"
124       Targets="GetTargetDir"
125       BuildInParallel="$(BuildInParallel)"
126       Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration); %(_MSBuildProjectReferenceExistent.SetPlatform)"
127       RemoveProperties="%(_MSBuildProjectReferenceExistent.GlobalPropertiesToRemove)"
128       Condition="'%(_MSBuildProjectReferenceExistent.Extension)' == '.xproj'">
129
130       <Output TaskParameter="TargetOutputs" ItemName="_XProjTargetDirs" />
131     </MSBuild>
132
133     <!-- The items in _XProjTargetDirs have the target directories as the main itemspecs and the originating project as metadata; reverse them -->
134     <ItemGroup>
135       <ProjectReferenceCreatingPackage Include="%(_XProjTargetDirs.OriginalItemSpec)">
136         <OutputBasePath>%(_XProjTargetDirs.Identity)</OutputBasePath>
137       </ProjectReferenceCreatingPackage>
138     </ItemGroup>
139
140     <ResolveNuGetPackageAssets AllowFallbackOnTargetSelection="$(DesignTimeBuild)"
141                                ContinueOnError="$(ContinueOnError)"
142                                IncludeFrameworkReferences="$(IncludeFrameworkReferencesFromNuGet)"
143                                NuGetPackagesDirectory="$(NuGetPackagesDirectory)"
144                                RuntimeIdentifier="$(NuGetRuntimeIdentifier)"
145                                ProjectLanguage="$(Language)"
146                                ProjectLockFile="$(ProjectLockFile)"
147                                ProjectReferencesCreatingPackages="@(ProjectReferenceCreatingPackage)"
148                                ContentPreprocessorValues="@(NuGetPreprocessorValue)"
149                                ContentPreprocessorOutputDirectory="$(IntermediateOutputPath)\NuGet"
150                                TargetMonikers="$(NuGetTargetMoniker);$(_NuGetTargetFallbackMoniker)">
151
152       <Output TaskParameter="ResolvedAnalyzers" ItemName="Analyzer" />
153       <Output TaskParameter="ResolvedCopyLocalItems" ItemName="ReferenceCopyLocalPaths" />
154       <Output TaskParameter="ResolvedReferences" ItemName="_ReferencesFromNuGetPackages" />
155       <Output TaskParameter="ReferencedPackages" ItemName="ReferencedNuGetPackages" />
156       <Output TaskParameter="ContentItems" ItemName="_NuGetContentItems" />
157       <Output TaskParameter="FileWrites" ItemName="FileWrites" />
158     </ResolveNuGetPackageAssets>
159
160     <ItemGroup>
161       <!-- Remove exact references, such as if a package had a framework reference to 'System' that we already have -->
162       <Reference Remove="@(_ReferencesFromNuGetPackages)" />
163
164       <!-- Remove simple name references that are already implicitly added -->
165       <_ReferencesFromNuGetPackages Remove="%(ReferencePath.FileName)" Condition="'%(ReferencePath.ResolvedFrom)' == 'ImplicitlyExpandTargetFramework'" />
166
167       <!-- Include NuGet references in the proper groups. Project-to-project references must go in the
168            _ResolvedProjectReferencePaths group which matches the behavior of the ResolveProjectReferences
169            target. This ensures that even if the assembly is missing on disk, it still makes it to the compiler. -->
170       <Reference Include="@(_ReferencesFromNuGetPackages)" Condition="'%(_ReferencesFromNuGetPackages.NuGetSourceType)' != 'Project'" />
171       <_ResolvedProjectReferencePaths Include="@(_ReferencesFromNuGetPackages)" Condition="'%(_ReferencesFromNuGetPackages.NuGetSourceType)' == 'Project'" />
172
173       <!-- Remove simple name references if we're directly providing a reference assembly to the compiler. For example,
174            consider a project with an Reference Include="System", and some NuGet package is providing System.dll -->
175       <Reference Remove="%(_ReferencesFromNuGetPackages.FileName)" Condition="'%(_ReferencesFromNuGetPackages.NuGetIsFrameworkReference)' == 'false'"/>
176     </ItemGroup>
177
178     <PropertyGroup Condition=" '$(AutoUnifyAssemblyReferences)' == 'true' ">
179       <!-- Normally Design Time Assembly Resolution (DTAR) won't consider these references.
180            Put DTAR in a mode where it will prefer the output of RAR and unify. -->
181       <DTARUseReferencesFromProject>true</DTARUseReferencesFromProject>
182     </PropertyGroup>
183
184     <!-- The items in _NuGetContentItems need to go into the appropriately-named item group, but the names depend upon the items
185          themselves. Split it apart. -->
186     <CreateItem Include="@(_NuGetContentItems)" Condition="'@(_NuGetContentItems)' != ''">
187       <Output TaskParameter="Include" ItemName="%(_NuGetContentItems.NuGetItemType)" />
188     </CreateItem>
189   </Target>
190
191   <Target Name="RuntimeImplementationProjectOutputGroup" Returns="@(RuntimeImplementationProjectOutputGroupOutput)" Condition="'$(ResolveNuGetPackages)' == 'true' and exists('$(ProjectLockFile)')">
192     <!-- This output group must contain the implementation assemblies for the host (i.e. design time) environment, not the
193          target environment. Thus, we explicitly pass the RuntimeIdentifier that doesn't have the -aot suffix -->
194     <ResolveNuGetPackageAssets AllowFallbackOnTargetSelection="$(DesignTimeBuild)"
195                                NuGetPackagesDirectory="$(NuGetPackagesDirectory)"
196                                RuntimeIdentifier="$(_NuGetRuntimeIdentifierWithoutAot)"
197                                ProjectLanguage="$(Language)"
198                                ProjectLockFile="$(ProjectLockFile)"
199                                TargetMonikers="$(NuGetTargetMoniker);$(_NuGetTargetFallbackMoniker)">
200
201       <Output TaskParameter="ResolvedCopyLocalItems" ItemName="NonAheadOfTimeRuntimeImplementations" />
202     </ResolveNuGetPackageAssets>
203
204     <ItemGroup>
205       <RuntimeImplementationProjectOutputGroupOutput Include="%(NonAheadOfTimeRuntimeImplementations.Identity)">
206         <FinalOutputPath>%(NonAheadOfTimeRuntimeImplementations.FullPath)</FinalOutputPath>
207         <TargetPath>%(NonAheadOfTimeRuntimeImplementations.FullPath)</TargetPath>
208       </RuntimeImplementationProjectOutputGroupOutput>
209     </ItemGroup>
210   </Target>
211
212   <!--
213     ============================================================
214             Framework injection into mixed-target applications
215     ============================================================
216   -->
217   <PropertyGroup>
218     <NuGetTargetFrameworkMonikerToInject Condition="'$(NuGetTargetFrameworkMonikerToInject)' == ''">.NETCore,Version=v5.0</NuGetTargetFrameworkMonikerToInject>
219     <NuGetTargetMonikerToInject Condition="'$(NuGetTargetMonikerToInject)' == ''">.NETCore,Version=v5.0</NuGetTargetMonikerToInject>
220     <_ComputeNetCoreFrameworkInjectionParametersBeforeTargets Condition="'$(AppxPackage)' == 'true' and '$(TargetPlatformIdentifier)' == 'UAP'">BeforeGenerateProjectPriFile</_ComputeNetCoreFrameworkInjectionParametersBeforeTargets>
221   </PropertyGroup>
222
223   <Target Name="ComputeNetCoreFrameworkInjectionParameters" BeforeTargets="$(_ComputeNetCoreFrameworkInjectionParametersBeforeTargets)" DependsOnTargets="_AddUnionWinmd" Condition="'$(_ComputeNetCoreFrameworkInjectionParametersBeforeTargets)' != ''">
224     <PropertyGroup>
225       <_PackagingOutputsIncludesFramework Condition="'%(PackagingOutputs.FileName)%(PackagingOutputs.Extension)' == 'System.Runtime.dll'">true</_PackagingOutputsIncludesFramework>
226       <_AppContainsManagedCodeForInjection Condition="'%(PackagingOutputs.Identity)' == '$(_TargetPlatformSdkDir)UnionMetadata\Windows.winmd'">true</_AppContainsManagedCodeForInjection>
227     </PropertyGroup>
228
229     <PropertyGroup>
230       <_NetCoreFrameworkInjectionNeeded Condition="'$(_PackagingOutputsIncludesFramework)' != 'true' and '$(_AppContainsManagedCodeForInjection)' == 'true'">true</_NetCoreFrameworkInjectionNeeded>
231     </PropertyGroup>
232   </Target>
233
234   <Target Name="InjectNetCoreFrameworkBlockIfLockFileExists" AfterTargets="ComputeNetCoreFrameworkInjectionParameters" Condition="'$(_NetCoreFrameworkInjectionNeeded)' == 'true' and '$(ResolveNuGetPackages)' == 'true' and Exists('$(ProjectLockFile)')">
235     <Error Text="One of your dependencies requires the .NET Framework, but the .NET Framework could not be found in the NuGet packages installed in this project.  Please install the appropriate .NET Framework packages required by your dependency." />
236   </Target>
237
238   <Target Name="InjectNetCoreFramework" AfterTargets="ComputeNetCoreFrameworkInjectionParameters" Condition="'$(_NetCoreFrameworkInjectionNeeded)' == 'true' and ('$(ResolveNuGetPackages)' != 'true' or !Exists('$(ProjectLockFile)'))">
239     <GetReferenceAssemblyPaths TargetFrameworkMoniker="$(NuGetTargetFrameworkMonikerToInject)" Condition="'$(FrameworkInjectionLockFile)' == ''">
240       <Output TaskParameter="ReferenceAssemblyPaths" ItemName="_NuGetInjectionSourceDirectories" />
241     </GetReferenceAssemblyPaths>
242
243     <PropertyGroup>
244       <FrameworkInjectionLockFile Condition="'$(FrameworkInjectionLockFile)' == ''">@(_NuGetInjectionSourceDirectories->'%(Identity)\project.lock.json')</FrameworkInjectionLockFile>
245       <FrameworkInjectionPackagesDirectory Condition="'$(FrameworkInjectionPackagesDirectory)' == ''">$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\NuGet\Repository', 'NETCoreSDK', null, RegistryView.Registry32, RegistryView.Default))</FrameworkInjectionPackagesDirectory>
246     </PropertyGroup>
247
248     <ResolveNuGetPackageAssets Condition="Exists('$(FrameworkInjectionLockFile)')"
249                                NuGetPackagesDirectory="$(FrameworkInjectionPackagesDirectory)"
250                                RuntimeIdentifier="$(NuGetRuntimeIdentifier)"
251                                TargetMonikers="$(NuGetTargetMonikerToInject)"
252                                ProjectLockFile="$(FrameworkInjectionLockFile)">
253
254       <Output TaskParameter="ResolvedCopyLocalItems" ItemName="_InjectNetCoreFrameworkPayload" />
255     </ResolveNuGetPackageAssets>
256
257     <PropertyGroup>
258       <_CoreRuntimePackageId Condition="'%(_InjectNetCoreFrameworkPayload.FileName)%(_InjectNetCoreFrameworkPayload.Extension)' == 'mscorlib.dll' OR '%(_InjectNetCoreFrameworkPayload.FileName)%(_InjectNetCoreFrameworkPayload.Extension)' == 'mscorlib.ni.dll'">%(_InjectNetCoreFrameworkPayload.NuGetPackageId)</_CoreRuntimePackageId>
259     </PropertyGroup>
260
261     <ItemGroup>
262       <PackagingOutputs Include="@(_InjectNetCoreFrameworkPayload)" Condition="'%(_InjectNetCoreFrameworkPayload.NuGetPackageId)' != '$(_CoreRuntimePackageId)' or '$(UseDotNetNativeToolchain)' == 'true'">
263         <TargetPath>%(Filename)%(Extension)</TargetPath>
264         <ProjectName>$(ProjectName)</ProjectName>
265         <OutputGroup>CopyLocalFilesOutputGroup</OutputGroup>
266       </PackagingOutputs>
267     </ItemGroup>
268   </Target>
269
270   <Import Project="$(MSBuildProjectDirectory)\$(MSBuildProjectName).nuget.targets" Condition="Exists('$(MSBuildProjectDirectory)\$(MSBuildProjectName).nuget.targets') AND '$(IncludeNuGetImports)' != 'false'" />
271 </Project>