CMakeLists.txt 4.7 KB

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