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 | |
| 27 | # libc++ is built with -nodefaultlibs, so we want all our checks to also |
| 28 | # use this option, otherwise we may end up with an inconsistency between |
| 29 | # the flags we think we require during configuration (if the checks are |
| 30 | # performed without -nodefaultlibs) and the flags that are actually |
| 31 | # required during compilation (which has the -nodefaultlibs). libc is |
| 32 | # required for the link to go through. We remove sanitizers from the |
| 33 | # configuration checks to avoid spurious link errors. |
Petr Hosek | 2a329ac | 2017-04-06 21:06:33 +0000 | [diff] [blame] | 34 | check_c_compiler_flag(-nodefaultlibs LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 35 | if (LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) |
| 36 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs") |
| 37 | if (LIBCXX_HAS_C_LIB) |
| 38 | list(APPEND CMAKE_REQUIRED_LIBRARIES c) |
| 39 | endif () |
| 40 | if (LIBCXX_USE_COMPILER_RT) |
Petr Hosek | c868a98 | 2017-04-05 22:53:05 +0000 | [diff] [blame] | 41 | list(APPEND CMAKE_REQUIRED_FLAGS -rtlib=compiler-rt) |
| 42 | find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY) |
| 43 | list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}") |
Shoaib Meenai | ed7ccd2 | 2020-04-23 21:19:11 -0700 | [diff] [blame] | 44 | elseif (LIBCXX_HAS_GCC_LIB) |
| 45 | list(APPEND CMAKE_REQUIRED_LIBRARIES gcc) |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 46 | elseif (LIBCXX_HAS_GCC_S_LIB) |
| 47 | list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s) |
| 48 | endif () |
Reid Kleckner | 4c43379 | 2017-03-31 00:34:05 +0000 | [diff] [blame] | 49 | if (MINGW) |
| 50 | # Mingw64 requires quite a few "C" runtime libraries in order for basic |
| 51 | # programs to link successfully with -nodefaultlibs. |
Martell Malone | 860299a | 2017-05-25 22:37:15 +0000 | [diff] [blame] | 52 | if (LIBCXX_USE_COMPILER_RT) |
Eric Fiselier | 07b9f0e | 2017-05-25 22:43:42 +0000 | [diff] [blame] | 53 | set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY}) |
Martell Malone | 860299a | 2017-05-25 22:37:15 +0000 | [diff] [blame] | 54 | else () |
| 55 | set(MINGW_RUNTIME gcc_s gcc) |
| 56 | endif() |
| 57 | set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32 |
| 58 | shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME} |
| 59 | moldname mingwex msvcrt) |
| 60 | list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES}) |
Reid Kleckner | 4c43379 | 2017-03-31 00:34:05 +0000 | [diff] [blame] | 61 | endif() |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 62 | if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize) |
| 63 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all") |
| 64 | endif () |
Ivan Krasin | 575076d | 2016-09-01 01:38:32 +0000 | [diff] [blame] | 65 | if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage) |
| 66 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters") |
| 67 | endif () |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 68 | endif () |
| 69 | |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 70 | # Check compiler pragmas |
| 71 | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
Petr Hosek | f40548e | 2019-05-30 05:38:06 +0000 | [diff] [blame] | 72 | cmake_push_check_state() |
| 73 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas") |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 74 | check_c_source_compiles(" |
| 75 | #pragma comment(lib, \"c\") |
| 76 | int main() { return 0; } |
| 77 | " LIBCXX_HAS_COMMENT_LIB_PRAGMA) |
Petr Hosek | f40548e | 2019-05-30 05:38:06 +0000 | [diff] [blame] | 78 | cmake_pop_check_state() |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 79 | endif() |
| 80 | |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 81 | if(NOT WIN32 OR MINGW) |
| 82 | include(CheckLibcxxAtomic) |
| 83 | endif() |
Michael J. Spencer | 8d8164e | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 84 | |
Michael J. Spencer | 8d8164e | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 85 | # Check libraries |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 86 | if(WIN32 AND NOT MINGW) |
| 87 | # TODO(compnerd) do we want to support an emulation layer that allows for the |
| 88 | # use of pthread-win32 or similar libraries to emulate pthreads on Windows? |
| 89 | set(LIBCXX_HAS_PTHREAD_LIB NO) |
| 90 | set(LIBCXX_HAS_M_LIB NO) |
| 91 | set(LIBCXX_HAS_RT_LIB NO) |
Louis Dionne | b2eca7f | 2019-05-02 17:43:48 +0000 | [diff] [blame] | 92 | set(LIBCXX_HAS_SYSTEM_LIB NO) |
| 93 | elseif(APPLE) |
| 94 | check_library_exists(System write "" LIBCXX_HAS_SYSTEM_LIB) |
| 95 | set(LIBCXX_HAS_PTHREAD_LIB NO) |
| 96 | set(LIBCXX_HAS_M_LIB NO) |
| 97 | set(LIBCXX_HAS_RT_LIB NO) |
Petr Hosek | dd5b41d | 2019-12-06 11:11:31 -0800 | [diff] [blame] | 98 | elseif(FUCHSIA) |
| 99 | set(LIBCXX_HAS_M_LIB NO) |
| 100 | set(LIBCXX_HAS_PTHREAD_LIB NO) |
| 101 | set(LIBCXX_HAS_RT_LIB NO) |
| 102 | set(LIBCXX_HAS_SYSTEM_LIB NO) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 103 | else() |
| 104 | check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB) |
| 105 | check_library_exists(m ccos "" LIBCXX_HAS_M_LIB) |
| 106 | check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB) |
Louis Dionne | b2eca7f | 2019-05-02 17:43:48 +0000 | [diff] [blame] | 107 | set(LIBCXX_HAS_SYSTEM_LIB NO) |
Saleem Abdulrasool | 61de6f2 | 2017-01-01 20:20:38 +0000 | [diff] [blame] | 108 | endif() |