CMakeLists.txt 4.6 KB

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