Conditional compilation based on CPU model

Is there any attribute that allows conditional compilation based on CPU model? Something in the means of #[cfg(target_cpu = "atmega328p")].

All of the target properties you can depend on are listed here: Conditional compilation - The Rust Reference

There isn’t one for CPU model, but even if there was, it wouldn’t be a good idea to use it because it would mean you cannot use the code with future CPUs which have similar properties but didn't exist when the code was written.

1 Like

Is there some standard way to mark a crate as compilable for certain processors, and selecting which modules belong to which processor?

I thought of cargo features, but each crate in the dependency tree would need to specify a gigantic list of features toggling features of dependencies etc...

It would be practical if the binary/toplevel crate could just pass the CPU model and its clock speed to all dependencies recursively, but I can not find a mechanism for this in the build system.

That sounds like you want to specify target-features, i.e. annotate your items with #[cfg(target-feature = "...")]. Items thus annotated will only be compiled when the target platform you are compiling for has that feature available (SIMD instructions, for example). You can find a list of target-features here.

That works only if i am trying to select specific features of the architecture, however a lot of processors (eg.: the AVR architeture) have many differences inbetween processor models, like special registers' addresses. For this, it would be best to select specifically the one processor, which the target features do not allow.