Skip to content

Commit

Permalink
Make novendor feature the default
Browse files Browse the repository at this point in the history
See: libbpf/libbpf-sys#64

The "static" feature will now get libbpf-sys to do all the static compilation
of the libraries required by libbpf rather than the application developer
statically compiling these libraries themselves, or relying on the
distribution to provide static versions.

The default feature will no longer link libbpf statically.  All libbpf
libraries, including libbpf, will now be dynamically linked to
distribution provided shared libraries

CHANGELOG NOTE #1: feature "static" allows libelf and zlib to be statically
linked. Completely static binaries can now be compiled via the command
`RUSTFLAGS='-C target-feature=+crt-static' cargo build --target
x86_64-unknown-linux-gnu`.
CHANGELOG NOTE #2: "static" feature will not work with musl.

Signed-off-by: Michael Mullin <mmullin@halcyon.ai>
  • Loading branch information
mmullin-halcyon authored and d-e-s-o committed Nov 6, 2023
1 parent 2221365 commit b7e95a3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libbpf-cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ path = "src/lib.rs"
[features]
# When turned on, link against system-installed libbpf instead of building
# and linking against vendored libbpf sources
novendor = ["libbpf-sys/novendor"]
static = ["libbpf-sys/static"]

[dependencies]
anyhow = "1.0.1"
Expand Down
6 changes: 3 additions & 3 deletions libbpf-cargo/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn extract_version(output: &str) -> Result<&str> {
/// Extract vendored libbpf header files to a temporary directory.
///
/// Directory and enclosed contents will be removed when return object is dropped.
#[cfg(not(feature = "novendor"))]
#[cfg(feature = "static")]
fn extract_libbpf_headers_to_disk(target_dir: &Path) -> Result<Option<PathBuf>> {
use std::fs::OpenOptions;
use std::io::Write;
Expand All @@ -68,8 +68,8 @@ fn extract_libbpf_headers_to_disk(target_dir: &Path) -> Result<Option<PathBuf>>
Ok(Some(parent_dir))
}

#[cfg(feature = "novendor")]
fn extract_libbpf_headers_to_disk(target_dir: &Path) -> Result<Option<PathBuf>> {
#[cfg(not(feature = "static"))]
fn extract_libbpf_headers_to_disk(_target_dir: &Path) -> Result<Option<PathBuf>> {
return Ok(None);
}

Expand Down
6 changes: 3 additions & 3 deletions libbpf-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ keywords = ["bpf", "ebpf", "libbpf"]
maintenance = { status = "actively-developed" }

[features]
# When turned on, link against system-installed libbpf instead of building
# and linking against vendored libbpf sources
novendor = ["libbpf-sys/novendor"]
# When turned on, compile and link all libbpf library dependencies statically
# this will vendor the C-libraries libelf, libz, and libbpf
# When turned off, dynamically link all libbpf library dependencies
static = ["libbpf-sys/static"]

[dependencies]
Expand Down
23 changes: 23 additions & 0 deletions libbpf-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@
//! libbpf-rs at your BPF object file
//! 1. Continue regular rust workflow (ie `cargo build`, `cargo run`, etc)
//!
//! ## Static Compilation
//!
//! By default, programs using libbpf-rs will dynamically link against
//! non-vendored (system/distrubution provided) shared libraries. These
//! non-vendored libraries will include libbpf.so, libz.so, and libelf.so
//! (please see crate libbpf-sys for more info).
//!
//! To use vendored versions of libbpf, libz and libelf please enable
//! the "static" feature. With usage of "static", vendored copies of
//! libbpf, libz, and libelf will be compiled and statically linked to your program.
//!
//! Due to the C-library libbpf being tightly coupled to the linux kernel's
//! headers, musl targets will not work with the "static" feature.
//! Please see: https://wiki.musl-libc.org/faq.html section
//! "Why am i getting error: redefinition of..." for more information.
//!
//! To have a fully statically compiled binary, you may be able statically link
//! with the gnu compiler. To do this, enable the "static" feature
//! and compile your program with the following command:
//!
//! $ RUSTFLAGS='-C target-feature+crt-static' \
//! cargo build --target x86_64-unknown-linux-gnu
//!
//! ## Design
//!
//! libbpf-rs models various "phases":
Expand Down

0 comments on commit b7e95a3

Please sign in to comment.