1
0

CMakeLists.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. cmake_minimum_required(VERSION 3.6)
  2. project(hv VERSION 1.1.0)
  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.mk
  8. option(WITH_PROTOCOL "compile protocol" OFF)
  9. option(WITH_HTTP "compile http" ON)
  10. option(WITH_HTTP_SERVER "compile http/server" ON)
  11. option(WITH_HTTP_CLIENT "compile http/client" ON)
  12. # WITH_CONSUL need WITH_HTTP_CLIENT=ON
  13. option(WITH_CONSUL "compile consul" OFF)
  14. option(ENABLE_IPV6 "ipv6" OFF)
  15. option(ENABLE_UDS "Unix Domain Socket" OFF)
  16. option(ENABLE_WINDUMP "Windows MiniDumpWriteDump" OFF)
  17. option(USE_MULTIMAP "MultiMap" OFF)
  18. option(WITH_CURL "with curl library" OFF)
  19. option(WITH_NGHTTP2 "with nghttp2 library" OFF)
  20. option(WITH_OPENSSL "with openssl 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(LIBRARY_OUTPUT_PATH ${LIBDIR})
  58. set(EXECUTABLE_OUTPUT_PATH ${BINDIR})
  59. set(INCDIRS . include 3rd/include)
  60. set(LIBDIRS . lib 3rd/lib)
  61. include_directories(${INCDIRS} ${SRCDIR})
  62. link_directories(${LIBDIRS})
  63. if(ENABLE_IPV6)
  64. add_definitions(-DENABLE_IPV6)
  65. endif()
  66. if(ENABLE_UDS)
  67. add_definitions(-DENABLE_UDS)
  68. endif()
  69. if(USE_MULTIMAP)
  70. add_definitions(-DUSE_MULTIMAP)
  71. endif()
  72. if(WITH_CURL)
  73. add_definitions(-DWITH_CURL)
  74. set(LIBS ${LIBS} curl)
  75. if(WIN32)
  76. set(LIBS ${LIBS} wldap32 advapi32 crypt32)
  77. endif()
  78. endif()
  79. if(WITH_NGHTTP2)
  80. add_definitions(-DWITH_NGHTTP2)
  81. set(LIBS ${LIBS} nghttp2)
  82. endif()
  83. if(WITH_OPENSSL)
  84. add_definitions(-DWITH_OPENSSL)
  85. set(LIBS ${LIBS} ssl crypto)
  86. endif()
  87. if(WITH_MBEDTLS)
  88. add_definitions(-DWITH_MBEDTLS)
  89. set(LIBS ${LIBS} mbedtls mbedx509 mbedcrypto)
  90. endif()
  91. if(WIN32)
  92. add_definitions(-D_WIN32_WINNT=0x0600)
  93. set(LIBS ${LIBS} winmm iphlpapi ws2_32)
  94. if(ENABLE_WINDUMP)
  95. add_definitions(-DENABLE_WINDUMP)
  96. set(LIBS ${LIBS} dbghelp)
  97. endif()
  98. endif()
  99. if(UNIX)
  100. set(LIBS ${LIBS} pthread m dl)
  101. if(CMAKE_COMPILER_IS_GNUCC)
  102. set(LIBS ${LIBS} rt)
  103. endif()
  104. endif()
  105. if(ANDROID)
  106. set(LIBS ${LIBS} log)
  107. endif()
  108. # see Makefile
  109. set(ALL_SRCDIRS . base utils event protocol http http/client http/server consul examples)
  110. set(LIBHV_SRCDIRS . base utils event evpp)
  111. set(LIBHV_HEADERS hv.h hconfig.h hexport.h)
  112. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${BASE_HEADERS} ${UTILS_HEADERS} ${EVENT_HEADERS} ${EVPP_HEADERS})
  113. if(WITH_PROTOCOL)
  114. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${PROTOCOL_HEADERS})
  115. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} protocol)
  116. endif()
  117. if(WITH_HTTP)
  118. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_HEADERS})
  119. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http)
  120. if(WITH_HTTP_SERVER)
  121. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
  122. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
  123. endif()
  124. if(WITH_HTTP_CLIENT)
  125. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_CLIENT_HEADERS})
  126. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
  127. if(WITH_CONSUL)
  128. set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CONSUL_HEADERS})
  129. set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} consul)
  130. endif()
  131. endif()
  132. endif()
  133. list_source_directories(LIBHV_SRCS ${LIBHV_SRCDIRS})
  134. if(BUILD_SHARED)
  135. add_library(hv SHARED ${LIBHV_SRCS})
  136. target_compile_definitions(hv PRIVATE HV_DYNAMICLIB)
  137. target_include_directories(hv PRIVATE ${LIBHV_SRCDIRS})
  138. target_link_libraries(hv ${LIBS})
  139. install(TARGETS hv LIBRARY DESTINATION lib)
  140. add_custom_target(libhv DEPENDS hv)
  141. endif()
  142. if(BUILD_STATIC)
  143. add_library(hv_static STATIC ${LIBHV_SRCS})
  144. target_compile_definitions(hv_static PRIVATE HV_STATICLIB)
  145. target_include_directories(hv_static PRIVATE ${LIBHV_SRCDIRS})
  146. install(TARGETS hv_static ARCHIVE DESTINATION lib)
  147. add_custom_target(libhv_static DEPENDS hv_static)
  148. endif()
  149. file(INSTALL ${LIBHV_HEADERS} DESTINATION include/hv)
  150. install(FILES ${LIBHV_HEADERS} DESTINATION include/hv)
  151. if(BUILD_EXAMPLES)
  152. add_subdirectory(examples)
  153. endif()
  154. if(BUILD_UNITTEST)
  155. add_subdirectory(unittest)
  156. endif()