Skip to content

Commit b961816

Browse files
committed
Configure hiredis features file before find package of hiredis
1 parent e30d4c4 commit b961816

File tree

4 files changed

+59
-40
lines changed

4 files changed

+59
-40
lines changed

.github/workflows/build-and-test.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
matrix:
2727
os: [ubuntu-latest]
2828
build_type: [Release]
29+
redis_version: [7.2.8]
2930
c_compiler: [gcc, clang]
3031
include:
3132
- os: ubuntu-latest
@@ -39,12 +40,21 @@ jobs:
3940
- uses: actions/checkout@v4
4041

4142
- name: libuv-dep
42-
run: sudo apt-get update && sudo apt-get install -y libuv1-dev wget unzip
43+
run: |
44+
sudo apt-get update
45+
sudo apt-get install -yq libuv1-dev wget unzip
4346
4447
- name: redis-dep
4548
run: |
46-
wget -L https://github.com/redis/redis/archive/refs/tags/7.2.3.zip -O redis-7.2.3.zip && unzip redis-7.2.3.zip
47-
cd redis-7.2.3 && make -j2 && ./src/redis-server --port 7000 --cluster-enabled yes --cluster-config-file nodes-7000.conf --daemonize yes && ./src/redis-server --port 7001 --cluster-enabled yes --cluster-config-file nodes-7001.conf --daemonize yes && ./src/redis-server --port 7002 --cluster-enabled yes --cluster-config-file nodes-7002.conf --daemonize yes && ./src/redis-cli --cluster-yes --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 && ./src/redis-server --daemonize yes
49+
wget -L https://github.com/redis/redis/archive/refs/tags/${{matrix.redis_version}}.zip -O redis-${{matrix.redis_version}}.zip
50+
unzip redis-${{matrix.redis_version}}.zip
51+
cd redis-${{matrix.redis_version}}
52+
make -j2
53+
./src/redis-server --port 7000 --cluster-enabled yes --cluster-config-file nodes-7000.conf --daemonize yes
54+
./src/redis-server --port 7001 --cluster-enabled yes --cluster-config-file nodes-7001.conf --daemonize yes
55+
./src/redis-server --port 7002 --cluster-enabled yes --cluster-config-file nodes-7002.conf --daemonize yes
56+
sleep 2
57+
./src/redis-cli --cluster-yes --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 && ./src/redis-server --daemonize yes
4858
4959
- name: hiredis-dep
5060
run: |

CMakeLists.txt

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,7 @@ else()
139139
endif()
140140

141141
# hiredis dependency
142-
find_package(hiredis QUIET)
143-
if(hiredis_FOUND)
144-
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS hiredis::hiredis)
145-
146-
if(REDIS_PLUS_PLUS_USE_TLS)
147-
find_package(hiredis_ssl REQUIRED)
148-
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS hiredis::hiredis_ssl)
149-
find_package(OpenSSL REQUIRED)
150-
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS ${OPENSSL_LIBRARIES})
151-
endif()
152-
else()
153-
find_path(HIREDIS_HEADER hiredis)
154-
find_library(HIREDIS_LIB hiredis)
155-
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS ${HIREDIS_LIB})
156-
157-
if(REDIS_PLUS_PLUS_USE_TLS)
158-
find_library(HIREDIS_TLS_LIB hiredis_ssl)
159-
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS ${HIREDIS_TLS_LIB})
160-
find_package(OpenSSL REQUIRED)
161-
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS ${OPENSSL_LIBRARIES})
162-
endif()
163-
endif()
142+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/FindHiredis.cmake)
164143

165144
# Check hiredis features
166145
message(STATUS "redis-plus-plus check hiredis features")
@@ -174,16 +153,17 @@ endif()
174153
set(HIREDIS_FEATURE_TEST_HEADER "${HIREDIS_FEATURE_TEST_INCLUDE}/hiredis/hiredis.h")
175154

176155
include(CheckSymbolExists)
177-
set(CMAKE_REQUIRED_LIBRARIES_BACK ${CMAKE_REQUIRED_LIBRARIES})
178-
set(CMAKE_REQUIRED_LIBRARIES ${HIREDIS_FEATURE_TEST_LIB})
156+
157+
# Add hiredis to CMAKE_REQUIRED_LIBRARIES
158+
list(APPEND CMAKE_REQUIRED_LIBRARIES hiredis)
179159

180160
CHECK_SYMBOL_EXISTS(redisEnableKeepAliveWithInterval ${HIREDIS_FEATURE_TEST_HEADER} REDIS_PLUS_PLUS_HAS_redisEnableKeepAliveWithInterval)
181161

182162
set(REDIS_PLUS_PLUS_GENERATED_HEADER_DIR ${CMAKE_CURRENT_BINARY_DIR}/${REDIS_PLUS_PLUS_HEADER_DIR})
183163
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/hiredis_features.h.in ${CMAKE_CURRENT_BINARY_DIR}/${REDIS_PLUS_PLUS_SOURCE_DIR}/hiredis_features.h)
184164

