Debian Codegraph @

libcxx/test/tools/clang_tidy_checks/CMakeLists.txt

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