Renaming the main executable on a feature

Hey All. This is a silly one, but I'm scratching my head to find a solution I like. I have an app that I would like to compile to a "real" version by default, but compile to a simulator when I request a "mock" feature. In addition, when compiled as the mock version, I'd like to rename the executable to "mockapp" instead of the default "realapp", to avoid confusion.

I'd also like to keep the implementation in src/main.rs if possible, as both versions are the same. (The real and mock versions used conditionally-compiled modules).

The closest I've come is this Cago.toml:

[package]
name = "realapp"
version = "0.1.0"
edition = "2021"

[features]
default = ["real"]
real = []
mock = []
    
[dependencies]

[[bin]]
name = "realapp"
required-features = ["real"]

[[bin]]
name = "mockapp"
path = "src/main.rs"
required-features = ["mock"]

I put a compile error into build.rs to make sure both features are not enabled, but this still gives me a build warning:

warning: file found to be present in multiple build targets: .../realapp/src/main.rs

Is there a way to suppress the cargo build warning?

Or even better, is there a way to just specify the mock feature without having to disable the defaults, so that the two couldn't collide (i.e. get rid of the "real" feature)?

Thanks!

I think two [[bin]] targets is the way to go here. Give them different source files, but put the common parts in a shared file. This will fit how Cargo wants to work better.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.