Skip to content

Commit ebba7a3

Browse files
committed
Fix build-std collisions.
1 parent 972b9f5 commit ebba7a3

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/cargo/core/compiler/context/compilation_files.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,5 +587,15 @@ fn compute_metadata<'a, 'cfg>(
587587
if let Ok(ref channel) = __cargo_default_lib_metadata {
588588
channel.hash(&mut hasher);
589589
}
590+
591+
// std units need to be kept separate from user dependencies. std crates
592+
// are differentiated in the Unit with `is_std` (for things like
593+
// `-Zforce-unstable-if-unmarked`), so they are always built separately.
594+
// This isn't strictly necessary for build dependencies which probably
595+
// don't need unstable support. A future experiment might be to set
596+
// `is_std` to false for build dependencies so that they can be shared
597+
// with user dependencies.
598+
unit.is_std.hash(&mut hasher);
599+
590600
Some(Metadata(hasher.finish()))
591601
}

src/cargo/core/compiler/fingerprint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
//! -C incremental=… flag | ✓ |
6161
//! mtime of sources | ✓[^3] |
6262
//! RUSTFLAGS/RUSTDOCFLAGS | ✓ |
63+
//! is_std | | ✓
6364
//!
6465
//! [^1]: Build script and bin dependencies are not included.
6566
//!

tests/build-std/main.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,59 @@ fn custom_test_framework() {
211211
.build_std_arg("core")
212212
.run();
213213
}
214+
215+
#[cargo_test(build_std)]
216+
fn common_crate_collision() {
217+
// Check for collision with `cc` crate in build dependencies.
218+
// This needs to be a full test since we need to use the real `cc` crate.
219+
// This is a bit fragile, since it assumes `cc` is used in libstd,
220+
// and that we are selecting the same features.
221+
222+
// Determine the version of CC used in the real std so that we use the
223+
// exact same version.
224+
let sysroot = paths::sysroot();
225+
let sysroot = Path::new(&sysroot);
226+
let contents =
227+
std::fs::read_to_string(sysroot.join("lib/rustlib/src/rust/Cargo.lock")).unwrap();
228+
let lock: toml::Value = toml::from_str(&contents).unwrap();
229+
let mut packages = lock["package"].as_array().unwrap().iter();
230+
let cc = packages
231+
.find(|p| p["name"].as_str().unwrap() == "cc")
232+
.unwrap();
233+
// Make sure there is only one `cc`.
234+
assert!(packages
235+
.find(|p| p["name"].as_str().unwrap() == "cc")
236+
.is_none());
237+
let cc_version = cc["version"].as_str().unwrap();
238+
239+
let p = project()
240+
.file(
241+
"Cargo.toml",
242+
&format!(
243+
r#"
244+
[package]
245+
name = "foo"
246+
version = "0.1.0"
247+
248+
[build-dependencies]
249+
cc = "={}"
250+
"#,
251+
cc_version
252+
),
253+
)
254+
.file("build.rs", "extern crate cc; fn main() {}")
255+
.file("src/main.rs", "fn main() {}")
256+
.build();
257+
258+
p.cargo("build -v")
259+
.build_std()
260+
.target_host()
261+
.with_stderr_contains(
262+
"[RUNNING] `rustc --crate-name cc [..]-Zforce-unstable-if-unmarked[..]",
263+
)
264+
.with_stderr_line_without(
265+
&["[RUNNING] `rustc --crate-name cc"],
266+
&["-Zforce-unstable-if-unmarked"],
267+
)
268+
.run();
269+
}

0 commit comments

Comments
 (0)