Skip to content

Commit

Permalink
Use chaining for custom .targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Nov 25, 2024
1 parent 1b6641a commit fdbe652
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ComputeSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.GlobalSt
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B86A48A7-18BD-4946-B995-3CE26B092DDF}"
ProjectSection(SolutionItems) = preProject
src\Directory.Build.BeforeMicrosoftCommon.targets = src\Directory.Build.BeforeMicrosoftCommon.targets
src\Directory.Build.BeforeMicrosoftNETSdkTargets.targets = src\Directory.Build.BeforeMicrosoftNETSdkTargets.targets
src\Directory.Build.props = src\Directory.Build.props
src\Directory.Build.targets = src\Directory.Build.targets
EndProjectSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@

<!-- Add a marker value to verify from a later .targets that this .targets has been correctly invoked -->
<PropertyGroup>
<_ComputeSharpCustomBeforeMicrosoftCommonTargetsValid>true</_ComputeSharpCustomBeforeMicrosoftCommonTargetsValid>
<_ComputeSharpBeforeMicrosoftNETSdkTargetsValid>true</_ComputeSharpBeforeMicrosoftNETSdkTargetsValid>
</PropertyGroup>

<!-- Import the original custom .targets, if it exists -->
<Import Project="$(_ComputeSharpCustomBeforeMicrosoftCommonTargets)"
Condition="'$(_ComputeSharpCustomBeforeMicrosoftCommonTargets)' != '' AND Exists('$(_ComputeSharpCustomBeforeMicrosoftCommonTargets)')"/>
</Project>
11 changes: 5 additions & 6 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@
</ItemGroup>

<!--
Setup the custom .targets to run as 'CustomBeforeMicrosoftCommonTargets'. We also make a copy of the original value so
that we can manually import it ourselves after ours. This is because we want to be able to read properties that are set
in .csproj files, and set properties depending on those before the .NET SDK .targets are imported. This is necessary
to ensure that the .NET tooling works correctly with some of them (eg. with 'IsAotCompatible').
Setup the custom .targets to run as 'BeforeMicrosoftNETSdkTargets'. This is because we want to be able to read properties
that are set in .csproj files, and set properties depending on those before the .NET SDK .targets are imported. This is
necessary to ensure that the .NET tooling works correctly with some of them (eg. with 'IsAotCompatible'). Note that the
.NET SDK doesn't use 'Exists' here, and '<Import>' supports multiple items, so we can just chain our .targets file directly.
-->
<PropertyGroup>
<_ComputeSharpCustomBeforeMicrosoftCommonTargets>$(CustomBeforeMicrosoftCommonTargets)</_ComputeSharpCustomBeforeMicrosoftCommonTargets>
<CustomBeforeMicrosoftCommonTargets>$(MSBuildThisFileDirectory)Directory.Build.BeforeMicrosoftCommon.targets</CustomBeforeMicrosoftCommonTargets>
<BeforeMicrosoftNETSdkTargets>$(BeforeMicrosoftNETSdkTargets);$(MSBuildThisFileDirectory)Directory.Build.BeforeMicrosoftNETSdkTargets.targets</BeforeMicrosoftNETSdkTargets>
</PropertyGroup>
</Project>
8 changes: 4 additions & 4 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@
</ItemGroup>
</Target>

<!-- Emit an error if our custom 'BeforeCommon' .targets file has not been invoked -->
<Target Name="_ComputeSharpCheckForInvalidCustomBeforeMicrosoftCommonTargets"
<!-- Emit an error if our custom 'BeforeMicrosoftNETSdkTargets' .targets file has not been invoked -->
<Target Name="_ComputeSharpCheckForInvalidBeforeMicrosoftNETSdkTargets"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CoreCompile">
<Error Condition ="'$(_ComputeSharpCustomBeforeMicrosoftCommonTargetsValid)' != 'true'"
Text="The '_ComputeSharpCustomBeforeMicrosoftCommonTargetsValid' marker property is not set."/>
<Error Condition ="'$(_ComputeSharpBeforeMicrosoftNETSdkTargetsValid)' != 'true'"
Text="The '_ComputeSharpBeforeMicrosoftNETSdkTargetsValid' marker property is not set."/>
</Target>
</Project>

0 comments on commit fdbe652

Please sign in to comment.