diff options
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | cmake/scripts/common/Macros.cmake | 4 | ||||
-rw-r--r-- | cmake/scripts/common/ProjectMacros.cmake | 33 | ||||
-rwxr-xr-x | cmake/scripts/linux/clang-check-test.sh.in | 24 | ||||
-rwxr-xr-x | cmake/scripts/linux/cppcheck-test.sh.in | 28 |
5 files changed, 93 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bb5fbb3d89..e425e6e5bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -239,6 +239,7 @@ add_compile_options(${ARCH_DEFINES} "${SYSTEM_DEFINES}" ${DEP_DEFINES} ${PATH_DE set(core_DEPENDS "" CACHE STRING "" FORCE) set(test_archives "" CACHE STRING "" FORCE) set(test_sources "" CACHE STRING "" FORCE) +set(sca_sources "" CACHE STRING "" FORCE) mark_as_advanced(core_DEPENDS) mark_as_advanced(test_archives) mark_as_advanced(test_sources) @@ -335,6 +336,9 @@ add_dependencies(${APP_NAME_LC}-test ${APP_NAME_LC}-libraries export-files) if(CORE_HOST_IS_TARGET) enable_testing() gtest_add_tests(${APP_NAME_LC}-test "" ${test_sources}) + if(NOT WIN32) + sca_add_tests() + endif() add_custom_target(check ${CMAKE_CTEST_COMMAND} WORKING_DIRECTORY ${PROJECT_BINARY_DIR}) add_dependencies(check ${APP_NAME_LC}-test) diff --git a/cmake/scripts/common/Macros.cmake b/cmake/scripts/common/Macros.cmake index 8bee3dadad..09c9c369be 100644 --- a/cmake/scripts/common/Macros.cmake +++ b/cmake/scripts/common/Macros.cmake @@ -89,6 +89,10 @@ function(core_add_library name) target_sources(lib${APP_NAME_LC} PRIVATE ${FILES}) set(CORE_LIBRARY lib${APP_NAME_LC} PARENT_SCOPE) endif() + foreach(src ${SOURCES}) + list(APPEND sca_sources ${CMAKE_CURRENT_SOURCE_DIR}/${src}) + endforeach() + set(sca_sources ${sca_sources} CACHE STRING "" FORCE) endfunction() # Add a test library, and add sources to list for gtest integration macros diff --git a/cmake/scripts/common/ProjectMacros.cmake b/cmake/scripts/common/ProjectMacros.cmake index 89ecca4e9e..641e0474fb 100644 --- a/cmake/scripts/common/ProjectMacros.cmake +++ b/cmake/scripts/common/ProjectMacros.cmake @@ -75,6 +75,39 @@ function(GTEST_ADD_TESTS executable extra_args) endforeach() endfunction() +function(sca_add_tests) + find_program(CLANGCHECK_COMMAND clang-check) + find_program(CPPCHECK_COMMAND cppcheck) + if(CLANGCHECK_COMMAND AND CMAKE_EXPORT_COMPILE_COMMANDS) + configure_file(${PROJECT_SOURCE_DIR}/cmake/scripts/linux/clang-check-test.sh.in + ${CORE_BUILD_DIR}/clang-check-test.sh) + endif() + if(CPPCHECK_COMMAND) + configure_file(${PROJECT_SOURCE_DIR}/cmake/scripts/linux/cppcheck-test.sh.in + ${CORE_BUILD_DIR}/cppcheck-test.sh) + set(CPPCHECK_INCLUDES) + foreach(inc ${INCLUDES}) + list(APPEND CPPCHECK_INCLUDES -I ${inc}) + endforeach() + endif() + foreach(src ${sca_sources}) + file(RELATIVE_PATH name ${PROJECT_SOURCE_DIR} ${src}) + get_filename_component(EXT ${src} EXT) + if(EXT STREQUAL .cpp) + if(CLANGCHECK_COMMAND AND CMAKE_EXPORT_COMPILE_COMMANDS) + add_test(NAME clang-check+${name} + COMMAND ${CORE_BUILD_DIR}/clang-check-test.sh ${CLANGCHECK_COMMAND} ${src} + CONFIGURATIONS analyze clang-check) + endif() + if(CPPCHECK_COMMAND) + add_test(NAME cppcheck+${name} + COMMAND ${CORE_BUILD_DIR}/cppcheck-test.sh ${CPPCHECK_COMMAND} ${src} ${CPPCHECK_INCLUDES} + CONFIGURATIONS analyze cppcheck) + endif() + endif() + endforeach() +endfunction() + function(whole_archive output) if(CMAKE_CXX_COMPILER_ID STREQUAL GNU OR CMAKE_CXX_COMPILER_ID STREQUAL Clang) set(${output} -Wl,--whole-archive ${ARGN} -Wl,--no-whole-archive PARENT_SCOPE) diff --git a/cmake/scripts/linux/clang-check-test.sh.in b/cmake/scripts/linux/clang-check-test.sh.in new file mode 100755 index 0000000000..3b30ec1755 --- /dev/null +++ b/cmake/scripts/linux/clang-check-test.sh.in @@ -0,0 +1,24 @@ +#!/bin/bash + +# This script performs a single analysis using clang-check +# It is used by the 'make test' target in the buildsystems +# Usually you should use 'ctest -C clang-check' rather than calling this script directly +# +# Parameters: $1 = Application binary +# $2 = Source file to process + +clangcheck_cmd=$1 +source_file=$2 + +tmpfil=`mktemp` +$clangcheck_cmd -p @CMAKE_BINARY_DIR@ -analyze $source_file &> $tmpfil +nerr=`cat $tmpfil | grep -v "warning: /usr/bin/c++: 'linker' input unused" | wc -l` +if test $nerr -gt 0 +then + cat $tmpfil + rm $tmpfil + exit 1 +fi + +rm $tmpfil +exit 0 diff --git a/cmake/scripts/linux/cppcheck-test.sh.in b/cmake/scripts/linux/cppcheck-test.sh.in new file mode 100755 index 0000000000..08cb672636 --- /dev/null +++ b/cmake/scripts/linux/cppcheck-test.sh.in @@ -0,0 +1,28 @@ +#!/bin/bash + +# This script performs a single analysis using cppcheck +# It is used by the 'make test' target in the buildsystems +# Usually you should use 'ctest -C cppcheck' rather than calling this script directly +# +# Parameters: $1 = Application binary +# $2 = Source file to process +# $3..$N = include path parameters (-I dir1 -I dir2 ...) + +cppcheck_cmd=$1 +source_file=$2 +shift 2 + +tmpfil=`mktemp` +$cppcheck_cmd $@ --force --enable=all --suppress=unusedFunction $source_file &> $tmpfil +nmatch=`cat $tmpfil | grep "\[.*\]" | wc -l` +nnone=`cat $tmpfil | grep "\[\\*]" | wc -l` +let "nval=$nmatch-$nnone" +if test $nval -gt 0 +then + cat $tmpfil + rm $tmpfil + exit 1 +fi + +rm $tmpfil +exit 0 |