Pass arbitrary arguments to linker from build script?

I need to be able to pass custom arguments to the linker from a build.rs script. Is there any way of doing this?

You can only directly set -l and -L args:
https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script

The unstable #[link_args] would give you more freedom. I think you could hack that from the build script by outputting a file, then include!() it in the main crate.
https://doc.rust-lang.org/nightly/reference/attributes.html#ffi-attributes

OK cool, thanks!

Another option might be to have the build script emit some cfg value, and then have that same cfg guard a rustflags section in Cargo.toml. This may work well if you have a fixed menu of options/bundles to pick from, and aren’t creating linker args truly dynamically.

Disclaimer: I’ve not tried this myself :slight_smile:

I’m pretty sure that build script cfg values are only passed to rustc, and not otherwise used by cargo.

But that might be another way to hack #[link_args] if they’re static:

#[cfg_attr(foo, link_args = "...")]

So [target.cfg(...)] is not “hooked up” with build script? That’s a shame. I should play around with this and see what’s actually possible …

I’m in Bazel land mostly, which has its own challenges :frowning: one of them is reverse engineering other crates’ build scripts, sadly.