1# The find_package changes these variables. This leaves the build in an odd 2# state. Calling cmake a second time tries to write site config information in 3# the system's libc++. Restoring these setting after testing fixes this issue. 4set(LLVM_DIR_SAVE ${LLVM_DIR}) 5set(Clang_DIR_SAVE ${Clang_DIR}) 6 7# Since the Clang C++ ABI is not stable the Clang libraries and clang-tidy 8# versions must match. Otherwise there likely will be ODR-violations. This had 9# led to crashes and incorrect output of the clang-tidy based checks. 10find_package(Clang ${CMAKE_CXX_COMPILER_VERSION}) 11if(NOT Clang_FOUND) 12 message(STATUS "Clang-tidy tests are disabled since the " 13 "Clang development package is unavailable.") 14 return() 15endif() 16if(NOT TARGET clangTidy) 17 message(STATUS "Clang-tidy tests are disabled since the " 18 "Clang development package has no clangTidy target.") 19 return() 20endif() 21 22set(LLVM_DIR "${LLVM_DIR_SAVE}" CACHE PATH "The directory containing a CMake configuration file for LLVM." FORCE) 23set(Clang_DIR "${Clang_DIR_SAVE}" CACHE PATH "The directory containing a CMake configuration file for Clang." FORCE) 24 25message(STATUS "Found system-installed LLVM ${LLVM_PACKAGE_VERSION} with headers in ${LLVM_INCLUDE_DIRS}") 26 27set(CMAKE_CXX_STANDARD 20) 28 29# Link only against clangTidy itself, not anything that clangTidy uses; otherwise we run setup code multiple times 30# which results in clang-tidy crashing 31set_target_properties(clangTidy PROPERTIES INTERFACE_LINK_LIBRARIES "") 32# ClangTargets.cmake doesn't set the include paths, so we have to do it 33target_include_directories(clangTidy INTERFACE 34 ${CLANG_INCLUDE_DIRS} 35 ${LLVM_INCLUDE_DIRS} 36 ) 37target_compile_options(clangTidy INTERFACE 38 -fno-rtti 39 -fno-sanitize=address,hwaddress,undefined,thread,leak # ignore any sanitizers 40 ) 41 42if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 43 target_compile_options(clangTidy INTERFACE 44 -fno-sanitize=memory,dataflow 45 ) 46endif() 47 48# In some cases even with the clangTidy target present the headers appear not to 49# be on the system. Run a short test to see whether the header is present. 50file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" " 51#if !__has_include(\"clang-tidy/ClangTidyCheck.h\") 52 # error No clang-tidy headers 53#endif 54int main(){} 55") 56try_compile(HAS_CLANG_TIDY_HEADERS 57 "${CMAKE_CURRENT_BINARY_DIR}" 58 "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" 59 LINK_LIBRARIES clangTidy) 60 61if(NOT HAS_CLANG_TIDY_HEADERS) 62 message(STATUS "Clang-tidy tests are disabled since the " 63 "clang-tidy headers are not present.") 64 return() 65endif() 66 67# The clangTidy plugin uses C++20, so ensure that we support C++20 when using libstdc++. 68# This is required because some versions of libstdc++ used as a system library on build platforms 69# we support do not support C++20 yet. 70# Note it has not been tested whether version 11 works. 71file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" " 72#include <version> 73#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 11 74 # error The libstdc++ version is too old. 75#endif 76int main(){} 77") 78try_compile(HAS_NEWER_STANDARD_LIBRARY 79 "${CMAKE_CURRENT_BINARY_DIR}" 80 "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" 81 LINK_LIBRARIES clangTidy) 82 83if(NOT HAS_NEWER_STANDARD_LIBRARY) 84 message(STATUS "Clang-tidy tests are disabled due to using " 85 "stdlibc++ older than version 11") 86 return() 87endif() 88message(STATUS "Clang-tidy tests are enabled.") 89 90set(SOURCES 91 abi_tag_on_virtual.cpp 92 header_exportable_declarations.cpp 93 hide_from_abi.cpp 94 internal_ftm_use.cpp 95 nodebug_on_aliases.cpp 96 proper_version_checks.cpp 97 qualify_declval.cpp 98 robust_against_adl.cpp 99 uglify_attributes.cpp 100 101 libcpp_module.cpp 102 ) 103 104add_library(cxx-tidy MODULE ${SOURCES}) 105target_link_libraries(cxx-tidy clangTidy) 106 107set_target_properties(cxx-tidy PROPERTIES 108 CXX_STANDARD 20 109 CXX_STANDARD_REQUIRED YES 110 CXX_EXTENSIONS NO) 111 112set_target_properties(cxx-tidy PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 113set(CMAKE_SHARED_MODULE_SUFFIX_CXX .plugin) # Use a portable suffix to simplify how we can find it from Lit 114 115add_dependencies(cxx-test-depends cxx-tidy) 116