Conditional compilation based on binary being compiled

Is it possible to perform conditional compilation in rust based on the binary being compiled?

#[cfg(name = "foo")] doesn't seem to work.

A related question: is it possible to have different build scripts for different binaries, i.e.

[[bin]]
name = “foo”
build = “foo_build.rs”

1 Like

Can you try this under your [[bin]] section ?

I do this with cargo workspaces. You can have the shared functionality in a library with optional features, and then each binary has its own cargo manifest and uses the main library as a dependency.

Individual binaries in one package can also have a required-features setting. However, to my knowledge you have to explicitly enable them. (I.e. cargo run --bin=name doesn’t enable the features automatically, but rather spits out a compilation error telling you to enable them). Individual crates in a workspace is more powerful.

Thanks all I used the technique suggested by @prataprc and @ExpHP to make a feature for each binary and require it for that binary.