FollyConfigChecks.cmake 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. include(CheckCXXSourceCompiles)
  2. include(CheckCXXSourceRuns)
  3. include(CheckFunctionExists)
  4. include(CheckIncludeFileCXX)
  5. include(CheckSymbolExists)
  6. include(CheckTypeSize)
  7. include(CheckCXXCompilerFlag)
  8. CHECK_INCLUDE_FILE_CXX(jemalloc/jemalloc.h FOLLY_USE_JEMALLOC)
  9. if(NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
  10. # clang only rejects unknown warning flags if -Werror=unknown-warning-option
  11. # is also specified.
  12. CHECK_CXX_COMPILER_FLAG(
  13. -Werror=unknown-warning-option
  14. COMPILER_HAS_UNKNOWN_WARNING_OPTION)
  15. if (COMPILER_HAS_UNKNOWN_WARNING_OPTION)
  16. set(CMAKE_REQUIRED_FLAGS
  17. "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-warning-option")
  18. endif()
  19. CHECK_CXX_COMPILER_FLAG(-Wshadow-local COMPILER_HAS_W_SHADOW_LOCAL)
  20. CHECK_CXX_COMPILER_FLAG(
  21. -Wshadow-compatible-local
  22. COMPILER_HAS_W_SHADOW_COMPATIBLE_LOCAL)
  23. if (COMPILER_HAS_W_SHADOW_LOCAL AND COMPILER_HAS_W_SHADOW_COMPATIBLE_LOCAL)
  24. set(FOLLY_HAVE_SHADOW_LOCAL_WARNINGS ON)
  25. list(APPEND FOLLY_CXX_FLAGS -Wshadow-compatible-local)
  26. endif()
  27. CHECK_CXX_COMPILER_FLAG(-Wnoexcept-type COMPILER_HAS_W_NOEXCEPT_TYPE)
  28. if (COMPILER_HAS_W_NOEXCEPT_TYPE)
  29. list(APPEND FOLLY_CXX_FLAGS -Wno-noexcept-type)
  30. endif()
  31. CHECK_CXX_COMPILER_FLAG(
  32. -Wnullability-completeness
  33. COMPILER_HAS_W_NULLABILITY_COMPLETENESS)
  34. if (COMPILER_HAS_W_NULLABILITY_COMPLETENESS)
  35. list(APPEND FOLLY_CXX_FLAGS -Wno-nullability-completeness)
  36. endif()
  37. CHECK_CXX_COMPILER_FLAG(
  38. -Winconsistent-missing-override
  39. COMPILER_HAS_W_INCONSISTENT_MISSING_OVERRIDE)
  40. if (COMPILER_HAS_W_INCONSISTENT_MISSING_OVERRIDE)
  41. list(APPEND FOLLY_CXX_FLAGS -Wno-inconsistent-missing-override)
  42. endif()
  43. CHECK_CXX_COMPILER_FLAG(-faligned-new COMPILER_HAS_F_ALIGNED_NEW)
  44. if (COMPILER_HAS_F_ALIGNED_NEW)
  45. list(APPEND FOLLY_CXX_FLAGS -faligned-new)
  46. endif()
  47. CHECK_CXX_COMPILER_FLAG(-fopenmp COMPILER_HAS_F_OPENMP)
  48. if (COMPILER_HAS_F_OPENMP)
  49. list(APPEND FOLLY_CXX_FLAGS -fopenmp)
  50. endif()
  51. endif()
  52. set(FOLLY_ORIGINAL_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  53. string(REGEX REPLACE
  54. "-std=(c|gnu)\\+\\+.."
  55. ""
  56. CMAKE_REQUIRED_FLAGS
  57. "${CMAKE_REQUIRED_FLAGS}")
  58. check_symbol_exists(pthread_atfork pthread.h FOLLY_HAVE_PTHREAD_ATFORK)
  59. # Unfortunately check_symbol_exists() does not work for memrchr():
  60. # it fails complaining that there are multiple overloaded versions of memrchr()
  61. check_function_exists(memrchr FOLLY_HAVE_MEMRCHR)
  62. check_symbol_exists(preadv sys/uio.h FOLLY_HAVE_PREADV)
  63. check_symbol_exists(pwritev sys/uio.h FOLLY_HAVE_PWRITEV)
  64. check_symbol_exists(clock_gettime time.h FOLLY_HAVE_CLOCK_GETTIME)
  65. check_function_exists(malloc_usable_size FOLLY_HAVE_MALLOC_USABLE_SIZE)
  66. set(CMAKE_REQUIRED_FLAGS "${FOLLY_ORIGINAL_CMAKE_REQUIRED_FLAGS}")
  67. check_cxx_source_compiles("
  68. #pragma GCC diagnostic error \"-Wattributes\"
  69. extern \"C\" void (*test_ifunc(void))() { return 0; }
  70. void func() __attribute__((ifunc(\"test_ifunc\")));
  71. int main() { return 0; }"
  72. FOLLY_HAVE_IFUNC
  73. )
  74. check_cxx_source_compiles("
  75. #include <type_traits>
  76. const bool val = std::is_trivially_copyable<bool>::value;
  77. int main() { return 0; }"
  78. FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
  79. )
  80. check_cxx_source_runs("
  81. int main(int, char**) {
  82. char buf[64] = {0};
  83. unsigned long *ptr = (unsigned long *)(buf + 1);
  84. *ptr = 0xdeadbeef;
  85. return (*ptr & 0xff) == 0xef ? 0 : 1;
  86. }"
  87. FOLLY_HAVE_UNALIGNED_ACCESS
  88. )
  89. check_cxx_source_compiles("
  90. int main(int argc, char** argv) {
  91. unsigned size = argc;
  92. char data[size];
  93. return 0;
  94. }"
  95. FOLLY_HAVE_VLA
  96. )
  97. check_cxx_source_compiles("
  98. extern \"C\" void configure_link_extern_weak_test() __attribute__((weak));
  99. int main(int argc, char** argv) {
  100. return configure_link_extern_weak_test == nullptr;
  101. }"
  102. FOLLY_HAVE_WEAK_SYMBOLS
  103. )
  104. check_cxx_source_runs("
  105. #include <dlfcn.h>
  106. int main() {
  107. void *h = dlopen(\"linux-vdso.so.1\", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
  108. if (h == nullptr) {
  109. return -1;
  110. }
  111. dlclose(h);
  112. return 0;
  113. }"
  114. FOLLY_HAVE_LINUX_VDSO
  115. )
  116. check_type_size(__int128 INT128_SIZE LANGUAGE CXX)
  117. if (NOT INT128_SIZE STREQUAL "")
  118. set(FOLLY_HAVE_INT128_T ON)
  119. check_cxx_source_compiles("
  120. #include <functional>
  121. #include <type_traits>
  122. #include <utility>
  123. static_assert(
  124. ::std::is_same<::std::make_signed<unsigned __int128>::type,
  125. __int128>::value,
  126. \"signed form of 'unsigned __uint128' must be '__int128'.\");
  127. static_assert(
  128. sizeof(::std::hash<__int128>{}(0)) > 0, \
  129. \"std::hash<__int128> is disabled.\");
  130. int main() { return 0; }"
  131. HAVE_INT128_TRAITS
  132. )
  133. if (HAVE_INT128_TRAITS)
  134. set(FOLLY_SUPPLY_MISSING_INT128_TRAITS OFF)
  135. else()
  136. set(FOLLY_SUPPLY_MISSING_INT128_TRAITS ON)
  137. endif()
  138. endif()
  139. check_cxx_source_runs("
  140. #include <cstddef>
  141. #include <cwchar>
  142. int main(int argc, char** argv) {
  143. return wcstol(L\"01\", nullptr, 10) == 1 ? 0 : 1;
  144. }"
  145. FOLLY_HAVE_WCHAR_SUPPORT
  146. )
  147. check_cxx_source_compiles("
  148. #include <ext/random>
  149. int main(int argc, char** argv) {
  150. __gnu_cxx::sfmt19937 rng;
  151. return 0;
  152. }"
  153. FOLLY_HAVE_EXTRANDOM_SFMT19937
  154. )
  155. check_cxx_source_compiles("
  156. #include <type_traits>
  157. #if !_LIBCPP_VERSION
  158. #error No libc++
  159. #endif
  160. int main() { return 0; }"
  161. FOLLY_USE_LIBCPP
  162. )
  163. check_cxx_source_compiles("
  164. #include <type_traits>
  165. #if !__GLIBCXX__
  166. #error No libstdc++
  167. #endif
  168. int main() { return 0; }"
  169. FOLLY_USE_LIBSTDCPP
  170. )
  171. check_cxx_source_runs("
  172. #include <string.h>
  173. #include <errno.h>
  174. int main(int argc, char** argv) {
  175. char buf[1024];
  176. buf[0] = 0;
  177. int ret = strerror_r(ENOMEM, buf, sizeof(buf));
  178. return ret;
  179. }"
  180. FOLLY_HAVE_XSI_STRERROR_R
  181. )
  182. check_cxx_source_runs("
  183. #include <stdarg.h>
  184. #include <stdio.h>
  185. int call_vsnprintf(const char* fmt, ...) {
  186. char buf[256];
  187. va_list ap;
  188. va_start(ap, fmt);
  189. int result = vsnprintf(buf, sizeof(buf), fmt, ap);
  190. va_end(ap);
  191. return result;
  192. }
  193. int main(int argc, char** argv) {
  194. return call_vsnprintf(\"%\", 1) < 0 ? 0 : 1;
  195. }"
  196. HAVE_VSNPRINTF_ERRORS
  197. )
  198. if (FOLLY_HAVE_LIBGFLAGS)
  199. # Older releases of gflags used the namespace "gflags"; newer releases
  200. # use "google" but also make symbols available in the deprecated "gflags"
  201. # namespace too. The folly code internally uses "gflags" unless we tell it
  202. # otherwise.
  203. check_cxx_source_compiles("
  204. #include <gflags/gflags.h>
  205. int main() {
  206. gflags::GetArgv();
  207. return 0;
  208. }
  209. "
  210. GFLAGS_NAMESPACE_IS_GFLAGS
  211. )
  212. if (GFLAGS_NAMESPACE_IS_GFLAGS)
  213. set(FOLLY_UNUSUAL_GFLAGS_NAMESPACE OFF)
  214. set(FOLLY_GFLAGS_NAMESPACE gflags)
  215. else()
  216. set(FOLLY_UNUSUAL_GFLAGS_NAMESPACE ON)
  217. set(FOLLY_GFLAGS_NAMESPACE google)
  218. endif()
  219. endif()