llvm-project
113 строк · 4.3 Кб
1include(CMakePushCheckState)2include(CheckLibraryExists)3include(CheckCCompilerFlag)4include(CheckCXXCompilerFlag)5include(CheckCSourceCompiles)6
7check_library_exists(c fopen "" LIBCXXABI_HAS_C_LIB)8if (NOT LIBCXXABI_USE_COMPILER_RT)9if (ANDROID)10check_library_exists(gcc __gcc_personality_v0 "" LIBCXXABI_HAS_GCC_LIB)11else ()12check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXXABI_HAS_GCC_S_LIB)13check_library_exists(gcc __aeabi_uldivmod "" LIBCXXABI_HAS_GCC_LIB)14endif ()15endif ()16
17# libc++abi is using -nostdlib++ at the link step when available,
18# otherwise -nodefaultlibs is used. We want all our checks to also
19# use one of these options, otherwise we may end up with an inconsistency between
20# the flags we think we require during configuration (if the checks are
21# performed without -nodefaultlibs) and the flags that are actually
22# required during compilation (which has the -nodefaultlibs). libc is
23# required for the link to go through. We remove sanitizers from the
24# configuration checks to avoid spurious link errors.
25
26check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)27if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)28set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")29else()30check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)31if (C_SUPPORTS_NODEFAULTLIBS_FLAG)32set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")33endif()34endif()35
36# Only link against compiler-rt manually if we use -nodefaultlibs, since
37# otherwise the compiler will do the right thing on its own.
38if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)39if (LIBCXXABI_HAS_C_LIB)40list(APPEND CMAKE_REQUIRED_LIBRARIES c)41endif ()42if (LIBCXXABI_USE_COMPILER_RT)43include(HandleCompilerRT)44find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY45FLAGS "${LIBCXXABI_COMPILE_FLAGS}")46if (LIBCXXABI_BUILTINS_LIBRARY)47list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXXABI_BUILTINS_LIBRARY}")48else()49message(WARNING "Could not find builtins library from libc++abi")50endif()51else ()52if (LIBCXXABI_HAS_GCC_S_LIB)53list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)54endif ()55if (LIBCXXABI_HAS_GCC_LIB)56list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)57endif ()58endif ()59if (MINGW)60# Mingw64 requires quite a few "C" runtime libraries in order for basic61# programs to link successfully with -nodefaultlibs.62if (LIBCXXABI_USE_COMPILER_RT)63set(MINGW_RUNTIME ${LIBCXXABI_BUILTINS_LIBRARY})64else ()65set(MINGW_RUNTIME gcc_s gcc)66endif()67set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi3268shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}69moldname mingwex msvcrt)70list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})71endif()72endif()73
74if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)75if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)76set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")77endif ()78if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)79set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")80endif ()81endif ()82
83# Check compiler pragmas
84if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")85cmake_push_check_state()86set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")87check_c_source_compiles("88#pragma comment(lib, \"c\")
89int main(void) { return 0; }
90" C_SUPPORTS_COMMENT_LIB_PRAGMA)91cmake_pop_check_state()92endif()93
94# Check compiler flags
95check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)96
97# Check libraries
98if(FUCHSIA)99set(LIBCXXABI_HAS_DL_LIB NO)100set(LIBCXXABI_HAS_PTHREAD_LIB NO)101check_library_exists(c __cxa_thread_atexit_impl ""102LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)103elseif(ANDROID)104set(LIBCXXABI_HAS_DL_LIB YES)105set(LIBCXXABI_HAS_PTHREAD_LIB NO)106check_library_exists(c __cxa_thread_atexit_impl ""107LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)108else()109check_library_exists(dl dladdr "" LIBCXXABI_HAS_DL_LIB)110check_library_exists(pthread pthread_once "" LIBCXXABI_HAS_PTHREAD_LIB)111check_library_exists(c __cxa_thread_atexit_impl ""112LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)113endif()114