Skip to content

Commit 8cd1b2e

Browse files
Make novendor feature the default
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 libbpf#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 libbpf#2: "static" feature will not work with musl. Signed-off-by: Michael Mullin <[email protected]>
1 parent e370760 commit 8cd1b2e

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

libbpf-cargo/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ path = "src/lib.rs"
2727
[features]
2828
# When turned on, link against system-installed libbpf instead of building
2929
# and linking against vendored libbpf sources
30-
novendor = ["libbpf-sys/novendor"]
30+
static = ["libbpf-sys/static"]
3131

3232
[dependencies]
3333
anyhow = "1.0.1"

libbpf-cargo/src/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn extract_version(output: &str) -> Result<&str> {
5151
/// Extract vendored libbpf header files to a temporary directory.
5252
///
5353
/// Directory and enclosed contents will be removed when return object is dropped.
54-
#[cfg(not(feature = "novendor"))]
54+
#[cfg(feature = "static")]
5555
fn extract_libbpf_headers_to_disk(target_dir: &Path) -> Result<Option<PathBuf>> {
5656
use std::fs::OpenOptions;
5757
use std::io::Write;
@@ -68,8 +68,8 @@ fn extract_libbpf_headers_to_disk(target_dir: &Path) -> Result<Option<PathBuf>>
6868
Ok(Some(parent_dir))
6969
}
7070

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

libbpf-rs/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ keywords = ["bpf", "ebpf", "libbpf"]
1515
maintenance = { status = "actively-developed" }
1616

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

2323
[dependencies]

libbpf-rs/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@
3535
//! libbpf-rs at your BPF object file
3636
//! 1. Continue regular rust workflow (ie `cargo build`, `cargo run`, etc)
3737
//!
38+
//! ## Static Compilation
39+
//!
40+
//! By default, programs using libbpf-rs will dynamically link against
41+
//! non-vendored (system/distrubution provided) shared libraries. These
42+
//! non-vendored libraries will include libbpf.so, libz.so, and libelf.so
43+
//! (please see crate libbpf-sys for more info).
44+
//!
45+
//! To use vendored versions of libbpf, libz and libelf please enable
46+
//! the "static" feature. With usage of "static", vendored copies of
47+
//! libbpf, libz, and libelf will be compiled and statically linked to your program.
48+
//!
49+
//! Due to the C-library libbpf being tightly coupled to the linux kernel's
50+
//! headers, musl targets will not work with the "static" feature.
51+
//! Please see: https://wiki.musl-libc.org/faq.html section
52+
//! "Why am i getting error: redefinition of..." for more information.
53+
//!
54+
//! To have a fully statically compiled binary, you may be able statically link
55+
//! with the gnu compiler. To do this, enable the "static" feature
56+
//! and compile your program with the following command:
57+
//!
58+
//! $ RUSTFLAGS='-C target-feature+crt-static' \
59+
//! cargo build --target x86_64-unknown-linux-gnu
60+
//!
3861
//! ## Design
3962
//!
4063
//! libbpf-rs models various "phases":

0 commit comments

Comments
 (0)