libbpf/libbpf-sys

Failed to link statically with `-C target-feature=+crt-static` and `--target`

Closed this issue · 2 comments

I'm working on an experimental ebpf implementation in conmon-rs, a container runtime monitor written in Rust: containers/conmon-rs#926

I'm not able to link the application statically anymore when using libbpf-rs, which directly depends on libbpf-sys, because of the error:

Running `rustc --crate-name libbpf_sys --edition=2018 /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/libbpf-sys-1.0.4+v1.0.1/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --crate-type staticlib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C debug-assertions=off --cfg 'feature="static"' -C metadata=a3f0ad521523a73e -C extra-filename=-a3f0ad521523a73e --out-dir /home/runner/work/conmon-rs/conmon-rs/target/release/deps -L dependency=/home/runner/work/conmon-rs/conmon-rs/target/release/deps --cap-lints allow -L native=/home/runner/work/conmon-rs/conmon-rs/target/release/build/libbpf-sys-a01c0f94146cf298/out -l static=elf -l static=z -l static=bpf`

error: could not find native static library `elf`, perhaps an -L flag is missing?

error: could not compile `libbpf-sys` due to previous error

Caused by:
  process didn't exit successfully: `rustc --crate-name libbpf_sys --edition=2018 /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/libbpf-sys-1.0.4+v1.0.1/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --crate-type staticlib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C debug-assertions=off --cfg 'feature="static"' -C metadata=a3f0ad521523a73e -C extra-filename=-a3f0ad521523a73e --out-dir /home/runner/work/conmon-rs/conmon-rs/target/release/deps -L dependency=/home/runner/work/conmon-rs/conmon-rs/target/release/deps --cap-lints allow -L native=/home/runner/work/conmon-rs/conmon-rs/target/release/build/libbpf-sys-a01c0f94146cf298/out -l static=elf -l static=z -l static=bpf` (exit status: 1)

It looks like that the RUSTFLAGS are being reset for this crate, but for others they got passed correctly, for example when looking into the build logs:

Running `rustc --crate-name clap --edition=2021 /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-3.2.22/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=z -C linker-plugin-lto -C codegen-units=1 -C debuginfo=2 --cfg 'feature="atty"' --cfg 'feature="cargo"' --cfg 'feature="clap_derive"' --cfg 'feature="color"' --cfg 'feature="default"' --cfg 'feature="deprecated"' --cfg 'feature="derive"' --cfg 'feature="env"' --cfg 'feature="once_cell"' --cfg 'feature="std"' --cfg 'feature="strsim"' --cfg 'feature="suggestions"' --cfg 'feature="termcolor"' --cfg 'feature="terminal_size"' --cfg 'feature="wrap_help"' -C metadata=4ceed62849ae43cd -C extra-filename=-4ceed62849ae43cd --out-dir /home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps -L dependency=/home/runner/work/conmon-rs/conmon-rs/target/release/deps --extern atty=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libatty-22b7da72fe42b183.rmeta --extern bitflags=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libbitflags-6cb1025d00e25f61.rmeta --extern clap_derive=/home/runner/work/conmon-rs/conmon-rs/target/release/deps/libclap_derive-00e157025f887d45.so --extern clap_lex=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libclap_lex-2b215555cb1ffb97.rmeta --extern indexmap=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libindexmap-7a358f6b01a7a487.rmeta --extern once_cell=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libonce_cell-66a71340f1f3f5d8.rmeta --extern strsim=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libstrsim-dbf9aece6da0347b.rmeta --extern termcolor=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libtermcolor-6e0510c9f30bb93d.rmeta --extern terminal_size=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libterminal_size-21e8b1454d21cbd2.rmeta --extern textwrap=/home/runner/work/conmon-rs/conmon-rs/target/x86_64-unknown-linux-gnu/release/deps/libtextwrap-3512bba442108cbc.rmeta --cap-lints allow -C target-feature=+crt-static -L /usr/lib/x86_64-linux-gnu -L src/backend/linux_raw/arch/outline/release`

https://github.com/containers/conmon-rs/actions/runs/3573239142/jobs/6007068777

We build the static binary by cross compiling and pre-setting RUSTFLAGS="-C target-feature=+crt-static -L /usr/lib/x86_64-linux-gnu" cargo build -v --release --target x86_64-unknown-linux-gnu --features static

All dependencies are in place in our CI, but it still cannot pickup the static libraries from /usr/lib/x86_64-linux-gnu because the RUSTFLAGS are unset. Do you have any idea what we can do about this?

Merged & released in 1.1.0+v1.1.0

When I build with musl like:
RUSTFLAGS="-C target-feature=+crt-static -L /usr/include/x86_64-linux-musl/" cargo build -v --release --target x86_64-unknown-linux-musl
It turns out:
error: could not find native static library elf, perhaps an -L flag is missing?
Do you have any idea on this? Thanks a lot