Search code examples
rustconfigrust-cargo

How to set cargo unstable options for a target?


I have .cargo/config.toml with following:

[unstable]
unstable-options = true
build-std = ["core", "alloc"]

But I need build-std only for one target (e.g. thumbv7em-none-eabihf).

Is there any way to declare it specifically for a target?

It could be something like this:

# wrong example, doesn't work apparently
[target.thumbv7em-none-eabihf.unstable]
build-std = ["core", "alloc"]

Note, I’m talking about configs only, I know how to configure it with cargo execution arguments. That’s needed to automatically ignore build-std for targets other then mentioned, e.g. default host target.


Solution

  • As of 2023-07-04 this is not possible to achieve using only the built-in cargo configuration. See the Background section for details.

    Workaround

    You will need to build with an external invocation of cargo for that target e.g. for "core" and "alloc"

    cargo build --target thumbv7em-none-eabihf -Z build-std="core,alloc"
    

    Tested with the relevant toolchain installed and this test program:

    #![feature(restricted_std)]
    
    fn main() {
        loop {}
    }
    

    with cargo invocation

    cargo build --target thumbv7em-none-eabihf -Z build-std="std,panic_abort"
    

    Background

    After some experimentation and searching, there is an open issue for cargo to add this feature here but progress appears to be slow as multiple different approaches have been explored. As build-std is still an unstable interface, available only in nightly versions of cargo, there are still conflicts with other features and some open as well as closed PRs related to changing the semantics. The tracking issue for this kind of feature is here.