Petr Hosek | f40548e | 2019-05-30 05:38:06 +0000 | [diff] [blame] | 1 | include(CMakePushCheckState) |
Michael J. Spencer | 8d8164e | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 2 | include(CheckLibraryExists) |
Martin Storsjö | 884c237 | 2021-10-24 01:11:20 +0300 | [diff] [blame] | 3 | include(CheckLinkerFlag) |
Petr Hosek | 2a329ac | 2017-04-06 21:06:33 +0000 | [diff] [blame] | 4 | include(CheckCCompilerFlag) |
Michael J. Spencer | 8d8164e | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 5 | include(CheckCXXCompilerFlag) |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 6 | include(CheckCSourceCompiles) |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 7 | |
Martin Storsjö | 884c237 | 2021-10-24 01:11:20 +0300 | [diff] [blame] | 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. |
| 15 | llvm_check_linker_flag("--unwindlib=none" LIBCXX_SUPPORTS_UNWINDLIB_NONE_FLAG) |
| 16 | if (LIBCXX_SUPPORTS_UNWINDLIB_NONE_FLAG) |
| 17 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none") |
| 18 | endif() |
| 19 | |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 20 | if(WIN32 AND NOT MINGW) |
| 21 | # NOTE(compnerd) this is technically a lie, there is msvcrt, but for now, lets |
| 22 | # let the default linking take care of that. |
| 23 | set(LIBCXX_HAS_C_LIB NO) |
| 24 | else() |
Petr Hosek | 76a474f | 2019-01-28 19:26:41 +0000 | [diff] [blame] | 25 | check_library_exists(c fopen "" LIBCXX_HAS_C_LIB) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 26 | endif() |
| 27 | |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 28 | if (NOT LIBCXX_USE_COMPILER_RT) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 29 | if(WIN32 AND NOT MINGW) |
| 30 | set(LIBCXX_HAS_GCC_S_LIB NO) |
| 31 | else() |
Shoaib Meenai | ed7ccd2 | 2020-04-23 21:19:11 -0700 | [diff] [blame] | 32 | if(ANDROID) |
| 33 | check_library_exists(gcc __gcc_personality_v0 "" LIBCXX_HAS_GCC_LIB) |
| 34 | else() |
| 35 | check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB) |
| 36 | endif() |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 37 | endif() |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 38 | endif() |
| 39 | |
Zbigniew Sarbinowski | 332cb64 | 2021-02-16 18:02:22 +0000 | [diff] [blame] | 40 | # libc++ is using -nostdlib++ at the link step when available, |
| 41 | # otherwise -nodefaultlibs is used. We want all our checks to also |
| 42 | # use one of these options, otherwise we may end up with an inconsistency between |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 43 | # the flags we think we require during configuration (if the checks are |
Zbigniew Sarbinowski | 332cb64 | 2021-02-16 18:02:22 +0000 | [diff] [blame] | 44 | # performed without one of those options) and the flags that are actually |
| 45 | # required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 46 | # required for the link to go through. We remove sanitizers from the |
| 47 | # configuration checks to avoid spurious link errors. |
Zbigniew Sarbinowski | 332cb64 | 2021-02-16 18:02:22 +0000 | [diff] [blame] | 48 | |
| 49 | check_c_compiler_flag(-nostdlib++ LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG) |
| 50 | if (LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG) |
| 51 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++") |
| 52 | else() |
| 53 | check_c_compiler_flag(-nodefaultlibs LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) |
| 54 | if (LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) |
| 55 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs") |
| 56 | endif() |
| 57 | endif() |
| 58 | |
| 59 | if (LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG OR LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 60 | if (LIBCXX_HAS_C_LIB) |
| 61 | list(APPEND CMAKE_REQUIRED_LIBRARIES c) |
| 62 | endif () |
| 63 | if (LIBCXX_USE_COMPILER_RT) |
Petr Hosek | ceb43a3 | 2020-09-28 17:37:20 -0700 | [diff] [blame] | 64 | include(HandleCompilerRT) |
| 65 | find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY |
| 66 | FLAGS ${LIBCXX_COMPILE_FLAGS}) |
Petr Hosek | c868a98 | 2017-04-05 22:53:05 +0000 | [diff] [blame] | 67 | list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}") |
Shoaib Meenai | ed7ccd2 | 2020-04-23 21:19:11 -0700 | [diff] [blame] | 68 | elseif (LIBCXX_HAS_GCC_LIB) |
| 69 | list(APPEND CMAKE_REQUIRED_LIBRARIES gcc) |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 70 | elseif (LIBCXX_HAS_GCC_S_LIB) |
| 71 | list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s) |
| 72 | endif () |
Reid Kleckner | 4c43379 | 2017-03-31 00:34:05 +0000 | [diff] [blame] | 73 | if (MINGW) |
| 74 | # Mingw64 requires quite a few "C" runtime libraries in order for basic |
| 75 | # programs to link successfully with -nodefaultlibs. |
Martell Malone | 860299a | 2017-05-25 22:37:15 +0000 | [diff] [blame] | 76 | if (LIBCXX_USE_COMPILER_RT) |
Eric Fiselier | 07b9f0e | 2017-05-25 22:43:42 +0000 | [diff] [blame] | 77 | set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY}) |
Martell Malone | 860299a | 2017-05-25 22:37:15 +0000 | [diff] [blame] | 78 | else () |
| 79 | set(MINGW_RUNTIME gcc_s gcc) |
| 80 | endif() |
| 81 | set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32 |
| 82 | shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME} |
| 83 | moldname mingwex msvcrt) |
| 84 | list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES}) |
Reid Kleckner | 4c43379 | 2017-03-31 00:34:05 +0000 | [diff] [blame] | 85 | endif() |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 86 | if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize) |
| 87 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all") |
| 88 | endif () |
Ivan Krasin | 575076d | 2016-09-01 01:38:32 +0000 | [diff] [blame] | 89 | if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage) |
| 90 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters") |
| 91 | endif () |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 92 | endif () |
| 93 | |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 94 | # Check compiler pragmas |
| 95 | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
Petr Hosek | f40548e | 2019-05-30 05:38:06 +0000 | [diff] [blame] | 96 | cmake_push_check_state() |
| 97 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas") |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 98 | check_c_source_compiles(" |
| 99 | #pragma comment(lib, \"c\") |
| 100 | int main() { return 0; } |
| 101 | " LIBCXX_HAS_COMMENT_LIB_PRAGMA) |
Petr Hosek | f40548e | 2019-05-30 05:38:06 +0000 | [diff] [blame] | 102 | cmake_pop_check_state() |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 103 | endif() |
| 104 | |
Michael J. Spencer | 8d8164e | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 105 | # Check libraries |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 106 | if(WIN32 AND NOT MINGW) |
| 107 | # TODO(compnerd) do we want to support an emulation layer that allows for the |
| 108 | # use of pthread-win32 or similar libraries to emulate pthreads on Windows? |
| 109 | set(LIBCXX_HAS_PTHREAD_LIB NO) |
| 110 | set(LIBCXX_HAS_M_LIB NO) |
| 111 | set(LIBCXX_HAS_RT_LIB NO) |
Louis Dionne | b2eca7f | 2019-05-02 17:43:48 +0000 | [diff] [blame] | 112 | set(LIBCXX_HAS_SYSTEM_LIB NO) |
Louis Dionne | e1374f1 | 2020-06-04 14:54:38 -0400 | [diff] [blame] | 113 | set(LIBCXX_HAS_ATOMIC_LIB NO) |
Louis Dionne | b2eca7f | 2019-05-02 17:43:48 +0000 | [diff] [blame] | 114 | elseif(APPLE) |
| 115 | check_library_exists(System write "" LIBCXX_HAS_SYSTEM_LIB) |
| 116 | set(LIBCXX_HAS_PTHREAD_LIB NO) |
| 117 | set(LIBCXX_HAS_M_LIB NO) |
| 118 | set(LIBCXX_HAS_RT_LIB NO) |
Louis Dionne | e1374f1 | 2020-06-04 14:54:38 -0400 | [diff] [blame] | 119 | set(LIBCXX_HAS_ATOMIC_LIB NO) |
Petr Hosek | dd5b41d | 2019-12-06 11:11:31 -0800 | [diff] [blame] | 120 | elseif(FUCHSIA) |
| 121 | set(LIBCXX_HAS_M_LIB NO) |
| 122 | set(LIBCXX_HAS_PTHREAD_LIB NO) |
| 123 | set(LIBCXX_HAS_RT_LIB NO) |
| 124 | set(LIBCXX_HAS_SYSTEM_LIB NO) |
Louis Dionne | e1374f1 | 2020-06-04 14:54:38 -0400 | [diff] [blame] | 125 | check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 126 | else() |
| 127 | check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB) |
| 128 | check_library_exists(m ccos "" LIBCXX_HAS_M_LIB) |
| 129 | check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB) |
Louis Dionne | b2eca7f | 2019-05-02 17:43:48 +0000 | [diff] [blame] | 130 | set(LIBCXX_HAS_SYSTEM_LIB NO) |
Louis Dionne | e1374f1 | 2020-06-04 14:54:38 -0400 | [diff] [blame] | 131 | check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 132 | endif() |