Skip to content

Commit d0a41be

Browse files
committed
Auto merge of #7132 - ehuss:metadata-kind, r=alexcrichton
Add kind/platform info to `cargo metadata` This adds an array `"dep_kinds"` to the resolve nodes of the `cargo metadata` output. It looks something like this: ```javascript "resolve": { "nodes": [ "id": "cargo 0.39.0 (path+file:///Users/eric/Proj/rust/cargo2)", "deps": [ { "name": "bufstream", "pkg": "bufstream 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "dep_kinds": [ { "kind": "dev", "target": null } ] }, { "name": "winapi", "pkg": "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "dep_kinds": [ { "kind": null, "target": "cfg(windows)" } ] } ] ] } ``` This allows one to filter the graph based on the dependency kind and platform. I'm not completely confident that this is the right course, but I can't think of a better design. In particular, it seems a little strange to include all platforms, but features get filtered. This is probably not a problem in practice (one can use `--all-features` to ensure all features are shown for the top-level packages). Filtering out based on platform is very difficult, because you cannot determine from the resolve alone which nodes will be host vs target. That requires the entire Unit graph. We may expose the Unit graph in the future, but this seems like a useful and simple step. This is a draft because I wanted to discuss this before moving forward. I'd like to add some more tests. cc #4632. This doesn't filter based on target, but does expose the target names. As mentioned above, I don't think filtering is possible. cc #5583. This adds more information. Closes #3984. Closes #4631 (I think). cc @sfackler who filed some of these issues.
2 parents 962b8de + b65ebc6 commit d0a41be

File tree

6 files changed

+311
-43
lines changed

6 files changed

+311
-43
lines changed

src/cargo/ops/cargo_output_metadata.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::core::compiler::{CompileKind, CompileTarget, TargetInfo};
22
use crate::core::resolver::{Resolve, ResolveOpts};
3-
use crate::core::{Package, PackageId, Workspace};
3+
use crate::core::{dependency, Dependency, Package, PackageId, Workspace};
44
use crate::ops::{self, Packages};
55
use crate::util::CargoResult;
6-
6+
use cargo_platform::Platform;
77
use serde::Serialize;
88
use std::collections::HashMap;
99
use std::path::PathBuf;
@@ -85,6 +85,22 @@ struct MetadataResolveNode {
8585
struct Dep {
8686
name: String,
8787
pkg: PackageId,
88+
dep_kinds: Vec<DepKindInfo>,
89+
}
90+
91+
#[derive(Serialize)]
92+
struct DepKindInfo {
93+
kind: dependency::Kind,
94+
target: Option<Platform>,
95+
}
96+
97+
impl From<&Dependency> for DepKindInfo {
98+
fn from(dep: &Dependency) -> DepKindInfo {
99+
DepKindInfo {
100+
kind: dep.kind(),
101+
target: dep.platform().cloned(),
102+
}
103+
}
88104
}
89105

90106
/// Builds the resolve graph as it will be displayed to the user.
@@ -167,12 +183,16 @@ fn build_resolve_graph_r(
167183
}),
168184
None => true,
169185
})
170-
.filter_map(|(dep_id, _deps)| {
186+
.filter_map(|(dep_id, deps)| {
171187
package_map
172188
.get(&dep_id)
173189
.and_then(|pkg| pkg.targets().iter().find(|t| t.is_lib()))
174190
.and_then(|lib_target| resolve.extern_crate_name(pkg_id, dep_id, lib_target).ok())
175-
.map(|name| Dep { name, pkg: dep_id })
191+
.map(|name| Dep {
192+
name,
193+
pkg: dep_id,
194+
dep_kinds: deps.iter().map(DepKindInfo::from).collect(),
195+
})
176196
})
177197
.collect();
178198
let dumb_deps: Vec<PackageId> = deps.iter().map(|dep| dep.pkg).collect();

src/doc/man/cargo-metadata.adoc

+22-8
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,14 @@ The output has the following format:
199199
"workspace_members": [
200200
"my-package 0.1.0 (path+file:///path/to/my-package)",
201201
],
202-
/* The resolved dependency graph, with the concrete versions and features
203-
selected. The set depends on the enabled features.
204-
This is null if --no-deps is specified.
205-
By default, this includes all dependencies for all target platforms.
206-
The `--filter-platform` flag may be used to narrow to a specific
207-
target triple.
208-
*/
202+
// The resolved dependency graph, with the concrete versions and features
203+
// selected. The set depends on the enabled features.
204+
//
205+
// This is null if --no-deps is specified.
206+
//
207+
// By default, this includes all dependencies for all target platforms.
208+
// The `--filter-platform` flag may be used to narrow to a specific
209+
// target triple.
209210
"resolve": {
210211
/* Array of nodes within the dependency graph.
211212
Each node is a package.
@@ -230,7 +231,20 @@ The output has the following format:
230231
*/
231232
"name": "bitflags",
232233
/* The Package ID of the dependency. */
233-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
234+
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
235+
/* Array of dependency kinds. Added in Cargo 1.40. */
236+
"dep_kinds": [
237+
{
238+
/* The dependency kind.
239+
"dev", "build", or null for a normal dependency.
240+
*/
241+
"kind": null,
242+
/* The target platform for the dependency.
243+
null if not a target dependency.
244+
*/
245+
"target": "cfg(windows)"
246+
}
247+
]
234248
}
235249
],
236250
/* Array of features enabled on this package. */

src/doc/man/generated/cargo-metadata.html

+22-8
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,14 @@ <h2 id="cargo_metadata_output_format">OUTPUT FORMAT</h2>
206206
"workspace_members": [
207207
"my-package 0.1.0 (path+file:///path/to/my-package)",
208208
],
209-
/* The resolved dependency graph, with the concrete versions and features
210-
selected. The set depends on the enabled features.
211-
This is null if --no-deps is specified.
212-
By default, this includes all dependencies for all target platforms.
213-
The `--filter-platform` flag may be used to narrow to a specific
214-
target triple.
215-
*/
209+
// The resolved dependency graph, with the concrete versions and features
210+
// selected. The set depends on the enabled features.
211+
//
212+
// This is null if --no-deps is specified.
213+
//
214+
// By default, this includes all dependencies for all target platforms.
215+
// The `--filter-platform` flag may be used to narrow to a specific
216+
// target triple.
216217
"resolve": {
217218
/* Array of nodes within the dependency graph.
218219
Each node is a package.
@@ -237,7 +238,20 @@ <h2 id="cargo_metadata_output_format">OUTPUT FORMAT</h2>
237238
*/
238239
"name": "bitflags",
239240
/* The Package ID of the dependency. */
240-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
241+
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
242+
/* Array of dependency kinds. Added in Cargo 1.40. */
243+
"dep_kinds": [
244+
{
245+
/* The dependency kind.
246+
"dev", "build", or null for a normal dependency.
247+
*/
248+
"kind": null,
249+
/* The target platform for the dependency.
250+
null if not a target dependency.
251+
*/
252+
"target": "cfg(windows)"
253+
}
254+
]
241255
}
242256
],
243257
/* Array of features enabled on this package. */

