Custom rustflag for specific binary only?

I have a project that builds multiple binaries using separate sources in src/ folder.

In this configuration, I want to pass custom rustflag to build one of these binaries using custom linker script (-Tlink.x, along with custom build.rs). How can I accomplish this?

I tried various ways to set rustflags, but all broke build of other binaries or depending libraries.

  1. Running cargo with RUSTFLAGS="..." cargo build ... caused other builds to fail, as all rustc invocation got this arg passed.
  2. Setting rustflags=... in [build] or [target.triplet] section of .cargo/config also failed, as rustflags applied to all other compilation/linking as well.

While it does not work, what I essentially want is to write following:

in .cargo/config

[build.foobinary]
rustflags = ...

or

in Cargo.toml

[[bin]]
name = foobinary
rustflags = ...

Is there any way to do something equivalent?

I found a resolution myself. Embedding custom linker arg in the source solved it:

#![feature(link_args)]
...
#[link_args="-Tlink.x"]
#[link_args="-Wl,--no-relax"]
extern "C" {
...declarations that relies on custom linker script...
}

did the trick, along with custom build.rs to copy link.x into linker script search path.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.