-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add feature dependencies to all targets but lib #2056
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
//! previously compiled dependency | ||
//! | ||
|
||
use std::collections::HashMap; | ||
use std::collections::{HashMap, HashSet}; | ||
use std::default::Default; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::Arc; | ||
|
@@ -153,6 +153,7 @@ pub fn compile_pkg<'a>(root_package: &Package, | |
|
||
(packages, resolved_with_overrides, registry.move_sources()) | ||
}; | ||
let all_feature = resolve_with_overrides.features(resolve_with_overrides.root()); | ||
|
||
let mut invalid_spec = vec![]; | ||
let pkgids = if spec.len() > 0 { | ||
|
@@ -182,7 +183,7 @@ pub fn compile_pkg<'a>(root_package: &Package, | |
Some(args) => { | ||
if to_builds.len() == 1 { | ||
let targets = try!(generate_targets(to_builds[0], profiles, | ||
mode, filter, release)); | ||
mode, filter, all_feature, release)); | ||
if targets.len() == 1 { | ||
let (target, profile) = targets[0]; | ||
let mut profile = profile.clone(); | ||
|
@@ -203,7 +204,7 @@ pub fn compile_pkg<'a>(root_package: &Package, | |
None => { | ||
for &to_build in to_builds.iter() { | ||
let targets = try!(generate_targets(to_build, profiles, mode, | ||
filter, release)); | ||
filter, all_feature, release)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The list of all features should actually come from the features enabled for the |
||
package_targets.push((to_build, targets)); | ||
} | ||
} | ||
|
@@ -280,6 +281,7 @@ fn generate_targets<'a>(pkg: &'a Package, | |
profiles: &'a Profiles, | ||
mode: CompileMode, | ||
filter: &CompileFilter, | ||
features: Option<&HashSet<String>>, | ||
release: bool) | ||
-> CargoResult<Vec<(&'a Target, &'a Profile)>> { | ||
let build = if release {&profiles.release} else {&profiles.dev}; | ||
|
@@ -290,7 +292,7 @@ fn generate_targets<'a>(pkg: &'a Package, | |
CompileMode::Build => build, | ||
CompileMode::Doc { .. } => &profiles.doc, | ||
}; | ||
return match *filter { | ||
let targets = match *filter { | ||
CompileFilter::Everything => { | ||
match mode { | ||
CompileMode::Bench => { | ||
|
@@ -361,6 +363,16 @@ fn generate_targets<'a>(pkg: &'a Package, | |
Ok(targets) | ||
} | ||
}; | ||
|
||
targets.map(|x| x.iter().filter_map(|&(t, p)| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm perhaps this changed from before? It looks like the It may also be more clear here to explicitly use if/else instead of a match for the logic, the multi-line guard for one of the arms was a little jarring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, instead of having Ah and as a final point I think this can use |
||
match (t.is_lib(), t.features()) { | ||
(true, _) | (false, None) => Some((t, p)), | ||
(false, Some(v)) if v.iter().filter(|&f| { | ||
features.map_or(true, |x| !x.contains(f)) | ||
}).count() == 0 => Some((t, p)), | ||
_ => None | ||
} | ||
}).collect()) | ||
} | ||
|
||
/// Read the `paths` configuration variable to discover all path overrides that | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use support::{project, execs}; | ||
use hamcrest::{assert_that, existing_file, not}; | ||
|
||
fn setup() { | ||
} | ||
|
||
test!(compile_simple_feature_deps { | ||
let p = project("foo") | ||
.file("Cargo.toml", r#" | ||
[project] | ||
name = "foo" | ||
version = "0.0.1" | ||
authors = [] | ||
|
||
[features] | ||
default = ["a"] | ||
a = [] | ||
|
||
[[bin]] | ||
name = "foo" | ||
features = ["a"] | ||
"#) | ||
.file("src/main.rs", "fn main() {}"); | ||
|
||
assert_that(p.cargo_process("build"), | ||
execs().with_status(0)); | ||
|
||
assert_that(&p.bin("foo"), existing_file()); | ||
|
||
assert_that(p.cargo_process("build").arg("--no-default-features"), | ||
execs().with_status(0)); | ||
|
||
assert_that(&p.bin("foo"), not(existing_file())); | ||
}); | ||
|
||
test!(compile_simple_feature_deps_args { | ||
let p = project("foo") | ||
.file("Cargo.toml", r#" | ||
[project] | ||
name = "foo" | ||
version = "0.0.1" | ||
authors = [] | ||
|
||
[features] | ||
a = [] | ||
|
||
[[bin]] | ||
name = "foo" | ||
features = ["a"] | ||
"#) | ||
.file("src/main.rs", "fn main() {}"); | ||
|
||
assert_that(p.cargo_process("build").arg("--features").arg("a"), | ||
execs().with_status(0)); | ||
|
||
assert_that(&p.bin("foo"), existing_file()); | ||
}); | ||
|
||
test!(compile_multiple_feature_deps { | ||
let p = project("foo") | ||
.file("Cargo.toml", r#" | ||
[project] | ||
name = "foo" | ||
version = "0.0.1" | ||
authors = [] | ||
|
||
[features] | ||
default = ["a", "b"] | ||
a = [] | ||
b = ["a"] | ||
c = [] | ||
|
||
[[bin]] | ||
name = "foo_1" | ||
path = "src/foo_1.rs" | ||
features = ["b", "c"] | ||
|
||
[[bin]] | ||
name = "foo_2" | ||
path = "src/foo_2.rs" | ||
features = ["a"] | ||
"#) | ||
.file("src/foo_1.rs", "fn main() {}") | ||
.file("src/foo_2.rs", "fn main() {}"); | ||
|
||
assert_that(p.cargo_process("build"), | ||
execs().with_status(0)); | ||
|
||
assert_that(&p.bin("foo_1"), not(existing_file())); | ||
assert_that(&p.bin("foo_2"), existing_file()); | ||
|
||
assert_that(p.cargo_process("build").arg("--features").arg("c"), | ||
execs().with_status(0)); | ||
|
||
assert_that(&p.bin("foo_1"), existing_file()); | ||
assert_that(&p.bin("foo_2"), existing_file()); | ||
|
||
assert_that(p.cargo_process("build").arg("--no-default-features"), | ||
execs().with_status(0)); | ||
|
||
assert_that(&p.bin("foo_1"), not(existing_file())); | ||
assert_that(&p.bin("foo_2"), not(existing_file())); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like below, it may be best to load the
all_feature
variable just above this usingto_builds[0]
to ensure it's a contained borrow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should be done here?
to_builds[0].summary().features()
returns a&HashMap<String, Vec<String>>
, which is a collection of features from the[features]
section ofCargo.toml
, but if I understand correctly all_feature should have a list of enabled features for the crate that's going to be built. I need some pointers, because I'm a bit lost.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah the
resolve.features(pkgid)
is how the set of activated features is learned about. So by moving theall_feature
variable down here it'll use the right package to learn the set of activated features.