Conditional compilation based on target CPU

For the purposes of embedded programming, it would be nice to be able to compile a crate that uses processor-specific features (inline asm, magic addresses, etc.) for multiple processor models. It could look like this: #[cfg(target_cpu = "atmega328p")]. This predicate could get the CPU model from a custom target json (defined in the top/bin crate) and compare it with the input.

I tried quite a few workarounds to compensate for this, but they don't work:

  • using cargo features, a slightly bigger dependency tree will end up with gigantic feature lists
  • using build.rs never really worked as I haven't really figured out how to pass information into dependencies, since I can't change their env vars nor rustc flags.

The same problem is present for CPU crystal frequency.

2 Likes

One mechanism we've talked about that might help: key-value features for Cargo. If a feature could take an enum, a string, or a number, rather than just a boolean, then that would help with configuration problems like this.

5 Likes

Could you please link the tracker/topic for those key-value features for Cargo here?

https://bern-rtos.org/ is using patchable configuration crate for that purpose.

1 Like

Here was one such proposal: Pre-RFC: Mutually-excusive, global features

3 Likes