folly-deps.cmake 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. include(CheckCXXSourceCompiles)
  2. include(CheckIncludeFileCXX)
  3. include(CheckFunctionExists)
  4. find_package(Boost 1.51.0 MODULE
  5. COMPONENTS
  6. context
  7. chrono
  8. date_time
  9. filesystem
  10. program_options
  11. regex
  12. system
  13. thread
  14. REQUIRED
  15. )
  16. list(APPEND FOLLY_LINK_LIBRARIES ${Boost_LIBRARIES})
  17. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIRS})
  18. find_package(DoubleConversion MODULE REQUIRED)
  19. list(APPEND FOLLY_LINK_LIBRARIES ${DOUBLE_CONVERSION_LIBRARY})
  20. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${DOUBLE_CONVERSION_INCLUDE_DIR})
  21. set(FOLLY_HAVE_LIBGFLAGS OFF)
  22. find_package(GFlags CONFIG QUIET)
  23. if (gflags_FOUND)
  24. message(STATUS "Found gflags from package config")
  25. set(FOLLY_HAVE_LIBGFLAGS ON)
  26. if (TARGET gflags-shared)
  27. list(APPEND FOLLY_SHINY_DEPENDENCIES gflags-shared)
  28. elseif (TARGET gflags)
  29. list(APPEND FOLLY_SHINY_DEPENDENCIES gflags)
  30. else()
  31. message(FATAL_ERROR "Unable to determine the target name for the GFlags package.")
  32. endif()
  33. list(APPEND CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARIES})
  34. list(APPEND CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
  35. else()
  36. find_package(GFlags MODULE)
  37. set(FOLLY_HAVE_LIBGFLAGS ${LIBGFLAGS_FOUND})
  38. list(APPEND FOLLY_LINK_LIBRARIES ${LIBGFLAGS_LIBRARY})
  39. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBGFLAGS_INCLUDE_DIR})
  40. list(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBGFLAGS_LIBRARY})
  41. list(APPEND CMAKE_REQUIRED_INCLUDES ${LIBGFLAGS_INCLUDE_DIR})
  42. endif()
  43. set(FOLLY_HAVE_LIBGLOG OFF)
  44. find_package(glog CONFIG QUIET)
  45. if (glog_FOUND)
  46. message(STATUS "Found glog from package config")
  47. set(FOLLY_HAVE_LIBGLOG ON)
  48. list(APPEND FOLLY_SHINY_DEPENDENCIES glog::glog)
  49. else()
  50. find_package(GLog MODULE)
  51. set(FOLLY_HAVE_LIBGLOG ${LIBGLOG_FOUND})
  52. list(APPEND FOLLY_LINK_LIBRARIES ${LIBGLOG_LIBRARY})
  53. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBGLOG_INCLUDE_DIR})
  54. endif()
  55. find_package(Libevent CONFIG QUIET)
  56. if(TARGET event)
  57. message(STATUS "Found libevent from package config")
  58. list(APPEND FOLLY_SHINY_DEPENDENCIES event)
  59. else()
  60. find_package(LibEvent MODULE REQUIRED)
  61. list(APPEND FOLLY_LINK_LIBRARIES ${LIBEVENT_LIB})
  62. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBEVENT_INCLUDE_DIR})
  63. endif()
  64. find_package(OpenSSL MODULE REQUIRED)
  65. list(APPEND FOLLY_LINK_LIBRARIES ${OPENSSL_LIBRARIES})
  66. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
  67. list(APPEND CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
  68. list(APPEND CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
  69. check_function_exists(ASN1_TIME_diff FOLLY_HAVE_OPENSSL_ASN1_TIME_DIFF)
  70. find_package(ZLIB MODULE)
  71. set(FOLLY_HAVE_LIBZ ${ZLIB_FOUND})
  72. if (ZLIB_FOUND)
  73. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRS})
  74. list(APPEND FOLLY_LINK_LIBRARIES ${ZLIB_LIBRARIES})
  75. endif()
  76. find_package(BZip2 MODULE)
  77. set(FOLLY_HAVE_LIBBZ2 ${BZIP2_FOUND})
  78. if (BZIP2_FOUND)
  79. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${BZIP2_INCLUDE_DIRS})
  80. list(APPEND FOLLY_LINK_LIBRARIES ${BZIP2_LIBRARIES})
  81. endif()
  82. find_package(LibLZMA MODULE)
  83. set(FOLLY_HAVE_LIBLZMA ${LIBLZMA_FOUND})
  84. if (LIBLZMA_FOUND)
  85. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBLZMA_INCLUDE_DIRS})
  86. list(APPEND FOLLY_LINK_LIBRARIES ${LIBLZMA_LIBRARIES})
  87. endif()
  88. find_package(LZ4 MODULE)
  89. set(FOLLY_HAVE_LIBLZ4 ${LZ4_FOUND})
  90. if (LZ4_FOUND)
  91. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LZ4_INCLUDE_DIR})
  92. list(APPEND FOLLY_LINK_LIBRARIES ${LZ4_LIBRARY})
  93. endif()
  94. find_package(Zstd MODULE)
  95. set(FOLLY_HAVE_LIBZSTD ${ZSTD_FOUND})
  96. if(ZSTD_FOUND)
  97. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${ZSTD_INCLUDE_DIR})
  98. list(APPEND FOLLY_LINK_LIBRARIES ${ZSTD_LIBRARY})
  99. endif()
  100. find_package(Snappy MODULE)
  101. set(FOLLY_HAVE_LIBSNAPPY ${SNAPPY_FOUND})
  102. if (SNAPPY_FOUND)
  103. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${SNAPPY_INCLUDE_DIR})
  104. list(APPEND FOLLY_LINK_LIBRARIES ${SNAPPY_LIBRARY})
  105. endif()
  106. find_package(LibDwarf)
  107. list(APPEND FOLLY_LINK_LIBRARIES ${LIBDWARF_LIBRARIES})
  108. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBDWARF_INCLUDE_DIRS})
  109. find_package(Libiberty)
  110. list(APPEND FOLLY_LINK_LIBRARIES ${LIBIBERTY_LIBRARIES})
  111. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBIBERTY_INCLUDE_DIRS})
  112. find_package(LibAIO)
  113. list(APPEND FOLLY_LINK_LIBRARIES ${LIBAIO_LIBRARIES})
  114. list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBAIO_INCLUDE_DIRS})
  115. list(APPEND FOLLY_LINK_LIBRARIES ${CMAKE_DL_LIBS})
  116. list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_DL_LIBS})
  117. set(FOLLY_USE_SYMBOLIZER OFF)
  118. CHECK_INCLUDE_FILE_CXX(elf.h FOLLY_HAVE_ELF_H)
  119. find_library(UNWIND_LIBRARIES NAMES unwind)
  120. if (UNWIND_LIBRARIES)
  121. list(APPEND FOLLY_LINK_LIBRARIES ${UNWIND_LIBRARIES})
  122. list(APPEND CMAKE_REQUIRED_LIBRARIES ${UNWIND_LIBRARIES})
  123. endif()
  124. check_function_exists(backtrace FOLLY_HAVE_BACKTRACE)
  125. if (FOLLY_HAVE_ELF_H AND FOLLY_HAVE_BACKTRACE AND LIBDWARF_FOUND)
  126. set(FOLLY_USE_SYMBOLIZER ON)
  127. endif()
  128. message(STATUS "Setting FOLLY_USE_SYMBOLIZER: ${FOLLY_USE_SYMBOLIZER}")
  129. # Using clang with libstdc++ requires explicitly linking against libatomic
  130. check_cxx_source_compiles("
  131. #include <atomic>
  132. int main(int argc, char** argv) {
  133. struct Test { int val; };
  134. std::atomic<Test> s;
  135. return static_cast<int>(s.is_lock_free());
  136. }"
  137. FOLLY_CPP_ATOMIC_BUILTIN
  138. )
  139. if(NOT FOLLY_CPP_ATOMIC_BUILTIN)
  140. list(APPEND CMAKE_REQUIRED_LIBRARIES atomic)
  141. list(APPEND FOLLY_LINK_LIBRARIES atomic)
  142. check_cxx_source_compiles("
  143. #include <atomic>
  144. int main(int argc, char** argv) {
  145. struct Test { int val; };
  146. std::atomic<Test> s2;
  147. return static_cast<int>(s2.is_lock_free());
  148. }"
  149. FOLLY_CPP_ATOMIC_WITH_LIBATOMIC
  150. )
  151. if (NOT FOLLY_CPP_ATOMIC_WITH_LIBATOMIC)
  152. message(
  153. FATAL_ERROR "unable to link C++ std::atomic code: you may need \
  154. to install GNU libatomic"
  155. )
  156. endif()
  157. endif()
  158. option(
  159. FOLLY_ASAN_ENABLED
  160. "Build folly with Address Sanitizer enabled."
  161. OFF
  162. )
  163. if (FOLLY_ASAN_ENABLED)
  164. if ("${CMAKE_CXX_COMPILER_ID}" MATCHES GNU)
  165. set(FOLLY_ASAN_ENABLED ON)
  166. set(FOLLY_ASAN_FLAGS -fsanitize=address,undefined)
  167. list(APPEND FOLLY_CXX_FLAGS ${FOLLY_ASAN_FLAGS})
  168. # All of the functions in folly/detail/Sse.cpp are intended to be compiled
  169. # with ASAN disabled. They are marked with attributes to disable the
  170. # sanitizer, but even so, gcc fails to compile them for some reason when
  171. # sanitization is enabled on the compile line.
  172. set_source_files_properties(
  173. "${CMAKE_SOURCE_DIR}/folly/detail/Sse.cpp"
  174. PROPERTIES COMPILE_FLAGS -fno-sanitize=address,undefined
  175. )
  176. elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES Clang)
  177. set(FOLLY_ASAN_ENABLED ON)
  178. set(
  179. FOLLY_ASAN_FLAGS
  180. -fno-common
  181. -fsanitize=address,undefined,integer,nullability
  182. -fno-sanitize=unsigned-integer-overflow
  183. )
  184. list(APPEND FOLLY_CXX_FLAGS ${FOLLY_ASAN_FLAGS})
  185. endif()
  186. endif()
  187. add_library(folly_deps INTERFACE)
  188. list(REMOVE_DUPLICATES FOLLY_INCLUDE_DIRECTORIES)
  189. target_include_directories(folly_deps INTERFACE ${FOLLY_INCLUDE_DIRECTORIES})
  190. target_link_libraries(folly_deps INTERFACE
  191. ${FOLLY_LINK_LIBRARIES}
  192. ${FOLLY_SHINY_DEPENDENCIES}
  193. ${FOLLY_ASAN_FLAGS}
  194. )