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) |
Petr Hosek | 2a329ac | 2017-04-06 21:06:33 +0000 | [diff] [blame] | 3 | include(CheckCCompilerFlag) |
Michael J. Spencer | 8d8164e | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 4 | include(CheckCXXCompilerFlag) |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 5 | include(CheckCSourceCompiles) |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 6 | |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 7 | if(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) |
| 11 | else() |
Petr Hosek | 76a474f | 2019-01-28 19:26:41 +0000 | [diff] [blame] | 12 | check_library_exists(c fopen "" LIBCXX_HAS_C_LIB) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 13 | endif() |
| 14 | |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 15 | if (NOT LIBCXX_USE_COMPILER_RT) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 16 | if(WIN32 AND NOT MINGW) |
| 17 | set(LIBCXX_HAS_GCC_S_LIB NO) |
| 18 | else() |
Shoaib Meenai | ed7ccd2 | 2020-04-23 21:19:11 -0700 | [diff] [blame] | 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() |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 24 | endif() |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 25 | endif() |
| 26 | |
Zbigniew Sarbinowski | 332cb64 | 2021-02-16 18:02:22 +0000 | [diff] [blame] | 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 |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 30 | # the flags we think we require during configuration (if the checks are |
Zbigniew Sarbinowski | 332cb64 | 2021-02-16 18:02:22 +0000 | [diff] [blame] | 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 |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 33 | # required for the link to go through. We remove sanitizers from the |
| 34 | # configuration checks to avoid spurious link errors. |
Zbigniew Sarbinowski | 332cb64 | 2021-02-16 18:02:22 +0000 | [diff] [blame] | 35 | |
| 36 | check_c_compiler_flag(-nostdlib++ LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG) |
| 37 | if (LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG) |
| 38 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++") |
| 39 | else() |
| 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() |
| 44 | endif() |
| 45 | |
| 46 | if (LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG OR LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 47 | if (LIBCXX_HAS_C_LIB) |
| 48 | list(APPEND CMAKE_REQUIRED_LIBRARIES c) |
| 49 | endif () |
| 50 | if (LIBCXX_USE_COMPILER_RT) |
Nehal J Wani | 8aa7ded | 2021-09-16 18:23:53 +0200 | [diff] [blame] | 51 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -rtlib=compiler-rt") |
Petr Hosek | c868a98 | 2017-04-05 22:53:05 +0000 | [diff] [blame] | 52 | find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY) |
| 53 | list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}") |
Shoaib Meenai | ed7ccd2 | 2020-04-23 21:19:11 -0700 | [diff] [blame] | 54 | elseif (LIBCXX_HAS_GCC_LIB) |
| 55 | list(APPEND CMAKE_REQUIRED_LIBRARIES gcc) |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 56 | elseif (LIBCXX_HAS_GCC_S_LIB) |
| 57 | list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s) |
| 58 | endif () |
Reid Kleckner | 4c43379 | 2017-03-31 00:34:05 +0000 | [diff] [blame] | 59 | if (MINGW) |
| 60 | # Mingw64 requires quite a few "C" runtime libraries in order for basic |
| 61 | # programs to link successfully with -nodefaultlibs. |
Martell Malone | 860299a | 2017-05-25 22:37:15 +0000 | [diff] [blame] | 62 | if (LIBCXX_USE_COMPILER_RT) |
Eric Fiselier | 07b9f0e | 2017-05-25 22:43:42 +0000 | [diff] [blame] | 63 | set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY}) |
Martell Malone | 860299a | 2017-05-25 22:37:15 +0000 | [diff] [blame] | 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}) |
Reid Kleckner | 4c43379 | 2017-03-31 00:34:05 +0000 | [diff] [blame] | 71 | endif() |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 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 () |
Ivan Krasin | 575076d | 2016-09-01 01:38:32 +0000 | [diff] [blame] | 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 () |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 78 | endif () |
| 79 | |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 80 | # Check compiler pragmas |
| 81 | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
Petr Hosek | f40548e | 2019-05-30 05:38:06 +0000 | [diff] [blame] | 82 | cmake_push_check_state() |
| 83 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas") |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 84 | check_c_source_compiles(" |
| 85 | #pragma comment(lib, \"c\") |
| 86 | int main() { return 0; } |
| 87 | " LIBCXX_HAS_COMMENT_LIB_PRAGMA) |
Petr Hosek | f40548e | 2019-05-30 05:38:06 +0000 | [diff] [blame] | 88 | cmake_pop_check_state() |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 89 | endif() |
| 90 | |
Michael J. Spencer | 8d8164e | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 91 | # Check libraries |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 92 | if(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) |
Louis Dionne | b2eca7f | 2019-05-02 17:43:48 +0000 | [diff] [blame] | 98 | set(LIBCXX_HAS_SYSTEM_LIB NO) |
Louis Dionne | e1374f1 | 2020-06-04 14:54:38 -0400 | [diff] [blame] | 99 | set(LIBCXX_HAS_ATOMIC_LIB NO) |
Louis Dionne | b2eca7f | 2019-05-02 17:43:48 +0000 | [diff] [blame] | 100 | elseif(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) |
Louis Dionne | e1374f1 | 2020-06-04 14:54:38 -0400 | [diff] [blame] | 105 | set(LIBCXX_HAS_ATOMIC_LIB NO) |
Petr Hosek | dd5b41d | 2019-12-06 11:11:31 -0800 | [diff] [blame] | 106 | elseif(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) |
Louis Dionne | e1374f1 | 2020-06-04 14:54:38 -0400 | [diff] [blame] | 111 | check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 112 | else() |
| 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) |
Louis Dionne | b2eca7f | 2019-05-02 17:43:48 +0000 | [diff] [blame] | 116 | set(LIBCXX_HAS_SYSTEM_LIB NO) |
Louis Dionne | e1374f1 | 2020-06-04 14:54:38 -0400 | [diff] [blame] | 117 | check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 118 | endif() |