Can Cargo profiles affect dependency features?

I want to have a custom Cargo profile that enables Tracy profiling. Is there syntax for [dependencies] that allows this.

I want to be able to build with

cargo --profiles tracy

and have

[dependencies]
profiling = { version = "1", default-features = false }
tracy-client = {version = "0.12" }

change to

[dependencies]
profiling = { version = "1", default-features = false, features = ["profile-with-tracy"] }
tracy-client = {version = "0.12", optional = true }

in both the top-level workspace and lower-level workspaces.

The Cargo book shows [dev-dependencies], which is the right idea but apparently not usable for custom profiles. There's [build-dependencies], but that doesn't seem suitable either. [features] can almost do this, but not quite, I think.

This must be possible somehow. Building with and without profiling is a common need.

That sounds like it should be cargo --features tracy.

That makes sense. Found the "/" syntax that makes this work. I wasn't clear on how to get a feature flag from the command line down to a crate.

[features]
# Feature "tracy" enables the network client tracy-client and turns on the profiling macros in "tracy"
tracy = ["tracy-client", "profiling/profile-with-tracy"]

[dependencies]
# The features flag "tracy" will add "profiling-with-tracy" to this.
profiling = { version = "1", default-features = false] }
# This should work, including tracy-client only if tracy is on.
tracy-client = {version = "0.12", optional = true }
1 Like

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.