Skip to content

Commit e514f98

Browse files
committed
Auto merge of #5122 - acmcarther:acm-emit-features-from-cargo-metadata, 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
2 parents 2659f40 + 6a2b646 commit e514f98

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/cargo/ops/cargo_output_metadata.rs

+2
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ fn serialize_resolve<S>(resolve: &Resolve, s: S) -> Result<S::Ok, S::Error>
9898
struct Node<'a> {
9999
id: &'a PackageId,
100100
dependencies: Vec<&'a PackageId>,
101+
features: Vec<&'a str>,
101102
}
102103

103104
resolve.iter().map(|id| {
104105
Node {
105106
id,
106107
dependencies: resolve.deps(id).collect(),
108+
features: resolve.features_sorted(id),
107109
}
108110
}).collect::<Vec<_>>().serialize(s)
109111
}

tests/testsuite/metadata.rs

+79
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fn cargo_metadata_simple() {
4242
"nodes": [
4343
{
4444
"dependencies": [],
45+
"features": [],
4546
"id": "foo 0.5.0 (path+file:[..]foo)"
4647
}
4748
],
@@ -117,6 +118,77 @@ crate-type = ["lib", "staticlib"]
117118
"nodes": [
118119
{
119120
"dependencies": [],
121+
"features": [],
122+
"id": "foo 0.5.0 (path+file:[..]foo)"
123+
}
124+
],
125+
"root": "foo 0.5.0 (path+file:[..]foo)"
126+
},
127+
"target_directory": "[..]foo[/]target",
128+
"version": 1,
129+
"workspace_root": "[..][/]foo"
130+
}"#));
131+
}
132+
133+
#[test]
134+
fn library_with_features() {
135+
let p = project("foo")
136+
.file("src/lib.rs", "")
137+
.file("Cargo.toml", r#"
138+
[package]
139+
name = "foo"
140+
version = "0.5.0"
141+
142+
[features]
143+
default = ["default_feat"]
144+
default_feat = []
145+
optional_feat = []
146+
"#)
147+
.build();
148+
149+
assert_that(p.cargo("metadata"), execs().with_json(r#"
150+
{
151+
"packages": [
152+
{
153+
"name": "foo",
154+
"version": "0.5.0",
155+
"id": "foo[..]",
156+
"source": null,
157+
"dependencies": [],
158+
"license": null,
159+
"license_file": null,
160+
"description": null,
161+
"targets": [
162+
{
163+
"kind": [
164+
"lib"
165+
],
166+
"crate_types": [
167+
"lib"
168+
],
169+
"name": "foo",
170+
"src_path": "[..][/]foo[/]src[/]lib.rs"
171+
}
172+
],
173+
"features": {
174+
"default": [
175+
"default_feat"
176+
],
177+
"default_feat": [],
178+
"optional_feat": []
179+
},
180+
"manifest_path": "[..]Cargo.toml"
181+
}
182+
],
183+
"workspace_members": ["foo 0.5.0 (path+file:[..]foo)"],
184+
"resolve": {
185+
"nodes": [
186+
{
187+
"dependencies": [],
188+
"features": [
189+
"default",
190+
"default_feat"
191+
],
120192
"id": "foo 0.5.0 (path+file:[..]foo)"
121193
}
122194
],
@@ -260,16 +332,19 @@ fn cargo_metadata_with_deps_and_version() {
260332
"dependencies": [
261333
"bar 0.0.1 (registry+[..])"
262334
],
335+
"features": [],
263336
"id": "foo 0.5.0 (path+file:[..]foo)"
264337
},
265338
{
266339
"dependencies": [
267340
"baz 0.0.1 (registry+[..])"
268341
],
342+
"features": [],
269343
"id": "bar 0.0.1 (registry+[..])"
270344
},
271345
{
272346
"dependencies": [],
347+
"features": [],
273348
"id": "baz 0.0.1 (registry+[..])"
274349
}
275350
],
@@ -334,6 +409,7 @@ name = "ex"
334409
"nodes": [
335410
{
336411
"id": "foo 0.1.0 (path+file:[..]foo)",
412+
"features": [],
337413
"dependencies": []
338414
}
339415
]
@@ -398,6 +474,7 @@ crate-type = ["rlib", "dylib"]
398474
"nodes": [
399475
{
400476
"id": "foo 0.1.0 (path+file:[..]foo)",
477+
"features": [],
401478
"dependencies": []
402479
}
403480
]
@@ -470,10 +547,12 @@ fn workspace_metadata() {
470547
"nodes": [
471548
{
472549
"dependencies": [],
550+
"features": [],
473551
"id": "baz 0.5.0 (path+file:[..]baz)"
474552
},
475553
{
476554
"dependencies": [],
555+
"features": [],
477556
"id": "bar 0.5.0 (path+file:[..]bar)"
478557
}
479558
],

0 commit comments

Comments
 (0)