Skip to content

Commit 278e5fd

Browse files
committed
rustbuild: Improve assert about building tools once
In developing #61557 I noticed that there were two parts of our tools that were rebuilt twice on CI. One was rustfmt fixed in #61557, but another was Cargo. The actual fix for Cargo's double compile was rust-lang/cargo#7010 and took some time to propagate here. In an effort to continue to assert that Cargo is itself not compiled twice, I updated the assertion in rustbuild at the time of working on #61557 but couldn't land it because the fix wouldn't be ready until the next bootstrap. The next bootstrap is now here, so the fix can now land! This does not change the behavior of rustbuild but it is intended to catch the previous iteration of compiling cargo twice. The main update here was to consider more files than those in `$target/release/deps` but also consider those in `$target/release`. That's where, for example, `libcargo.rlib` shows up and it's the file we learn about, and that's what we want to deduplicate.
1 parent 71f9384 commit 278e5fd

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

src/bootstrap/tool.rs

+48-21
Original file line numberDiff line numberDiff line change
@@ -109,36 +109,63 @@ impl Step for ToolBuild {
109109
continue
110110
}
111111

112-
// Don't worry about libs that turn out to be host dependencies
113-
// or build scripts, we only care about target dependencies that
114-
// are in `deps`.
115-
if let Some(maybe_target) = val.1
116-
.parent() // chop off file name
117-
.and_then(|p| p.parent()) // chop off `deps`
118-
.and_then(|p| p.parent()) // chop off `release`
119-
.and_then(|p| p.file_name())
120-
.and_then(|p| p.to_str())
121-
{
122-
if maybe_target != &*target {
123-
continue
112+
// Don't worry about compiles that turn out to be host
113+
// dependencies or build scripts. To skip these we look for
114+
// anything that goes in `.../release/deps` but *doesn't* go in
115+
// `$target/release/deps`. This ensure that outputs in
116+
// `$target/release` are still considered candidates for
117+
// deduplication.
118+
if let Some(parent) = val.1.parent() {
119+
if parent.ends_with("release/deps") {
120+
let maybe_target = parent
121+
.parent()
122+
.and_then(|p| p.parent())
123+
.and_then(|p| p.file_name())
124+
.and_then(|p| p.to_str())
125+
.unwrap();
126+
if maybe_target != &*target {
127+
continue;
128+
}
124129
}
125130
}
126131

132+
// Record that we've built an artifact for `id`, and if one was
133+
// already listed then we need to see if we reused the same
134+
// artifact or produced a duplicate.
127135
let mut artifacts = builder.tool_artifacts.borrow_mut();
128136
let prev_artifacts = artifacts
129137
.entry(target)
130138
.or_default();
131-
if let Some(prev) = prev_artifacts.get(&*id) {
132-
if prev.1 != val.1 {
133-
duplicates.push((
134-
id.to_string(),
135-
val,
136-
prev.clone(),
137-
));
139+
let prev = match prev_artifacts.get(&*id) {
140+
Some(prev) => prev,
141+
None => {
142+
prev_artifacts.insert(id.to_string(), val);
143+
continue;
138144
}
139-
return
145+
};
146+
if prev.1 == val.1 {
147+
return; // same path, same artifact
140148
}
141-
prev_artifacts.insert(id.to_string(), val);
149+
150+
// If the paths are different and one of them *isn't* inside of
151+
// `release/deps`, then it means it's probably in
152+
// `$target/release`, or it's some final artifact like
153+
// `libcargo.rlib`. In these situations Cargo probably just
154+
// copied it up from `$target/release/deps/libcargo-xxxx.rlib`,
155+
// so if the features are equal we can just skip it.
156+
let prev_no_hash = prev.1.parent().unwrap().ends_with("release/deps");
157+
let val_no_hash = val.1.parent().unwrap().ends_with("release/deps");
158+
if prev.2 == val.2 || !prev_no_hash || !val_no_hash {
159+
return;
160+
}
161+
162+
// ... and otherwise this looks like we duplicated some sort of
163+
// compilation, so record it to generate an error later.
164+
duplicates.push((
165+
id.to_string(),
166+
val,
167+
prev.clone(),
168+
));
142169
}
143170
});
144171

0 commit comments

Comments
 (0)