From ad416ab53155a1560d43f6721b51ac521f7cbc04 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Mon, 24 Oct 2022 22:59:56 -0600 Subject: [PATCH] Check for minimum required clang-tidy version --- CMakeLists.txt | 9 ++++++--- cmake/Tidy.cmake | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 cmake/Tidy.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index fabb1c60e..6fb2e6a10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -534,7 +534,10 @@ endif() sfml_set_option(CLANG_FORMAT_EXECUTABLE clang-format STRING "Override clang-format executable, requires version 12, 13, or 14") add_custom_target(format - COMMAND ${CMAKE_COMMAND} "-DCLANG_FORMAT_EXECUTABLE=${CLANG_FORMAT_EXECUTABLE}" -P ./cmake/Format.cmake - WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" VERBATIM) + COMMAND ${CMAKE_COMMAND} -DCLANG_FORMAT_EXECUTABLE=${CLANG_FORMAT_EXECUTABLE} -P ./cmake/Format.cmake + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} VERBATIM) -add_custom_target(tidy COMMAND run-clang-tidy -p ${PROJECT_BINARY_DIR} VERBATIM) +sfml_set_option(CLANG_TIDY_EXECUTABLE clang-tidy STRING "Override clang-tidy executable, requires minimum version 12") +add_custom_target(tidy + COMMAND ${CMAKE_COMMAND} -DCLANG_TIDY_EXECUTABLE=${CLANG_TIDY_EXECUTABLE} -DPROJECT_BINARY_DIR=${PROJECT_BINARY_DIR} -P ./cmake/Tidy.cmake + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} VERBATIM) diff --git a/cmake/Tidy.cmake b/cmake/Tidy.cmake new file mode 100644 index 000000000..48b5a0a75 --- /dev/null +++ b/cmake/Tidy.cmake @@ -0,0 +1,23 @@ +# Check executable exists +if(NOT EXISTS ${CLANG_TIDY_EXECUTABLE}) + find_program(CLANG_TIDY_EXEC_TEMP ${CLANG_TIDY_EXECUTABLE}) + if(CLANG_TIDY_EXEC_TEMP) + set(CLANG_TIDY_EXECUTABLE ${CLANG_TIDY_EXEC_TEMP}) + unset(CLANG_TIDY_EXEC_TEMP) + else() + message(FATAL_ERROR "Unable to find clang-tidy executable: \"${CLANG_TIDY_EXECUTABLE}\"") + endif() +endif() + +# Check executable version +execute_process(COMMAND ${CLANG_TIDY_EXECUTABLE} --version OUTPUT_VARIABLE CLANG_TIDY_VERSION) +string(REGEX MATCH "version ([0-9]+)" CLANG_TIDY_VERSION ${CLANG_TIDY_VERSION}) +unset(CLANG_TIDY_VERSION) +if(CMAKE_MATCH_1 GREATER_EQUAL 12) + message(STATUS "Using clang-tidy version ${CMAKE_MATCH_1}") +else() + message(FATAL_ERROR "clang-tidy version ${CMAKE_MATCH_1} is too low") +endif() + +# Run +execute_process(COMMAND run-clang-tidy -clang-tidy-binary ${CLANG_TIDY_EXECUTABLE} -p ${PROJECT_BINARY_DIR})