Can we use feature flags for build-dependencies & dev-dependencies in Cargo.toml?

298 Views Asked by At

I'm using feature flags for optional dependencies in the Cargo.toml file, which works as expected- compiles less number of crates without the feature flag "bar".

[features]
bar = ["dep:foo"]

[dependencies]
foo = { version = "0.0.1", optional = true }

I want to use feature flags similarly for optional build-dependencies and optional dev-dependencies? I have read the Cargo book section Feature resolver version 2: https://doc.rust-lang.org/cargo/reference/features.html#feature-resolver-version-2 which says:

  • Build-dependencies and proc-macros do not share features with normal dependencies.
  • Dev-dependencies do not activate features unless building a target that needs them (like tests or examples).

I'm not sure what this means. Can I use it for optional compilation as below?

[features]
build-bar = ["dep:build-foo"]

[build-dependencies]
build-foo = { git = "https://github.com/Me/MyRepo", branch = "feat1", optional = true }

0

There are 0 best solutions below