CMakeLists.txt 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. cmake_minimum_required(VERSION 3.6)
  2. project(hv VERSION 1.3.3)
  3. option(BUILD_SHARED "build shared library" ON)
  4. option(BUILD_STATIC "build static library" ON)
  5. option(BUILD_EXAMPLES "build examples" ON)
  6. option(BUILD_UNITTEST "build unittest" OFF)
  7. # see config.ini
  8. option(WITH_PROTOCOL "compile protocol" OFF)
  9. option(WITH_EVPP "compile evpp" ON)
  10. option(WITH_HTTP "compile http" ON)
  11. option(WITH_HTTP_SERVER "compile http/server" ON)
  12. option(WITH_HTTP_CLIENT "compile http/client" ON)
  13. option(WITH_MQTT "compile mqtt" OFF)
  14. option(ENABLE_UDS "Unix Domain Socket" OFF)
  15. option(USE_MULTIMAP "MultiMap" OFF)
  16. option(WITH_CURL "with curl library (deprecated)" OFF)
  17. option(WITH_NGHTTP2 "with nghttp2 library" OFF)
  18. option(WITH_OPENSSL "with openssl library" OFF)
  19. option(WITH_GNUTLS "with gnutls library" OFF)
  20. option(WITH_MBEDTLS "with mbedtls library" OFF)
  21. option(WITH_KCP "compile event/kcp" OFF)
  22. if(WIN32 OR MINGW)
  23. option(WITH_WEPOLL "compile event/wepoll -> use iocp" ON)
  24. option(ENABLE_WINDUMP "Windows MiniDumpWriteDump" OFF)
  25. option(BUILD_FOR_MT "build for /MT" OFF)
  26. if(BUILD_FOR_MT)
  27. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
  28. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
  29. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
  30. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
  31. endif()
  32. endif()
  33. message(STATUS "CMAKE_SOURCE_DIR=${CMAKE_SOURCE_DIR}")
  34. message(STATUS "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}")
  35. if(NOT "${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
  36. set(BUILD_EXAMPLES OFF)
  37. endif()
  38. if(IOS)
  39. set(BUILD_SHARED OFF)
  40. set(BUILD_EXAMPLES OFF)
  41. endif()
  42. set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
  43. include(utils)
  44. include(vars)
  45. # see configure
  46. # Checks for header files
  47. check_header("stdbool.h")
  48. check_header("stdint.h")
  49. check_header("stdatomic.h")
  50. check_header("sys/types.h")
  51. check_header("sys/stat.h")
  52. check_header("sys/time.h")
  53. check_header("fcntl.h")
  54. check_header("pthread.h")
  55. check_header("endian.h")
  56. check_header("sys/endian.h")
  57. # Checks for functions
  58. if(NOT MSVC)
  59. set(CMAKE_REQUIRED_LIBRARIES "-pthread")
  60. endif()
  61. check_function("gettid" "unistd.h")
  62. check_function("strlcpy" "string.h")
  63. check_function("strlcat" "string.h")
  64. check_function("clock_gettime" "time.h")
  65. check_function("gettimeofday" "sys/time.h")
  66. check_function("pthread_spin_lock" "pthread.h")
  67. check_function("pthread_mutex_timedlock" "pthread.h")
  68. check_function("sem_timedwait" "semaphore.h")
  69. check_function("pipe" "unistd.h")
  70. check_function("socketpair" "sys/socket.h")
  71. check_function("eventfd" "sys/eventfd.h")
  72. check_function("setproctitle" "unistd.h")
  73. if (NOT HAVE_CLOCK_GETTIME)
  74. include(CheckLibraryExists)
  75. check_library_exists(rt clock_gettime "" HAVE_CLOCK_GETTIME_IN_RT)
  76. if (HAVE_CLOCK_GETTIME_IN_RT)
  77. set(HAVE_CLOCK_GETTIME ${HAVE_CLOCK_GETTIME_IN_RT})
  78. endif()
  79. endif()
  80. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/hconfig.h.in ${CMAKE_CURRENT_SOURCE_DIR}/hconfig.h)
  81. # see Makefile.in
  82. set(CMAKE_C_STANDARD 99)
  83. set(CMAKE_C_STANDARD_REQUIRED True)
  84. set(CMAKE_CXX_STANDARD 11)
  85. set(CMAKE_CXX_STANDARD_REQUIRED True)
  86. set(INCDIR include)
  87. set(SRCDIR src)
  88. set(LIBDIR lib)
  89. set(BINDIR bin)
  90. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${LIBDIR})
  91. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${LIBDIR})
  92. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BINDIR})
  93. message(STATUS "CMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
  94. set(INCDIRS . include 3rd/include)
  95. set(LIBDIRS . lib 3rd/lib)
  96. include_directories(${INCDIRS} ${SRCDIR})
  97. link_directories(${LIBDIRS})
  98. message(STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
  99. if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
  100. add_definitions(-DDEBUG)
  101. else()
  102. add_definitions(-DNDEBUG)
  103. endif()
  104. if(ENABLE_UDS)
  105. add_definitions(-DENABLE_UDS)
  106. endif()
  107. if(USE_MULTIMAP)
  108. add_definitions(-DUSE_MULTIMAP)
  109. endif()
  110. if(WITH_CURL)
  111. add_definitions(-DWITH_CURL)
  112. set(LIBS ${LIBS} curl)
  113. if(WIN32 OR MINGW)
  114. set(LIBS ${LIBS} wldap32 advapi32 crypt32)
  115. endif()
  116. endif()
  117. if(WITH_NGHTTP2)
  118. add_definitions(-DWITH_NGHTTP2)
  119. set(LIBS ${LIBS} nghttp2)
  120. endif()
  121. if(WITH_OPENSSL)
  122. add_definitions(-DWITH_OPENSSL)
  123. find_package(OpenSSL)
  124. if(OpenSSL_FOUND)
  125. set(LIBS ${LIBS} OpenSSL::SSL OpenSSL::Crypto)
  126. else()
  127. set(LIBS ${LIBS} ssl crypto)
  128. endif()
  129. endif()
  130. if(WITH_GNUTLS)
  131. add_definitions(-DWITH_GNUTLS)
  132. set(LIBS ${LIBS} gnutls)
  133. endif()
  134. if(WITH_MBEDTLS)
  135. add_definitions(-DWITH_MBEDTLS)
  136. set(LIBS ${LIBS} mbedtls mbedx509 mbedcrypto)
  137. endif()
  138. if(WIN32 OR MINGW)
  139. add_definitions(-DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_WARNINGS -D_WIN32_WINNT=0x0600)
  140. set(LIBS ${LIBS} secur32 crypt32 winmm iphlpapi ws2_32)
  141. if(ENABLE_WINDUMP)
  142. add_definitions(-DENABLE_WINDUMP)
  143. set(LIBS ${LIBS} dbghelp)
  144. endif()
  145. endif()
  146. if(ANDROID)
  147. set(LIBS ${LIBS} log)
  148. elseif(UNIX AND NOT MINGW)
  149. set(LIBS ${LIBS} pthread m dl)
  150. find_library(RT_LIBRARY rt)
  151. if(RT_LIBRARY)
  152. set(LIBS ${LIBS} rt)
  153. endif()
  154. endif()
  155. if(APPLE)
  156. set(LIBS ${LIBS} "-framework CoreFoundation" "-framework Security")
  157. endif()
  158. # see Makefile
  159. set(ALL_SRCDIRS . base ssl event event/kcp util cpputil evpp protocol http http/client http/server mqtt)
  160. set(CORE_SRCDIRS . base ssl event)
  161. if(WIN32 OR MINGW)
  162. if(WITH_WEPOLL)
  163. set(CORE_SRCDIRS ${CORE_SRCDIRS} event/wepoll)
  164. endif()
  165. endif()
  166. if(WITH_KCP)
  167. set(CORE_SRCDIRS ${CORE_SRCDIRS} event/kcp)
  168. endif()
  169. set(LIBHV_SRCDIRS ${CORE_SRCDIRS} util)
  170. set(LIBHV_HEADERS hv.h hconfig.h hexport.h)
  171. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${BASE_HEADERS} ${SSL_HEADERS} ${EVENT_HEADERS} ${UTIL_HEADERS})
  172. if(WITH_PROTOCOL)
  173. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${PROTOCOL_HEADERS})
  174. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} protocol)
  175. endif()
  176. if(WITH_EVPP)
  177. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CPPUTIL_HEADERS} ${EVPP_HEADERS})
  178. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} cpputil evpp)
  179. if(WITH_HTTP)
  180. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_HEADERS})
  181. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http)
  182. if(WITH_NGHTTP2)
  183. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP2_HEADERS})
  184. endif()
  185. if(WITH_HTTP_SERVER)
  186. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
  187. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
  188. endif()
  189. if(WITH_HTTP_CLIENT)
  190. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_CLIENT_HEADERS})
  191. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
  192. endif()
  193. endif()
  194. if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND CMAKE_COMPILER_IS_GNUCC)
  195. set(LIBS ${LIBS} stdc++)
  196. endif()
  197. endif()
  198. if(WITH_MQTT)
  199. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${MQTT_HEADERS})
  200. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} mqtt)
  201. endif()
  202. list_source_directories(LIBHV_SRCS ${LIBHV_SRCDIRS})
  203. file(INSTALL ${LIBHV_HEADERS} DESTINATION include/hv)
  204. file(INSTALL ${LIBHV_HEADERS} DESTINATION ${PROJECT_SOURCE_DIR}/include/hv)
  205. if(BUILD_SHARED)
  206. add_library(hv SHARED ${LIBHV_SRCS})
  207. target_compile_definitions(hv PRIVATE HV_DYNAMICLIB)
  208. target_include_directories(hv PRIVATE ${LIBHV_SRCDIRS}
  209. INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>)
  210. target_link_libraries(hv ${LIBS})
  211. install(TARGETS hv
  212. EXPORT libhvConfig
  213. ARCHIVE DESTINATION lib
  214. LIBRARY DESTINATION lib
  215. RUNTIME DESTINATION bin)
  216. add_custom_target(libhv DEPENDS hv)
  217. endif()
  218. if(BUILD_STATIC)
  219. add_library(hv_static STATIC ${LIBHV_SRCS})
  220. target_compile_definitions(hv_static PUBLIC HV_STATICLIB)
  221. target_include_directories(hv_static PRIVATE ${LIBHV_SRCDIRS}
  222. INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>)
  223. target_link_libraries(hv_static ${LIBS})
  224. install(TARGETS hv_static
  225. EXPORT libhvConfig
  226. ARCHIVE DESTINATION lib)
  227. add_custom_target(libhv_static DEPENDS hv_static)
  228. endif()
  229. install(FILES ${LIBHV_HEADERS} DESTINATION include/hv)
  230. install(EXPORT libhvConfig DESTINATION lib/cmake/libhv)
  231. if(BUILD_SHARED)
  232. set(HV_LIBRARIES hv CACHE INTERNAL "link hv libraries")
  233. else()
  234. add_definitions(-DHV_STATICLIB)
  235. set(HV_LIBRARIES hv_static ${LIBS} CACHE INTERNAL "link hv libraries")
  236. endif()
  237. if(BUILD_EXAMPLES)
  238. add_subdirectory(examples)
  239. # for httpd -c etc/httpd.conf
  240. file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR})
  241. file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR}/bin)
  242. file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR}/examples)
  243. endif()
  244. if(BUILD_UNITTEST)
  245. add_subdirectory(unittest)
  246. endif()
  247. # CPack settings
  248. set(CPACK_PACKAGE_NAME "libhv")
  249. set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
  250. set(CPACK_PACKAGE_RELEASE 1)
  251. set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A high-performance C/C++ network library")
  252. set(CPACK_PACKAGE_VENDOR "libhv")
  253. set(CPACK_PACKAGE_CONTACT "ithewei <ithewei@163.com>")
  254. set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
  255. set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
  256. set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
  257. # Specify the package generators
  258. set(CPACK_GENERATOR "TGZ;DEB;RPM")
  259. # Enable CPack debug output
  260. set(CPACK_PACKAGE_DEBUG True)
  261. # https://cmake.org/cmake/help/latest/variable/CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION.html
  262. set(CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION "ON")
  263. include(CPack)