1
0

CMakeLists.txt 6.2 KB

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