Skip to content

Conversation

@amirbou
Copy link
Contributor

@amirbou amirbou commented Nov 25, 2025

This solves issue #106

Add per-target LIBBPF_SYS_LIBRARY_PATH environment variable to help cross-compilation environments.

This solves an issue when cross-compiling an application that uses libbpf-sys in build.rs(Usually via libbpf-cargo).

In that case, the libbpf-sys crate is built twice - once for the host triplet (let's say x86_64-unknown-linux-gnu, when compiling build.rs) and once for the target triplet (let's say aarch64-linux-android, when compiling the actual application).

If we set LIBBPF_SYS_LIBRARY_PATH to a path that contains the libelf for the target triplet, then build.rs will link to the incorrect library, failing the build.

I have taken a similar approach as the BINDGEN_EXTRA_CLANG_ARGS_<TARGET> flag in rust-bindgen allowing for both underscores and hyphens for the target.

Copy link
Contributor

@d-e-s-o d-e-s-o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good to me, thanks, but left one question and a comment. Could you guide us through how you have tested it?

README.md Outdated

- `LIBBPF_SYS_EXTRA_CFLAGS` can be used to pass extra cflags when vendoring libbpf, libz or libelf.
- `LIBBPF_SYS_LIBRARY_PATH`: colon separated paths for the linker to find native libs.
- `LIBBPF_SYS_LIBRARY_PATH_<TARGET>`: similar to `LIBBPF_SYS_LIBRARY_PATH`, but used to set per-target library path, to help cross-compilation environments. If `LIBBPF_SYS_LIBRARY_PATH_<TARGET>` and `LIBBPF_SYS_LIBRARY_PATH` are defined, the paths from both will be used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably clarify that this is the target triple. "target" is pretty much as ambigious as it gets and at least that would be marginally better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

build.rs Outdated
Comment on lines 219 to 221
env::var(global_lib_path),
env::var(target_lib_path),
env::var(target_lib_path_underscored),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be ordered so that the "global" thing is last? My understanding is that ordering matters and if we don't try the more specialized one first, can't we run into cases where we still end up picking the wrong library? After all, the linker will likely pick the first present library it finds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't expect anyone to actually specify both, but I do agree the "target" one should take precedence

@amirbou
Copy link
Contributor Author

amirbou commented Nov 25, 2025

Hey, let me know if the changes look good, and I'll squash them for the merge.

As for testing, I have opened a repository to help test cross-compilation for Android - #amirbou/libbpf-cross-compile. It depends on a fork I made for libbpf-rs that was changed to include this patch.

It first prepares the library dependencies (libelf and libbpf) using make and then uses the new environment variables in .cargo/config.toml to build libbpf-rs as a normal dependency and libbpf-cargo as a build dependency.

Let me know if you have any questions about it :)

Copy link
Collaborator

@danielocfb danielocfb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me now. Please go ahead and squash and we can get this in. Thanks!

@danielocfb danielocfb linked an issue Nov 25, 2025 that may be closed by this pull request
@amirbou amirbou force-pushed the per-target-sys-library-path branch 2 times, most recently from 7f45f02 to a263929 Compare November 26, 2025 21:37
…ross-compilation environments

This solves an issue when cross-compiling an application that uses libbpf-sys in build.rs(Usually via libbpf-cargo).

In that case, the libbpf-sys crate is built twice - once for the host triplet (let's say x86_64-unknown-linux-gnu, when compiling build.rs) and once for the target triplet (let's say aarch64-linux-android, when compiling the actual application).

If we set `LIBBPF_SYS_LIBRARY_PATH` to a path that contains the libelf for the target triplet, then `build.rs` will link to the incorrect library, failing the build.
@amirbou amirbou force-pushed the per-target-sys-library-path branch from a263929 to 237815d Compare November 26, 2025 21:38
@danielocfb danielocfb merged commit 8c0c66d into libbpf:master Nov 26, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problem about LIBBPF_SYS_LIBRARY_PATH and build.rs

3 participants