Skip to content

Commit 14158dc

Browse files
committed
build: statically link OpenSSL into the static library
Pass OPENSSL_STATIC=1 to the staticlib cargo_build() invocation so openssl-sys links OpenSSL statically into libscylla-cpp-driver_static.a. This makes the static archive truly self-contained — users only need to link system libraries and OpenSSL's pkg-config dependencies. Since the cdylib and staticlib builds share CARGO_TARGET_DIR but pass different OPENSSL_STATIC values, they must not run concurrently — openssl-sys declares rerun-if-env-changed for that variable, and parallel invocations would thrash its build script cache. Serialize by making the staticlib target depend on the cdylib target when both are enabled. Append platform-specific system libraries to the static .pc file's Libs: line via @native_static_libs@. These are needed by the Rust runtime (pthreads, libdl, libm, etc.) and must be provided by the final linker when linking the static archive into an executable. Add libssl-dev to the list of build dependencies, since the static build needs static OpenSSL libraries, which are not provided by libssl1.1.
1 parent 226a0ae commit 14158dc

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ install-java8-if-missing:
256256
@sudo apt install -y openjdk-8-jre
257257

258258
install-build-dependencies: update-apt-cache-if-needed
259-
@sudo apt-get install -y libssl1.1 libuv1-dev libkrb5-dev libc6-dbg
259+
@sudo apt-get install -y libssl1.1 libssl-dev libuv1-dev libkrb5-dev libc6-dbg
260260

261261
# Alias for backward compatibility
262262
install-bin-dependencies: install-build-dependencies

scylla-rust-wrapper/CMakeLists.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,21 @@ if(CASS_BUILD_SHARED)
7878
endif()
7979
if(CASS_BUILD_STATIC)
8080
cargo_build(NAME scylla_cpp_driver CRATE_TYPE staticlib
81-
ENV "OPENSSL_LIB_DIR=${OPENSSL_LIB_DIR}"
81+
ENV "OPENSSL_STATIC=1"
82+
"OPENSSL_LIB_DIR=${OPENSSL_LIB_DIR}"
8283
"OPENSSL_INCLUDE_DIR=${OPENSSL_INCLUDE_DIR}")
8384
create_copy($<TARGET_FILE:scylla_cpp_driver_staticlib> ${INSTALL_NAME_STATIC})
8485
add_library(scylla-cpp-driver_static STATIC IMPORTED GLOBAL)
8586
add_dependencies(scylla-cpp-driver_static ${INSTALL_NAME_STATIC}_copy)
8687
set_target_properties(scylla-cpp-driver_static PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/${INSTALL_NAME_STATIC})
8788
endif()
89+
# When both builds share CARGO_TARGET_DIR, they must not run concurrently:
90+
# they pass different OPENSSL_STATIC values, which causes openssl-sys's build
91+
# script to re-run (due to rerun-if-env-changed). Concurrent cargo invocations
92+
# with different env fingerprints would thrash the shared build script cache.
93+
if(CASS_BUILD_SHARED AND CASS_BUILD_STATIC)
94+
add_dependencies(scylla_cpp_driver_staticlib_target scylla_cpp_driver_cdylib_target)
95+
endif()
8896

8997
#-------------------------------------
9098
# Installation
@@ -192,6 +200,17 @@ if(CASS_BUILD_STATIC)
192200
# Static library pkg-config file goes to dev package
193201
if(CASS_INSTALL_PKG_CONFIG)
194202
if(PKG_CONFIG_FOUND)
203+
# System libraries needed when linking the static Rust archive.
204+
# Ideally these would come from `cargo rustc -- --print native-static-libs`,
205+
# but that flag is nightly-only. These are hardcoded approximations that
206+
# may need updating if dependencies or Rust versions change.
207+
if(APPLE)
208+
set(native_static_libs "-lm -lpthread -ldl -framework Security -framework CoreFoundation")
209+
elseif(WIN32)
210+
set(native_static_libs "-lws2_32 -lbcrypt -luserenv -lntdll")
211+
else()
212+
set(native_static_libs "-lm -lpthread -ldl -lrt")
213+
endif()
195214
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scylla-cpp-driver_static.pc.in" "scylla-cpp-driver_static.pc" @ONLY)
196215
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/scylla-cpp-driver_static.pc"
197216
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"

scylla-rust-wrapper/scylla-cpp-driver_static.pc.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Name: scylla-cpp-rs-driver
77
Description: ScyllaDB CPP RS Driver
88
Version: @version@
99
Requires.private: openssl
10-
Libs: -L${libdir} -lscylla-cpp-driver_static
10+
Libs: -L${libdir} -lscylla-cpp-driver_static @native_static_libs@
1111
Cflags: -I${includedir}
1212
URL: https://github.com/scylladb/cpp-rs-driver/

0 commit comments

Comments
 (0)