CMakeLists.txt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. cmake_minimum_required(VERSION 3.6)
  2. project(hv VERSION 1.2.4)
  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(ENABLE_IPV6 "ipv6" OFF)
  14. option(ENABLE_UDS "Unix Domain Socket" OFF)
  15. option(ENABLE_WINDUMP "Windows MiniDumpWriteDump" OFF)
  16. option(USE_MULTIMAP "MultiMap" OFF)
  17. option(WITH_CURL "with curl library" 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 "with kcp" OFF)
  23. set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
  24. include(utils)
  25. include(vars)
  26. # see configure
  27. # Checks for header files
  28. check_header("stdbool.h")
  29. check_header("stdint.h")
  30. check_header("stdatomic.h")
  31. check_header("sys/types.h")
  32. check_header("sys/stat.h")
  33. check_header("sys/time.h")
  34. check_header("fcntl.h")
  35. check_header("pthread.h")
  36. # Checks for functions
  37. if(NOT MSVC)
  38. set(CMAKE_REQUIRED_LIBRARIES "-pthread")
  39. endif()
  40. check_function("gettid" "unistd.h")
  41. check_function("strlcpy" "string.h")
  42. check_function("strlcat" "string.h")
  43. check_function("clock_gettime" "time.h")
  44. check_function("gettimeofday" "sys/time.h")
  45. check_function("pthread_spin_lock" "pthread.h")
  46. check_function("pthread_mutex_timedlock" "pthread.h")
  47. check_function("sem_timedwait" "semaphore.h")
  48. check_function("pipe" "unistd.h")
  49. check_function("socketpair" "sys/socket.h")
  50. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/hconfig.h.in ${CMAKE_CURRENT_SOURCE_DIR}/hconfig.h)
  51. # see Makefile.in
  52. set(CMAKE_C_STANDARD 99)
  53. set(CMAKE_C_STANDARD_REQUIRED True)
  54. set(CMAKE_CXX_STANDARD 11)
  55. set(CMAKE_CXX_STANDARD_REQUIRED True)
  56. set(INCDIR include)
  57. set(SRCDIR src)
  58. set(LIBDIR lib)
  59. set(BINDIR bin)
  60. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${LIBDIR})
  61. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${LIBDIR})
  62. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BINDIR})
  63. message(STATUS "CMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
  64. set(INCDIRS . include 3rd/include)
  65. set(LIBDIRS . lib 3rd/lib)
  66. include_directories(${INCDIRS} ${SRCDIR})
  67. link_directories(${LIBDIRS})
  68. message(STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
  69. if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
  70. add_definitions(-DDEBUG)
  71. else()
  72. add_definitions(-DNDEBUG)
  73. endif()
  74. if(ENABLE_IPV6)
  75. add_definitions(-DENABLE_IPV6)
  76. endif()
  77. if(ENABLE_UDS)
  78. add_definitions(-DENABLE_UDS)
  79. endif()
  80. if(USE_MULTIMAP)
  81. add_definitions(-DUSE_MULTIMAP)
  82. endif()
  83. if(WITH_CURL)
  84. add_definitions(-DWITH_CURL)
  85. set(LIBS ${LIBS} curl)
  86. if(WIN32)
  87. set(LIBS ${LIBS} wldap32 advapi32 crypt32)
  88. endif()
  89. endif()
  90. if(WITH_NGHTTP2)
  91. add_definitions(-DWITH_NGHTTP2)
  92. set(LIBS ${LIBS} nghttp2)
  93. endif()
  94. if(WITH_OPENSSL)
  95. add_definitions(-DWITH_OPENSSL)
  96. set(LIBS ${LIBS} ssl crypto)
  97. endif()
  98. if(WITH_GNUTLS)
  99. add_definitions(-DWITH_GNUTLS)
  100. set(LIBS ${LIBS} gnutls)
  101. endif()
  102. if(WITH_MBEDTLS)
  103. add_definitions(-DWITH_MBEDTLS)
  104. set(LIBS ${LIBS} mbedtls mbedx509 mbedcrypto)
  105. endif()
  106. if(WIN32)
  107. add_definitions(-D_WIN32_WINNT=0x0600)
  108. set(LIBS ${LIBS} winmm iphlpapi ws2_32)
  109. if(ENABLE_WINDUMP)
  110. add_definitions(-DENABLE_WINDUMP)
  111. set(LIBS ${LIBS} dbghelp)
  112. endif()
  113. endif()
  114. if(ANDROID)
  115. set(LIBS ${LIBS} log)
  116. elseif(UNIX)
  117. set(LIBS ${LIBS} pthread m dl)
  118. if(CMAKE_COMPILER_IS_GNUCC)
  119. set(LIBS ${LIBS} rt)
  120. endif()
  121. endif()
  122. if(APPLE)
  123. set(LIBS ${LIBS} "-framework CoreFoundation" "-framework Security")
  124. endif()
  125. # see Makefile
  126. set(ALL_SRCDIRS . base ssl event event/kcp util cpputil evpp protocol http http/client http/server)
  127. set(CORE_SRCDIRS . base ssl event)
  128. if(WITH_KCP)
  129. set(CORE_SRCDIRS ${CORE_SRCDIRS} event/kcp)
  130. endif()
  131. set(LIBHV_SRCDIRS ${CORE_SRCDIRS} util)
  132. set(LIBHV_HEADERS hv.h hconfig.h hexport.h)
  133. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${BASE_HEADERS} ${SSL_HEADERS} ${EVENT_HEADERS} ${UTIL_HEADERS})
  134. if(WITH_PROTOCOL)
  135. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${PROTOCOL_HEADERS})
  136. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} protocol)
  137. endif()
  138. if(WITH_EVPP)
  139. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CPPUTIL_HEADERS} ${EVPP_HEADERS})
  140. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} cpputil evpp)
  141. if(WITH_HTTP)
  142. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_HEADERS})
  143. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http)
  144. if(WITH_NGHTTP2)
  145. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP2_HEADERS})
  146. endif()
  147. if(WITH_HTTP_SERVER)
  148. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
  149. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
  150. endif()
  151. if(WITH_HTTP_CLIENT)
  152. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_CLIENT_HEADERS})
  153. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
  154. endif()
  155. endif()
  156. if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND CMAKE_COMPILER_IS_GNUCC)
  157. set(LIBS ${LIBS} stdc++)
  158. endif()
  159. endif()
  160. list_source_directories(LIBHV_SRCS ${LIBHV_SRCDIRS})
  161. if(BUILD_SHARED)
  162. add_library(hv SHARED ${LIBHV_SRCS})
  163. target_compile_definitions(hv PRIVATE HV_DYNAMICLIB)
  164. target_include_directories(hv PRIVATE ${LIBHV_SRCDIRS})
  165. target_link_libraries(hv ${LIBS})
  166. install(TARGETS hv
  167. ARCHIVE DESTINATION lib
  168. LIBRARY DESTINATION lib
  169. RUNTIME DESTINATION bin)
  170. add_custom_target(libhv DEPENDS hv)
  171. endif()
  172. if(BUILD_STATIC)
  173. add_library(hv_static STATIC ${LIBHV_SRCS})
  174. target_compile_definitions(hv_static PRIVATE HV_STATICLIB)
  175. target_include_directories(hv_static PRIVATE ${LIBHV_SRCDIRS})
  176. install(TARGETS hv_static DESTINATION lib)
  177. add_custom_target(libhv_static DEPENDS hv_static)
  178. endif()
  179. file(INSTALL ${LIBHV_HEADERS} DESTINATION include/hv)
  180. install(FILES ${LIBHV_HEADERS} DESTINATION include/hv)
  181. if(BUILD_SHARED)
  182. set(HV_LIBRARIES hv CACHE INTERNAL "link hv libraries")
  183. else()
  184. add_definitions(-DHV_STATICLIB)
  185. set(HV_LIBRARIES hv_static ${LIBS} CACHE INTERNAL "link hv libraries")
  186. endif()
  187. if(BUILD_EXAMPLES)
  188. add_subdirectory(examples)
  189. # for httpd -c etc/httpd.conf
  190. file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR})
  191. file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR}/bin)
  192. file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR}/examples)
  193. endif()
  194. if(BUILD_UNITTEST)
  195. add_subdirectory(unittest)
  196. endif()