From 179b6702f9d65c111e3b0deec6854b63dd5fb683 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Thu, 19 Sep 2019 16:27:37 +0200 Subject: [PATCH 1/9] Use CMake's own Cppcheck mechanism Enable only when CMAKE_BUILD_TYPE is Debug. Thus forcing the developer to see the output if cppcheck program is available. Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 26 ++++++++++++++++++++++++++ cmake/FindCppcheck.cmake | 31 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 cmake/FindCppcheck.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index d4d16aaa..40f551b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -444,6 +444,32 @@ install( DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig ) +############################################################################### +# CMake enabled static analysis + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + # Different tests are only executed when the corresponding + # tools are available. Presence in the system is checked individually. + # + # Run analysis only when the build type is Debug, + # not any of the Release variants. + # In the future Cppcheck and other tools will improve + # and find new faults in our code. + # We do not want to create headache for the future admins and packagers, + # only for the future programmers. + + if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.10") + # CMAKE__CPPCHECK only supported in 3.10 and upwards. + # E.g. CMAKE__CPPLINT already since CMake 3.8. + find_package(Cppcheck) + if(Cppcheck_FOUND) + set(CMAKE_C_CPPCHECK + "${Cppcheck_EXECUTABLE};--quiet;--std=c89;--enable=warning,style,performance,portability") + endif(Cppcheck_FOUND) + endif(CMAKE_VERSION VERSION_GREATER_EQUAL "3.10") + +endif(CMAKE_BUILD_TYPE STREQUAL "Debug") + ############################################################################### # Subdirectories add_subdirectory(lib) diff --git a/cmake/FindCppcheck.cmake b/cmake/FindCppcheck.cmake new file mode 100644 index 00000000..95d27a03 --- /dev/null +++ b/cmake/FindCppcheck.cmake @@ -0,0 +1,31 @@ +find_program( + Cppcheck_EXECUTABLE + NAMES cppcheck + DOC "Cppcheck static analysis tool (http://cppcheck.sourceforge.net)" +) + +if (Cppcheck_EXECUTABLE) + execute_process( + COMMAND ${Cppcheck_EXECUTABLE} --version + OUTPUT_VARIABLE cppcheck_version_out + RESULT_VARIABLE cppcheck_version_error + ERROR_VARIABLE cppcheck_version_suppress + ) + if (NOT cppcheck_version_error) + string(REPLACE "\n" "" cppcheck_version_out "${cppcheck_version_out}") + string(REGEX REPLACE "Cppcheck (.+)" "\\1" cppcheck_version "${cppcheck_version_out}") + endif () +endif () + +if (cppcheck_version) + set(Cppcheck_FOUND 1 CACHE INTERNAL "Cppcheck version ${cppcheck_version} found") + set(Cppcheck_VERSION "${cppcheck_version}" CACHE INTERNAL "Cppcheck version number") +endif () + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args( + Cppcheck + REQUIRED_VARS Cppcheck_EXECUTABLE + VERSION_VAR Cppcheck_VERSION +) + From 6dd9402c017cf944f1022b1224bd9b42a8122123 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Tue, 24 Sep 2019 12:02:52 +0200 Subject: [PATCH 2/9] Add Cppcheck to Travis build --- .travis.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e73ecca1..bbc1ab4e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,10 +49,12 @@ matrix: - os: linux compiler: clang env: SCAN_BUILD=YES + before_install: + - sudo apt-get install -y cppcheck before_script: - - if [ "$(uname)" = "Linux" ]; then sudo apt-get update; sudo apt-get install -y texlive texinfo texi2html doxygen; fi - - if [ "$(uname)" = "Darwin" ]; then brew update; brew cask install mactex; brew install texi2html texinfo doxygen; fi + - if [ "$(uname)" = "Linux" ]; then sudo apt-get update; sudo apt-get install -y texlive texinfo texi2html doxygen cppcheck; fi + - if [ "$(uname)" = "Darwin" ]; then brew update; brew cask install mactex; brew install texi2html texinfo doxygen cppcheck; fi script: - chmod +x travis.sh From 46753c9b759238d2f62d9531c2d467006a2b11c1 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Mon, 28 Oct 2019 10:43:28 +0100 Subject: [PATCH 3/9] Use CMAKE_C_STANDARD with Cppcheck run Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40f551b6..2f2d6eec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -463,8 +463,12 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") # E.g. CMAKE__CPPLINT already since CMake 3.8. find_package(Cppcheck) if(Cppcheck_FOUND) + set(cppcheck_c_standard "c${CMAKE_C_STANDARD}") + if("${cppcheck_c_standard}" STREQUAL "c90") + set(cppcheck_c_standard "c89") + endif() set(CMAKE_C_CPPCHECK - "${Cppcheck_EXECUTABLE};--quiet;--std=c89;--enable=warning,style,performance,portability") + "${Cppcheck_EXECUTABLE};--quiet;--std=${cppcheck_c_standard};--enable=warning,style,performance,portability") endif(Cppcheck_FOUND) endif(CMAKE_VERSION VERSION_GREATER_EQUAL "3.10") From 984b1a3bf4e2ee07672a01cb08cb573076f6b732 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Mon, 28 Oct 2019 10:58:05 +0100 Subject: [PATCH 4/9] Force Cppcheck never fail the build Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f2d6eec..c144b3f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -468,7 +468,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") set(cppcheck_c_standard "c89") endif() set(CMAKE_C_CPPCHECK - "${Cppcheck_EXECUTABLE};--quiet;--std=${cppcheck_c_standard};--enable=warning,style,performance,portability") + "${Cppcheck_EXECUTABLE};--quiet;--error-exitcode=0;--std=${cppcheck_c_standard};--enable=warning,style,performance,portability") endif(Cppcheck_FOUND) endif(CMAKE_VERSION VERSION_GREATER_EQUAL "3.10") From 4923313d41d76ac7564ca6edd04e09d310608ec1 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Wed, 30 Oct 2019 09:51:57 +0100 Subject: [PATCH 5/9] Add CMake option ENABLE_TREAT_WARNINGS_AS_ERRORS Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c144b3f2..434ecdfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,9 @@ if(NOT CHECK_ENABLE_TESTS) # TODO Remove this option by Check 0.15.0! endif(NOT CHECK_ENABLE_TESTS) +option(ENABLE_TREAT_WARNINGS_AS_ERRORS + "Fail build if compiler, linker, static analysis tools or other QA tool finds faults" OFF) + option(CHECK_ENABLE_GCOV "Turn on test coverage" OFF) if (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU") From 71631242b617cd9d6ee6fb30d875d6ba0f0ceb07 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Wed, 30 Oct 2019 09:52:44 +0100 Subject: [PATCH 6/9] Apply CMake option ENABLE_TREAT_WARNINGS_AS_ERRORS to Cppcheck Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 434ecdfb..9c837f46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -470,8 +470,12 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") if("${cppcheck_c_standard}" STREQUAL "c90") set(cppcheck_c_standard "c89") endif() + set(cppcheck_error_exitcode "0") + if(ENABLE_TREAT_WARNINGS_AS_ERRORS) + set(cppcheck_error_exitcode "1") + endif() set(CMAKE_C_CPPCHECK - "${Cppcheck_EXECUTABLE};--quiet;--error-exitcode=0;--std=${cppcheck_c_standard};--enable=warning,style,performance,portability") + "${Cppcheck_EXECUTABLE};--quiet;--error-exitcode=${cppcheck_error_exitcode};--std=${cppcheck_c_standard};--enable=warning,style,performance,portability") endif(Cppcheck_FOUND) endif(CMAKE_VERSION VERSION_GREATER_EQUAL "3.10") From 919c809d2dfcfba7904f4323671db48aea497d0e Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Wed, 30 Oct 2019 09:53:52 +0100 Subject: [PATCH 7/9] Build in Travis with CMake option ENABLE_TREAT_WARNINGS_AS_ERRORS=ON Signed-off-by: Mikko Johannes Koivunalho --- travis.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/travis.sh b/travis.sh index f455a31c..4c993b70 100644 --- a/travis.sh +++ b/travis.sh @@ -31,7 +31,7 @@ fi if [ "${USE_CMAKE}" = 'YES' ] ; then cmake --version - cmake . || exit 1 + cmake . -DENABLE_TREAT_WARNINGS_AS_ERRORS=ON || exit 1 make || exit 1 ctest -V || exit 1 sudo make install || exit 1 @@ -84,7 +84,7 @@ if [ "${PRE_RELEASE_CHECK}" = 'YES' ]; then tar xf check-*.tar.gz cd check-* cmake --version - cmake . || exit 1 + cmake . -DENABLE_TREAT_WARNINGS_AS_ERRORS=ON || exit 1 make || exit 1 fi From dd2eef26418498ba789a758d452e910151cd9888 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Wed, 30 Oct 2019 10:08:37 +0100 Subject: [PATCH 8/9] Split long string CMAKE_C_CPPCHECK into a list Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c837f46..c5aa3da7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -474,8 +474,11 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") if(ENABLE_TREAT_WARNINGS_AS_ERRORS) set(cppcheck_error_exitcode "1") endif() - set(CMAKE_C_CPPCHECK - "${Cppcheck_EXECUTABLE};--quiet;--error-exitcode=${cppcheck_error_exitcode};--std=${cppcheck_c_standard};--enable=warning,style,performance,portability") + list(APPEND CMAKE_C_CPPCHECK "${Cppcheck_EXECUTABLE}") + list(APPEND CMAKE_C_CPPCHECK "--quiet") + list(APPEND CMAKE_C_CPPCHECK "--error-exitcode=${cppcheck_error_exitcode}") + list(APPEND CMAKE_C_CPPCHECK "--std=${cppcheck_c_standard}") + list(APPEND CMAKE_C_CPPCHECK "--enable=warning,style,performance,portability") endif(Cppcheck_FOUND) endif(CMAKE_VERSION VERSION_GREATER_EQUAL "3.10") From f20e9417e00a53a8ccbcdb42c022637b35aea47a Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Fri, 1 Nov 2019 21:13:23 +0100 Subject: [PATCH 9/9] Enable use of _ROOT variables in build E.g. cmake -D Cppcheck_ROOT= (install_root is the directory which contains dirs bin, share and possible others. Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c5aa3da7..a5b1c2ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,9 @@ if(POLICY CMP0076) # target_sources() leaves relative source file paths unmodified. (OLD) cmake_policy(SET CMP0076 OLD) endif() +if(POLICY CMP0074) + cmake_policy(SET CMP0074 NEW) # Use _ROOT variables +endif() project(Check DESCRIPTION "Unit Testing Framework for C" LANGUAGES C)