Skip to content

Commit 6721322

Browse files
Fix linker errors when building with CMake, TLS and -Wl,--no-undefined (#248)
Add missing dependencies, and make valkeyContextRegisterFuncs visible in shared libraries since it's used by libvalkey_rdma.so. Furthermore, let valkey_unittest.a bundle all libraries to avoid ODR warnings due to the added dependencies. Signed-off-by: Andreas Stieger <Andreas.Stieger@gmx.de> Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech> Co-authored-by: Björn Svensson <bjorn.a.svensson@est.tech>
1 parent efe1ec9 commit 6721322

File tree

3 files changed

+33
-41
lines changed

3 files changed

+33
-41
lines changed

CMakeLists.txt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,7 @@ IF(ENABLE_TLS)
206206
# Produce object files that contain debug info.
207207
target_compile_options(valkey_tls PRIVATE /Z7)
208208
endif()
209-
TARGET_LINK_LIBRARIES(valkey_tls PRIVATE OpenSSL::SSL)
210-
if(WIN32 OR CYGWIN)
211-
target_link_libraries(valkey_tls PRIVATE valkey)
212-
endif()
209+
target_link_libraries(valkey_tls PRIVATE valkey::valkey OpenSSL::SSL)
213210
CONFIGURE_FILE(valkey_tls.pc.in valkey_tls.pc @ONLY)
214211

215212
INSTALL(TARGETS valkey_tls
@@ -261,7 +258,7 @@ if(ENABLE_RDMA)
261258
add_library(valkey_rdma ${valkey_rdma_sources})
262259
add_library(valkey::valkey_rdma ALIAS valkey_rdma)
263260

264-
target_link_libraries(valkey_rdma LINK_PRIVATE ${RDMACM_LIBRARIES} ${IBVERBS_LIBRARIES})
261+
target_link_libraries(valkey_rdma PRIVATE valkey::valkey ${RDMACM_LIBRARIES} ${IBVERBS_LIBRARIES})
265262
target_include_directories(valkey_rdma
266263
PRIVATE
267264
$<INSTALL_INTERFACE:include>
@@ -310,26 +307,31 @@ endif()
310307

311308
# Add tests
312309
if(NOT DISABLE_TESTS)
313-
if(BUILD_SHARED_LIBS)
314-
# Test using a static library since symbols are not hidden then.
315-
# Use same source, include dirs and dependencies as the shared library.
316-
add_library(valkey_unittest STATIC ${valkey_sources})
317-
get_target_property(include_directories valkey::valkey INCLUDE_DIRECTORIES)
318-
target_include_directories(valkey_unittest PUBLIC ${include_directories})
319-
get_target_property(link_libraries valkey::valkey LINK_LIBRARIES)
320-
if(link_libraries)
321-
target_link_libraries(valkey_unittest PUBLIC ${link_libraries})
322-
endif()
323-
# Create libvalkey_unittest.a in the tests directory.
324-
set_target_properties(valkey_unittest PROPERTIES
325-
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests")
326-
else()
327-
# Target is an alias for the static library.
328-
add_library(valkey_unittest ALIAS valkey)
310+
# Unit tests uses a static library to ensure all symbols are visible.
311+
# This single library also bundles TLS and RDMA when enabled.
312+
add_library(valkey_unittest STATIC ${valkey_sources} ${valkey_tls_sources} ${valkey_rdma_sources})
313+
314+
# Mirror the include directories.
315+
get_target_property(include_directories valkey::valkey INCLUDE_DIRECTORIES)
316+
target_include_directories(valkey_unittest PUBLIC ${include_directories})
317+
318+
# Mirror the link libraries.
319+
get_target_property(link_libraries valkey::valkey LINK_LIBRARIES)
320+
if(link_libraries)
321+
target_link_libraries(valkey_unittest PUBLIC ${link_libraries})
322+
endif()
323+
if(ENABLE_TLS)
324+
target_link_libraries(valkey_unittest PRIVATE OpenSSL::SSL)
329325
endif()
326+
if(ENABLE_RDMA)
327+
target_link_libraries(valkey_unittest PRIVATE ${RDMACM_LIBRARIES} ${IBVERBS_LIBRARIES})
328+
endif()
329+
330+
# Create libvalkey_unittest.a in the tests directory.
331+
set_target_properties(valkey_unittest PROPERTIES
332+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests")
330333

331334
# Make sure ctest prints the output when a test fails.
332-
# Must be set before including CTest.
333335
set(CMAKE_CTEST_ARGUMENTS "--output-on-failure")
334336
include(CTest)
335337
add_subdirectory(tests)

src/valkey_private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ static inline int valkeyContextUpdateCommandTimeout(valkeyContext *c,
124124
return VALKEY_OK;
125125
}
126126

127-
int valkeyContextRegisterFuncs(valkeyContextFuncs *funcs, enum valkeyConnectionType type);
127+
/* Visible although private since required by libvalkey_rdma.so */
128+
LIBVALKEY_API int valkeyContextRegisterFuncs(valkeyContextFuncs *funcs, enum valkeyConnectionType type);
128129
void valkeyContextRegisterTcpFuncs(void);
129130
void valkeyContextRegisterUnixFuncs(void);
130131
void valkeyContextRegisterUserfdFuncs(void);

tests/CMakeLists.txt

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,39 +63,28 @@ else()
6363
add_compile_options("-UNDEBUG")
6464
endif()
6565

66-
# Make sure ctest gives the output when tests fail
67-
list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")
68-
69-
# Add non-cluster tests
66+
# Add the non-cluster test binary.
67+
# Some tests are unit tests which require us to include private headers.
7068
add_executable(client_test client_test.c)
7169
target_include_directories(client_test PRIVATE "${PROJECT_SOURCE_DIR}/src")
7270
target_link_libraries(client_test valkey_unittest)
73-
if(TLS_LIBRARY)
71+
add_test(NAME client_test COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/test.sh")
72+
if(ENABLE_TLS)
7473
target_compile_definitions(client_test PUBLIC VALKEY_TEST_TLS=1)
75-
target_link_libraries(client_test ${TLS_LIBRARY})
74+
set_property(TEST client_test APPEND PROPERTY ENVIRONMENT "TEST_TLS=1")
7675
endif()
7776
if(ENABLE_RDMA)
7877
target_compile_definitions(client_test PUBLIC VALKEY_TEST_RDMA=1)
79-
target_link_libraries(client_test valkey_rdma)
78+
set_property(TEST client_test APPEND PROPERTY ENVIRONMENT "TEST_RDMA=1")
8079
endif()
8180
if(LIBEVENT_LIBRARY)
8281
target_compile_definitions(client_test PUBLIC VALKEY_TEST_ASYNC=1)
8382
target_link_libraries(client_test ${LIBEVENT_LIBRARY})
83+
set_property(TEST client_test APPEND PROPERTY ENVIRONMENT "TEST_CLUSTER=1")
8484
endif()
85-
86-
add_test(NAME client_test COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/test.sh")
8785
if(TEST_WITH_REDIS_VERSION)
8886
set_property(TEST client_test APPEND PROPERTY ENVIRONMENT "VALKEY_SERVER=redis-server")
8987
endif()
90-
if(TLS_LIBRARY)
91-
set_property(TEST client_test APPEND PROPERTY ENVIRONMENT "TEST_TLS=1")
92-
endif()
93-
if(ENABLE_RDMA)
94-
set_property(TEST client_test APPEND PROPERTY ENVIRONMENT "TEST_RDMA=1")
95-
endif()
96-
if(LIBEVENT_LIBRARY)
97-
set_property(TEST client_test APPEND PROPERTY ENVIRONMENT "TEST_CLUSTER=1")
98-
endif()
9988

10089
# Unit tests
10190
add_executable(ut_parse_cmd ut_parse_cmd.c test_utils.c)

0 commit comments

Comments
 (0)