0

I build my solution using MSBUILD on command line like this:

msbuild %SOLUTION% /m /fl /flp:LogFile="%OUTPUTFILE%" /p:Configuration=%BUILDCONFIG% /p:RunCodeAnalysis=True

Having /p:RunCodeAnalysis=True creats code analysis results for both native (PREfast) and managed (FxCop) code, but my problem is, that the analysis result files for native code are not placed into $(OutDir) like it is the case for the manged code results. They are stored in the obj folder of each assembly (=$(IntDir)) instead.

I tracked down the path to the standard target file Microsoft.CodeAnalysis.Targets, and then changed the line

<MergedOutputCodeAnalysisFile>$(IntDir)vc.nativecodeanalysis.all.xml</MergedOutputCodeAnalysisFile>

to

<MergedOutputCodeAnalysisFile>$(OutDir)$(TargetName).nativecodeanalysis.TEST.xml</MergedOutputCodeAnalysisFile>

and it worked, but I can't ask every developer to change this file on his/her system, so I need a way to set this inside the project files. I have already tried following methods, but had no success:

  1. Add a property to each project file (on the root level):

     <PropertyGroup>
         <OutputCodeAnalysisFile>$(OutDir)$(TargetName).NativeCodeAnalysis.TEST.xml</OutputCodeAnalysisFile>
     </PropertyGroup>
    
  2. Calling MSBUILD with the desired property value:

    msbuild %SOLUTION% /m /fl /flp:LogFile="%OUTPUTFILE%" /p:Configuration=%BUILDCONFIG% /p:RunCodeAnalysis=True /p:MergedOutputCodeAnalysisFile="$(OutDir)$(TargetName).nativecodeanalysis.TEST.xml"
    
  3. Using target injection by adding this lines to the vcxproj file after <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />:

    <PropertyGroup>
      <RunMergeNativeCodeAnalysisDependsOn>
        $(RunMergeNativeCodeAnalysisDependsOn);
        CustomOutputNativeCodeAnalysisFile
      </RunMergeNativeCodeAnalysisDependsOn>
    </PropertyGroup>
    
    <Target Name="CustomOutputNativeCodeAnalysisFile">
      <PropertyGroup>
        <OutputCodeAnalysisFile>$(OutDir)$(TargetName).NativeCodeAnalysis.TEST.xml</OutputCodeAnalysisFile>
      </PropertyGroup>
    </Target>
    

Does anybody know, how to solve the problem without touching the standard code analysis target?

Mehrdad Mirreza
  • 984
  • 12
  • 20

3 Answers3

0

Unfortunately, I have been unsuccessful in my own attempts to change where the individual files are output using the same strategies you identified above. However, I have succeeded in changing the location of the merged file, which might be a workaround for your problem.

The important thing to remember is that MSBuild uses the last property when evaluating multiple property sheets.

I found the Microsoft.CodeAnalysis.Targets file was included in a line at the end of the .vcxproj I was using:

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

Note: You can trace backward from the Microsoft.CodeAnalysis.Targets - I'll leave that as an exercise for the reader.

What I ended up doing was defining a .props file with the following contents:

<?xml version="1.0" encoding="utf-8"?>
  <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup Label="Configuration">
    <RunCodeAnalysis>true</RunCodeAnalysis>
    <CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
  </PropertyGroup>
  <ItemDefinitionGroup>
    <ClCompile>
      <EnablePREfast>true</EnablePREfast>
      <AdditionalOptions>%(ClCompile.AdditionalOptions)  /analyze:quiet</AdditionalOptions>
    </ClCompile>
  </ItemDefinitionGroup>
  <PropertyGroup>          
    <MergedOutputCodeAnalysisFile>$(OutDir)$(ProjectName).vc.nativecodeanalysis.all.xml</MergedOutputCodeAnalysisFile>
  </PropertyGroup>
  </Project>

Once I had this .props file, I then included it after the Microsoft.Cpp.targets file in my .vcxproj:

<ImportGroup>
    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    <!-- IMPORTANT. The analysis.props file you made *must* come after the Microsoft.Cpp.targets file! -->
    <Import Project="..\..\Config\VSPropertySheets\CodeAnalysis.props" />
 </ImportGroup>

This took the *.nativecodeanalysis.xml files generated and set the merged output file to something I specified.

I know this didn't answer your question, but it may give you an alternative solution to getting the results of the analysis in a single location. Unfortunately, I don't have the reputation for a comment or I would have added it as one. I am hoping to pursue this line of investigation a bit more this weekend and see if I can actually solve your issue.

Kevin K
  • 408
  • 2
  • 18
0

just change the option in project properties: right click on project, select properties on Tab Code analysis/General, uncheck Enable Code analysis on Build

0

I believe the most standard way of doing this (altering/overriding MSBuild project properties) is to use Directory.build.props, which allows you to override MSBuild properties at a Solution wide level or Project wide level - based on location of the file - at least for C++ and C#.

Screenshot of Enabling Clang-Tidy and Code Analysis On Build in Directory.build.props

coolhandle01
  • 81
  • 14