Set linker from within build script

Is there a way to set/override the linker binary from within a build.rs file?
I would like to set it to the binary name I retrieve from a PKG_CONFIG information for cross-compilation.
Setting the environment variable CARGO_TARGET_<triple>_LINKER before calling cargo build or setting it in config.toml works.

So far I found/tried the following without success:

  • I found some (old) references to println!("cargo:rustc-linker=<linker>"); but that doesn't seem to do anything.
  • The "cargo:rustc-link-arg" only adds options to the linker, but can't select it.
  • The "cargo:rustc-env" seems to set environment variables visible to the compiled source code
  • Using std::env::set::var("CARGO_TARGET_<triple>_LINKER", "linker"); also doesn't work
  • cargo::rustc-flags=FLAGS only allows for -l or -L flags, but not -C linker=

I don't think there is a way to do this. If it was added, how should multiple build scripts setting the linker to different values be handled?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.