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