Skip to content

Conversation

cstyles
Copy link

@cstyles cstyles commented Aug 25, 2025

Sister PR to #787.

The build script currently expects a very specific filesystem layout in order to statically link. If you check out the source code for librdkafka somewhere and compile it with ./configure and make, the build artifacts will exist in the src/ directory under the project directory and everything will work. The rdkafka-sys crate does something similar behind the scenes by default.

However, sometimes we already have librdkafka built for us. For example, when installed via a package manager like Homebrew or APT, we will have the build artifacts available somewhere like /opt/homebrew/lib or /usr/lib/aarch64-linux-gnu. In those cases, the automatic appending of src/ to the path in DEP_LIBRDKAFKA_STATIC_ROOT is problematic.

Taking a page from jemallocator1, another Rust crate that wraps a C/C++ library, we can instead accept a path to the static library file and pass its parent (the directory in which it resides) to cargo:rustc-link-search. This supports both the case where we have the library installed via a package manager (by setting DEP_LIBRDKAFKA_STATIC_ROOT to something like /opt/homebrew/lib/librdkafka.a) and the case where we're building from source ourselves (by pointing to something like ~/Downloads/librdkafka/src/librdkafka.a).

Footnotes

  1. https://github.com/tikv/jemallocator/blob/5c5a9f79b3be3c19e253527f16a091132a455c2e/jemalloc-sys/build.rs#L126-L137

The build script currently expects a very specific filesystem layout in
order to statically link. If you check out the source code for
librdkafka somewhere and compile it with `./configure` and `make`, the
build artifacts will exist in the `src/` directory under the project
directory and everything will work. The rdkafka-sys crate does something
similar behind the scenes by default.

However, sometimes we already have librdkafka built for us. For example,
when installed via a package manager like Homebrew or APT, we will have
the build artifacts available somewhere like `/opt/homebrew/lib` or
`/usr/lib/aarch64-linux-gnu`. In those cases, the automatic appending of
`src/` to the path in `DEP_LIBRDKAFKA_STATIC_ROOT` is problematic.

Taking a page from jemallocator[^1], another Rust crate that wraps a
C/C++ library, we can instead accept a path to the static library file
and pass its parent (the directory in which it resides) to
`cargo:rustc-link-search`. This supports both the case where we have the
library installed via a package manager (by setting
`DEP_LIBRDKAFKA_STATIC_ROOT` to something like
`/opt/homebrew/lib/librdkafka.a`) and the case where we're building from
source ourselves (by pointing to something like
`~/Downloads/librdkafka/src/librdkafka.a`).

[^1]: https://github.com/tikv/jemallocator/blob/5c5a9f79b3be3c19e253527f16a091132a455c2e/jemalloc-sys/build.rs#L126-L137
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant