Reputation: 469
I have a CMakefile that defines cppcheck as described in Cppcheck support in CMake works great, but I want to be able to compile my code over and over again and not run cppcheck until I think I am close to done, and then run cppcheck prior to commit.
cppcheck is too slow to have as part of my edit/compile/test/debug cycle.
What is the best practice for this? I am thinking something like define a variable and generate files, and build and build and build. e.g.
In CMakeLists.txt
if(CPPCHECK STREQUAL "yes")
set(CMAKE_CXX_CPPCHECK "cppcheck")
endif()
While writing code
cmake3 -G "Unix Makefiles" ..
cmake3 --build .
cmake3 --build .
Then before committing code
cmake3 -G "Unix Makefiles" -DCPPCHECK=yes ..
cmake3 --build .
Is there a better way?
Upvotes: 4
Views: 2475
Reputation: 1914
I do it mostly like the question described - only run code analysis if a particular variable is set. I typically follow a pattern like this in my top-level CMakeLists.txt
:
option(ENABLE_CODE_ANALYSIS "Run code analysis" OFF)
message(STATUS "ENABLE_CODE_ANALYSIS ${ENABLE_CODE_ANALYSIS}")
if(ENABLE_CODE_ANALYSIS)
find_program(cppcheck cppcheck)
message(STATUS "cppcheck ${cppcheck}")
if(NOT (cppcheck MATCHES "NOTFOUND"))
# Notes:
# - unmatchedSuppression needed since not all source files are
# analyzed simultaneously
# - unusedFunction needed since not all source files are
# analyzed simultaneously
# - template needed to force "warning" into output string, and
# to make the 'id' available for suppression
set(CMAKE_CXX_CPPCHECK "${cppcheck}"
"--enable=all"
"--inconclusive"
"--inline-suppr"
"--quiet"
"--suppress=unmatchedSuppression"
"--suppress=unusedFunction"
"--template='{file}:{line}: warning: {id} ({severity}): {message}'")
endif()
endif(ENABLE_CODE_ANALYSIS)
Typically I will do the same for clang-tidy
, and have ENABLE_CODE_ANALYSIS
enable both cppcheck
and clang-tidy
.
I default to no code analysis, because code analysis tends to be slow. But it can be enabled by doing cmake -G "my generator" -DENABLE_CODE_ANALYSIS=ON ..
when generating the build system.
Upvotes: 5