1
0

CMakeLists.txt 4.6 KB

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