Skip to content

Commit d3879c2

Browse files
aviramhaeyalb181
andauthored
add feature to statically link against existing librdkafka (#750)
* add feature to statically link against existing librdkafka * add docs --------- Co-authored-by: Eyal Bukchin <[email protected]>
1 parent 7d998c5 commit d3879c2

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ zstd = ["rdkafka-sys/zstd"]
6565
zstd-pkg-config = ["rdkafka-sys/zstd-pkg-config"]
6666
external-lz4 = ["rdkafka-sys/external-lz4"]
6767
external_lz4 = ["rdkafka-sys/external_lz4"]
68+
static-linking = ["rdkafka-sys/static-linking"]
6869

6970
[package.metadata.docs.rs]
7071
# docs.rs doesn't allow writing to ~/.cargo/registry (reasonably), so we have to

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ the system's version of librdkafka. Example:
176176
rdkafka = { version = "0.25", features = ["dynamic-linking"] }
177177
```
178178

179+
If you'd like to compile librdkafka statically yourself, then use
180+
that, you can use `static-linking` while supplying `DEP_LIBRDKAFKA_STATIC_ROOT`
181+
with path to where librdkafka was built.
182+
179183
For a full listing of features, consult the [rdkafka-sys crate's
180184
documentation][rdkafka-sys-features]. All of rdkafka-sys features are
181185
re-exported as rdkafka features.

rdkafka-sys/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ external-lz4 = ["lz4-sys"]
9393
# Deprecated alias for the `external-lz4` feature.
9494
external_lz4 = ["external-lz4"]
9595

96+
# Link against precompiled static build of librdkafka
97+
static-linking = []
98+
9699
[package.metadata.docs.rs]
97100
# docs.rs doesn't allow writing to ~/.cargo/registry (reasonably), so we have to
98101
# use the CMake build for a proper out-of-tree build.

rdkafka-sys/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ system, and it will configure the compiler to dynamically link against it.
4545
The system version of librdkafka must exactly match the version of
4646
librdkafka bundled with this crate.
4747

48+
The **`static-linking`** feature can be used to link rdkafka to a locally
49+
built version of librdkafka: if the feature is enabled, the build script
50+
will try to find `DEP_LIBRDKAFKA_STATIC_ROOT` environment variable
51+
and it will statically link against it.
52+
4853
The **`cmake-build`** feature builds librdkafka with its [CMake] build
4954
system, rather than its default [mklove]-based build system. This feature
5055
requires that CMake is installed on the build machine.

rdkafka-sys/build.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,19 @@ fn main() {
7373
process::exit(1);
7474
}
7575
}
76-
} else {
76+
} else if env::var("CARGO_FEATURE_STATIC_EXTERNAL").is_ok() {
77+
if let Ok(rdkafka_dir) = env::var("DEP_LIBRDKAFKA_STATIC_ROOT") {
78+
println!("cargo:rustc-link-search=native={}/src", rdkafka_dir);
79+
println!("cargo:rustc-link-lib=static=rdkafka");
80+
println!("cargo:root={}", rdkafka_dir);
81+
} else {
82+
eprintln!("Path to DEP_LIBRDKAFKA_STATIC_ROOT not set. Static linking failed. Exiting.");
83+
process::exit(1);
84+
}
85+
eprintln!("librdkafka will be linked statically using prebuilt binaries");
86+
87+
}
88+
else {
7789
// Ensure that we are in the right directory
7890
let rdkafkasys_root = Path::new("rdkafka-sys");
7991
if rdkafkasys_root.exists() {

0 commit comments

Comments
 (0)