summaryrefslogtreecommitdiff
path: root/Tools/codeAnalysis.targets
blob: 856b665534af807f5019234fa0cba61d7504a8d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- 
    We should be using $(BuildToolsTaskDir) for this, however we are currently forcing that property to 
    always be net45 (because we use Mono to run our tests and Mono needs to load the net45 version)
  -->
  <PropertyGroup>
    <CodeAnalyzerDir Condition="'$(CodeAnalyzerDir)'=='' and '$(RunningOnCore)'!='true'">$(ToolsDir)net46/analyzers/</CodeAnalyzerDir>
    <CodeAnalyzerDir Condition="'$(CodeAnalyzerDir)'=='' and '$(RunningOnCore)'=='true'">$(ToolsDir)analyzers/</CodeAnalyzerDir>
  </PropertyGroup>

  <!-- PInvokeChecker data files-->
  <PropertyGroup Condition="'$(OSGroup)'=='Windows_NT' and '$(EnableDotnetAnalyzers)' == 'true'">
    <!-- Just validate for OneCore: default -->
    <ValidPInvokeMappings>$(MSBuildThisFileDirectory)PinvokeAnalyzer_Win32Apis.txt</ValidPInvokeMappings>

    <!-- Further restrict to API allowed for UWP -->
    <EnablePinvokeUWPAnalyzer Condition="'$(EnablePinvokeUWPAnalyzer)' == ''">true</EnablePinvokeUWPAnalyzer>
    <ValidPInvokeMappings Condition="'$(UWPCompatible)'=='true' and '$(EnablePinvokeUWPAnalyzer)' == 'true'">$(MSBuildThisFileDirectory)PinvokeAnalyzer_Win32UWPApis.txt</ValidPInvokeMappings>
  </PropertyGroup>

  <PropertyGroup Condition="'$(EnableFxCopAnalyzers)' == ''">
    <!-- %24 = $ -->
    <EnableFxCopAnalyzers Condition="$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectDirectory), 'src%24'))">true</EnableFxCopAnalyzers>
    <EnableFxCopAnalyzers Condition="'$(EnableFxCopAnalyzers)' != 'true'">false</EnableFxCopAnalyzers>
  </PropertyGroup>

  <ItemGroup Condition="$(EnableFxCopAnalyzers)">
    <Analyzer Include="$(CodeAnalyzerDir)*.dll" Exclude="$(CodeAnalyzerDir)Microsoft.DotNet.CodeAnalysis.dll"/>
  </ItemGroup>
  <PropertyGroup Condition="$(EnableFxCopAnalyzers)">
    <CodeAnalysisRuleSet Condition="'$(CodeAnalysisRuleSet)' == ''">$(MSBuildThisFileDirectory)Default.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup>
    <EnablePInvokeAnalyzer Condition="'$(EnablePInvokeAnalyzer)' == ''">true</EnablePInvokeAnalyzer>
  </PropertyGroup>
  <ItemGroup Condition="'$(EnableDotnetAnalyzers)' == 'true'">
    <!-- Enable the Microsoft.Dotnet.CodeAnalysis.dll analyzer -->
    <Analyzer Include="$(CodeAnalyzerDir)Microsoft.DotNet.CodeAnalysis.dll"/>

    <AdditionalFiles Include="$(MSBuildProjectDirectory)/*.analyzerdata" />
    <AdditionalFiles Include="$(MSBuildProjectDirectory)/*.analyzerdata.$(Platform)" />
    <AdditionalFiles Include="$(MSBuildProjectDirectory)/*.analyzerdata.$(TargetGroup)" />

    <!-- Add the necessary valid Pinvoke mapping file -->
    <AdditionalFiles Condition="'$(ValidPInvokeMappings)' != ''" Include="$(ValidPInvokeMappings)"/>

    <!-- Disable any analyzers that should not run -->
    <DisabledAnalyzers Condition="'$(IsTestProject)' == 'true' or '$(TargetsUnix)' == 'true' or '$(EnablePInvokeAnalyzer)' != 'true'" Include="PInvokeAnalyzer"/>
    <DisabledAnalyzers Condition="'$(TargetsWindows)'=='true' and ($(TargetGroup.Contains('net46')) or $(TargetGroup.Contains('net45')))" Include="PInvokeAnalyzer" />
  </ItemGroup>

  <!-- If we have specified any analyzers that need to be disabled write them into the file -->
  <Target Name="GenerateAnalyzerConfigFile" BeforeTargets="CoreCompile" Condition="'@(DisabledAnalyzers)'!='' and '$(EnableDotnetAnalyzers)' == 'true'" >
    <!-- Write the file only if it changed -->
    <ReadLinesFromFile File="$(IntermediateOutputPath)/disabledAnalyzers.config">
      <Output TaskParameter="Lines" ItemName="_DisabledAnalyzers" />
    </ReadLinesFromFile>

    <WriteLinesToFile File="$(IntermediateOutputPath)/disabledAnalyzers.config" Lines="@(DisabledAnalyzers)" Overwrite="true" 
                      Condition="'@(DisabledAnalyzers)' != '@(_DisabledAnalyzers)'"/>

    <ItemGroup>
      <AdditionalFiles Include="$(IntermediateOutputPath)/disabledAnalyzers.config"/>
      <FileWrites Include="$(IntermediateOutputPath)/disabledAnalyzers.config"/>
    </ItemGroup>
  </Target>

</Project>