Pass arbitrary arguments to linker from build script?

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

You can only directly set -l and -L args:

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.

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.