Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
glandium opened this issue May 8, 2018 · 5 comments · Fixed by #7565
Closed

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

glandium opened this issue May 8, 2018 · 5 comments · Fixed by #7565
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. A-documenting-cargo-itself Area: Cargo's documentation A-features Area: features — conditional compilation

Comments

@glandium
Copy link
Contributor

glandium commented May 8, 2018

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.

@alexcrichton
Copy link
Member

This is currently expected behavior in that Cargo doesn't allow activating features at build-script-time but rather they must be activated manually by the user (as they affect crate graph resolution). We may wish to provide a warning, here, however, or something like tha.t

@alexcrichton alexcrichton added the A-diagnostics Area: Error and warning messages generated by Cargo itself. label May 8, 2018
swsnr added a commit to swsnr/mdcat that referenced this issue Aug 26, 2018
We can't enable features in build.rs, see
rust-lang/cargo#5499, and cargo doesn't
support target-specific default features, see
rust-lang/cargo#1197.

Essentially we build as we used to do before we added features for
terminal emulators.  Downsides are more dependencies than needed—you
don’t need iterm2 on Linux after all—and Windows doesn't build with
default features at all.

This reverts commit 544943a.
@kevinaboos
Copy link

I just learned from the Cargo IRC channel that this is the expected behavior. It'd definitely be nice to at least mention it in the docs.

However, I'd agree with @glandium that it would be ideal if there was a build-script-time setting that could activate conditional dependencies. It doesn't have to be based on features, it could be based on other configs, such as "cargo:rustc-cfg=mycfg". Maybe something like "cargo:cargo-cfg=XYZ" that affects cargo dependencies too.

@nox
Copy link
Contributor

nox commented Jan 16, 2019

I'm confused, did that get changed? winapi seems to be doing something like that.

@nox
Copy link
Contributor

nox commented Jan 16, 2019

Oh, I just unconfused myself, they do have influence over the current crate being built, not the dependencies, disregard me.

@Eh2406
Copy link
Contributor

Eh2406 commented Sep 23, 2019

With Cargos current architecture, this is technically not possible. The build script is run (once) after the dependencies are built, so without time travel it can not add dependencies.
Leaving open as better docs and better error messages is reasonable.

@Eh2406 Eh2406 added the A-documenting-cargo-itself Area: Cargo's documentation label Sep 23, 2019
@bors bors closed this as completed in #7565 Nov 7, 2019
@bors bors closed this as completed in 22b08c4 Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. A-documenting-cargo-itself Area: Cargo's documentation A-features Area: features — conditional compilation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants