This post provides step-by-step instructions on how to integrate Visual Studio 2010's code analysis into your build. If you've not yet done so, please read my first post for an overview of what will be achieved. If you'd prefer, you can integrate Visual Studio 2008's code analysis by following the instructions in my previous post. Whilst you will need a machine with Visual Studio 2010 installed, this won't be a requirement for building your project once it's set up correctly. I assume Visual Studio is installed in the default location - adjust paths as necessary.
Assume we are starting with the following directory structure:
Project
Lib
Src
Step 1: Copy Code Analysis Tooling
Copy the entire contents of C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop to Lib\Code Analysis.
Copy the entire contents of C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\CodeAnalysis to Lib\Code Analysis.
Copy the following files to Lib\Code Analysis:
- C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualC\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualC.Dll
- C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.CodeAnalysis.Sdk\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
- C:\Program Files\Microsoft Visual Studio 10.0\DIA SDK\bin\msdia100.dll
- C:\Program Files\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\msvcp100.dll
- C:\Program Files\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\msvcr100.dll
Step 2: Create Code Analysis Targets File
Create a file called CodeAnalysis.targets and put it in your Src directory. Here is a starting point for the contents of this file. You should tweak as necessary for your needs:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!--
Inject our own target before the code analysis runs.
-->
<RunCodeAnalysisDependsOn>
ConfigureCodeAnalysis;
$(RunCodeAnalysisDependsOn);
</RunCodeAnalysisDependsOn>
<!--
Ensure code analysis is run
-->
<RunCodeAnalysis>True</RunCodeAnalysis>
<!--
Set this to false if you don't want all code analysis violations to be treated as errors.
-->
<CodeAnalysisTreatWarningsAsErrors>True</CodeAnalysisTreatWarningsAsErrors>
<!--
This should be set to resolve to the Lib directory, which must contain the code analysis tooling.
-->
<PathToLib>$(MSBuildProjectDirectory)\..\..\Lib\</PathToLib>
<!--
This should be set to resolve to the directory containing this targets file.
-->
<PathToTargets>$(MSBuildProjectDirectory)\..\</PathToTargets>
<!--
Setting these properties is required in order for the code analysis targets to execute correctly.
Without setting these, it will look for the tooling under default installation directories instead
-->
<CodeAnalysisTargets>$(PathToLib)\Code Analysis\Microsoft.CodeAnalysis.Targets</CodeAnalysisTargets>
<CodeAnalysisPath>$(PathToLib)\Code Analysis</CodeAnalysisPath>
<!--
Assign default code analysis rules
-->
<CodeAnalysisRuleSet>$(PathToTargets)CodeAnalysis.Default.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<UsingTask AssemblyFile="$(PathToLib)\MSBuildSdcTasks\Microsoft.Sdc.Tasks.dll" TaskName="StringComparison"/>
<Target Name="ConfigureCodeAnalysis">
<!--
Assume that any projects with ".Tests" in their names are test projects
-->
<StringComparison Comparison="Contains" Param1="$(AssemblyName)" Param2=".Tests">
<Output TaskParameter="Result" PropertyName="IsTestProject"/>
</StringComparison>
<!--
Assign different rules for test projects (more relaxed)
-->
<CreateProperty Condition="$(IsTestProject)" Value="$(PathToTargets)CodeAnalysis.Tests.ruleset">
<Output TaskParameter="Value" PropertyName="CodeAnalysisRuleSet"/>
</CreateProperty>
</Target>
</Project>
Step 3: Create Rule Sets
Create files called CodeAnalysis.Default.ruleset and CodeAnalysis.Tests.ruleset in your Src directory. You can create these files with Visual Studio 2010 by choosing File / New / File / Code Analysis Rule Set. Alternatively, you can just copy the files from the example in the download.
Step 4: Enable Code Analysis for Relevant Projects
For every project that requires code analysis, open the .csproj file and insert the following before the import of Microsoft.CSharp.targets:
<Import Project="..\CodeAnalysis.targets" />
NOTE: it's very important that this be inserted before the import of Microsoft.CSharp.targets, not after.
Step 5: Tweak as Necessary
You may wish to tweak the CodeAnalysis.targets, CodeAnalysis.Default.ruleset, and CodeAnalysis.Tests.ruleset files in order to alter the rules that are enabled, the conditions in which sets of rules are used, etc. As mentioned above, you can use VS2010 to create and edit .ruleset files.
Code analysis is now integrated with your projects. Where you're building from is irrelevant - whether it's Visual Studio, the command line, or your build server. In all cases code analysis will execute for your project. You can download an example solution showing all this in action below.
Troubleshooting
If you get CA0001 Phx.FatalError, you likely need to register the msdia100.dll on the build machine:
regsvr32 msdia100.dllYou could possibly incorporate this into your build script, too, if your build user has sufficient rights.