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.
4 set(LLVM_DIR_SAVE ${LLVM_DIR})
5 set(Clang_DIR_SAVE ${Clang_DIR})
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.
10 find_package(Clang ${CMAKE_CXX_COMPILER_VERSION})
12 message(STATUS "Clang-tidy tests are disabled since the "
13 "Clang development package is unavailable.")
16 if(NOT TARGET clangTidy)
17 message(STATUS "Clang-tidy tests are disabled since the "
18 "Clang development package has no clangTidy target.")
22 set(LLVM_DIR "${LLVM_DIR_SAVE}" CACHE PATH "The directory containing a CMake configuration file for LLVM." FORCE)
23 set(Clang_DIR "${Clang_DIR_SAVE}" CACHE PATH "The directory containing a CMake configuration file for Clang." FORCE)
25 message(STATUS "Found system-installed LLVM ${LLVM_PACKAGE_VERSION} with headers in ${LLVM_INCLUDE_DIRS}")
27 set(CMAKE_CXX_STANDARD 20)
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
31 set_target_properties(clangTidy PROPERTIES INTERFACE_LINK_LIBRARIES "")
32 # ClangTargets.cmake doesn't set the include paths, so we have to do it
33 target_include_directories(clangTidy INTERFACE
37 target_compile_options(clangTidy INTERFACE
39 -fno-sanitize=address,hwaddress,undefined,thread,leak # ignore any sanitizers
42 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
43 target_compile_options(clangTidy INTERFACE
44 -fno-sanitize=memory,dataflow
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.
50 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" "
51 #if !__has_include(\"clang-tidy/ClangTidyCheck.h\")
52 # error No clang-tidy headers
56 try_compile(HAS_CLANG_TIDY_HEADERS
57 "${CMAKE_CURRENT_BINARY_DIR}"
58 "${CMAKE_CURRENT_BINARY_DIR}/test.cpp"
59 LINK_LIBRARIES clangTidy)
61 if(NOT HAS_CLANG_TIDY_HEADERS)
62 message(STATUS "Clang-tidy tests are disabled since the "
63 "clang-tidy headers are not present.")
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.
71 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" "
73 #if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 11
74 # error The libstdc++ version is too old.
78 try_compile(HAS_NEWER_STANDARD_LIBRARY
79 "${CMAKE_CURRENT_BINARY_DIR}"
80 "${CMAKE_CURRENT_BINARY_DIR}/test.cpp"
81 LINK_LIBRARIES clangTidy)
83 if(NOT HAS_NEWER_STANDARD_LIBRARY)
84 message(STATUS "Clang-tidy tests are disabled due to using "
85 "stdlibc++ older than version 11")
88 message(STATUS "Clang-tidy tests are enabled.")
91 abi_tag_on_virtual.cpp
92 header_exportable_declarations.cpp
95 nodebug_on_aliases.cpp
96 proper_version_checks.cpp
98 robust_against_adl.cpp
104 add_library(cxx-tidy MODULE ${SOURCES})
105 target_link_libraries(cxx-tidy clangTidy)
107 set_target_properties(cxx-tidy PROPERTIES
109 CXX_STANDARD_REQUIRED YES
112 set_target_properties(cxx-tidy PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
113 set(CMAKE_SHARED_MODULE_SUFFIX_CXX .plugin) # Use a portable suffix to simplify how we can find it from Lit
115 add_dependencies(cxx-test-depends cxx-tidy)