CMakeLists.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. CMAKE_MINIMUM_REQUIRED(VERSION 3.4.0)
  2. INCLUDE(GNUInstallDirs)
  3. PROJECT(hiredis)
  4. OPTION(ENABLE_SSL "Build hiredis_ssl for SSL support" OFF)
  5. OPTION(DISABLE_TESTS "If tests should be compiled or not" OFF)
  6. OPTION(ENABLE_SSL_TESTS, "Should we test SSL connections" OFF)
  7. MACRO(getVersionBit name)
  8. SET(VERSION_REGEX "^#define ${name} (.+)$")
  9. FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/hiredis.h"
  10. VERSION_BIT REGEX ${VERSION_REGEX})
  11. STRING(REGEX REPLACE ${VERSION_REGEX} "\\1" ${name} "${VERSION_BIT}")
  12. ENDMACRO(getVersionBit)
  13. getVersionBit(HIREDIS_MAJOR)
  14. getVersionBit(HIREDIS_MINOR)
  15. getVersionBit(HIREDIS_PATCH)
  16. getVersionBit(HIREDIS_SONAME)
  17. SET(VERSION "${HIREDIS_MAJOR}.${HIREDIS_MINOR}.${HIREDIS_PATCH}")
  18. MESSAGE("Detected version: ${VERSION}")
  19. PROJECT(hiredis VERSION "${VERSION}")
  20. SET(ENABLE_EXAMPLES OFF CACHE BOOL "Enable building hiredis examples")
  21. SET(hiredis_sources
  22. alloc.c
  23. async.c
  24. dict.c
  25. hiredis.c
  26. net.c
  27. read.c
  28. sds.c
  29. sockcompat.c)
  30. SET(hiredis_sources ${hiredis_sources})
  31. IF(WIN32)
  32. ADD_COMPILE_DEFINITIONS(_CRT_SECURE_NO_WARNINGS WIN32_LEAN_AND_MEAN)
  33. ENDIF()
  34. ADD_LIBRARY(hiredis SHARED ${hiredis_sources})
  35. SET_TARGET_PROPERTIES(hiredis
  36. PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE
  37. VERSION "${HIREDIS_SONAME}")
  38. IF(WIN32 OR MINGW)
  39. TARGET_LINK_LIBRARIES(hiredis PRIVATE ws2_32)
  40. ENDIF()
  41. TARGET_INCLUDE_DIRECTORIES(hiredis PUBLIC $<INSTALL_INTERFACE:.> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
  42. CONFIGURE_FILE(hiredis.pc.in hiredis.pc @ONLY)
  43. INSTALL(TARGETS hiredis
  44. EXPORT hiredis-targets
  45. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  46. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  47. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
  48. INSTALL(FILES hiredis.h read.h sds.h async.h alloc.h
  49. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)
  50. INSTALL(DIRECTORY adapters
  51. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)
  52. INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis.pc
  53. DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
  54. export(EXPORT hiredis-targets
  55. FILE "${CMAKE_CURRENT_BINARY_DIR}/hiredis-targets.cmake"
  56. NAMESPACE hiredis::)
  57. SET(CMAKE_CONF_INSTALL_DIR share/hiredis)
  58. SET(INCLUDE_INSTALL_DIR include)
  59. include(CMakePackageConfigHelpers)
  60. configure_package_config_file(hiredis-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config.cmake
  61. INSTALL_DESTINATION ${CMAKE_CONF_INSTALL_DIR}
  62. PATH_VARS INCLUDE_INSTALL_DIR)
  63. INSTALL(EXPORT hiredis-targets
  64. FILE hiredis-targets.cmake
  65. NAMESPACE hiredis::
  66. DESTINATION ${CMAKE_CONF_INSTALL_DIR})
  67. INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config.cmake
  68. DESTINATION ${CMAKE_CONF_INSTALL_DIR})
  69. IF(ENABLE_SSL)
  70. IF (NOT OPENSSL_ROOT_DIR)
  71. IF (APPLE)
  72. SET(OPENSSL_ROOT_DIR "/usr/local/opt/openssl")
  73. ENDIF()
  74. ENDIF()
  75. FIND_PACKAGE(OpenSSL REQUIRED)
  76. SET(hiredis_ssl_sources
  77. ssl.c)
  78. ADD_LIBRARY(hiredis_ssl SHARED
  79. ${hiredis_ssl_sources})
  80. IF (APPLE)
  81. SET_PROPERTY(TARGET hiredis_ssl PROPERTY LINK_FLAGS "-Wl,-undefined -Wl,dynamic_lookup")
  82. ENDIF()
  83. SET_TARGET_PROPERTIES(hiredis_ssl
  84. PROPERTIES
  85. WINDOWS_EXPORT_ALL_SYMBOLS TRUE
  86. VERSION "${HIREDIS_SONAME}")
  87. TARGET_INCLUDE_DIRECTORIES(hiredis_ssl PRIVATE "${OPENSSL_INCLUDE_DIR}")
  88. TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE ${OPENSSL_LIBRARIES})
  89. IF (WIN32 OR MINGW)
  90. TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE hiredis)
  91. ENDIF()
  92. CONFIGURE_FILE(hiredis_ssl.pc.in hiredis_ssl.pc @ONLY)
  93. INSTALL(TARGETS hiredis_ssl
  94. EXPORT hiredis_ssl-targets
  95. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  96. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  97. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
  98. INSTALL(FILES hiredis_ssl.h
  99. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)
  100. INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl.pc
  101. DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
  102. export(EXPORT hiredis_ssl-targets
  103. FILE "${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-targets.cmake"
  104. NAMESPACE hiredis::)
  105. SET(CMAKE_CONF_INSTALL_DIR share/hiredis_ssl)
  106. configure_package_config_file(hiredis_ssl-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-config.cmake
  107. INSTALL_DESTINATION ${CMAKE_CONF_INSTALL_DIR}
  108. PATH_VARS INCLUDE_INSTALL_DIR)
  109. INSTALL(EXPORT hiredis_ssl-targets
  110. FILE hiredis_ssl-targets.cmake
  111. NAMESPACE hiredis::
  112. DESTINATION ${CMAKE_CONF_INSTALL_DIR})
  113. INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-config.cmake
  114. DESTINATION ${CMAKE_CONF_INSTALL_DIR})
  115. ENDIF()
  116. IF(NOT DISABLE_TESTS)
  117. ENABLE_TESTING()
  118. ADD_EXECUTABLE(hiredis-test test.c)
  119. IF(ENABLE_SSL_TESTS)
  120. ADD_DEFINITIONS(-DHIREDIS_TEST_SSL=1)
  121. TARGET_LINK_LIBRARIES(hiredis-test hiredis hiredis_ssl)
  122. ELSE()
  123. TARGET_LINK_LIBRARIES(hiredis-test hiredis)
  124. ENDIF()
  125. ADD_TEST(NAME hiredis-test
  126. COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test.sh)
  127. ENDIF()
  128. # Add examples
  129. IF(ENABLE_EXAMPLES)
  130. ADD_SUBDIRECTORY(examples)
  131. ENDIF(ENABLE_EXAMPLES)