Skip to content

Commit

Permalink
Auto merge of #5122 - acmcarther:acm-emit-features-from-cargo-metadat…
Browse files Browse the repository at this point in the history
…a, r=alexcrichton

Emit Resolve.features_sorted in "cargo metadata"

This PR adds `features` to `metadata.resolve.nodes[*]` using the Resolve object's known features. This is different from the `features` fields that already exist elsewhere within metadata as this one is the actual features that need to be used in order to build the code.

Context: I'm currently using Cargo's internals to synthesize BUILD files for the Bazel build tool. `cargo metadata` currently provides almost everything I need in order to avoid using Cargo's internals -- *except* for this.

cc google/cargo-raze#7
  • Loading branch information
bors committed Mar 6, 2018
2 parents 2659f40 + 6a2b646 commit e514f98
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ fn serialize_resolve<S>(resolve: &Resolve, s: S) -> Result<S::Ok, S::Error>
struct Node<'a> {
id: &'a PackageId,
dependencies: Vec<&'a PackageId>,
features: Vec<&'a str>,
}

resolve.iter().map(|id| {
Node {
id,
dependencies: resolve.deps(id).collect(),
features: resolve.features_sorted(id),
}
}).collect::<Vec<_>>().serialize(s)
}
79 changes: 79 additions & 0 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn cargo_metadata_simple() {
"nodes": [
{
"dependencies": [],
"features": [],
"id": "foo 0.5.0 (path+file:[..]foo)"
}
],
Expand Down Expand Up @@ -117,6 +118,77 @@ crate-type = ["lib", "staticlib"]
"nodes": [
{
"dependencies": [],
"features": [],
"id": "foo 0.5.0 (path+file:[..]foo)"
}
],
"root": "foo 0.5.0 (path+file:[..]foo)"
},
"target_directory": "[..]foo[/]target",
"version": 1,
"workspace_root": "[..][/]foo"
}"#));
}

#[test]
fn library_with_features() {
let p = project("foo")
.file("src/lib.rs", "")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.5.0"
[features]
default = ["default_feat"]
default_feat = []
optional_feat = []
"#)
.build();

assert_that(p.cargo("metadata"), execs().with_json(r#"
{
"packages": [
{
"name": "foo",
"version": "0.5.0",
"id": "foo[..]",
"source": null,
"dependencies": [],
"license": null,
"license_file": null,
"description": null,
"targets": [
{
"kind": [
"lib"
],
"crate_types": [
"lib"
],
"name": "foo",
"src_path": "[..][/]foo[/]src[/]lib.rs"
}
],
"features": {
"default": [
"default_feat"
],
"default_feat": [],
"optional_feat": []
},
"manifest_path": "[..]Cargo.toml"
}
],
"workspace_members": ["foo 0.5.0 (path+file:[..]foo)"],
"resolve": {
"nodes": [
{
"dependencies": [],
"features": [
"default",
"default_feat"
],
"id": "foo 0.5.0 (path+file:[..]foo)"
}
],
Expand Down Expand Up @@ -260,16 +332,19 @@ fn cargo_metadata_with_deps_and_version() {
"dependencies": [
"bar 0.0.1 (registry+[..])"
],
"features": [],
"id": "foo 0.5.0 (path+file:[..]foo)"
},
{
"dependencies": [
"baz 0.0.1 (registry+[..])"
],
"features": [],
"id": "bar 0.0.1 (registry+[..])"
},
{
"dependencies": [],
"features": [],
"id": "baz 0.0.1 (registry+[..])"
}
],
Expand Down Expand Up @@ -334,6 +409,7 @@ name = "ex"
"nodes": [
{
"id": "foo 0.1.0 (path+file:[..]foo)",
"features": [],
"dependencies": []
}
]
Expand Down Expand Up @@ -398,6 +474,7 @@ crate-type = ["rlib", "dylib"]
"nodes": [
{
"id": "foo 0.1.0 (path+file:[..]foo)",
"features": [],
"dependencies": []
}
]
Expand Down Expand Up @@ -470,10 +547,12 @@ fn workspace_metadata() {
"nodes": [
{
"dependencies": [],
"features": [],
"id": "baz 0.5.0 (path+file:[..]baz)"
},
{
"dependencies": [],
"features": [],
"id": "bar 0.5.0 (path+file:[..]bar)"
}
],
Expand Down

0 comments on commit e514f98

Please sign in to comment.