Search code examples
c++visual-studiomsbuildvisual-studio-2017cpp-core-guidelines

Cpp Core Guidelines Checker using console and MSBuild


I have solution with some projects that are set up for building. I want to run guideline checkers for solutions. Using this MSDN article I don't want to modify project files, so I created batfile:

call "%VS140COMNTOOLS%\VsMSBuildCmd.bat"
msbuild Olymp.sln /p:EnableCppCoreCheck=true /p:RunCodeAnalysis=true /p:CodeAnalysisRuleSet="C:\Program Files (x86)\Microsoft Visual Studio\Preview\Community\Team Tools\Static Analysis Tools\Rule Sets\CppCoreCheckRules.ruleset" /t:Rebuild

Project is set up to NOT perform code analysis enter image description here

But I'm overriding it using bat file When I run this code I have no output about warnings (as on the MSDN), that are exists:

E:\Code\Olymp\Olymp>msbuild Olymp.sln /p:EnableCppCoreCheck=true /p:RunCodeAnalysis=true /p:CodeAnalysisRuleSet="C:\Program Files (x86)\Microsoft Visual Studio\Preview\Community\Team Tools\Static Analysis Tools\Rule Sets\CppCoreCheckRules.ruleset" /t:Rebuild
Microsoft (R) Build Engine version 14.0.25420.1
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
Build started 10.04.2018 18:42:59.
Project "E:\Code\Olymp\Olymp\Olymp.sln" on node 1 (Rebuild target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|x64".
Project "E:\Code\Olymp\Olymp\Olymp.sln" (1) is building "E:\Code\Olymp\Olymp\Olymp\Olymp.vcxproj" (2) on node 1 (Rebuil
d target(s)).
_PrepareForClean:
  Deleting file "x64\Debug\Olymp.tlog\Olymp.lastbuildstate".
InitializeBuildStatus:
  Creating "x64\Debug\Olymp.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /D CODE_
  ANALYSIS /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"x64\Debug\\" /Fd"x64\D
  ebug\vc140.pdb" /Gd /TP /analyze /errorReport:queue   /analyze:quiet main.cpp
  main.cpp
Link:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\link.exe /ERRORREPORT:QUEUE /OUT:"E:\Code\Olymp\
  Olymp\x64\Debug\Olymp.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.
  lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiA
  ccess='false'" /manifest:embed /DEBUG:FASTLINK /PDB:"E:\Code\Olymp\Olymp\x64\Debug\Olymp.pdb" /TLBID:1 /DYNAMICBASE /
  NXCOMPAT /IMPLIB:"E:\Code\Olymp\Olymp\x64\Debug\Olymp.lib" /MACHINE:X64 x64\Debug\main.obj
  Olymp.vcxproj -> E:\Code\Olymp\Olymp\x64\Debug\Olymp.exe
  Olymp.vcxproj -> E:\Code\Olymp\Olymp\x64\Debug\Olymp.pdb (Partial PDB)
FinalizeBuildStatus:
  Deleting file "x64\Debug\Olymp.tlog\unsuccessfulbuild".
  Touching "x64\Debug\Olymp.tlog\Olymp.lastbuildstate".
Done Building Project "E:\Code\Olymp\Olymp\Olymp\Olymp.vcxproj" (Rebuild target(s)).

Done Building Project "E:\Code\Olymp\Olymp\Olymp.sln" (Rebuild target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.68

Yes, I can collect all cpps from the project and try to run analyzer as a cl.exe argument, but I want to use msbuild. What is wrong with my setup?


Solution

  • Cpp Core Guidelines Checker using console and MSBuild

    I found you have set the environment variables for the Visual Studio 2015 command line by the script call "%VS140COMNTOOLS%\VsMSBuildCmd.bat". The %VS140COMNTOOLS% is used to set the environment variables for the Visual Studio 2015 command line not Visual Studio 2017. Then you can build your project successfully without any error, so I suspect this project may created by Visual Studio 2015.

    If yes, you will get the result with no output about warnings. That because the core guideline checkers are installed by default in Visual Studio 2017 not in Visual Studio 2015, they are available as a NuGet package for Visual Studio 2015.

    To set the environment variables for the Visual Studio 2017 command line, you can call VsMSBuildCmd.bat from the location:

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools
    

    Since you are using the Preview version Visual Studio, it should be:

    C:\Program Files (x86)\Microsoft Visual Studio\Preview\Community\Common7\Tools
    

    To verify this via MSBuild, I have created a test sample project with Visual Studio 2017(Not modify project files), then created a bat file with following scripts in it:

    call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsMSBuildCmd.bat"
    MSBuild "C:\Users\Admin\Source\repos\ConsoleApplication1\ConsoleApplication1.sln" /p:EnableCppCoreCheck=true /p:RunCodeAnalysis=true /p:CodeAnalysisRuleSet="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Static Analysis Tools\Rule Sets\CppCoreCheckRules.ruleset" /t:Rebuild
    pause
    

    After executed the bat file, I got the warnings:

    enter image description here

    To verify this, you can also create a new project with Visual Studio 2017, then build it with that bat file.

    Hope this helps.