xref: /openbsd-src/gnu/llvm/libcxx/cmake/config-ix.cmake (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
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(WIN32 AND NOT MINGW)
18  # NOTE(compnerd) this is technically a lie, there is msvcrt, but for now, lets
19  # let the default linking take care of that.
20  set(LIBCXX_HAS_C_LIB NO)
21else()
22  check_library_exists(c fopen "" LIBCXX_HAS_C_LIB)
23endif()
24
25if (NOT LIBCXX_USE_COMPILER_RT)
26  if(WIN32 AND NOT MINGW)
27    set(LIBCXX_HAS_GCC_S_LIB NO)
28  else()
29    if(ANDROID)
30      check_library_exists(gcc __gcc_personality_v0 "" LIBCXX_HAS_GCC_LIB)
31    else()
32      check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB)
33    endif()
34  endif()
35endif()
36
37# libc++ is using -nostdlib++ at the link step when available,
38# otherwise -nodefaultlibs is used. We want all our checks to also
39# use one of these options, otherwise we may end up with an inconsistency between
40# the flags we think we require during configuration (if the checks are
41# performed without one of those options) and the flags that are actually
42# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
43# required for the link to go through. We remove sanitizers from the
44# configuration checks to avoid spurious link errors.
45
46check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
47if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
48  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
49else()
50  check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
51  if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
52    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
53  endif()
54endif()
55
56if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
57  if (LIBCXX_HAS_C_LIB)
58    list(APPEND CMAKE_REQUIRED_LIBRARIES c)
59  endif ()
60  if (LIBCXX_USE_COMPILER_RT)
61    include(HandleCompilerRT)
62    find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
63                             FLAGS ${LIBCXX_COMPILE_FLAGS})
64    if (LIBCXX_BUILTINS_LIBRARY)
65      list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")
66    else()
67      message(WARNING "Could not find builtins library from libc++")
68    endif()
69  elseif (LIBCXX_HAS_GCC_LIB)
70    list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
71  elseif (LIBCXX_HAS_GCC_S_LIB)
72    list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
73  endif ()
74  if (MINGW)
75    # Mingw64 requires quite a few "C" runtime libraries in order for basic
76    # programs to link successfully with -nodefaultlibs.
77    if (LIBCXX_USE_COMPILER_RT)
78      set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY})
79    else ()
80      set(MINGW_RUNTIME gcc_s gcc)
81    endif()
82    set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
83                        shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
84                        moldname mingwex msvcrt)
85    list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
86  endif()
87  if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
88    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
89  endif ()
90  if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
91    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")
92  endif ()
93endif ()
94
95# Check compiler pragmas
96if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
97  cmake_push_check_state()
98  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
99  check_c_source_compiles("
100#pragma comment(lib, \"c\")
101int main(void) { return 0; }
102" C_SUPPORTS_COMMENT_LIB_PRAGMA)
103  cmake_pop_check_state()
104endif()
105
106# Check libraries
107if(WIN32 AND NOT MINGW)
108  # TODO(compnerd) do we want to support an emulation layer that allows for the
109  # use of pthread-win32 or similar libraries to emulate pthreads on Windows?
110  set(LIBCXX_HAS_PTHREAD_LIB NO)
111  set(LIBCXX_HAS_M_LIB NO)
112  set(LIBCXX_HAS_RT_LIB NO)
113  set(LIBCXX_HAS_SYSTEM_LIB NO)
114  set(LIBCXX_HAS_ATOMIC_LIB NO)
115elseif(APPLE)
116  check_library_exists(System write "" LIBCXX_HAS_SYSTEM_LIB)
117  set(LIBCXX_HAS_PTHREAD_LIB NO)
118  set(LIBCXX_HAS_M_LIB NO)
119  set(LIBCXX_HAS_RT_LIB NO)
120  set(LIBCXX_HAS_ATOMIC_LIB NO)
121elseif(FUCHSIA)
122  set(LIBCXX_HAS_M_LIB NO)
123  set(LIBCXX_HAS_PTHREAD_LIB NO)
124  set(LIBCXX_HAS_RT_LIB NO)
125  set(LIBCXX_HAS_SYSTEM_LIB NO)
126  check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
127else()
128  check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)
129  check_library_exists(m ccos "" LIBCXX_HAS_M_LIB)
130  check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB)
131  set(LIBCXX_HAS_SYSTEM_LIB NO)
132  check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
133endif()
134