1
0

CMakeLists.txt 7.0 KB

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