Skip to content

Features enabled through build scripts should have an effect on dependencies #5499

Closed
@glandium

Description

@glandium

I've been using build scripts to enable features based on rust versions automatically, such that if you build with a version that added a new feature to the language or libstd, users of the crate can automatically get those without having to manually enable features.

But sometimes, the feature needs another crate as dependency. In that case, what you really want is an optional dependency, and for the feature to enable it. But the only way to enable a feature from a build script only enables the feature from rustc perspective, not cargo's.

So, say you have a Cargo.toml with something like:

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

or

[features]
foo = ["bar"]

[dependencies]
bar = { version = "0.1", optional = true }

A build script that does:

fn main() {
    println!("cargo:rustc-cfg=feature=\"foo\"");
}

won't make the optional dependency built in either case, which makes sense because it's all about rustc, but should, IMHO, be supported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.A-documenting-cargo-itselfArea: Cargo's documentationA-featuresArea: features — conditional compilation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions