Skip to content

#[link(name = "...")] is poorly supported #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Ralith opened this issue Apr 6, 2025 · 5 comments
Open

#[link(name = "...")] is poorly supported #62

Ralith opened this issue Apr 6, 2025 · 5 comments

Comments

@Ralith
Copy link
Contributor

Ralith commented Apr 6, 2025

Some FFI crates define bindings with the form #[link(name = "foo")] extern "C" { ... }, and rely on the buildscript to emit a cargo:rustc-link-search directive to find the foo library, whose unqualified name is forwarded directly to the linker by rustc. Reindeer silently drops the rustc-link-search directive, leading to linker failures when those FFI definitions are statically used. Major examples include pyo3 and windows-sys.

A workaround is to place these libraries in a filegroup, then arrange for that filegroup to be added to the linker's search path via linker_flags in downstream targets or the toolchain. I haven't managed to get linker_flags in a fixup to do anything.

@Ralith
Copy link
Contributor Author

Ralith commented Apr 6, 2025

A reasonable solution might be to have a fixup (or default behavior...) to propagate rustc-link-search paths to downstream targets, perhaps raising a warning if they're not hermetic.

@Ralith
Copy link
Contributor Author

Ralith commented Apr 24, 2025

It looks like -Z link-native-libraries=no is the intended solution for this situation on the rustc side. Unfortunately not usable with stable Rust... de-facto requiring nightly Rust for nontrivial buck2 Rust builds is unfortunate.

@capickett
Copy link
Contributor

Unfortunately not usable with stable Rust... de-facto requiring nightly Rust for nontrivial buck2 Rust builds is unfortunate.

We do have support in the buck2 prelude to enable nightly features on non-nightly toolchains. Setting nightly_features on the rust_toolchain to True adds the RUSTC_BOOTSTRAP=1 environment variable, which allows for then using -Z flags and
unstable features. We use this extensively in Meta.

https://github.com/facebook/buck2-prelude/blob/main/rust/rust_toolchain.bzl#L128

@Ralith
Copy link
Contributor Author

Ralith commented Apr 24, 2025

Thanks! That should work well in practice. Ideally we could set this flag without also globally enabling nightly features, which might not otherwise be desired, but that's a fairly small footgun.

@Ralith
Copy link
Contributor Author

Ralith commented Apr 26, 2025

For posterity: Passing -Z link-native-libraries=no also disables the dependencies Rust's std pulls in on Windows, at https://github.com/rust-lang/rust/blob/master/library/std/src/sys/pal/windows/c/windows_sys.rs. With a system CXX toolchain, these additional rustc_flags get things working again:
"-Clink-arg=msvcrt.lib", "-Clink-arg=ws2_32.lib", "-Clink-arg=ntdll.lib", "-Clink-arg=userenv.lib". More may be necessary depending on what you call.

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

No branches or pull requests

2 participants