CSharp.Common.targets 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <Project>
  2. <!-- For 'legacy' .csproj files, set map TargetFrameworkVersion back to TargetFramework -->
  3. <PropertyGroup Condition=" '$(TargetFramework)' == '' AND '$(TargetFrameworks)' == '' ">
  4. <TargetFramework>net$(TargetFrameworkVersion.TrimStart('vV').Replace('.',''))</TargetFramework>
  5. <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
  6. </PropertyGroup>
  7. <PropertyGroup>
  8. <!-- Ensure API changes show up clearly in PRs. -->
  9. <AddPublicApiAnalyzers Condition=" '$(AddPublicApiAnalyzers)' == '' AND
  10. '$(IsImplementationProject)' == 'true' AND
  11. ! $(RepoRelativeProjectDir.Contains('Tools')) ">true</AddPublicApiAnalyzers>
  12. <AddPublicApiAnalyzers Condition=" '$(AddPublicApiAnalyzers)' == '' ">false</AddPublicApiAnalyzers>
  13. <!-- Public members should not use oblivious types. Not done with all nullable annotations. -->
  14. <NoWarn>$(NoWarn);RS0041</NoWarn>
  15. <_IsSrcProject Condition="'$(IsImplementationProject)' == 'true' OR
  16. '$(IsAnalyzersProject)' == true OR
  17. '$(IsSpecificationTestProject)' == 'true'">true</_IsSrcProject>
  18. <Nullable Condition=" '$(Nullable)' == '' AND '$(_IsSrcProject)' == 'true'">enable</Nullable>
  19. <!-- Strip out non-public nullable annotations for src projects. This should help with size reduction -->
  20. <Features Condition="'$(_IsSrcProject)' == 'true'">$(Features);nullablePublicOnly</Features>
  21. <!-- Turn off platform compatibility analyzer warnings in test, test assets, and samples. -->
  22. <NoWarn Condition=" $(IsTestProject) OR
  23. '$(IsTestAssetProject)' == 'true' OR
  24. '$(ISBenchmarkProject)' == 'true' OR
  25. '$(IsSampleProject)' == 'true' OR
  26. '$(IsMicrobenchmarksProject)' == 'true'">$(NoWarn);CA1416</NoWarn>
  27. <!-- Turn off unused usings warning for multi-targeted projects. Fixing them using ifdefs looks worse than ignoring the warnings. -->
  28. <NoWarn Condition="'$(TargetFrameworks)' != ''">$(NoWarn);IDE0005</NoWarn>
  29. <!--
  30. Turn off ConfigureAwait analyzer rule in the projects that are exclusively meant to run in ASP.NET Core or Blazor contexts.
  31. Since we don't have a good way to detect this, we'll use the presence of ns2.0 as a proxy.
  32. -->
  33. <NoWarn Condition="'$(_IsSrcProject)' != 'true' OR !$(TargetFrameworks.Contains('netstandard'))">$(NoWarn);CA2007</NoWarn>
  34. <!-- Enable .NET code style analysis during build for src projects. -->
  35. <EnforceCodeStyleInBuild Condition="'$(EnforceCodeStyleInBuild)' == ''">true</EnforceCodeStyleInBuild>
  36. <!-- Enable trimming annotation validation in DI. See https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/feature-switches.md -->
  37. <VerifyDependencyInjectionOpenGenericServiceTrimmability Condition=" $(IsTestProject) ">true</VerifyDependencyInjectionOpenGenericServiceTrimmability>
  38. </PropertyGroup>
  39. <!-- Enable implicit usings for projects that target the default TFM -->
  40. <PropertyGroup Condition="'$(ImplicitUsings)' == '' AND '$(TargetFrameworks)' == '' AND '$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)'">
  41. <ImplicitUsings>enable</ImplicitUsings>
  42. </PropertyGroup>
  43. <ItemGroup Condition="'$(ImplicitUsings)' == 'enable'">
  44. <!-- We should make it obvious when Linq is being used in shipping code -->
  45. <Using Remove="System.Linq" Condition="'$(_IsSrcProject)' == 'true'" />
  46. <!-- System.Net.Http types will frequently conflict with ASP.NET Core types-->
  47. <Using Remove="System.Net.Http" />
  48. <Using Include="Xunit" Condition=" $(IsTestProject) " />
  49. </ItemGroup>
  50. <ItemGroup Condition=" '$(DotNetBuildFromSource)' != 'true' AND $(AddPublicApiAnalyzers) ">
  51. <Reference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" ExcludeAssets="Compile" PrivateAssets="All" />
  52. </ItemGroup>
  53. <Target Name="_CheckIgnoredPublicApiFiles"
  54. BeforeTargets="Build;Restore"
  55. Condition=" Exists('$(MSBuildProjectDirectory)\PublicAPI.Shipped.txt') AND ! $(AddPublicApiAnalyzers) ">
  56. <Warning Text="Public API baseline files ignored." />
  57. </Target>
  58. <Target Name="_RemovePublicApiAnalyzer" BeforeTargets="RazorCoreCompile">
  59. <ItemGroup>
  60. <_PublicAPIAnalyzers Include="@(Analyzer->WithMetadataValue('NuGetPackageId','Microsoft.CodeAnalysis.PublicApiAnalyzers'))" />
  61. <Analyzer Remove="@(_PublicAPIAnalyzers)" />
  62. </ItemGroup>
  63. </Target>
  64. <Target Name="_RestorePublicApiAnalyzer" AfterTargets="RazorCoreCompile">
  65. <ItemGroup>
  66. <Analyzer Include="@(_PublicAPIAnalyzers)" />
  67. </ItemGroup>
  68. </Target>
  69. <Target Name="GetCustomAssemblyAttributes"
  70. BeforeTargets="GetAssemblyAttributes"
  71. DependsOnTargets="InitializeSourceControlInformation">
  72. <ItemGroup>
  73. <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="'$(SourceRevisionId)' != ''">
  74. <_Parameter1>CommitHash</_Parameter1>
  75. <_Parameter2>$(SourceRevisionId)</_Parameter2>
  76. </AssemblyAttribute>
  77. <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="$(RepositoryUrl.StartsWith('https://github.com'))">
  78. <_Parameter1>SourceCommitUrl</_Parameter1>
  79. <_Parameter2>$(RepositoryUrl)/tree/$(SourceRevisionId)</_Parameter2>
  80. </AssemblyAttribute>
  81. <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="'$(Serviceable)' == 'true'">
  82. <_Parameter1>Serviceable</_Parameter1>
  83. <_Parameter2>True</_Parameter2>
  84. </AssemblyAttribute>
  85. </ItemGroup>
  86. </Target>
  87. <!-- Reference assemblies are shipped only in targeting packs. -->
  88. <Target Name="_RemoveReferenceAssembly" BeforeTargets="_GetPackageFiles;GenerateNuspec" Condition="$(HasReferenceAssembly)">
  89. <ItemGroup>
  90. <_PackageFilesToExclude Include="@(IntermediateRefAssembly);$(TargetRefPath)" />
  91. </ItemGroup>
  92. </Target>
  93. <Choose>
  94. <!--
  95. C# 8.0's nullable annotations only work for .NET Core 3.0+ by default. However it can be made to work for other target frameworks
  96. by compiling the annotations (as internal types).
  97. The code block that follows sets it up so projects in this repo that target ns2.0 or netfx can compile when Nullable is configured.
  98. Based on https://github.com/dotnet/runtime/blob/93b6c449d4f31ddd7d573d1d3769e681d5ebceb9/src/libraries/Directory.Build.targets#L215-L222
  99. SuppressNullableAttributesImport to disable. Used when attributes are already present from another project because of InternalsVisibleTo.
  100. -->
  101. <When Condition=" ('$(Nullable)' == 'annotations' OR '$(Nullable)' == 'enable') AND
  102. '$(SuppressNullableAttributesImport)' != 'true' AND
  103. (('$(TargetFrameworkIdentifier)' == '.NETStandard' AND $([MSBuild]::VersionLessThanOrEquals('$(TargetFrameworkVersion)', '2.1'))) OR '$(TargetFrameworkIdentifier)' == '.NETFramework')">
  104. <PropertyGroup>
  105. <DefineConstants>$(DefineConstants),INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
  106. <!-- Repo-specific property to enable nullability warnings for ns2.0 -->
  107. <NoWarn Condition=" '$(WarnOnNullable)' != 'true' ">$(NoWarn);nullable</NoWarn>
  108. </PropertyGroup>
  109. <ItemGroup>
  110. <Compile Include="$(SharedSourceRoot)Nullable\NullableAttributes.cs" />
  111. </ItemGroup>
  112. </When>
  113. </Choose>
  114. <Target Name="ValidateBenchmarks" BeforeTargets="Test" Condition="'$(IsMicrobenchmarksProject)' == 'true' AND '$(SkipMicrobenchmarksValidation)' != 'true'">
  115. <Message Text="%0ARunning benchmark validation for:%0A$(MSBuildProjectName)%0A"
  116. Importance="High" />
  117. <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="GetTargetPath">
  118. <Output TaskParameter="TargetOutputs" PropertyName="ExecPath" />
  119. </MSBuild>
  120. <Exec Command="dotnet $(ExecPath) -f * --validate-fast" IgnoreStandardErrorWarningFormat="true" StandardOutputImportance="low" />
  121. </Target>
  122. <Import Project="Packaging.targets" />
  123. <Import Project="ResolveReferences.targets" />
  124. </Project>