-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cppcheck requires cmake v3.19.0 or later
- Loading branch information
Showing
1 changed file
with
57 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |