CMakeLists.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. cmake_minimum_required(VERSION 3.6)
  2. project(hv VERSION 1.2.1)
  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. if(ENABLE_IPV6)
  66. add_definitions(-DENABLE_IPV6)
  67. endif()
  68. if(ENABLE_UDS)
  69. add_definitions(-DENABLE_UDS)
  70. endif()
  71. if(USE_MULTIMAP)
  72. add_definitions(-DUSE_MULTIMAP)
  73. endif()
  74. if(WITH_CURL)
  75. add_definitions(-DWITH_CURL)
  76. set(LIBS ${LIBS} curl)
  77. if(WIN32)
  78. set(LIBS ${LIBS} wldap32 advapi32 crypt32)
  79. endif()
  80. endif()
  81. if(WITH_NGHTTP2)
  82. add_definitions(-DWITH_NGHTTP2)
  83. set(LIBS ${LIBS} nghttp2)
  84. endif()
  85. if(WITH_OPENSSL)
  86. add_definitions(-DWITH_OPENSSL)
  87. set(LIBS ${LIBS} ssl crypto)
  88. endif()
  89. if(WITH_GNUTLS)
  90. add_definitions(-DWITH_GNUTLS)
  91. set(LIBS ${LIBS} gnutls)
  92. endif()
  93. if(WITH_MBEDTLS)
  94. add_definitions(-DWITH_MBEDTLS)
  95. set(LIBS ${LIBS} mbedtls mbedx509 mbedcrypto)
  96. endif()
  97. if(WIN32)
  98. add_definitions(-D_WIN32_WINNT=0x0600)
  99. set(LIBS ${LIBS} winmm iphlpapi ws2_32)
  100. if(ENABLE_WINDUMP)
  101. add_definitions(-DENABLE_WINDUMP)
  102. set(LIBS ${LIBS} dbghelp)
  103. endif()
  104. endif()
  105. if(ANDROID)
  106. set(LIBS ${LIBS} log)
  107. elseif(UNIX)
  108. set(LIBS ${LIBS} pthread m dl)
  109. if(CMAKE_COMPILER_IS_GNUCC)
  110. set(LIBS ${LIBS} rt)
  111. endif()
  112. endif()
  113. if(APPLE)
  114. set(LIBS ${LIBS} "-framework CoreFoundation" "-framework Security")
  115. endif()
  116. # see Makefile
  117. set(ALL_SRCDIRS . base ssl event util cpputil evpp protocol http http/client http/server)
  118. set(LIBHV_SRCDIRS . base ssl event util)
  119. set(LIBHV_HEADERS hv.h hconfig.h hexport.h)
  120. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${BASE_HEADERS} ${SSL_HEADERS} ${EVENT_HEADERS} ${UTIL_HEADERS})
  121. if(WITH_PROTOCOL)
  122. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${PROTOCOL_HEADERS})
  123. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} protocol)
  124. endif()
  125. if(WITH_EVPP)
  126. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CPPUTIL_HEADERS} ${EVPP_HEADERS})
  127. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} cpputil evpp)
  128. if(WITH_HTTP)
  129. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_HEADERS})
  130. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http)
  131. if(WITH_HTTP_SERVER)
  132. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
  133. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
  134. endif()
  135. if(WITH_HTTP_CLIENT)
  136. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_CLIENT_HEADERS})
  137. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
  138. endif()
  139. endif()
  140. if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND CMAKE_COMPILER_IS_GNUCC)
  141. set(LIBS ${LIBS} stdc++)
  142. endif()
  143. endif()
  144. list_source_directories(LIBHV_SRCS ${LIBHV_SRCDIRS})
  145. if(BUILD_SHARED)
  146. add_library(hv SHARED ${LIBHV_SRCS})
  147. target_compile_definitions(hv PRIVATE HV_DYNAMICLIB)
  148. target_include_directories(hv PRIVATE ${LIBHV_SRCDIRS})
  149. target_link_libraries(hv ${LIBS})
  150. install(TARGETS hv
  151. ARCHIVE DESTINATION lib
  152. LIBRARY DESTINATION lib
  153. RUNTIME DESTINATION bin)
  154. add_custom_target(libhv DEPENDS hv)
  155. endif()
  156. if(BUILD_STATIC)
  157. add_library(hv_static STATIC ${LIBHV_SRCS})
  158. target_compile_definitions(hv_static PRIVATE HV_STATICLIB)
  159. target_include_directories(hv_static PRIVATE ${LIBHV_SRCDIRS})
  160. install(TARGETS hv_static DESTINATION lib)
  161. add_custom_target(libhv_static DEPENDS hv_static)
  162. endif()
  163. file(INSTALL ${LIBHV_HEADERS} DESTINATION include/hv)
  164. install(FILES ${LIBHV_HEADERS} DESTINATION include/hv)
  165. if(BUILD_SHARED)
  166. set(HV_LIBRARIES hv CACHE INTERNAL "link hv libraries")
  167. else()
  168. set(HV_LIBRARIES hv_static ${LIBS} CACHE INTERNAL "link hv libraries")
  169. endif()
  170. if(BUILD_EXAMPLES)
  171. add_subdirectory(examples)
  172. endif()
  173. if(BUILD_UNITTEST)
  174. add_subdirectory(unittest)
  175. endif()