|
| 1 | +if(CMAKE_GENERATOR MATCHES "Ninja|Unix Makefiles") |
| 2 | + message(STATUS "🔧 CMAKE_EXPORT_COMPILE_COMMANDS will be used to enable clang-tidy") |
| 3 | + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 4 | +else() |
| 5 | + message(WARNING "🚧 CMAKE_EXPORT_COMPILE_COMMANDS can't be used because the CMAKE_GENERATOR is ${CMAKE_GENERATOR}. |
| 6 | +You have to use Ninja or Unix Makefiles. |
| 7 | +Without CMAKE_EXPORT_COMPILE_COMMANDS, clang-tidy will not work. |
| 8 | +CMAKE_EXPORT_COMPILE_COMMANDS is used to generate a JSON file that contains all the compiler commands used to build the project. |
| 9 | +This file is used by clang-tidy to know how to compile the project.") |
| 10 | +endif() |
| 11 | + |
| 12 | +set(CLANG-TIDY_MINIMUM_MAJOR_VERSION 18) |
| 13 | + |
| 14 | +function(get_clang_tidy_version clang_tidy_path) |
| 15 | + set(CLANG_TIDY_VERSION_OUTPUT "") |
| 16 | + execute_process( |
| 17 | + COMMAND ${clang_tidy_path} --version |
| 18 | + OUTPUT_VARIABLE CLANG_TIDY_VERSION_OUTPUT |
| 19 | + ) |
| 20 | + string(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" CLANG_TIDY_VERSION_OUTPUT ${CLANG_TIDY_VERSION_OUTPUT}) |
| 21 | + set(CLANG_TIDY_MAJOR_VERSION ${CMAKE_MATCH_1} PARENT_SCOPE) |
| 22 | + set(CLANG_TIDY_MINOR_VERSION ${CMAKE_MATCH_2} PARENT_SCOPE) |
| 23 | + set(CLANG_TIDY_PATCH_VERSION ${CMAKE_MATCH_3} PARENT_SCOPE) |
| 24 | +endfunction() |
| 25 | + |
| 26 | +function(check_clang-tidy_version validator_result_var item) |
| 27 | + set(${validator_result_var} FALSE PARENT_SCOPE) |
| 28 | + get_clang_tidy_version(${item}) |
| 29 | + if (CLANG_TIDY_MAJOR_VERSION LESS CLANG-TIDY_MINIMUM_MAJOR_VERSION) |
| 30 | + message(DEBUG "clang-tidy (version: ${CLANG_TIDY_MAJOR_VERSION}.${CLANG_TIDY_MINOR_VERSION}.${CLANG_TIDY_PATCH_VERSION}) found at ${item}") |
| 31 | + message(DEBUG "but clang-tidy with version >= ${CLANG-TIDY_MINIMUM_MAJOR_VERSION} must be used.") |
| 32 | + set(${validator_result_var} FALSE PARENT_SCOPE) |
| 33 | + else() |
| 34 | + set(${validator_result_var} TRUE PARENT_SCOPE) |
| 35 | + endif() |
| 36 | +endfunction() |
| 37 | + |
| 38 | +function(print_clang_tidy_install_instructions) |
| 39 | + message(STATUS "🛠️ Please install clang-tidy to enable code formatting") |
| 40 | + if(UNIX) |
| 41 | + if(APPLE) |
| 42 | + message(STATUS "🍎 On MacOS, you can install clang-tidy with:") |
| 43 | + message(STATUS "\t> brew install clang-tidy") |
| 44 | + else() |
| 45 | + message(STATUS "🐧 On Ubuntu, you can install clang-tidy with:") |
| 46 | + message(STATUS "\t> sudo apt-get install clang-tidy") |
| 47 | + endif() |
| 48 | + elseif(WIN32) |
| 49 | + message(STATUS "🪟 On Windows, you can install clang-tidy with:") |
| 50 | + message(STATUS "\t> winget llvm") |
| 51 | + endif() |
| 52 | +endfunction() |
| 53 | + |
| 54 | +find_program(CLANG_TIDY clang-tidy |
| 55 | + VALIDATOR check_clang-tidy_version) |
| 56 | + |
| 57 | +if(NOT CLANG_TIDY) |
| 58 | + message(WARNING "❗clang-tidy with version >= ${CLANG-TIDY_MINIMUM_MAJOR_VERSION} not found") |
| 59 | + |
| 60 | + print_clang_tidy_install_instructions() |
| 61 | +else() |
| 62 | + get_clang_tidy_version(${CLANG_TIDY}) |
| 63 | + message(STATUS "✅ clang-tidy (version: ${CLANG_TIDY_MAJOR_VERSION}.${CLANG_TIDY_MINOR_VERSION}.${CLANG_TIDY_PATCH_VERSION}) found at ${CLANG_TIDY}") |
| 64 | + |
| 65 | + if(ACTIVATE_LINTER_DURING_COMPILATION) |
| 66 | + message(STATUS "🔧 clang-tidy will be activated during compilation") |
| 67 | + set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY}) |
| 68 | + else() |
| 69 | + message(STATUS "🔧 clang-tidy will not be activated during compilation") |
| 70 | + set(CMAKE_CXX_CLANG_TIDY "") |
| 71 | + endif() |
| 72 | + |
| 73 | + find_package (Python COMPONENTS Interpreter) |
| 74 | + if(Python_Interpreter_FOUND) |
| 75 | + message(DEBUG "Python found at ${Python_EXECUTABLE}") |
| 76 | + get_filename_component(CLANG_TIDY_FOLDER ${CLANG_TIDY} DIRECTORY) |
| 77 | + find_file(CLANG_TIDY_PYTHON_SCRIPT run-clang-tidy PATHS ${CLANG_TIDY_FOLDER} NO_DEFAULT_PATH) |
| 78 | + if(CLANG_TIDY_PYTHON_SCRIPT) |
| 79 | + message(DEBUG "run-clang-tidy.py found at ${CLANG_TIDY_PYTHON_SCRIPT}") |
| 80 | + endif() |
| 81 | + set(CLANG_TIDY_COMMAND ${Python_EXECUTABLE} ${CLANG_TIDY_PYTHON_SCRIPT}) |
| 82 | + else() |
| 83 | + set(CLANG_TIDY_COMMAND ${CLANG_TIDY}) |
| 84 | + endif() |
| 85 | + |
| 86 | + set(CLANG_TIDY_COMMON_ARGUMENTS |
| 87 | + $<$<NOT:$<BOOL:CLANG_TIDY_PYTHON_SCRIPT>>:->-use-color |
| 88 | + -p ${CMAKE_BINARY_DIR}) |
| 89 | + |
| 90 | + set( |
| 91 | + PATTERNS |
| 92 | + include/*.hpp |
| 93 | + test/*.cpp |
| 94 | + test/*.hpp |
| 95 | + CACHE STRING |
| 96 | + "; separated patterns relative to the project source dir to analyse" |
| 97 | + ) |
| 98 | + |
| 99 | + set(ALL_FILES_TO_FORMAT "") |
| 100 | + foreach(PATTERN ${PATTERNS}) |
| 101 | + file(GLOB_RECURSE FILES_TO_ANALYZE ${CMAKE_SOURCE_DIR}/${PATTERN}) |
| 102 | + list(APPEND ALL_FILES_TO_ANALYZE ${FILES_TO_ANALYZE}) |
| 103 | + endforeach() |
| 104 | + |
| 105 | + add_custom_target( |
| 106 | + clang-tidy |
| 107 | + COMMAND ${CLANG_TIDY_COMMAND} $<$<NOT:$<BOOL:CLANG_TIDY_PYTHON_SCRIPT>>:->-fix ${CLANG_TIDY_COMMON_ARGUMENTS} ${ALL_FILES_TO_ANALYZE} |
| 108 | + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 109 | + COMMENT "Running clang-tidy on all files" |
| 110 | + ) |
| 111 | + |
| 112 | + add_custom_target( |
| 113 | + clang-tidy_dry_run |
| 114 | + COMMAND ${CLANG_TIDY_COMMAND} ${CLANG_TIDY_COMMON_ARGUMENTS} |
| 115 | + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 116 | + COMMENT "Running dry clang-tidy on all files" |
| 117 | + ) |
| 118 | +endif() |
0 commit comments