1 <?xml version="1.0" encoding="utf-8"?>
2 <Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
4 <UsingTask TaskName="PrereleaseResolveNuGetPackageAssets" AssemblyFile="$(BuildToolsTaskDir)Microsoft.DotNet.Build.Tasks.dll"/>
5 <UsingTask TaskName="ValidateProjectDependencyVersions" AssemblyFile="$(BuildToolsTaskDir)Microsoft.DotNet.Build.Tasks.dll"/>
6 <UsingTask TaskName="IsRestoreRequired" AssemblyFile="$(BuildToolsTaskDir)Microsoft.DotNet.Build.Tasks.dll"/>
9 <ProjectJson Condition="'$(ProjectJson)' == '' and '$(ContainsPackageReferences)' == 'true'">$(MSBuildProjectFullPath)</ProjectJson>
10 <RestoreOutputPath Condition="'$(RestoreOutputPath)' == ''">$(MSBuildProjectDirectory)</RestoreOutputPath>
11 <RestoreOutputPath Condition="'$(ContainsPackageReferences)' != 'true' And '$(ProjectJson)' != ''">$([System.IO.Path]::GetDirectoryName('$(ProjectJson)'))/obj</RestoreOutputPath>
12 <!-- SDK targets need this to be set in addition to RestoreOutputPath. See https://github.com/dotnet/sdk/issues/1057 -->
13 <ProjectAssetsFile>$(RestoreOutputPath)/project.assets.json</ProjectAssetsFile>
14 <ResolveNugetProjectFile Condition="'$(ResolveNugetProjectFile)' == ''">$(MSBuildProjectFullPath)</ResolveNugetProjectFile>
15 <RestorePackages Condition="'$(RestorePackages)'!='false' and Exists('$(ProjectJson)') and '$(DesignTimeBuild)' != 'true'">true</RestorePackages>
16 <PrereleaseResolveNuGetPackages Condition="'$(PrereleaseResolveNuGetPackages)'!='false' and Exists('$(ProjectJson)')">true</PrereleaseResolveNuGetPackages>
19 For now, prevent built-in task (if available) from running.
20 More changes are needed to light up on their availability
21 and use them instead of what we have here. See buildtools
24 <ResolveNugetPackages>false</ResolveNugetPackages>
27 <!-- Restoring packages during a background (designtime) build will cause VS 2015 (v14) to get into an endless loop of resolving references. -->
28 <Target Name="RestorePackages"
29 BeforeTargets="ResolveNuGetPackages;ValidatePackageVersions"
30 Condition="'$(RestorePackages)'=='true' and !('$(VSDesignTimeBuild)'=='true' and '$(VisualStudioVersion)' >= '14.0')">
32 <Error Condition="'$(DnuRestoreCommand)'=='' and Exists('$(ProjectJson)')" Text="RestorePackages target needs a predefined DnuRestoreCommand property set in order to restore $(ProjectJson)" />
34 <IsRestoreRequired ProjectJsons="$(ProjectJson)" PackagesFolder="$(PackagesDir)">
35 <Output TaskParameter="RestoreRequired" PropertyName="RestoreRequired" />
39 <_RepoApiAdditionalRestoreArgs Condition="'$(DotNetRestoreSourcePropsPath)' != ''">/p:DotNetRestoreSourcePropsPath=$(DotNetRestoreSourcePropsPath)</_RepoApiAdditionalRestoreArgs>
40 <_RepoApiAdditionalRestoreArgs Condition="'$(DotNetPackageVersionPropsPath)' != ''">$(_RepoApiAdditionalRestoreArgs) /p:DotNetPackageVersionPropsPath=$(DotNetPackageVersionPropsPath)</_RepoApiAdditionalRestoreArgs>
41 <_RepoApiAdditionalRestoreArgs Condition="'$(DotNetBuildOffline)' != ''">$(_RepoApiAdditionalRestoreArgs) /p:DotNetBuildOffline=$(DotNetBuildOffline)</_RepoApiAdditionalRestoreArgs>
42 <_RepoApiAdditionalRestoreArgs Condition="'$(EnablePackageRestoreLogger)' == 'true'">$(_RepoApiAdditionalRestoreArgs) /l:PackageRestoreLogger,$(MSBuildThisFileDirectory)Microsoft.DotNet.Build.Tasks.dll</_RepoApiAdditionalRestoreArgs>
43 <AdditionalRestoreArgs Condition="'$(_RepoApiAdditionalRestoreArgs)' != ''">$(AdditionalRestoreArgs) $(_RepoApiAdditionalRestoreArgs)</AdditionalRestoreArgs>
45 <_DnuRestoreCommandRidPortion Condition="'$(RidSpecificAssets)' == 'true'">-r $(NuGetRuntimeIdentifier)</_DnuRestoreCommandRidPortion>
46 <_DnuRestoreCommandFull>$(DnuRestoreCommand) $(ProjectJson) /p:TargetGroup=$(TargetGroup) /p:ConfigurationGroup=$(ConfigurationGroup) /p:ArchGroup=$(ArchGroup) /p:OSGroup=$(OSGroup) /p:TargetFramework=$(NuGetTargetMonikerShort) $(_DnuRestoreCommandRidPortion) $(AdditionalRestoreArgs)</_DnuRestoreCommandFull>
48 <Exec Condition="Exists('$(ProjectJson)') AND '$(RestoreRequired)' == 'true' AND '$(EnablePackageRestoreLogger)' != 'true'" Command="$(_DnuRestoreCommandFull)" StandardOutputImportance="Low" CustomErrorRegularExpression="^Unable to locate .*" />
49 <Exec Condition="Exists('$(ProjectJson)') AND '$(RestoreRequired)' == 'true' AND '$(EnablePackageRestoreLogger)' == 'true'" Command="$(_DnuRestoreCommandFull)" CustomErrorRegularExpression="^Unable to locate .*" />
51 <!-- if lock file exists be sure to update timestamp otherwise we could get in a state of aways calling restore but the lock file not being updated -->
52 <Touch Condition="Exists('$(ProjectAssetsFile)') AND '$(RestoreRequired)' == 'true'" Files="$(ProjectAssetsFile)" />
55 <ItemGroup Condition="'$(ResolvePackages)'=='true' or '$(PrereleaseResolveNuGetPackages)'=='true'">
56 <CustomAdditionalCompileInputs Condition="Exists('$(ProjectJson)')" Include="$(ProjectJson)" />
59 <ResolveAssemblyReferencesDependsOn>
60 $(ResolveAssemblyReferencesDependsOn);
62 ValidatePackageVersions;
63 </ResolveAssemblyReferencesDependsOn>
67 <!-- temporarily accept the old name NuGetTargetFrameworkMoniker until all projects are moved forward -->
68 <NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == ''">$(NuGetTargetFrameworkMoniker)</NuGetTargetMoniker>
69 <UseTargetPlatformAsNuGetTargetMoniker Condition="'$(UseTargetPlatformAsNuGetTargetMoniker)' == '' AND '$(TargetFrameworkMoniker)' == '.NETCore,Version=v5.0'">true</UseTargetPlatformAsNuGetTargetMoniker>
70 <NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == '' AND '$(UseTargetPlatformAsNuGetTargetMoniker)' == 'true'">$(TargetPlatformIdentifier),Version=v$([System.Version]::Parse('$(TargetPlatformMinVersion)').ToString(3))</NuGetTargetMoniker>
71 <NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == '' AND '$(UseTargetPlatformAsNuGetTargetMoniker)' != 'true'">$(TargetFrameworkMoniker)</NuGetTargetMoniker>
72 <BaseNuGetRuntimeIdentifier Condition="'$(BaseNuGetRuntimeIdentifier)' == '' and '$(TargetPlatformIdentifier)' == 'UAP'">win10</BaseNuGetRuntimeIdentifier>
73 <BaseNuGetRuntimeIdentifier Condition="'$(BaseNuGetRuntimeIdentifier)' == ''">win</BaseNuGetRuntimeIdentifier>
74 <CopyNuGetImplementations Condition="'$(CopyNuGetImplementations)' == '' and '$(OutputType)' != 'library' and ('$(OutputType)' != 'winmdobj' or '$(AppxPackage)' == 'true')">true</CopyNuGetImplementations>
77 <!-- If a RuntimeIndentifier wasn't already specified, let's go generate it -->
78 <PropertyGroup Condition="'$(NuGetRuntimeIdentifier)' == '' and '$(CopyNuGetImplementations)' == 'true'">
79 <_NuGetRuntimeIdentifierWithoutAot>$(BaseNuGetRuntimeIdentifier)-$(PlatformTarget.ToLower())</_NuGetRuntimeIdentifierWithoutAot>
80 <NuGetRuntimeIdentifier>$(_NuGetRuntimeIdentifierWithoutAot)</NuGetRuntimeIdentifier>
81 <NuGetRuntimeIdentifier Condition="'$(UseDotNetNativeToolchain)' == 'true'">$(_NuGetRuntimeIdentifierWithoutAot)-aot</NuGetRuntimeIdentifier>
84 <!-- Some projects want to explicitly use no runtime identifier, and do not want to receive any default value provided by other targets. -->
85 <PropertyGroup Condition="'$(NuGetRuntimeIdentifier)' == 'None'">
86 <NuGetRuntimeIdentifier></NuGetRuntimeIdentifier>
89 <Target Name="ResolveNuGetPackages"
90 Condition="'$(PrereleaseResolveNuGetPackages)'=='true'"
91 DependsOnTargets="$(ResolveNugetPackagesDependsOn)">
93 <PrereleaseResolveNuGetPackageAssets Condition="Exists('$(ProjectAssetsFile)')"
94 AllowFallbackOnTargetSelection="true"
95 IncludeFrameworkReferences="false"
96 NuGetPackagesDirectory="$(PackagesDir)"
97 RuntimeIdentifier="$(NuGetRuntimeIdentifier)"
98 ProjectLanguage="$(Language)"
99 ProjectLockFile="$(ProjectAssetsFile)"
100 TargetMonikers="$(NuGetTargetMoniker)"
101 ProjectReferencesCreatingPackages="@(ProjectReference)">
102 <Output TaskParameter="ResolvedAnalyzers" ItemName="Analyzer" />
103 <Output TaskParameter="ResolvedReferences" ItemName="_ReferenceFromPackage" />
104 <Output TaskParameter="ResolvedCopyLocalItems" ItemName="_ReferenceCopyLocalPathsFromPackage" />
105 <Output TaskParameter="ReferencedPackages" ItemName="ReferencedNuGetPackages" />
106 </PrereleaseResolveNuGetPackageAssets>
108 <!-- We may have package references that we want to replace with project references -->
110 <!-- Intersect project-refs with package-refs.
111 Project refs may be in _ResolvedProjectReferencePaths or Reference items.
112 Copy local may be in _ResolvedProjectReferencePaths or ReferenceCopyLocalPaths.
113 Copy local items may also be in any item like Content but we currently don't strip those.-->
114 <_ReferenceFileNamesToRemove Include="@(_ReferenceFromPackage)" Condition="'@(_ResolvedProjectReferencePaths->'%(FileName)%(Extension)')' == '%(FileName)%(Extension)'" />
115 <_ReferenceFileNamesToRemove Include="@(_ReferenceFromPackage)" Condition="'@(Reference->'%(FileName)%(Extension)')' == '%(FileName)%(Extension)'" />
117 <!-- If local copy is disabled remove all references, otherwise remove only project refrerences -->
118 <_ReferenceCopyLocalPathsFileNamesToRemove Include="@(_ReferenceCopyLocalPathsFromPackage)" Condition="'$(DisableReferenceCopyLocal)' == 'true' OR '@(_ResolvedProjectReferencePaths->'%(FileName)%(Extension)')' == '%(FileName)%(Extension)'" />
119 <_ReferenceCopyLocalPathsFileNamesToRemove Include="@(_ReferenceCopyLocalPathsFromPackage)" Condition="'$(DisableReferenceCopyLocal)' == 'true' OR '@(ReferenceCopyLocalPaths->'%(FileName)%(Extension)')' == '%(FileName)%(Extension)'" />
121 <!-- strip from the resolved package output -->
122 <_ReferenceFromPackage Remove="@(_ReferenceFileNamesToRemove)" />
123 <_ReferenceCopyLocalPathsFromPackage Remove="@(_ReferenceCopyLocalPathsFileNamesToRemove)" />
125 <!-- add the filtered resolved package output -->
126 <Reference Include="@(_ReferenceFromPackage)" />
127 <ReferenceCopyLocalPaths Include="@(_ReferenceCopyLocalPathsFromPackage)" />
130 <Message Text="Excluding @(_ReferenceFileNamesToRemove);@(_ReferenceCopyLocalPathsFileNamesToRemove) from package references since the same file is provided by a project reference."
131 Condition="'@(_ReferenceFileNamesToRemove)' != '' or '@(_ReferenceCopyLocalPathsFileNamesToRemove)' != ''"/>
134 <Target Name="RemoveTransitiveCompileReferences"
135 AfterTargets="ResolveNuGetPackages">
136 <ItemGroup Condition="'$(OmitTransitiveCompileReferences)' == 'true'">
137 <!-- get all references from nuget packages as ID so that we can substract the direct ref IDs-->
138 <_ReferenceAsPackageId Include="@(Reference->'%(NuGetPackageId)')" Condition="'%(Reference.NuGetPackageId)' != ''">
139 <OriginalIdentity>%(Identity)</OriginalIdentity>
140 </_ReferenceAsPackageId>
142 <!-- Indirect references are any references whose PackageId isn't in the direct reference set: ReferencedNuGetPackages -->
143 <_IndirectReferenceAsPackageId Include="@(_ReferenceAsPackageId)" Exclude="@(ReferencedNuGetPackages)"/>
145 <!-- Transform back to original -->
146 <IndirectReference Include="@(_IndirectReferenceAsPackageId->'%(OriginalIdentity)')" />
148 <Reference Remove="@(IndirectReference)"/>
152 <Target Name="ValidatePackageVersions"
153 Condition="'$(RestorePackages)'=='true' and '$(ValidatePackageVersions)'=='true' and Exists('$(ProjectJson)')">
154 <ValidateProjectDependencyVersions ProjectJsons="$(ProjectJson)"
155 ProhibitFloatingDependencies="$(ProhibitFloatingDependencies)"
156 ValidationPatterns="@(ValidationPattern)" />
159 <Target Name="FilterTargetingPackResolvedNugetPackages"
160 Condition="'$(SkipFilterTargetingPackResolvedNugetPackages)' != 'true'"
161 AfterTargets="ResolveNuGetPackages" >
163 <_TargetingPackPrefix>Microsoft.TargetingPack</_TargetingPackPrefix>
164 <TargetingPackReferenceCoreAssembly Condition="'$(TargetingPackReferenceCoreAssembly)' == '' and '%(TargetingPackReference.Identity)' == 'System.Private.CoreLib'">System.Private.CoreLib</TargetingPackReferenceCoreAssembly>
165 <TargetingPackReferenceCoreAssembly Condition="'$(TargetingPackReferenceCoreAssembly)' == ''">mscorlib</TargetingPackReferenceCoreAssembly>
168 S.P.CoreLib is generally architecture specific so disable the msbuild warning about
169 referencing it from an MSIL project.
171 <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch Condition="'$(TargetingPackReferenceCoreAssembly)' == 'System.Private.CoreLib'">None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
175 Add the mscorlib and windows to the reference set by default to avoid a lot of duplication in projects
176 They only act as a filter so if they aren't present in the packages references it will not impact anything.
178 <ItemGroup Condition="'$(ExcludeDefaultTargetingPackReferences)' != 'true'">
179 <TargetingPackReference Include="$(TargetingPackReferenceCoreAssembly)" Condition="'$(TargetingPackReferenceCoreAssembly)' != ''" />
180 <TargetingPackReference Include="Windows" />
184 <!-- Filter out all references coming out of the targeting pack packages except for TargetingPackReferences -->
185 <ResolvedTargetingPackReference Include="@(Reference)"
186 Condition="$([System.String]::new('%(Reference.NuGetPackageId)').StartsWith('$(_TargetingPackPrefix)'))" />
187 <ResolvedTargetingPackReferenceFilename Include="@(ResolvedTargetingPackReference -> '%(Filename)')">
188 <OriginalIdentity>%(Identity)</OriginalIdentity>
189 </ResolvedTargetingPackReferenceFilename>
190 <ResolvedTargetingPackReferenceFilename Remove="@(TargetingPackReference)" />
191 <PackageReferencesToRemove Include="@(ResolvedTargetingPackReferenceFilename -> '%(OriginalIdentity)')" />
192 <Reference Remove="@(PackageReferencesToRemove)" />
194 <!-- Filter out the copy-local set of references coming from the targeting pack packages -->
195 <PackageCopyLocalToRemove Include="@(ReferenceCopyLocalPaths)"
196 Condition="$([System.String]::new('%(ReferenceCopyLocalPaths.NuGetPackageId)').StartsWith('$(_TargetingPackPrefix)'))" />
197 <ReferenceCopyLocalPaths Remove="@(PackageCopyLocalToRemove)" />
200 <Message Importance="Low"
201 Text="Removed all ResolvedTagetingPackReferences that were not specified explicitly as a TargetingPackReference=[@(TargetingPackReference)]. PackageReferencesToRemove=[@(PackageReferencesToRemove)]." />