Changes according to review

We need first clang-tidy for all

If found, we should find run-clang-tidy.py for the check taget
Then we need a compile_commands.json created by cmake or mesonbuild.

And this has to be linkd to the source tree, if the binary tree is
outsite the source tree.

That must be true for ninja generator to generate absolute pathes at
compile_commands.json!
pull/1440/head
ClausKlein 6 years ago
parent a28ddf1c3a
commit 239e8c0e32

@ -1,15 +1,18 @@
option(ENABLE_CLANG_TIDY "Add run-clang-tidy automatically to builds" OFF) option(SPDLOG_ENABLE_TIDY "Add clang-tidy automatically as prebuild step" OFF)
find_program(CLANG_TIDY_EXE find_program(CLANG_TIDY_EXE
NAMES run-clang-tidy.py run-clang-tidy run-clang-tidy-7 NAMES clang-tidy clang-tidy-7
HINTS ${CMAKE_CURRENT_SOURCE_DIR}/scripts
DOC "Path to clang-tidy executable") DOC "Path to clang-tidy executable")
find_program(CLANG_TIDY_RUNNER
NAMES run-clang-tidy run-clang-tidy-7 run-clang-tidy.py
HINTS ${CMAKE_CURRENT_SOURCE_DIR}/scripts)
if(CLANG_TIDY_EXE) if(CLANG_TIDY_EXE)
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}") message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
set(DCMAKE_EXPORT_COMPILE_COMMANDS ON) set(DCMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CLANG_TIDY_CMD ${CLANG_TIDY_EXE}) set(CLANG_TIDY_CMD ${CLANG_TIDY_EXE})
message(STATUS "cmake source dir: ${CMAKE_CURRENT_SOURCE_DIR}") message(STATUS "cmake source dir: ${CMAKE_CURRENT_SOURCE_DIR}")
if(ENABLE_CLANG_TIDY) if(SPDLOG_ENABLE_TIDY)
# NOTE: the project config file .clang-tidy is not found if the # NOTE: the project config file .clang-tidy is not found if the
# binary tree is not part of the source tree! CK # binary tree is not part of the source tree! CK
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_CMD} CACHE STRING "" FORCE) set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_CMD} CACHE STRING "" FORCE)
@ -17,6 +20,7 @@ if(CLANG_TIDY_EXE)
set(CMAKE_CXX_CLANG_TIDY "" CACHE STRING "" FORCE) # delete it set(CMAKE_CXX_CLANG_TIDY "" CACHE STRING "" FORCE) # delete it
endif() endif()
if(CLANG_TIDY_RUNNER)
if(NOT TARGET check) if(NOT TARGET check)
add_custom_target(check) add_custom_target(check)
message(STATUS "check target added") message(STATUS "check target added")
@ -26,11 +30,12 @@ if(CLANG_TIDY_EXE)
add_custom_command(TARGET check PRE_BUILD add_custom_command(TARGET check PRE_BUILD
# -p BUILD_PATH Path used to read a compile command database (compile_commands.json). # -p BUILD_PATH Path used to read a compile command database (compile_commands.json).
# NOTE: we use defaults checks from .clang-tidy and examples only! # NOTE: we use defaults checks from .clang-tidy and examples only!
COMMAND ${CLANG_TIDY_EXE} -p ${CMAKE_CURRENT_BINARY_DIR} ### ${CMAKE_CURRENT_SOURCE_DIR}/example COMMAND ${CLANG_TIDY_RUNNER} -p ${CMAKE_CURRENT_BINARY_DIR} ### ${CMAKE_CURRENT_SOURCE_DIR}/example
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" # location of compile_commands.json WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" # location of compile_commands.json
COMMENT "Running check on targets at ${CMAKE_CURRENT_SOURCE_DIR} ..." COMMENT "Running check on targets at ${CMAKE_CURRENT_SOURCE_DIR} ..."
VERBATIM VERBATIM
) )
endif()
else() else()
message(AUTHOR_WARNING "run-clang-tidy not found!") message(AUTHOR_WARNING "clang-tidy not found!")
endif() endif()

Loading…
Cancel
Save