Abort compilation if one of the features is not selected

219 Views Asked by At

I'm trying to implement a choice option inside Cargo.toml. The idea is that I have 'platforms' - i.e chips that my code runs on. At any given moment, only one of the platforms must be selected, selecting both or selecting neither is not allowed and should abort the compilation as soon as possible.

I created a feature for each platform. Selecting the feature activates the required features inside the hardware support dependencies, pulling the right definitions for my chip, i.e:

[features]
platform-nrf52840 = [
  "embassy-nrf/nrf52840",
  "nrf-softdevice/nrf52840"
  ...
]

platform-nrf52832 = [
  "embassy-nrf/nrf52832",
  "nrf-softdevice/nrf52832",
  ...
]

It works when I select the right platform (i.e cargo build --features platform-nrf52840), but I would like to enforce that one and only one of supported platforms must be supplied at any given time.

I know that this was discussed before as missing feature of Cargo but I wonder if there are workarounds.

So far I tried adding the following guard inside build.rs:

#[cfg(not(any(feature = "platform-nrf52832", feature = "platform-nrf52840")))]
compile_error! {
    "You need to select precisely one of the supported platforms.

    Most likely you need to run `cargo build` with --features flag, i.e:
        `cargo build --features platform-nrf52832`

    See Cargo.toml a the list of supported platforms"
}

The problem with that approach is that AFAIK build.rs is only used when compiling my own project. Crates that are built before start compiling without the correct features enabled, leading to compilation errors.

0

There are 0 best solutions below