Skip to content

Commit a7faecc

Browse files
committed
Add kind/platform info to cargo metadata
1 parent 5da4b4d commit a7faecc

File tree

6 files changed

+299
-22
lines changed

6 files changed

+299
-22
lines changed

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 24 additions & 4 deletions
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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ The output has the following format:
201201
],
202202
/* The resolved dependency graph, with the concrete versions and features
203203
selected. The set depends on the enabled features.
204+
205+
All platform-specific dependencies are listed regardless of the current
206+
target.
207+
204208
This is null if --no-deps is specified.
205209
By default, this includes all dependencies for all target platforms.
206210
The `--filter-platform` flag may be used to narrow to a specific
@@ -230,7 +234,20 @@ The output has the following format:
230234
*/
231235
"name": "bitflags",
232236
/* The Package ID of the dependency. */
233-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
237+
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
238+
/* Array of dependency kinds. Added in Cargo 1.38. */
239+
"dep_kinds": [
240+
{
241+
/* The dependency kind.
242+
"dev", "build", or null for a normal dependency.
243+
*/
244+
"kind": null,
245+
/* The target platform for the dependency.
246+
null if not a target dependency.
247+
*/
248+
"target": "cfg(windows)"
249+
}
250+
]
234251
}
235252
],
236253
/* Array of features enabled on this package. */

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ <h2 id="cargo_metadata_output_format">OUTPUT FORMAT</h2>
208208
],
209209
/* The resolved dependency graph, with the concrete versions and features
210210
selected. The set depends on the enabled features.
211+
212+
All platform-specific dependencies are listed regardless of the current
213+
target.
214+
211215
This is null if --no-deps is specified.
212216
By default, this includes all dependencies for all target platforms.
213217
The `--filter-platform` flag may be used to narrow to a specific
@@ -237,7 +241,20 @@ <h2 id="cargo_metadata_output_format">OUTPUT FORMAT</h2>
237241
*/
238242
"name": "bitflags",
239243
/* The Package ID of the dependency. */
240-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
244+
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
245+
/* Array of dependency kinds. Added in Cargo 1.38. */
246+
"dep_kinds": [
247+
{
248+
/* The dependency kind.
249+
"dev", "build", or null for a normal dependency.
250+
*/
251+
"kind": null,
252+
/* The target platform for the dependency.
253+
null if not a target dependency.
254+
*/
255+
"target": "cfg(windows)"
256+
}
257+
]
241258
}
242259
],
243260
/* Array of features enabled on this package. */

src/etc/man/cargo-metadata.1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ The output has the following format:
222222
],
223223
/* The resolved dependency graph, with the concrete versions and features
224224
selected. The set depends on the enabled features.
225+
226+
All platform\-specific dependencies are listed regardless of the current
227+
target.
228+
225229
This is null if \-\-no\-deps is specified.
226230
By default, this includes all dependencies for all target platforms.
227231
The `\-\-filter\-platform` flag may be used to narrow to a specific
@@ -251,7 +255,20 @@ The output has the following format:
251255
*/
252256
"name": "bitflags",
253257
/* The Package ID of the dependency. */
254-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)"
258+
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)",
259+
/* Array of dependency kinds. Added in Cargo 1.38. */
260+
"dep_kinds": [
261+
{
262+
/* The dependency kind.
263+
"dev", "build", or null for a normal dependency.
264+
*/
265+
"kind": null,
266+
/* The target platform for the dependency.
267+
null if not a target dependency.
268+
*/
269+
"target": "cfg(windows)"
270+
}
271+
]
255272
}
256273
],
257274
/* Array of features enabled on this package. */

0 commit comments

Comments
 (0)