diff --git a/libbpf-cargo/Cargo.toml b/libbpf-cargo/Cargo.toml index fc55f234..80b5d496 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 5adac728..4d643d68 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 e5f7571c..ed70e255 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":