Integrating Clang-Tidy into the build system
We can adapt our existing CMake setup so that it includes Clang-Tidy checks, similar to what we did with Clang-Format. Here’s a sample CMake script that sets up custom targets for running Clang-Tidy on a C++ project:
# Generate compilation database in the build directory
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Find clang-tidy
find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy)
# Gather all source files from the root directory recursively
file(GLOB_RECURSE ALL_SOURCE_FILES
*.cpp
*.cc
*.c++
*.c
*.C
*.h
*.hpp
*.hxx
)
# Exclude files in the build directory
list(FILTER ALL_SOURCE_FILES EXCLUDE REGEX “^${CMAKE_BINARY_DIR}.*”)
# Create custom target to run clang-tidy
if(CLANG_TIDY_EXECUTABLE)
add_custom_target...