From 4c54f6c3877c646ba8d4382b7cda7024c5217fad Mon Sep 17 00:00:00 2001 From: badaix Date: Fri, 10 May 2024 13:32:32 +0200 Subject: [PATCH] cppcheck requires cmake v3.19.0 or later --- cmake/cppcheck.cmake | 110 ++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 53 deletions(-) diff --git a/cmake/cppcheck.cmake b/cmake/cppcheck.cmake index 115d700e..65d14b5f 100644 --- a/cmake/cppcheck.cmake +++ b/cmake/cppcheck.cmake @@ -1,64 +1,68 @@ -find_program(CPPCHECK_BINARY cppcheck NAMES cppcheck) +if(CMAKE_VERSION VERSION_EQUAL "3.19.0" OR CMAKE_VERSION VERSION_GREATER + "3.19.0") -# If CppCheck executable found -if(CPPCHECK_BINARY) - if(NOT DEFINED ENV{CPPCHECK_CACHE_DIR}) - file(REAL_PATH "~/.cache/cppcheck" cache_dir EXPAND_TILDE) - set(ENV{CPPCHECK_CACHE_DIR} ${cache_dir}) - endif() + find_program(CPPCHECK_BINARY cppcheck NAMES cppcheck) - set(CPPCHECK_CACHE_DIR - $ENV{CPPCHECK_CACHE_DIR} - CACHE STRING "cppcheck cache directory") + # If CppCheck executable found + if(CPPCHECK_BINARY) + if(NOT DEFINED ENV{CPPCHECK_CACHE_DIR}) + file(REAL_PATH "~/.cache/cppcheck" cache_dir EXPAND_TILDE) + set(ENV{CPPCHECK_CACHE_DIR} ${cache_dir}) + endif() - # Check CppCheck version - set(CPP_CHECK_CMD ${CPPCHECK_BINARY} --version) - execute_process( - COMMAND ${CPP_CHECK_CMD} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - RESULT_VARIABLE CPP_CHECK_RESULT - OUTPUT_VARIABLE CPP_CHECK_VERSION - ERROR_VARIABLE CPP_CHECK_ERROR) + set(CPPCHECK_CACHE_DIR + $ENV{CPPCHECK_CACHE_DIR} + CACHE STRING "cppcheck cache directory") - include(ProcessorCount) - ProcessorCount(CPU_CORES) + # Check CppCheck version + set(CPP_CHECK_CMD ${CPPCHECK_BINARY} --version) + execute_process( + COMMAND ${CPP_CHECK_CMD} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + RESULT_VARIABLE CPP_CHECK_RESULT + OUTPUT_VARIABLE CPP_CHECK_VERSION + ERROR_VARIABLE CPP_CHECK_ERROR) - # Check if version could be extracted - if(CPP_CHECK_RESULT EQUAL 0) + include(ProcessorCount) + ProcessorCount(CPU_CORES) - # Append desired arguments to CppCheck - list( - APPEND - CPPCHECK_BINARY - # Use all the available CPU cores - "-j 4" - # ${CPU_CORES}" - "--std=c++17" - "--enable=all" - "--cppcheck-build-dir=${CPPCHECK_CACHE_DIR}" - "--project=${CMAKE_BINARY_DIR}/compile_commands.json" - "--error-exitcode=10" - "--inline-suppr" - "--suppress=unusedFunction" - "--suppress=noExplicitConstructor" - "--suppress=preprocessorErrorDirective") + # Check if version could be extracted + if(CPP_CHECK_RESULT EQUAL 0) - if(CPPCHECK_ARGS) - set(CPPCHECK_ARGS_LIST ${CPPCHECK_ARGS}) - separate_arguments(CPPCHECK_ARGS_LIST) - list(APPEND CPPCHECK_BINARY ${CPPCHECK_ARGS_LIST}) - endif() + # Append desired arguments to CppCheck + list( + APPEND + CPPCHECK_BINARY + # Use all the available CPU cores + "-j 4" + # ${CPU_CORES}" + "--std=c++17" + "--enable=all" + "--cppcheck-build-dir=${CPPCHECK_CACHE_DIR}" + "--project=${CMAKE_BINARY_DIR}/compile_commands.json" + "--error-exitcode=10" + "--inline-suppr" + "--suppress=unusedFunction" + "--suppress=noExplicitConstructor" + "--suppress=preprocessorErrorDirective") - add_custom_command( - OUTPUT create-cache-dir - COMMAND ${CMAKE_COMMAND} -E make_directory ${CPPCHECK_CACHE_DIR} - COMMENT "Creating cppcheck cache dir: '${CPPCHECK_CACHE_DIR}'") + if(CPPCHECK_ARGS) + set(CPPCHECK_ARGS_LIST ${CPPCHECK_ARGS}) + separate_arguments(CPPCHECK_ARGS_LIST) + list(APPEND CPPCHECK_BINARY ${CPPCHECK_ARGS_LIST}) + endif() - add_custom_target( - cppcheck - COMMAND ${CPPCHECK_BINARY} - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - COMMENT "Static code analysis using ${CPP_CHECK_VERSION}" - DEPENDS create-cache-dir) + add_custom_command( + OUTPUT create-cache-dir + COMMAND ${CMAKE_COMMAND} -E make_directory ${CPPCHECK_CACHE_DIR} + COMMENT "Creating cppcheck cache dir: '${CPPCHECK_CACHE_DIR}'") + + add_custom_target( + cppcheck + COMMAND ${CPPCHECK_BINARY} + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMENT "Static code analysis using ${CPP_CHECK_VERSION}" + DEPENDS create-cache-dir) + endif() endif() endif()