Skip to content

Commit

Permalink
Avoid project evaluations for single tfms (#8377)
Browse files Browse the repository at this point in the history
* Avoid project evaluations for single tfms

To avoid additional project evaluations when a project doesn't multi-target, don't set the SetTargetFramework property and undefine the TargetFramework property. For consistency also undefine the RuntimeIdentifier property when a project is RID agnostic. This mimics the msbuild's Common.targets behavior which is disabled because the SkipGetTargetFrameworkProperties attribute is set to the annotated P2Ps.

This change shows a drastic speed improvement when building the libs.ref projects in dotnet/runtime and changing the projects to use the TargetFramework property instead of TargetFrameworks when the project doesn't multi-target.

* Update BinPlace.targets

* Update BinPlace.targets
  • Loading branch information
ViktorHofer authored Jan 27, 2022
1 parent cf9b545 commit 93656ab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,25 @@ public override bool Execute()
Log.LogError($"Not able to find a compatible supported target framework for {referringTargetFramework} in Project {Path.GetFileName(projectReference.ItemSpec)}. The Supported Configurations are {string.Join(", ", targetFrameworks)}");
}

projectReference.SetMetadata("SetTargetFramework", "TargetFramework=" + bestTargetFramework);
// Mimic msbuild's Common.targets behavior: https://github.com/dotnet/msbuild/blob/3c8fb11a080a5a15199df44fabf042a22e9ad4da/src/Tasks/Microsoft.Common.CurrentVersion.targets#L1842-L1853
if (projectReference.GetMetadata("HasSingleTargetFramework") != "true")
{
projectReference.SetMetadata("SetTargetFramework", "TargetFramework=" + bestTargetFramework);
}
else
{
// If the project has a single TargetFramework, we need to Undefine TargetFramework to avoid another project evaluation.
string undefineProperties = projectReference.GetMetadata("UndefineProperties");
projectReference.SetMetadata("UndefineProperties", undefineProperties + ";TargetFramework");
}

if (projectReference.GetMetadata("IsRidAgnostic") == "true")
{
// If the project is RID agnostic, undefine the RuntimeIdentifier property to avoid another evaluation. -->
string undefineProperties = projectReference.GetMetadata("UndefineProperties");
projectReference.SetMetadata("UndefineProperties", undefineProperties + ";RuntimeIdentifier");
}

projectReference.SetMetadata("SkipGetTargetFrameworkProperties", "true");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
<Target Name="BinPlace"
DependsOnTargets="GetBinPlaceTargetFramework;BinPlaceFiles;BinPlaceProps"
AfterTargets="CopyFilesToOutputDirectory"
Condition="'$(EnableBinPlacing)' == 'true' OR '@(BinPlaceDir)' != ''" />
Condition="'$(EnableBinPlacing)' == 'true' or '@(BinPlaceDir)' != ''" />

<Target Name="BinPlaceFiles"
Condition="'@(BinPlaceDir)' != ''"
DependsOnTargets="GetBinPlaceItems"
Inputs="@(BinPlaceDir);%(BinPlaceDir.ItemName);%(BinPlaceDir.Identity)"
Outputs="unused" >
Outputs="unused">

<PropertyGroup>
<_BinPlaceItemName>%(BinPlaceDir.ItemName)</_BinPlaceItemName>
Expand Down Expand Up @@ -117,11 +117,17 @@
</Target>

<Target Name="GetBinPlaceTargetFramework">
<!-- find which, if any, supported target framework of this project is best
for each binplace targetFramework -->
<ItemGroup>
<!-- This needs to be a separate item as batching doesn't work when passing in the ValueOrDefault result directly. -->
<_supportedTargetFramework Include="$(TargetFrameworks)" Condition="'$(TargetFrameworks)' != ''" />
<_supportedTargetFramework Include="$(TargetFramework)" Condition="'@(_supportedTargetFramework)' == '' and '$(TargetFramework)' != ''" />
</ItemGroup>

<!-- Find which, if any, supported target framework of this project is best
for each binplace targetFramework. -->
<ChooseBestTargetFrameworksTask BuildTargetFrameworks="@(BinPlaceTargetFrameworks)"
SupportedTargetFrameworks="$(TargetFrameworks)"
RuntimeGraph="$(RuntimeGraph)" >
SupportedTargetFrameworks="@(_supportedTargetFramework)"
RuntimeGraph="$(RuntimeGraph)">
<Output TaskParameter="BestTargetFrameworks" ItemName="_bestBinPlaceTargetFrameworks" />
</ChooseBestTargetFrameworksTask>

Expand Down

0 comments on commit 93656ab

Please sign in to comment.