After FxCop 10 Microsoft stopped shipping a separate installer for FxCop. Officially one can currently only run code analysis (FxCop 12.0 / 14.0 / 15.0) after installing Visual Studio 2013 / 2015 / 2017. However, we are adamant about not installing Visual Studio on the build agents (the installation needs then to be kept in-sync with what we have got on the developer computers etc.).
So how would i go about getting FxCop 12.0 / 14.0 / 15.0 to work on a build agent, preferrably without installing anything else? I would accept adding a few binaries and msbuild files to source control, though. Otherwise: Is there a way to only install the FxCop stuff using Visual Studio 2013 / 2015 / 2017 installer?
Note: we are using Teamcity as build server.
As there are multiple valid answers for specific environments and FxCop versions, I've taken the liberty of linking them here for easier access:
For FxCop 14.0 / VS2015 see this answer
Okay, i've invested 6 hours and now it's working. I've added all necessary executables, dlls and msbuild targets to source control.
These are the files that i had to add to source control: (Please consider that this might violate some license agreements)
(source control)\dev\tools\FxCop
│
├[amd64]
│ │
│ └msdia120.dll
├[Engines]
│ │
│ ├IntrospectionAnalysisEngine.dll
│ └PhoenixAnalysisEngine.dll
├[Msbuild]
│ │
│ ├fxcoptask.dll
│ ├Microsoft.CodeAnalysis.Targets
│ ├Microsoft.VisualStudio.CodeAnalysis.dll
│ └Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
├[Repository]
│ │
│ ├[Compatibility]
│ │ │
│ │ ├Desktop2.0.xml
│ │ ├Desktop2.0SP1.xml
│ │ ├Desktop2.0SP2.xml
│ │ ├Desktop3.0.xml
│ │ ├Desktop3.0SP1.xml
│ │ ├Desktop3.0SP2.xml
│ │ ├Desktop3.5.xml
│ │ └Desktop3.5SP1.xml
│ └system32.bin
├[Rules]
│ │
│ ├DataflowRules.dll
│ ├DesignRules.dll
│ ├GlobalizationRules.dll
│ ├InteroperabilityRules.dll
│ ├MaintainabilityRules.dll
│ ├MobilityRules.dll
│ ├NamingRules.dll
│ ├PerformanceRules.dll
│ ├PortabilityRules.dll
│ ├ReliabilityRules.dll
│ ├SecurityRules.dll
│ ├SecurityTransparencyRules.dll
│ └UsageRules.dll
├[Xml]
│ │
│ ├CodeAnalysisReport.xsl
│ ├FxCopReport.xsl
│ └VSConsoleOutput.xsl
├Architecture-msil.dll
├CodeAnalysis.dll
├CustomDictionary.xml
├FxCopCmd.exe
├FxCopCmd.exe.config
├FxCopCommon.dll
├FxCopSdk.dll
├Microsoft.Cci.dll
├Microsoft.VisualStudio.CodeAnalysis.Common.dll
├Microsoft.VisualStudio.CodeAnalysis.DataflowModels.dll
├Microsoft.VisualStudio.CodeAnalysis.dll
├Microsoft.VisualStudio.CodeAnalysis.Interop.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.xml
├msdia120.dll
├mssp7en.dll
├mssp7en.lex
├phx.dll
└Runtime-vccrt-win-msil.dll
Copy them as follows:
entire FxCop installation folder contents from
%programfiles(x86)%\Microsoft Visual Studio 12.0\Team Tools\Static Analysis Tools\FxCop
from Visual Studio 2013 C++ redist, or any other place: (also see legal information) copy msdia120 x86 and x64 to:
msdia120.dll (874 KiB)
amd64\msdia120.dll (1.07 MiB)
from the Global Assembly Cache (C:\Windows\Microsoft.NET\assembly\GAC_MSIL\_NameOfTheAssembly_\
) of a computer where VS2013 is installed, copy the following DLLs to:
(Make sure the DLLs are version 12.0 !)
Msbuild\Microsoft.VisualStudio.CodeAnalysis.dll
Msbuild\Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
All files from %programfiles(x86)%\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis
to
Msbuild\fxcoptask.dll
Msbuild\Microsoft.CodeAnalysis.Targets
(Furthermore you need the appropriate Windows (7.1 / 8.1) SDK for building the .net 4.0 / 4.5 application installed on the build agent)
Additionally we had to adjust the project msbuild file as follows:
<!--Must import code analysis target before importing csharp targets, so that the correct code analysis targets gets imported. -->
<Import Project="$(ProjectBuildScriptDir)Custom.CodeAnalysis.targets"/>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
And this is what our Custom.CodeAnalysis.targets contains:
<PropertyGroup>
<!-- Code analysis settings. -->
<CodeAnalysisCulture>en-US</CodeAnalysisCulture>
<!-- change this so it points to your ruleset or remove it entirely -->
<CodeAnalysisRuleSet>$(SourcesDir)Custom.ruleset</CodeAnalysisRuleSet>
<!-- this must refer to the source control directory where you copied FxCopCommand.exe (and the rest of the FxCop files and directories...) to -->
<CodeAnalysisPath>$(ToolsDir)FxCop\</CodeAnalysisPath>
<!-- this must refer to the source control directory where you copied fxcoptask.dll, Microsoft.CodeAnalysis.Targets, Microsoft.VisualStudio.CodeaAnalysis.dll and Microsoft.VisualStudio.CodeaAnalysis.Sdk.dll to -->
<CodeAnalysisTargets>$(CodeAnalysisPath)Msbuild\Microsoft.CodeAnalysis.Targets</CodeAnalysisTargets>
</PropertyGroup>
<!-- configure this according to your wishes -->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<!-- Report code analysis results to TeamCity -->
<Target Name="ReportCodeAnalysisResults" AfterTargets="RunCodeAnalysis" Condition="'$(RunCodeAnalysis)' == 'true' And '$(IsRunningOnTeamCity)' == 'true'">
<Message Text="##teamcity[importData type='FxCop' path='$(CodeAnalysisLogFile)']" Importance="High" />
</Target>
</Project>