Skip to content

Commit

Permalink
Move AOT properties to new .targets file
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Nov 24, 2024
1 parent 045be47 commit e494b8c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
44 changes: 37 additions & 7 deletions src/Directory.Build.BeforeMicrosoftCommon.targets
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>

<!-- Properties exclusive to .NET 8 projects -->
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">

<!--
Enable trimming support. This can't be in a target unlike the other properties
set below, because if that is the case the trimmable attribute will be set but
the analyzers will not run, so warnings will be skipped.
-->
<IsAotCompatible>true</IsAotCompatible>

<!-- Emit the [DisableRuntimeMarshalling] attribute (also enables the associated analyzer) -->
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
</PropertyGroup>

<!--
Emit the [SupportedOSVersion] attribute if needed. Note that the .NET SDK already emits this attribute
automatically, but only when targeting the Windows TFM. We use this custom target to emit the same
attribute for projects only targeting plain .NET as well. This makes them easier to consume from
projects that are not using the Windows TFM as well. Doing so isn't strictly needed anyway unless
you're specifically consuming the Windows SDK projections, which those projects are not doing.
-->
<Target Name="EmitSupportedOSVersionAttributeForTargetOS"
BeforeTargets="PrepareForBuild">
<ItemGroup Condition="'$(SupportedOSPlatformVersion)' != '' AND !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows'))">
<AssemblyAttribute Include="System.Runtime.Versioning.SupportedOSPlatformAttribute">
<_Parameter1>Windows$(SupportedOSPlatformVersion)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Target>

<!-- Emit the [ComVisible(false)] attribute for UWP and WinUI targets -->
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows'))">
<AssemblyAttribute Include="System.Runtime.InteropServices.ComVisibleAttribute">
<_Parameter1>false</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<!-- Add a marker value to verify from a later .targets that this .targets has been correctly invoked -->
<PropertyGroup>
Expand All @@ -9,11 +46,4 @@
<!-- Import the original custom .targets, if it exists -->
<Import Project="$(_ComputeSharpCustomBeforeMicrosoftCommonTargets)"
Condition="'$(_ComputeSharpCustomBeforeMicrosoftCommonTargets)' != '' AND Exists('$(_ComputeSharpCustomBeforeMicrosoftCommonTargets)')"/>

<!-- Emit an error if our custom 'BeforeCommon' .targets file has not been invoked -->
<Target Name="_ComputeSharpCheckForInvalidCustomBeforeMicrosoftCommonTargets"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CoreCompile">
<Error Condition ="'$(_ComputeSharpCustomBeforeMicrosoftCommonTargetsValid)' != 'true'"
Text="The '_ComputeSharpCustomBeforeMicrosoftCommonTargetsValid' marker property is not set."/>
</Target>
</Project>
45 changes: 7 additions & 38 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,6 @@
Visible="false" />
</ItemGroup>

<!-- Properties exclusive to .NET 8 projects -->
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">

<!--
Enable trimming support. This can't be in a target unlike the other properties
set below, because if that is the case the trimmable attribute will be set but
the analyzers will not run, so warnings will be skipped.
-->
<IsAotCompatible>true</IsAotCompatible>

<!-- Emit the [DisableRuntimeMarshalling] attribute (also enables the associated analyzer) -->
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
</PropertyGroup>

<!--
Emit the [SupportedOSVersion] attribute if needed. Note that the .NET SDK already emits this attribute
automatically, but only when targeting the Windows TFM. We use this custom target to emit the same
attribute for projects only targeting plain .NET as well. This makes them easier to consume from
projects that are not using the Windows TFM as well. Doing so isn't strictly needed anyway unless
you're specifically consuming the Windows SDK projections, which those projects are not doing.
-->
<Target Name="EmitSupportedOSVersionAttributeForTargetOS"
BeforeTargets="PrepareForBuild">
<ItemGroup Condition="'$(SupportedOSPlatformVersion)' != '' AND !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows'))">
<AssemblyAttribute Include="System.Runtime.Versioning.SupportedOSPlatformAttribute">
<_Parameter1>Windows$(SupportedOSPlatformVersion)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Target>

<!-- Emit the [ComVisible(false)] attribute for UWP and WinUI targets -->
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows'))">
<AssemblyAttribute Include="System.Runtime.InteropServices.ComVisibleAttribute">
<_Parameter1>false</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<!--
The following target has been ported from TerraFX.Interop.Windows.
See: https://github.com/terrafx/terrafx.interop.windows.
Expand All @@ -99,7 +62,6 @@
[module: System.Runtime.CompilerServices.SkipLocalsInitAttribute]]]>
</GeneratedSkipLocalsInitFileLines>
</PropertyGroup>

<Target Name="GenerateSkipLocalsInit"
BeforeTargets="BeforeCompile;CoreCompile"
DependsOnTargets="PrepareForBuild"
Expand All @@ -115,4 +77,11 @@
<Compile Include="$(GeneratedSkipLocalsInitFile)" />
</ItemGroup>
</Target>

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

0 comments on commit e494b8c

Please sign in to comment.