• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1include(CMakePushCheckState)
2include(CheckLibraryExists)
3include(LLVMCheckCompilerLinkerFlag)
4include(CheckCCompilerFlag)
5include(CheckCXXCompilerFlag)
6include(CheckCSourceCompiles)
7
8# The compiler driver may be implicitly trying to link against libunwind.
9# This is normally ok (libcxx relies on an unwinder), but if libunwind is
10# built in the same cmake invocation as libcxx and we've got
11# LIBCXXABI_USE_LLVM_UNWINDER set, we'd be linking against the just-built
12# libunwind (and the compiler implicit -lunwind wouldn't succeed as the newly
13# built libunwind isn't installed yet). For those cases, it'd be good to
14# link with --uwnindlib=none. Check if that option works.
15llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
16
17if (NOT LIBCXX_USE_COMPILER_RT)
18  if(WIN32 AND NOT MINGW)
19    set(LIBCXX_HAS_GCC_S_LIB NO)
20  else()
21    if(ANDROID)
22      check_library_exists(gcc __gcc_personality_v0 "" LIBCXX_HAS_GCC_LIB)
23    else()
24      check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB)
25    endif()
26  endif()
27endif()
28
29# libc++ is using -nostdlib++ at the link step when available,
30# otherwise -nodefaultlibs is used. We want all our checks to also
31# use one of these options, otherwise we may end up with an inconsistency between
32# the flags we think we require during configuration (if the checks are
33# performed without one of those options) and the flags that are actually
34# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
35# required for the link to go through. We remove sanitizers from the
36# configuration checks to avoid spurious link errors.
37
38check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
39if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
40  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
41else()
42  check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
43  if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
44    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
45  endif()
46endif()
47
48if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
49  if (LIBCXX_USE_COMPILER_RT)
50    include(HandleCompilerRT)
51    find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
52                             FLAGS ${LIBCXX_COMPILE_FLAGS})
53    if (LIBCXX_BUILTINS_LIBRARY)
54      list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")
55    else()
56      message(WARNING "Could not find builtins library from libc++")
57    endif()
58  elseif (LIBCXX_HAS_GCC_LIB)
59    list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
60  elseif (LIBCXX_HAS_GCC_S_LIB)
61    list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
62  endif ()
63  if (MINGW)
64    # Mingw64 requires quite a few "C" runtime libraries in order for basic
65    # programs to link successfully with -nodefaultlibs.
66    if (LIBCXX_USE_COMPILER_RT)
67      set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY})
68    else ()
69      set(MINGW_RUNTIME gcc_s gcc)
70    endif()
71    set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
72                        shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
73                        moldname mingwex msvcrt)
74    list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
75  endif()
76  if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
77    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
78  endif ()
79  if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
80    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")
81  endif ()
82endif ()
83
84# Check compiler pragmas
85if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
86  cmake_push_check_state()
87  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
88  check_c_source_compiles("
89#pragma comment(lib, \"c\")
90int main(void) { return 0; }
91" C_SUPPORTS_COMMENT_LIB_PRAGMA)
92  cmake_pop_check_state()
93endif()
94
95# Check libraries
96if(WIN32 AND NOT MINGW)
97  # TODO(compnerd) do we want to support an emulation layer that allows for the
98  # use of pthread-win32 or similar libraries to emulate pthreads on Windows?
99  set(LIBCXX_HAS_PTHREAD_LIB NO)
100  set(LIBCXX_HAS_RT_LIB NO)
101  set(LIBCXX_HAS_ATOMIC_LIB NO)
102elseif(APPLE)
103  set(LIBCXX_HAS_PTHREAD_LIB NO)
104  set(LIBCXX_HAS_RT_LIB NO)
105  set(LIBCXX_HAS_ATOMIC_LIB NO)
106elseif(FUCHSIA)
107  set(LIBCXX_HAS_PTHREAD_LIB NO)
108  set(LIBCXX_HAS_RT_LIB NO)
109  check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
110elseif(ANDROID)
111  set(LIBCXX_HAS_PTHREAD_LIB NO)
112  set(LIBCXX_HAS_RT_LIB NO)
113  set(LIBCXX_HAS_ATOMIC_LIB NO)
114else()
115  check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)
116  check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB)
117  check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
118endif()
119