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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::collections::{BTreeSet, HashSet};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use log::debug;
use super::{fingerprint, Context, FileFlavor, Unit};
use crate::util::paths;
use crate::util::{internal, CargoResult};
fn render_filename<P: AsRef<Path>>(path: P, basedir: Option<&str>) -> CargoResult<String> {
let path = path.as_ref();
let relpath = match basedir {
None => path,
Some(base) => match path.strip_prefix(base) {
Ok(relpath) => relpath,
_ => path,
},
};
relpath
.to_str()
.ok_or_else(|| internal("path not utf-8"))
.map(|f| f.replace(" ", "\\ "))
}
fn add_deps_for_unit<'a, 'b>(
deps: &mut BTreeSet<PathBuf>,
context: &mut Context<'a, 'b>,
unit: &Unit<'a>,
visited: &mut HashSet<Unit<'a>>,
) -> CargoResult<()> {
if !visited.insert(*unit) {
return Ok(());
}
if !unit.mode.is_run_custom_build() {
let dep_info_loc = fingerprint::dep_info_loc(context, unit);
if let Some(paths) = fingerprint::parse_dep_info(
unit.pkg.root(),
context.files().host_root(),
&dep_info_loc,
)? {
for path in paths {
deps.insert(path);
}
} else {
debug!(
"can't find dep_info for {:?} {}",
unit.pkg.package_id(),
unit.target
);
return Err(internal("dep_info missing"));
}
}
let key = (unit.pkg.package_id(), unit.kind);
if let Some(output) = context.build_script_outputs.lock().unwrap().get(&key) {
for path in &output.rerun_if_changed {
deps.insert(path.into());
}
}
let unit_deps = Vec::from(context.unit_deps(unit));
for dep in unit_deps {
let source_id = dep.unit.pkg.package_id().source_id();
if source_id.is_path() {
add_deps_for_unit(deps, context, &dep.unit, visited)?;
}
}
Ok(())
}
pub fn output_depinfo<'a, 'b>(cx: &mut Context<'a, 'b>, unit: &Unit<'a>) -> CargoResult<()> {
let bcx = cx.bcx;
let mut deps = BTreeSet::new();
let mut visited = HashSet::new();
let success = add_deps_for_unit(&mut deps, cx, unit, &mut visited).is_ok();
let basedir_string;
let basedir = match bcx.config.build_config()?.dep_info_basedir.clone() {
Some(value) => {
basedir_string = value
.resolve_path(bcx.config)
.as_os_str()
.to_str()
.ok_or_else(|| internal("build.dep-info-basedir path not utf-8"))?
.to_string();
Some(basedir_string.as_str())
}
None => None,
};
let deps = deps
.iter()
.map(|f| render_filename(f, basedir))
.collect::<CargoResult<Vec<_>>>()?;
for output in cx
.outputs(unit)?
.iter()
.filter(|o| o.flavor != FileFlavor::DebugInfo)
{
if let Some(ref link_dst) = output.hardlink {
let output_path = link_dst.with_extension("d");
if success {
let target_fn = render_filename(link_dst, basedir)?;
if let Ok(previous) = fingerprint::parse_rustc_dep_info(&output_path) {
if previous.len() == 1 && previous[0].0 == target_fn && previous[0].1 == deps {
continue;
}
}
let mut outfile = BufWriter::new(File::create(output_path)?);
write!(outfile, "{}:", target_fn)?;
for dep in &deps {
write!(outfile, " {}", dep)?;
}
writeln!(outfile)?;
} else if output_path.exists() {
paths::remove_file(output_path)?;
}
}
}
Ok(())
}