185165
# Restore CMAKE_REQUIRED_LIBRARIES
186-
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_BACK})
166+
list(POP_BACK CMAKE_REQUIRED_LIBRARIES)
187167

188168
# Build static library
189169
option(REDIS_PLUS_PLUS_BUILD_STATIC "Build static library" ON)
@@ -273,11 +253,10 @@ if(REDIS_PLUS_PLUS_BUILD_SHARED)
273253

274254
if(hiredis_FOUND)
275255
target_include_directories(${SHARED_LIB} PUBLIC $<BUILD_INTERFACE:${hiredis_INCLUDE_DIRS}>)
276-
target_link_libraries(${SHARED_LIB} PUBLIC ${REDIS_PLUS_PLUS_HIREDIS_LIBS})
277256
else()
278257
target_include_directories(${SHARED_LIB} PUBLIC $<BUILD_INTERFACE:${HIREDIS_HEADER}>)
279-
target_link_libraries(${SHARED_LIB} PUBLIC ${REDIS_PLUS_PLUS_HIREDIS_LIBS})
280258
endif()
259+
target_link_libraries(${SHARED_LIB} PUBLIC ${REDIS_PLUS_PLUS_HIREDIS_LIBS})
281260

282261
if(REDIS_PLUS_PLUS_BUILD_ASYNC)
283262
target_include_directories(${SHARED_LIB} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${REDIS_PLUS_PLUS_ASYNC_FUTURE_HEADER}>)

cmake/FindHiredis.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
find_package(hiredis QUIET)
2+
if(hiredis_FOUND)
3+
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS hiredis::hiredis)
4+
5+
if(NOT hiredis_INCLUDE_DIRS)
6+
# This can happen when hiredis is included with FetchContent with OVERRIDE_FIND_PACKAGE
7+
find_path(
8+
hiredis_INCLUDE_DIRS
9+
hiredis.h
10+
PATHS
11+
${CMAKE_BINARY_DIR}/_deps/hiredis
12+
${CMAKE_CURRENT_BINARY_DIR}/_deps/hiredis
13+
NO_CACHE
14+
REQUIRED
15+
)
16+
17+
# Remove the trailing /hiredis from the include path so that we can include hiredis with hiredis/hiredis.h
18+
get_filename_component(hiredis_INCLUDE_DIRS "${hiredis_INCLUDE_DIRS}" DIRECTORY)
19+
endif()
20+
21+
if(REDIS_PLUS_PLUS_USE_TLS)
22+
find_package(hiredis_ssl REQUIRED)
23+
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS hiredis::hiredis_ssl)
24+
find_package(OpenSSL REQUIRED)
25+
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS ${OPENSSL_LIBRARIES})
26+
endif()
27+
else()
28+
find_path(HIREDIS_HEADER hiredis)
29+
find_library(HIREDIS_LIB hiredis)
30+
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS ${HIREDIS_LIB})
31+
32+
if(REDIS_PLUS_PLUS_USE_TLS)
33+
find_library(HIREDIS_TLS_LIB hiredis_ssl)
34+
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS ${HIREDIS_TLS_LIB})
35+
find_package(OpenSSL REQUIRED)
36+
list(APPEND REDIS_PLUS_PLUS_HIREDIS_LIBS ${OPENSSL_LIBRARIES})
37+
endif()
38+
endif()

test/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@ set(REDIS_PLUS_PLUS_TEST_SOURCES src/sw/redis++/test_main.cpp)
77
add_executable(${PROJECT_NAME} ${REDIS_PLUS_PLUS_TEST_SOURCES})
88

99
# hiredis dependency
10-
find_path(HIREDIS_HEADER hiredis REQUIRED)
11-
target_include_directories(${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:${HIREDIS_HEADER}>)
10+
include(${CMAKE_CURRENT_LIST_DIR}/../cmake/FindHiredis.cmake)
1211

13-
find_library(TEST_HIREDIS_LIB NAMES libhiredis.a libhiredisd.a)
14-
if(NOT TEST_HIREDIS_LIB)
15-
find_library(TEST_HIREDIS_LIB NAMES libhiredis_static.a libhiredis_staticd.a)
16-
if(NOT TEST_HIREDIS_LIB)
17-
find_library(TEST_HIREDIS_LIB NAMES hiredis hiredisd)
18-
endif()
19-
endif()
20-
target_link_libraries(${PROJECT_NAME} ${TEST_HIREDIS_LIB})
12+
target_link_libraries(${PROJECT_NAME} ${REDIS_PLUS_PLUS_HIREDIS_LIBS})
2113

2214
if(REDIS_PLUS_PLUS_USE_TLS)
2315
find_package(OpenSSL REQUIRED)

0 commit comments

Comments
 (0)