config-ix.cmake 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. include(CMakePushCheckState)
  2. include(CheckLibraryExists)
  3. include(CheckCCompilerFlag)
  4. include(CheckCXXCompilerFlag)
  5. include(CheckCSourceCompiles)
  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()
  11. check_library_exists(c fopen "" LIBCXX_HAS_C_LIB)
  12. endif()
  13. if (NOT LIBCXX_USE_COMPILER_RT)
  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()
  19. endif()
  20. # libc++ is built with -nodefaultlibs, so we want all our checks to also
  21. # use this option, otherwise we may end up with an inconsistency between
  22. # the flags we think we require during configuration (if the checks are
  23. # performed without -nodefaultlibs) and the flags that are actually
  24. # required during compilation (which has the -nodefaultlibs). libc is
  25. # required for the link to go through. We remove sanitizers from the
  26. # configuration checks to avoid spurious link errors.
  27. check_c_compiler_flag(-nodefaultlibs LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG)
  28. if (LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG)
  29. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
  30. if (LIBCXX_HAS_C_LIB)
  31. list(APPEND CMAKE_REQUIRED_LIBRARIES c)
  32. endif ()
  33. if (LIBCXX_USE_COMPILER_RT)
  34. list(APPEND CMAKE_REQUIRED_FLAGS -rtlib=compiler-rt)
  35. find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
  36. list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")
  37. elseif (LIBCXX_HAS_GCC_S_LIB)
  38. list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
  39. endif ()
  40. if (MINGW)
  41. # Mingw64 requires quite a few "C" runtime libraries in order for basic
  42. # programs to link successfully with -nodefaultlibs.
  43. if (LIBCXX_USE_COMPILER_RT)
  44. set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY})
  45. else ()
  46. set(MINGW_RUNTIME gcc_s gcc)
  47. endif()
  48. set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
  49. shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
  50. moldname mingwex msvcrt)
  51. list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
  52. endif()
  53. if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
  54. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
  55. endif ()
  56. if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
  57. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters")
  58. endif ()
  59. endif ()
  60. # Check compiler pragmas
  61. if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  62. cmake_push_check_state()
  63. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
  64. check_c_source_compiles("
  65. #pragma comment(lib, \"c\")
  66. int main() { return 0; }
  67. " LIBCXX_HAS_COMMENT_LIB_PRAGMA)
  68. cmake_pop_check_state()
  69. endif()
  70. if(NOT WIN32 OR MINGW)
  71. include(CheckLibcxxAtomic)
  72. endif()
  73. # Check libraries
  74. if(WIN32 AND NOT MINGW)
  75. # TODO(compnerd) do we want to support an emulation layer that allows for the
  76. # use of pthread-win32 or similar libraries to emulate pthreads on Windows?
  77. set(LIBCXX_HAS_PTHREAD_LIB NO)
  78. set(LIBCXX_HAS_M_LIB NO)
  79. set(LIBCXX_HAS_RT_LIB NO)
  80. set(LIBCXX_HAS_SYSTEM_LIB NO)
  81. elseif(APPLE)
  82. check_library_exists(System write "" LIBCXX_HAS_SYSTEM_LIB)
  83. set(LIBCXX_HAS_PTHREAD_LIB NO)
  84. set(LIBCXX_HAS_M_LIB NO)
  85. set(LIBCXX_HAS_RT_LIB NO)
  86. else()
  87. check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)
  88. check_library_exists(m ccos "" LIBCXX_HAS_M_LIB)
  89. check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB)
  90. set(LIBCXX_HAS_SYSTEM_LIB NO)
  91. endif()