Does [target.'cfg()'] in cargo/.config not support user-supplied features?

Hi,

I'm working on some bare-metal Rust code. Different supported boards need different linker scripts (they expect code to be loaded at different addresses for one thing). I'd like to select the board at build time. I'd like to define in the Cargo.toml:

[features]
systemfoo = []

and in cargo/.config:

[target.'cfg(feature = "systemfoo")']
rustflags = "..."

then build it with:

cargo build --features systemfoo --target cpu_arch

while [target.cpu_arch] is parsed correctly in .config, the [target.'cfg()'] section is not. Swapping for a toolchain defined variable, such as...

[target.'cfg(target_vendor = "unknown")']

...will work. So it appears as though Cargo ignores user-supplied features in .config. Is that right? What am I missing, please? Can anyone suggest a way to pass custom build-time features to be applied to .config?

Many thanks!

(BTW I have a build.rs that assembles and links the board-specific low-level code. That's able to read the selected board via an environment variable set by Cargo from the --features switch. However, it doesn't appear this feature is used for target.cfg()).

2 Likes

FWIW setting RUSTFLAGS to use the linker script is a workaround but is really ugly, eg:

RUSTFLAGS='-C link-arg=-Tsrc/platform/cpu_arch/systemfoo/link.ld' cargo build --release --target cpu_arch --features systemfoo

And this can't be set from build.rs.