Skip to content

Commit 9aa98b3

Browse files
authored
Merge pull request #872 from Sergio0694/dev/custom-beforecommon-targets
Refactor solution .targets files, enable detailed ILC warnings
2 parents ebef42b + fdbe652 commit 9aa98b3

5 files changed

Lines changed: 69 additions & 38 deletions

ComputeSharp.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.GlobalSt
102102
EndProject
103103
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B86A48A7-18BD-4946-B995-3CE26B092DDF}"
104104
ProjectSection(SolutionItems) = preProject
105+
src\Directory.Build.BeforeMicrosoftNETSdkTargets.targets = src\Directory.Build.BeforeMicrosoftNETSdkTargets.targets
105106
src\Directory.Build.props = src\Directory.Build.props
106107
src\Directory.Build.targets = src\Directory.Build.targets
107108
EndProjectSection

build/Directory.Build.props

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
<Nullable>enable</Nullable>
66
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
77

8+
<!--
9+
Show full details for all trim/AOT warnings produced during builds.
10+
Normally, the linker would only show a generic warning per assembly.
11+
-->
12+
<TrimmerSingleWarn>false</TrimmerSingleWarn>
13+
814
<!--
915
Enable the latest warning wave, which shows additional warnings for invalid language features that are disabled by default.
1016
For additional info, see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/warning-waves.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
4+
<!-- Properties exclusive to .NET 8 projects -->
5+
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
6+
7+
<!--
8+
Enable trimming support. This can't be in a target unlike the other properties
9+
set below, because if that is the case the trimmable attribute will be set but
10+
the analyzers will not run, so warnings will be skipped.
11+
-->
12+
<IsAotCompatible>true</IsAotCompatible>
13+
14+
<!-- Emit the [DisableRuntimeMarshalling] attribute (also enables the associated analyzer) -->
15+
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
16+
</PropertyGroup>
17+
18+
<!--
19+
Emit the [SupportedOSVersion] attribute if needed. Note that the .NET SDK already emits this attribute
20+
automatically, but only when targeting the Windows TFM. We use this custom target to emit the same
21+
attribute for projects only targeting plain .NET as well. This makes them easier to consume from
22+
projects that are not using the Windows TFM as well. Doing so isn't strictly needed anyway unless
23+
you're specifically consuming the Windows SDK projections, which those projects are not doing.
24+
-->
25+
<Target Name="EmitSupportedOSVersionAttributeForTargetOS"
26+
BeforeTargets="PrepareForBuild">
27+
<ItemGroup Condition="'$(SupportedOSPlatformVersion)' != '' AND !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows'))">
28+
<AssemblyAttribute Include="System.Runtime.Versioning.SupportedOSPlatformAttribute">
29+
<_Parameter1>Windows$(SupportedOSPlatformVersion)</_Parameter1>
30+
</AssemblyAttribute>
31+
</ItemGroup>
32+
</Target>
33+
34+
<!-- Emit the [ComVisible(false)] attribute for UWP and WinUI targets -->
35+
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows'))">
36+
<AssemblyAttribute Include="System.Runtime.InteropServices.ComVisibleAttribute">
37+
<_Parameter1>false</_Parameter1>
38+
</AssemblyAttribute>
39+
</ItemGroup>
40+
41+
<!-- Add a marker value to verify from a later .targets that this .targets has been correctly invoked -->
42+
<PropertyGroup>
43+
<_ComputeSharpBeforeMicrosoftNETSdkTargetsValid>true</_ComputeSharpBeforeMicrosoftNETSdkTargetsValid>
44+
</PropertyGroup>
45+
</Project>

