burtonageo
burtonageo

Reputation: 437

How do I 'pass down' feature flags to subdependencies in Cargo?

I'm writing a library in Cargo. If this library depends on another library like libc, which exposes a feature (in this case, use_std), how do I make a feature I expose enable or disable that feature in my dependency?

Looking at the cargo documentation, it looks like there's no official way specified to do this.

Upvotes: 28

Views: 7437

Answers (3)

Jose V
Jose V

Reputation: 1864

This worked for me:

# Cargo.toml
[features]
default = ["prettyprint"]
prettyprint = ["arrow-cast/prettyprint"]

The funny thing is that I did not have arrow-cast in my deps, so I needed to add it. Because my original dep (just arrow) was using the feature of a another crate (in this case arrow-cast). Just check the Cargo.toml of the crate that you are using to tie up the loose ends. default is convenient so that it is always enabled, you can remove it down the road if you don't need it on by default.

Upvotes: 0

Anastasios Andronidis
Anastasios Andronidis

Reputation: 6740

Just to augment other answers, I found that when I was including my own projects from a different path, I had to also include default-features = false in my dependency when i was compiling with my flags. Like this:

[package]
name = "A"
version = "0.1.0"
edition.workspace = true

[features]
stage = ["B/stage"]

[dependencies]
eyre.workspace = true
tokio.workspace = true

[dependencies.B]
path = ".."
default-features = false  # Without this, I couldn't pass the `stage` feature down to my dependency

Upvotes: 0

Shepmaster
Shepmaster

Reputation: 431779

From the documentation you linked to:

# Features can be used to reexport features of other packages. The `session`
# feature of package `awesome` will ensure that the `session` feature of the
# package `cookie` is also enabled.
session = ["cookie/session"]

Is that sufficient?

Upvotes: 29

Related Questions