src/etc/man/cargo-metadata.1

+22-8
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,14 @@ The output has the following format:
220220
"workspace_members": [
221221
"my\-package 0.1.0 (path+file:///path/to/my\-package)",
222222
],
223-
/* The resolved dependency graph, with the concrete versions and features
224-
selected. The set depends on the enabled features.
225-
This is null if \-\-no\-deps is specified.
226-
By default, this includes all dependencies for all target platforms.
227-
The `\-\-filter\-platform` flag may be used to narrow to a specific
228-
target triple.
229-
*/
223+
// The resolved dependency graph, with the concrete versions and features
224+
// selected. The set depends on the enabled features.
225+
//
226+
// This is null if \-\-no\-deps is specified.
227+
//
228+
// By default, this includes all dependencies for all target platforms.
229+
// The `\-\-filter\-platform` flag may be used to narrow to a specific
230+
// target triple.
230231
"resolve": {
231232
/* Array of nodes within the dependency graph.
232233
Each node is a package.
@@ -251,7 +252,20 @@ The output has the following format:
251252
*/
252253
"name": "bitflags",
253254
/* The Package ID of the dependency. */
254-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)"
255+
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)",
256+
/* Array of dependency kinds. Added in Cargo 1.40. */
257+
"dep_kinds": [
258+
{
259+
/* The dependency kind.
260+
"dev", "build", or null for a normal dependency.
261+
*/
262+
"kind": null,
263+
/* The target platform for the dependency.
264+
null if not a target dependency.
265+
*/
266+
"target": "cfg(windows)"
267+
}
268+
]
255269
}
256270
],
257271
/* Array of features enabled on this package. */

0 commit comments

Comments
 (0)