forked from EmbarkStudios/krates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_v2.rs
107 lines (95 loc) · 3.05 KB
/
resolver_v2.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use clap::Parser;
use krates::cm::MetadataCommand;
use std::{collections::BTreeMap, fmt};
/// Prints the runtime and build-time dependency graphs, matching Cargo resolver v2
#[derive(Parser, Debug)]
struct Args {
#[arg(short, long)]
manifest_path: String,
#[arg(long)]
features: Vec<String>,
#[arg(long)]
all_features: bool,
#[arg(long)]
no_default_features: bool,
#[arg(long)]
target: Option<String>,
}
#[derive(Debug)]
pub struct Simple {
id: krates::Kid,
features: BTreeMap<String, Vec<String>>,
}
pub type Graph = krates::Krates<Simple>;
impl From<krates::cm::Package> for Simple {
fn from(pkg: krates::cm::Package) -> Self {
Self {
id: pkg.id.into(),
features: pkg.features,
}
}
}
impl fmt::Display for Simple {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.id.repr)
}
}
fn main() {
let args = Args::parse();
let manifest_path = args.manifest_path;
let target_triple = args.target.unwrap_or_else(|| rustc_host_target_triple());
let metadata = {
let mut cmd = krates::Cmd::new();
if args.all_features {
cmd.all_features();
}
if args.no_default_features {
cmd.no_default_features();
}
if !args.features.is_empty() {
cmd.features(args.features);
}
// Restrict the dependencies to the default platform
cmd.other_options(vec!["--filter-platform".to_owned(), target_triple]);
cmd.manifest_path(manifest_path);
let metadata_cmd: MetadataCommand = cmd.into();
metadata_cmd
.exec()
.expect("Failed to invoke `cargo metadata`")
};
let normal_deps: Graph = {
let mut builder = krates::Builder::new();
builder.ignore_kind(krates::DepKind::Dev, krates::Scope::All);
builder.ignore_kind(krates::DepKind::Build, krates::Scope::All);
builder
.build_with_metadata(metadata.clone(), krates::NoneFilter)
.unwrap()
};
let build_deps: Graph = {
let mut builder = krates::Builder::new();
builder.ignore_kind(krates::DepKind::Dev, krates::Scope::All);
builder
.build_with_metadata(metadata, krates::NoneFilter)
.unwrap()
};
let normal_nodes = normal_deps.graph().raw_nodes();
let build_nodes = build_deps.graph().raw_nodes();
println!("Normal dependency tree:");
println!("{:#?}", normal_nodes);
println!("Build-time depdency tree:");
println!("{:#?}", build_nodes);
}
/// Returns the default target triple for the rustc we're using
fn rustc_host_target_triple() -> String {
use std::io::BufRead;
std::process::Command::new("rustc")
.arg("-vV")
.output()
.expect("Failed to invoke rustc! Is it in your $PATH?")
.stdout
.lines()
.map(|l| l.unwrap())
.find(|l| l.starts_with("host: "))
.map(|l| l[6..].to_string())
.expect("Failed to parse rustc output to determine the current platform!")
}