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