CMakeLists.txt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. cmake_minimum_required(VERSION 3.0)
  2. project(hv VERSION 1.20.8)
  3. option(BUILD_SHARED "build shared library" ON)
  4. option(BUILD_STATIC "build static library" ON)
  5. # see config.mk
  6. option(WITH_PROTOCOL "compile protocol" ON)
  7. option(WITH_HTTP "compile http" ON)
  8. option(WITH_HTTP_SERVER "compile http/server" ON)
  9. option(WITH_HTTP_CLIENT "compile http/client" ON)
  10. # WITH_CONSUL need WITH_HTTP_CLIENT=ON
  11. option(WITH_CONSUL "compile consul" OFF)
  12. option(ENABLE_IPV6 "ipv6" OFF)
  13. option(ENABLE_UDS "Unix Domain Socket" OFF)
  14. option(ENABLE_WINDUMP "Windows MiniDumpWriteDump" 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_MBEDTLS "with mbedtls library" OFF)
  20. set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
  21. include(utils)
  22. include(vars)
  23. # see configure
  24. # Checks for header files
  25. check_header("stdbool.h")
  26. check_header("stdint.h")
  27. check_header("stdatomic.h")
  28. check_header("sys/types.h")
  29. check_header("sys/stat.h")
  30. check_header("sys/time.h")
  31. check_header("fcntl.h")
  32. check_header("pthread.h")
  33. # Checks for functions
  34. if(NOT MSVC)
  35. set(CMAKE_REQUIRED_LIBRARIES "pthread")
  36. endif()
  37. check_function("gettid" "unistd.h")
  38. check_function("strlcpy" "string.h")
  39. check_function("strlcat" "string.h")
  40. check_function("clock_gettime" "time.h")
  41. check_function("gettimeofday" "sys/time.h")
  42. check_function("pthread_spin_lock" "pthread.h")
  43. check_function("pthread_mutex_timedlock" "pthread.h")
  44. check_function("sem_timedwait" "semaphore.h")
  45. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/hconfig.h.in ${CMAKE_CURRENT_SOURCE_DIR}/hconfig.h)
  46. # see Makefile.in
  47. set(CMAKE_C_STANDARD 99)
  48. set(CMAKE_C_STANDARD_REQUIRED True)
  49. set(CMAKE_CXX_STANDARD 11)
  50. set(CMAKE_CXX_STANDARD_REQUIRED True)
  51. set(INCDIR include)
  52. set(SRCDIR src)
  53. set(LIBDIR lib)
  54. set(BINDIR bin)
  55. set(LIBRARY_OUTPUT_PATH ${LIBDIR})
  56. set(EXECUTABLE_OUTPUT_PATH ${BINDIR})
  57. set(INCDIRS . include 3rd/include)
  58. set(LIBDIRS . lib 3rd/lib)
  59. include_directories(${INCDIRS} ${SRCDIR})
  60. link_directories(${LIBDIRS})
  61. if(ENABLE_IPV6)
  62. add_definitions(-DENABLE_IPV6)
  63. endif()
  64. if(ENABLE_UDS)
  65. add_definitions(-DENABLE_UDS)
  66. endif()
  67. if(USE_MULTIMAP)
  68. add_definitions(-DUSE_MULTIMAP)
  69. endif()
  70. if(WITH_CURL)
  71. add_definitions(-DWITH_CURL)
  72. set(LIBS ${LIBS} curl)
  73. if(WIN32)
  74. set(LIBS ${LIBS} wldap32 advapi32 crypt32)
  75. endif()
  76. endif()
  77. if(WITH_NGHTTP2)
  78. add_definitions(-DWITH_NGHTTP2)
  79. set(LIBS ${LIBS} nghttp2)
  80. endif()
  81. if(WITH_OPENSSL)
  82. add_definitions(-DWITH_OPENSSL)
  83. set(LIBS ${LIBS} ssl crypto)
  84. endif()
  85. if(WITH_MBEDTLS)
  86. add_definitions(-DWITH_MBEDTLS)
  87. set(LIBS ${LIBS} mbedtls mbedx509 mbedcrypto)
  88. endif()
  89. if(WIN32)
  90. add_definitions(-D_WIN32_WINNT=0x0600)
  91. set(LIBS ${LIBS} winmm iphlpapi ws2_32)
  92. if(ENABLE_WINDUMP)
  93. add_definitions(-DENABLE_WINDUMP)
  94. set(LIBS ${LIBS} dbghelp)
  95. endif()
  96. endif()
  97. if(UNIX)
  98. set(LIBS ${LIBS} pthread m dl)
  99. if(CMAKE_COMPILER_IS_GNUCC)
  100. set(LIBS ${LIBS} rt)
  101. endif()
  102. endif()
  103. if(ANDROID)
  104. set(LIBS ${LIBS} log)
  105. endif()
  106. # see Makefile
  107. set(ALL_SRCDIRS . base utils event protocol http http/client http/server consul examples)
  108. set(LIBHV_SRCDIRS . base utils event)
  109. set(LIBHV_HEADERS hv.h hconfig.h hexport.h)
  110. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${BASE_HEADERS} ${UTILS_HEADERS} ${EVENT_HEADERS})
  111. if(WITH_PROTOCOL)
  112. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${PROTOCOL_HEADERS})
  113. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} protocol)
  114. endif()
  115. if(WITH_HTTP)
  116. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_HEADERS})
  117. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http)
  118. if(WITH_HTTP_SERVER)
  119. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
  120. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
  121. endif()
  122. if(WITH_HTTP_CLIENT)
  123. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_CLIENT_HEADERS})
  124. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
  125. if(WITH_CONSUL)
  126. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CONSUL_HEADERS})
  127. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} consul)
  128. endif()
  129. endif()
  130. endif()
  131. list_source_directories(LIBHV_SRCS ${LIBHV_SRCDIRS})
  132. if(BUILD_SHARED)
  133. add_library(hv SHARED ${LIBHV_SRCS})
  134. target_compile_definitions(hv PRIVATE HV_DYNAMICLIB)
  135. target_include_directories(hv PRIVATE ${LIBHV_SRCDIRS})
  136. target_link_libraries(hv ${LIBS})
  137. install(TARGETS hv LIBRARY DESTINATION lib)
  138. add_custom_target(libhv DEPENDS hv)
  139. endif()
  140. if(BUILD_STATIC)
  141. add_library(hv_static STATIC ${LIBHV_SRCS})
  142. target_compile_definitions(hv_static PRIVATE HV_STATICLIB)
  143. target_include_directories(hv_static PRIVATE ${LIBHV_SRCDIRS})
  144. install(TARGETS hv_static ARCHIVE DESTINATION lib)
  145. add_custom_target(libhv_static DEPENDS hv_static)
  146. endif()
  147. file(INSTALL ${LIBHV_HEADERS} DESTINATION include/hv)
  148. install(FILES ${LIBHV_HEADERS} DESTINATION include/hv)
  149. add_subdirectory(unittest)
  150. add_subdirectory(examples)