CSharp.Common.targets 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. <Nullable Condition=" '$(Nullable)' == '' AND (
  16. '$(IsImplementationProject)' == 'true' OR
  17. '$(IsAnalyzersProject)' == true OR
  18. '$(IsSpecificationTestProject)' == 'true') ">enable</Nullable>
  19. <!-- Turn off platform compatibility analyzer warnings in test, test assets, and samples. -->
  20. <NoWarn Condition=" '$(IsTestProject)' == 'true' OR
  21. '$(IsTestAssetProject)' == 'true' OR
  22. '$(ISBenchmarkProject)' == 'true' OR
  23. '$(IsSampleProject)' == 'true' OR
  24. '$(IsMicrobenchmarksProject)' == 'true'">$(NoWarn);CA1416</NoWarn>
  25. </PropertyGroup>
  26. <ItemGroup Condition=" '$(DotNetBuildFromSource)' != 'true' AND $(AddPublicApiAnalyzers) ">
  27. <Reference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" ExcludeAssets="Compile" PrivateAssets="All" />
  28. </ItemGroup>
  29. <Target Name="_CheckIgnoredPublicApiFiles"
  30. BeforeTargets="Build;Restore"
  31. Condition=" Exists('$(MSBuildProjectDirectory)\PublicAPI.Shipped.txt') AND ! $(AddPublicApiAnalyzers) ">
  32. <Warning Text="Public API baseline files ignored." />
  33. </Target>
  34. <Target Name="_RemovePublicApiAnalyzer" BeforeTargets="RazorCoreCompile">
  35. <ItemGroup>
  36. <_PublicAPIAnalyzers Include="@(Analyzer->WithMetadataValue('NuGetPackageId','Microsoft.CodeAnalysis.PublicApiAnalyzers'))" />
  37. <Analyzer Remove="@(_PublicAPIAnalyzers)" />
  38. </ItemGroup>
  39. </Target>
  40. <Target Name="_RestorePublicApiAnalyzer" AfterTargets="RazorCoreCompile">
  41. <ItemGroup>
  42. <Analyzer Include="@(_PublicAPIAnalyzers)" />
  43. </ItemGroup>
  44. </Target>
  45. <Target Name="GetCustomAssemblyAttributes"
  46. BeforeTargets="GetAssemblyAttributes"
  47. DependsOnTargets="InitializeSourceControlInformation">
  48. <ItemGroup>
  49. <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="'$(SourceRevisionId)' != ''">
  50. <_Parameter1>CommitHash</_Parameter1>
  51. <_Parameter2>$(SourceRevisionId)</_Parameter2>
  52. </AssemblyAttribute>
  53. <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="$(RepositoryUrl.StartsWith('https://github.com'))">
  54. <_Parameter1>SourceCommitUrl</_Parameter1>
  55. <_Parameter2>$(RepositoryUrl)/tree/$(SourceRevisionId)</_Parameter2>
  56. </AssemblyAttribute>
  57. <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="'$(Serviceable)' == 'true'">
  58. <_Parameter1>Serviceable</_Parameter1>
  59. <_Parameter2>True</_Parameter2>
  60. </AssemblyAttribute>
  61. <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="'$(Trimmable)' == 'true'">
  62. <_Parameter1>IsTrimmable</_Parameter1>
  63. <_Parameter2>True</_Parameter2>
  64. </AssemblyAttribute>
  65. </ItemGroup>
  66. </Target>
  67. <!-- Reference assemblies are shipped only in targeting packs. -->
  68. <Target Name="_RemoveReferenceAssembly" BeforeTargets="_GetPackageFiles;GenerateNuspec" Condition="$(HasReferenceAssembly)">
  69. <ItemGroup>
  70. <_PackageFilesToExclude Include="@(IntermediateRefAssembly);$(TargetRefPath)" />
  71. </ItemGroup>
  72. </Target>
  73. <Choose>
  74. <!--
  75. 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
  76. by compiling the annotations (as internal types).
  77. 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.
  78. Based on https://github.com/dotnet/runtime/blob/93b6c449d4f31ddd7d573d1d3769e681d5ebceb9/src/libraries/Directory.Build.targets#L215-L222
  79. SuppressNullableAttributesImport to disable. Used when attributes are already present from another project because of InternalsVisibleTo.
  80. -->
  81. <When Condition=" ('$(Nullable)' == 'annotations' OR '$(Nullable)' == 'enable') AND
  82. '$(SuppressNullableAttributesImport)' != 'true' AND
  83. (('$(TargetFrameworkIdentifier)' == '.NETStandard' AND $([MSBuild]::VersionLessThanOrEquals('$(TargetFrameworkVersion)', '2.0'))) OR '$(TargetFrameworkIdentifier)' == '.NETFramework')">
  84. <PropertyGroup>
  85. <DefineConstants>$(DefineConstants),INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
  86. <NoWarn>$(NoWarn);nullable</NoWarn>
  87. </PropertyGroup>
  88. <ItemGroup>
  89. <Compile Include="$(SharedSourceRoot)Nullable\NullableAttributes.cs" />
  90. </ItemGroup>
  91. </When>
  92. </Choose>
  93. <Target Name="ValidateBenchmarks" BeforeTargets="Test" Condition="'$(IsMicrobenchmarksProject)' == 'true' AND '$(SkipMicrobenchmarksValidation)' != 'true'">
  94. <Message Text="%0ARunning benchmark validation for:%0A$(MSBuildProjectName)%0A"
  95. Importance="High" />
  96. <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="GetTargetPath">
  97. <Output TaskParameter="TargetOutputs" PropertyName="ExecPath" />
  98. </MSBuild>
  99. <Exec Command="dotnet $(ExecPath) -f * --validate-fast" IgnoreStandardErrorWarningFormat="true" StandardOutputImportance="low" />
  100. </Target>
  101. <Import Project="Packaging.targets" />
  102. <Import Project="ResolveReferences.targets" />
  103. </Project>