Specify ruleset for Visual Studio code analysis on command line or from CMake - c++

I am trying to enable code analysis for my CMake driven project. I generate Visual Studio 2013 solutions, and would like to enable code analysis with a specific rule set. I know that I can enable the code analysis by specifying add_definitions("/analyze") in my CMake file, but how do I control which rule set is used?
As far as I can see, the only way to control the ruleset used is via the xml element CodeAnalysisRuleSet in the project file, but I cannot access this from CMake.

If you are trying to solve this for a command line build--either via cmake --build or direct invocation of msbuild--and not when using the generated solution with Visual Studio, you can set the relevant properties when you invoke the build.
With cmake driving the build:
PS c:\build-dir> cmake --build . -- '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=NativeRecommendedRules.ruleset'
With MSBuild driving the build:
PS c:\build-dir> msbuild ALL_BUILD.vcxproj '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=NativeRecommendedRules'
If you have a custom ruleset in a custom directory, you will also need to set the property CodeAnalysisRuleSetDirectories:
PS c:\build-dir> cmake --build . -- '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=custom.ruleset' `
'/p:CodeAnalysisRuleSetDirectories=c:\src\ruletsets'
(Tested with CMake 3.8.0, MSBuild 15.3.409.57025, and Visual C++ 2017 19.11.25506.)

I am not sure how far this works for VS2013 solutions. With Visual Studio 2015 I am using Project User Templates (*.vcxproj.user) with something along the following lines:
C:\MyProject\Template.USERNAME.user
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<CodeAnalysisRuleSet>#CMAKE_SOURCE_DIR#\SecurityRecommended.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
</Project>
You will probably have to change a few things like the ToolsVersion to 12.x.
The #CMAKE_SOURCE_DIR# will automatically get replaced by cmake when you use the CONFIGURE_FILE function to generate the actual PROJECT.vcxproj.user file. I have the following line in the CMakeLists.txt of my projects:
SET(USER_NAME $ENV{USERNAME} CACHE STRING UserName)
SET(USER_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.vcxproj.user)
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/Template.${USER_NAME}.user ${USER_FILE} #ONLY)
In the end, I would assume you could user-define anything that's in the PROJECT.vcxproj with those templates.

VS2017:In CMakeSettings.json section "configuration" add parameter "codeAnalysisRuleset": "NativeRecommendedRules.ruleset". Then in Solution Explorer right-click on need file .cpp select menu "Run Code Analysis on File". You can choose from a variety of analyzers in VS folder "c:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Static Analysis Tools\Rule Sets\NativeRecommendedRules.ruleset"

Related

Bullet Physics, CMake fails to build project

I'm trying to use CMake to build the Bullet Physics 3 source code for Visual Studio 2012.
I've tried using the CMake GUI to build the projects and the command line version. Each time I get a set of vcxproj and sln files:
project.sln
ZERO_CHECK.vcxproj
ALL_BUILD.vcxproj
BulletCollision.vcxproj
BulletDynamics.vcxproj
BulletSoftBody.vcxproj
LinearMath.vcxproj
This is not what I expected. The documentation here: says there should be BULLET_PHYSICS.sln that does not appear.
Furthermore when I try to import the projects into Visual Studio and do a build all of the #include lines fail. They're trying to find the files originally referenced in the Bullet Physics source directory and failing rather than looking the the current Visual Studio project folder where I've put the CMake built output. I tried manually editing all the project and solution files which contained many occurrences of the original source path and not my new build directory but this made no difference.
Why is CMake failing to build Bullet Physics properly?

MsBuild can't find lib

I am using the following script to build a C++ project:
msbuild D:\proj.vcxproj /p:Configuration="Release" /p:Platform="x64" /p:BuildProjectReferences=false /p:AssemblySearchPaths="D:\Import\GenericAIC\Include" /p:AdditionalLibPaths="D:\Export\Release"
I am getting an error that it can't find a certain lib. that lib is present in the D:\Export\Release folder which is set as p:AdditionalLibPaths
The project compiles perfectly in Visual studio.
Any thoughts?
Since you are building a project and not a solution,
you don't have the $SolutionDir, and MSBuild cant interpert all the macros in the project file.
you just need to add /Property:SolutionDir="" and everything will compile.

Working directory for google test in Visual Studio

I have a Visual Studio 2012 C++ solution generated using CMake in which I use google test for unit tests. This works mostly fine, but in one of my tests I want to read a settings file from a local directory. To find the file I copy the file as a post build step from my source code tree to the build and install directory using the following CMake commands:
install(FILES ./adapters/settingFile.txt DESTINATION .)
add_custom_command(TARGET testAdapters POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/adapters/settingFile.txt"
"${CMAKE_CURRENT_BINARY_DIR}"
COMMENT "Copying elastix parameter files")
This works fine: after building my test the settingFile.txt is in the same location as the testAdapters.exe. Using a right click on the testAdapters project and starting a Debug session also works find.
However if I choose to run the test from within the "Test Explorer" window, either by "Run All" or by right clicking the test and choosing "Run selected tests", the test cannot find settingsFile.txt. By right clicking and choosing "Debug selected tests" I found that running the test from the "Test Explorer" the working directory defaults to the visual studio program directory: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE. I can think of several possible solutions, but don't know how to achieve this:
Set the working directory for the "Test Explorer"
Set the working directory for each test executable
Set the working directory for all google tests
Using CMake set some define that points to a user specified location and use that in the test code. (I consider this a rather ugly solution)
I need a solution that is platform independent. Does anyone know how to achieve (1) or (2) or do you know of a better solution?
With the current version 0.12.3 of GTA you can at least achieve (1):
Tools
Options
Google Test Adapter (or use Search Options)
General
Working directory (at the bottom)
Unfortunately GTA seems to only support $(ExecutableDir) (the default) and $(SolutionDir). It seems that GTA cannot tell which project is the unit test project, so it is not possible to use the project directory as a start directory.

How does Visual Studio 2010 hosts MSBuild for C++ projects?

I have a solution with several C++ projects. For some of the projects I need some custom file copy, e.g. to copy some configuration files to the output directory or to copy the output files of one project to a specific folder after build.
In some cases I don't want or cannot add these files to the projects directly through the Visual Studio IDE. I created simple .targets files which I can reuse and add to the projects which need the file copying.
Here is a simple example .targets file for copying configuration files:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
CopyCustom
</BuildDependsOn>
</PropertyGroup>
<ItemGroup>
<CustomFiles Include="$(ProjectDir)Config\**\*.xml" />
</ItemGroup>
<PropertyGroup>
<DestCustFolder>$(OutDir)Config\</DestCustFolder>
</PropertyGroup>
<Target Name="CopyCustom"
Inputs="#(CustomFiles )"
Outputs="#(CustomFiles ->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')">
<Message Text="Copy custom files..." />
<Copy SourceFiles="#(CustomFiles )" DestinationFiles="#(CustomFiles->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')" SkipUnchangedFiles="true" />
</Target>
</Project>
Through the "Build Customization" dialog in Visual Studio I add it to the project so it will be included like this at the end of the project file:
<ImportGroup Label="ExtensionTargets">
<Import Project="..\Targets\CopyCustom.targets" />/
</ImportGroup>
This should enable incremental build of my custom target. If I just edit one of my custom files (and none of the C++ files) and build it form the console with
msbuild foo1.vcxproj
it will actually detect the changes and does an incremental build for my custom target. If no changes are made the target is skipped.
If I do however build inside Visual Studio it will not detect changes to the custom files and only and gives me the message that the project is up to data:
========== Build: 0 succeeded, 0 failed, 5 up-to-date, 0 skipped ==========
I would have to additionally change one of the C++ files to make it check all targets again and to the incremental build.
I was expecting that Visual Studio just executes MSBuild which will then do the up-to-date check on the projects, so it should be the same result as running MSBuild from the console. I was trying to get more information by setting the verbosity level to diagnostic but I just get the same line. It seems to me that MSBuild is not even executed for the project but Visual Studio itself determines that the project is up-to-date.
So I was wondering how Visual Studio actually determines when it should execute MSBuild for a project.
I asked basically the same question before on the MSDN forum but couldn't get a clear answer.
See this suggestion on Microsoft Connect.
Basically you need to set DisableFastUpToDateCheck property to true to disable the fast-up-to-date check.
Just add to your vcxproj or your targets file:
<PropertyGroup>
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>
I found an answer by looking into the book "Inside the Microsoft Build Engine, Second Edition".
Note: I also updated the same in my question in the MSDN forum but I will mainly duplicate the text here again for completeness.
On page 280 they actually saying that the IDE does a "fast up-to-date check" on the project-level. It only spawns a project build and does a more fine-grained check on the individual tasks if this rough project-level check fails.
When running MSBuild from the command line however there is always a fine-grained up-to-date check on the individual tools.
So the IDE only seems to do this fast check on the files which are added to the projects directly and set as one of the "Input File" types.
In my opinion this is not a good design. I would prefer that the IDE is only used to edit the MSBuild project files and then just invokes MSBuild to do the up-to-date check. This would make it much clearer.
I can understand that in a solution with a lot of projects it can make the up-to-date check much faster but there should be at least an option to disable the fast up-to-date check. I was checking the IDE if there is a way to disable this behavior but could not find anything.
The solution suggested here actually works and I am using it at the moment. But I added several custom targets for different kinds of custom files. If I add a new custom file I should never forget to set it to "Custom Build Tool" otherwise the incremental build for this file will not work.
I guess a solution would be to make a full build customization for my custom files and use specific file extensions for them so Visual Studio will automatically detect them when I add them to the project and sets the right Item Type.

How do I build all configurations of a Visual Studio 2008 C++ project on the command line?

I'd like to build all the configurations of a VS 2008 C++ project on the command line. Something like:
devenv TheProject.vcproj /build /nologo
But this doesn't work because the /build command insists on having a configuration following it like this:
devenv TheProject.vcproj /build "Release|Win32" /nologo
Is there a way to get a command line build of all configurations within the vcproj file?
I was thinking you can do what you want with MSBUILD, but it appears that it isn't much better for this than DEVENV.
You still have to specify each configuration on the command line, although it would be easy to write a batch file to accomplish this with either MSBUILD or DEVENV.
It looks like previous versions of the development environment may have supported an "ALL" option, but VS 2008 does not.
http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/8701b3d0-d5c9-45fb-8dd4-e7700c8caca6/
Not directly, but you can have projects depend on other projects - so you could have an 'all' or 'install' project with a dependacy of everything else.
Haven't used VS in a long time. But the project properties panel used to show the command line generated for linking and compiling a project for a particular configuration. It used to be under the Advanced tab. Will using that directly from the command line serve your purpose? This method will not use the VS IDE at all.
Alternatively,
Steps:
Create a project which has a dependency on all other projects.
Write a script which builds this project with different configurations sequentially. You cannot create a single configuration which encapsulates all other configurations.