117 lines · plain
1# The find_package changes these variables. This leaves the build in an odd2# state. Calling cmake a second time tries to write site config information in3# 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-tidy8# versions must match. Otherwise there likely will be ODR-violations. This had9# led to crashes and incorrect output of the clang-tidy based checks.10find_package(Clang ${CMAKE_CXX_COMPILER_VERSION})11 12set(LLVM_DIR "${LLVM_DIR_SAVE}" CACHE PATH "The directory containing a CMake configuration file for LLVM." FORCE)13set(Clang_DIR "${Clang_DIR_SAVE}" CACHE PATH "The directory containing a CMake configuration file for Clang." FORCE)14 15if(NOT Clang_FOUND)16 message(STATUS "Clang-tidy tests are disabled since the "17 "Clang development package is unavailable.")18 return()19endif()20if(NOT TARGET clangTidy)21 message(STATUS "Clang-tidy tests are disabled since the "22 "Clang development package has no clangTidy target.")23 return()24endif()25 26message(STATUS "Found system-installed LLVM ${LLVM_PACKAGE_VERSION} with headers in ${LLVM_INCLUDE_DIRS}")27 28set(CMAKE_CXX_STANDARD 20)29 30# Link only against clangTidy itself, not anything that clangTidy uses; otherwise we run setup code multiple times31# which results in clang-tidy crashing32set_target_properties(clangTidy PROPERTIES INTERFACE_LINK_LIBRARIES "")33# ClangTargets.cmake doesn't set the include paths, so we have to do it34target_include_directories(clangTidy INTERFACE35 ${CLANG_INCLUDE_DIRS}36 ${LLVM_INCLUDE_DIRS}37 )38target_compile_options(clangTidy INTERFACE39 -fno-rtti40 -fno-sanitize=address,hwaddress,undefined,thread,leak # ignore any sanitizers41 )42 43if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")44 target_compile_options(clangTidy INTERFACE45 -fno-sanitize=memory,dataflow46 )47endif()48 49# In some cases even with the clangTidy target present the headers appear not to50# be on the system. Run a short test to see whether the header is present.51file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" "52#if !__has_include(\"clang-tidy/ClangTidyCheck.h\")53 # error No clang-tidy headers54#endif55int main(){}56")57try_compile(HAS_CLANG_TIDY_HEADERS58 "${CMAKE_CURRENT_BINARY_DIR}"59 "${CMAKE_CURRENT_BINARY_DIR}/test.cpp"60 LINK_LIBRARIES clangTidy)61 62if(NOT HAS_CLANG_TIDY_HEADERS)63 message(STATUS "Clang-tidy tests are disabled since the "64 "clang-tidy headers are not present.")65 return()66endif()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 platforms70# we support do not support C++20 yet.71# Note it has not been tested whether version 11 works.72file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test.cpp" "73#include <version>74#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 1175 # error The libstdc++ version is too old.76#endif77int main(){}78")79try_compile(HAS_NEWER_STANDARD_LIBRARY80 "${CMAKE_CURRENT_BINARY_DIR}"81 "${CMAKE_CURRENT_BINARY_DIR}/test.cpp"82 LINK_LIBRARIES clangTidy)83 84if(NOT HAS_NEWER_STANDARD_LIBRARY)85 message(STATUS "Clang-tidy tests are disabled due to using "86 "stdlibc++ older than version 11")87 return()88endif()89message(STATUS "Clang-tidy tests are enabled.")90 91set(SOURCES92 abi_tag_on_virtual.cpp93 header_exportable_declarations.cpp94 hide_from_abi.cpp95 internal_ftm_use.cpp96 nodebug_on_aliases.cpp97 proper_version_checks.cpp98 robust_against_adl.cpp99 robust_against_operator_ampersand.cpp100 uglify_attributes.cpp101 102 libcpp_module.cpp103 )104 105add_library(cxx-tidy MODULE ${SOURCES})106target_link_libraries(cxx-tidy clangTidy)107 108set_target_properties(cxx-tidy PROPERTIES109 CXX_STANDARD 20110 CXX_STANDARD_REQUIRED YES111 CXX_EXTENSIONS NO)112 113set_target_properties(cxx-tidy PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})114set(CMAKE_SHARED_MODULE_SUFFIX_CXX .plugin) # Use a portable suffix to simplify how we can find it from Lit115 116add_dependencies(cxx-test-depends cxx-tidy)117