CMakeLists.txt 6.4 KB

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