src/Directory.Build.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,14 @@
8383
<!-- Pack the license file, if present -->
8484
<None Condition="$(IsLicenseFileAvailableForPacking)" Include="LICENSE.txt" Pack="true" PackagePath="\" Visible="False" />
8585
</ItemGroup>
86+
87+
<!--
88+
Setup the custom .targets to run as 'BeforeMicrosoftNETSdkTargets'. This is because we want to be able to read properties
89+
that are set in .csproj files, and set properties depending on those before the .NET SDK .targets are imported. This is
90+
necessary to ensure that the .NET tooling works correctly with some of them (eg. with 'IsAotCompatible'). Note that the
91+
.NET SDK doesn't use 'Exists' here, and '<Import>' supports multiple items, so we can just chain our .targets file directly.
92+
-->
93+
<PropertyGroup>
94+
<BeforeMicrosoftNETSdkTargets>$(BeforeMicrosoftNETSdkTargets);$(MSBuildThisFileDirectory)Directory.Build.BeforeMicrosoftNETSdkTargets.targets</BeforeMicrosoftNETSdkTargets>
95+
</PropertyGroup>
8696
</Project>

src/Directory.Build.targets

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,6 @@
4242
Visible="false" />
4343
</ItemGroup>
4444

45-
<!-- Properties exclusive to .NET 8 projects -->
46-
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
47-
48-
<!--
49-
Enable trimming support. This can't be in a target unlike the other properties
50-
set below, because if that is the case the trimmable attribute will be set but
51-
the analyzers will not run, so warnings will be skipped.
52-
-->
53-
<IsAotCompatible>true</IsAotCompatible>
54-
55-
<!-- Emit the [DisableRuntimeMarshalling] attribute (also enables the associated analyzer) -->
56-
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
57-
</PropertyGroup>
58-
59-
<!--
60-
Emit the [SupportedOSVersion] attribute if needed. Note that the .NET SDK already emits this attribute
61-
automatically, but only when targeting the Windows TFM. We use this custom target to emit the same
62-
attribute for projects only targeting plain .NET as well. This makes them easier to consume from
63-
projects that are not using the Windows TFM as well. Doing so isn't strictly needed anyway unless
64-
you're specifically consuming the Windows SDK projections, which those projects are not doing.
65-
-->
66-
<Target Name="EmitSupportedOSVersionAttributeForTargetOS"
67-
BeforeTargets="PrepareForBuild">
68-
<ItemGroup Condition="'$(SupportedOSPlatformVersion)' != '' AND !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows'))">
69-
<AssemblyAttribute Include="System.Runtime.Versioning.SupportedOSPlatformAttribute">
70-
<_Parameter1>Windows$(SupportedOSPlatformVersion)</_Parameter1>
71-
</AssemblyAttribute>
72-
</ItemGroup>
73-
</Target>
74-
75-
<!-- Emit the [ComVisible(false)] attribute for UWP and WinUI targets -->
76-
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows'))">
77-
<AssemblyAttribute Include="System.Runtime.InteropServices.ComVisibleAttribute">
78-
<_Parameter1>false</_Parameter1>
79-
</AssemblyAttribute>
80-
</ItemGroup>
81-
8245
<!--
8346
The following target has been ported from TerraFX.Interop.Windows.
8447
See: https://github.com/terrafx/terrafx.interop.windows.
@@ -99,7 +62,6 @@
9962
[module: System.Runtime.CompilerServices.SkipLocalsInitAttribute]]]>
10063
</GeneratedSkipLocalsInitFileLines>
10164
</PropertyGroup>
102-
10365
<Target Name="GenerateSkipLocalsInit"
10466
BeforeTargets="BeforeCompile;CoreCompile"
10567
DependsOnTargets="PrepareForBuild"
@@ -115,4 +77,11 @@
11577
<Compile Include="$(GeneratedSkipLocalsInitFile)" />
11678
</ItemGroup>
11779
</Target>
80+
81+
<!-- Emit an error if our custom 'BeforeMicrosoftNETSdkTargets' .targets file has not been invoked -->
82+
<Target Name="_ComputeSharpCheckForInvalidBeforeMicrosoftNETSdkTargets"
83+
BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CoreCompile">
84+
<Error Condition ="'$(_ComputeSharpBeforeMicrosoftNETSdkTargetsValid)' != 'true'"
85+
Text="The '_ComputeSharpBeforeMicrosoftNETSdkTargetsValid' marker property is not set."/>
86+
</Target>
11887
</Project>

0 commit comments

Comments
 (0)