Skip to content

Fetch hiredis at build time #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
cmake_minimum_required(VERSION 3.1)

# Options from https://github.com/Nordix/hiredis-cluster/blob/master/CMakeLists.txt
option(DOWNLOAD_HIREDIS "Download the dependency hiredis from GitHub" ON)
option(ENABLE_SSL "Enable SSL/TLS support" OFF)
option(DISABLE_TESTS "Disable compilation of test" OFF)

set(REDIS_PLUS_PLUS_VERSION "1.3.3")
message(STATUS "redis-plus-plus version: ${REDIS_PLUS_PLUS_VERSION}")

Expand All @@ -12,7 +17,7 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
endif()
message(STATUS "redis-plus-plus build type: ${CMAKE_BUILD_TYPE}")

set(REDIS_PLUS_PLUS_DEFAULT_CXX_STANDARD 17)
set(REDIS_PLUS_PLUS_DEFAULT_CXX_STANDARD 11)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ?

if(NOT REDIS_PLUS_PLUS_CXX_STANDARD)
set(REDIS_PLUS_PLUS_CXX_STANDARD ${REDIS_PLUS_PLUS_DEFAULT_CXX_STANDARD} CACHE STRING "Set CXX standard" FORCE)
set_property(CACHE REDIS_PLUS_PLUS_CXX_STANDARD PROPERTY STRINGS "11" "14" "17" "20")
Expand Down Expand Up @@ -109,8 +114,50 @@ else()
set(REDIS_PLUS_PLUS_DEPENDS "hiredis")
endif()

# From https://github.com/Nordix/hiredis-cluster/blob/master/CMakeLists.txt
if(DOWNLOAD_HIREDIS)
message("Downloading dependency 'hiredis'..")

include(FetchContent)
FetchContent_Declare(hiredis
GIT_REPOSITORY https://github.com/redis/hiredis
GIT_TAG v1.0.2
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/hiredis"
)

# Disable tests in hiredis
set(DISABLE_TESTS_OLD ${DISABLE_TESTS})
set(DISABLE_TESTS ON CACHE INTERNAL "")
FetchContent_GetProperties(hiredis)
if(NOT hiredis_POPULATED)
FetchContent_Populate(hiredis)
add_subdirectory(${hiredis_SOURCE_DIR} ${hiredis_BINARY_DIR})
endif()
set(DISABLE_TESTS ${DISABLE_TESTS_OLD} CACHE INTERNAL "")

# Create an empty *-config.cmake for find_package
# See: https://github.com/abandonware-pjz37/cmake-find-package-include/blob/master/hooks/fetch.cmake
set(hiredis_stub_dir "${CMAKE_CURRENT_BINARY_DIR}/generated/pkg")

file(WRITE "${hiredis_stub_dir}/hiredis-config.cmake" "")

set(hiredis_DIR ${hiredis_stub_dir})
# Set variables normally got from hiredis-config.cmake
set(hiredis_LIBRARIES hiredis::hiredis)
set(hiredis_INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}/_deps")
set(HIREDIS_HEADER {hiredis_INCLUDE_DIRS})

if(ENABLE_SSL)
file(WRITE "${hiredis_stub_dir}/hiredis_ssl-config.cmake" "")
set(hiredis_ssl_DIR ${hiredis_stub_dir})
endif()

else()
message("Expecting to find dependencies in path..")
endif()

# hiredis dependency
find_package(hiredis QUIET)
find_package(hiredis REQUIRED)
if(hiredis_FOUND)
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS hiredis::hiredis)

Expand All @@ -129,6 +176,11 @@ else()
endif()
endif()

if(NOT TARGET hiredis::hiredis)
# Add target to support older hiredis releases
add_library(hiredis::hiredis ALIAS hiredis)
endif()

# Build static library
option(REDIS_PLUS_PLUS_BUILD_STATIC "Build static library" ON)
message(STATUS "redis-plus-plus build static library: ${REDIS_PLUS_PLUS_BUILD_STATIC}")
Expand All @@ -145,6 +197,7 @@ if(REDIS_PLUS_PLUS_BUILD_STATIC)
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${REDIS_PLUS_PLUS_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${TLS_SUB_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${CXX_UTILS_DIR}>
$<BUILD_INTERFACE:${hiredis_INCLUDE_DIRS}>
$<INSTALL_INTERFACE:include>)

if(hiredis_FOUND)
Expand Down Expand Up @@ -182,7 +235,7 @@ if(REDIS_PLUS_PLUS_BUILD_STATIC)
endif()

# Build shared library
option(REDIS_PLUS_PLUS_BUILD_SHARED "Build shared library" ON)
option(REDIS_PLUS_PLUS_BUILD_SHARED "Build shared library" OFF)
message(STATUS "redis-plus-plus build shared library: ${REDIS_PLUS_PLUS_BUILD_SHARED}")

if(REDIS_PLUS_PLUS_BUILD_SHARED)
Expand All @@ -194,6 +247,7 @@ if(REDIS_PLUS_PLUS_BUILD_SHARED)
list(APPEND REDIS_PLUS_PLUS_TARGETS ${SHARED_LIB})

target_include_directories(${SHARED_LIB} PUBLIC
$<BUILD_INTERFACE:${hiredis_INCLUDE_DIRS}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${REDIS_PLUS_PLUS_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${TLS_SUB_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${CXX_UTILS_DIR}>
Expand Down
2 changes: 1 addition & 1 deletion cmake/redis++-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(CMakeFindDependencyMacro)

string(REPLACE "," ";" REDIS_PLUS_PLUS_DEPENDS_LIST @REDIS_PLUS_PLUS_DEPENDS@)
foreach(REDIS_PLUS_PLUS_DEP ${REDIS_PLUS_PLUS_DEPENDS_LIST})
find_dependency(${REDIS_PLUS_PLUS_DEP} REQUIRED)
find_dependency(${REDIS_PLUS_PLUS_DEP} REQUIRED PATHS @hiredis_stub_dir@)
endforeach()

include("${CMAKE_CURRENT_LIST_DIR}/redis++-targets.cmake")
Expand Down
20 changes: 11 additions & 9 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ set(REDIS_PLUS_PLUS_TEST_SOURCES src/sw/redis++/test_main.cpp)
add_executable(${PROJECT_NAME} ${REDIS_PLUS_PLUS_TEST_SOURCES})

# hiredis dependency
find_path(HIREDIS_HEADER hiredis REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:${HIREDIS_HEADER}>)
find_package(hiredis REQUIRED)
if(hiredis_FOUND)
set(HIREDIS_HEADER ${hiredis_INCLUDE_DIRS})
endif()

find_library(TEST_HIREDIS_LIB libhiredis.a)
if(NOT TEST_HIREDIS_LIB)
find_library(TEST_HIREDIS_LIB libhiredis_static.a)
if(NOT TEST_HIREDIS_LIB)
find_library(TEST_HIREDIS_LIB hiredis)
endif()
if(NOT TARGET hiredis::hiredis)
# Add target to support older hiredis releases
add_library(hiredis::hiredis ALIAS hiredis)
endif()
target_link_libraries(${PROJECT_NAME} ${TEST_HIREDIS_LIB})

find_path(HIREDIS_HEADER hiredis REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:${HIREDIS_HEADER}>)
target_link_libraries(${PROJECT_NAME} hiredis::hiredis)

if(REDIS_PLUS_PLUS_USE_TLS)
find_package(OpenSSL REQUIRED)
Expand Down