Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CMake's own Cppcheck mechanism (DO NOT MERGE) #222

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PackageName>_ROOT variables
endif()
project(Check
DESCRIPTION "Unit Testing Framework for C"
LANGUAGES C)
Expand Down Expand Up @@ -75,6 +78,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")
Expand Down Expand Up @@ -444,6 +450,43 @@ 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_<LANG>_CPPCHECK only supported in 3.10 and upwards.
# E.g. CMAKE_<LANG>_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(cppcheck_error_exitcode "0")
if(ENABLE_TREAT_WARNINGS_AS_ERRORS)
set(cppcheck_error_exitcode "1")
endif()
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")

endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

###############################################################################
# Subdirectories
add_subdirectory(lib)
Expand Down
31 changes: 31 additions & 0 deletions cmake/FindCppcheck.cmake
Original file line number Diff line number Diff line change
@@ -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
)

4 changes: 2 additions & 2 deletions travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down