From 8cd1b2ed34f0dc269dc004a6620db8c6f819fca3 Mon Sep 17 00:00:00 2001 From: Michael Mullin Date: Wed, 31 May 2023 15:43:22 -0400 Subject: [PATCH] Make novendor feature the default See: https://github.com/libbpf/libbpf-sys/pull/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 --- libbpf-cargo/Cargo.toml | 2 +- libbpf-cargo/src/build.rs | 6 +++--- libbpf-rs/Cargo.toml | 6 +++--- libbpf-rs/src/lib.rs | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/libbpf-cargo/Cargo.toml b/libbpf-cargo/Cargo.toml index 1396bba7..e76c5047 100644 --- a/libbpf-cargo/Cargo.toml +++ b/libbpf-cargo/Cargo.toml @@ -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" diff --git a/libbpf-cargo/src/build.rs b/libbpf-cargo/src/build.rs index 14bf656f..a86f1c14 100644 --- a/libbpf-cargo/src/build.rs +++ b/libbpf-cargo/src/build.rs @@ -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> { use std::fs::OpenOptions; use std::io::Write; @@ -68,8 +68,8 @@ fn extract_libbpf_headers_to_disk(target_dir: &Path) -> Result> Ok(Some(parent_dir)) } -#[cfg(feature = "novendor")] -fn extract_libbpf_headers_to_disk(target_dir: &Path) -> Result> { +#[cfg(not(feature = "static"))] +fn extract_libbpf_headers_to_disk(_target_dir: &Path) -> Result> { return Ok(None); } diff --git a/libbpf-rs/Cargo.toml b/libbpf-rs/Cargo.toml index 46c166c5..928d30b1 100644 --- a/libbpf-rs/Cargo.toml +++ b/libbpf-rs/Cargo.toml @@ -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] diff --git a/libbpf-rs/src/lib.rs b/libbpf-rs/src/lib.rs index 4912c214..85162a2d 100644 --- a/libbpf-rs/src/lib.rs +++ b/libbpf-rs/src/lib.rs @@ -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":