Debian Codegraph @

libcxx/test/tools/clang_tidy_checks/CMakeLists.txt

raw
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})
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.
10 find_package(Clang ${CMAKE_CXX_COMPILER_VERSION})
11
12 set(LLVM_DIR "${LLVM_DIR_SAVE}" CACHE PATH "The directory containing a CMake configuration file for LLVM." FORCE)
13 set(Clang_DIR "${Clang_DIR_SAVE}" CACHE PATH "The directory containing a CMake configuration file for Clang." FORCE)
14
15 if(NOT Clang_FOUND)
16 message(STATUS "Clang-tidy tests are disabled since the "
17 "Clang development package is unavailable.")
18 return()
19 endif()
20 if(NOT TARGET clangTidy)
21 message(STATUS "Clang-tidy tests are disabled since the "
22 "Clang development package has no clangTidy target.")
23 return()
24 endif()
25
26 message(STATUS "Found system-installed LLVM ${LLVM_PACKAGE_VERSION} with headers in ${LLVM_INCLUDE_DIRS}")
27
28 set(CMAKE_CXX_STANDARD 20)
29
30 # Link only against clangTidy itself, not anything that clangTidy uses; otherwise we run setup code multiple times
31 # which results in clang-tidy crashing
32 set_target_properties(clangTidy PROPERTIES INTERFACE_LINK_LIBRARIES "")
33 # ClangTargets.cmake doesn't set the include paths, so we have to do it
34 target_include_directories(clangTidy INTERFACE
35 ${CLANG_INCLUDE_DIRS}
36 ${LLVM_INCLUDE_DIRS}
37 )
38 target_compile_options(clangTidy INTERFACE
39 -fno-rtti
40 -fno-sanitize=address,hwaddress,undefined,thread,leak # ignore any sanitizers
41 )
42
43 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
44 target_compile_options(clangTidy INTERFACE
45 -fno-sanitize=memory,dataflow
46 )
47 endif()
48
49 # In some cases even with the clangTidy target present the headers appear not to
50 # be on the system. Run a short test to see whether the header is present.
51 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" "
52 #if !__has_include(\"clang-tidy/ClangTidyCheck.h\")
53 # error No clang-tidy headers
54 #endif
55 int main(){}
56 ")
57 try_compile(HAS_CLANG_TIDY_HEADERS
58 "${CMAKE_CURRENT_BINARY_DIR}"
59 "${CMAKE_CURRENT_BINARY_DIR}/test.cpp"
60 LINK_LIBRARIES clangTidy)
61
62 if(NOT HAS_CLANG_TIDY_HEADERS)
63 message(STATUS "Clang-tidy tests are disabled since the "
64 "clang-tidy headers are not present.")
65 return()
66 endif()
67
68 # The clangTidy plugin uses C++20, so ensure that we support C++20 when using libstdc++.
69 # This is required because some versions of libstdc++ used as a system library on build platforms
70 # we support do not support C++20 yet.
71 # Note it has not been tested whether version 11 works.
72 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" "
73 #include <version>
74 #if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 11
75 # error The libstdc++ version is too old.
76 #endif
77 int main(){}
78 ")
79 try_compile(HAS_NEWER_STANDARD_LIBRARY
80 "${CMAKE_CURRENT_BINARY_DIR}"
81 "${CMAKE_CURRENT_BINARY_DIR}/test.cpp"
82 LINK_LIBRARIES clangTidy)
83
84 if(NOT HAS_NEWER_STANDARD_LIBRARY)
85 message(STATUS "Clang-tidy tests are disabled due to using "
86 "stdlibc++ older than version 11")
87 return()
88 endif()
89 message(STATUS "Clang-tidy tests are enabled.")
90
91 set(SOURCES
92 abi_tag_on_virtual.cpp
93 header_exportable_declarations.cpp
94 hide_from_abi.cpp
95 internal_ftm_use.cpp
96 nodebug_on_aliases.cpp
97 proper_version_checks.cpp
98 robust_against_adl.cpp
99 robust_against_operator_ampersand.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)