Skip to content

Instantly share code, notes, and snippets.

@dasMulli
Last active November 6, 2020 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dasMulli/c8ba2ffc20aa74268ac54c2a7fea7463 to your computer and use it in GitHub Desktop.
Save dasMulli/c8ba2ffc20aa74268ac54c2a7fea7463 to your computer and use it in GitHub Desktop.
TFM based feature flags as compiler constants
<Project>
<ItemGroup>
<FeatureFlag Include="SUPPORTS_ECDSA" MinimumTargetFrameworks="netstandard1.6;net47" />
<FeatureFlag Include="SUPPORTS_GENERIC_HOST" MinimumTargetFrameworks="netcoreapp2.2;netstandard2.1" />
<FeatureFlag Include="SUPPORTS_SERVICE_PROVIDER_IN_HTTP_MESSAGE_HANDLER_BUILDER" MinimumTargetFrameworks="netcoreapp2.2;netstandard2.1" />
<FeatureFlag Include="SUPPORTS_CERTIFICATE_HASHING_WITH_SPECIFIED_ALGORITHM" MinimumTargetFrameworks="netcoreapp2.1;netstandard2.1;net48" />
</ItemGroup>
<!--
Use BeforeCompile here (instead of e.g. CoreCompile) so that the consants
are picked up by _GenerateCompileDependencyCache and and incremental compilation
picks up changes to the FeatureFlag items.
-->
<Target Name="CreateFeatureFlagCompilerConstants" BeforeTargets="BeforeCompile">
<ItemGroup>
<_FeatureFlagsByMinimumTargetFramework Include="%(FeatureFlag.MinimumTargetFrameworks)"
FeatureFlag="@(FeatureFlag)" />
<_FeatureFlagsByMinimumTargetFramework Compatible="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)','%(Identity)'))" />
<_CompatibleFeatureFlags Include="@(_FeatureFlagsByMinimumTargetFramework->WithMetadataValue('Compatible','True')->'%(FeatureFlag)'->Distinct())" />
</ItemGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);@(_CompatibleFeatureFlags)</DefineConstants>
</PropertyGroup>
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment