1
0

CMakeLists.txt 9.7 KB

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