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