Skip to content

Commit 0a675c5

Browse files
committed
Auto merge of #71815 - Mark-Simulacrum:no-llvm-rebuild, r=jonas-schievink
Don't bust caches on x.py check/build switches Fixes #71152
2 parents c1e0552 + 7f645ab commit 0a675c5

File tree

4 files changed

+89
-51
lines changed

4 files changed

+89
-51
lines changed

src/bootstrap/builder.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,17 @@ impl<'a> Builder<'a> {
765765
}
766766

767767
// Set a flag for `check`/`clippy`/`fix`, so that certain build
768-
// scripts can do less work (e.g. not building/requiring LLVM).
768+
// scripts can do less work (i.e. not building/requiring LLVM).
769769
if cmd == "check" || cmd == "clippy" || cmd == "fix" {
770-
cargo.env("RUST_CHECK", "1");
770+
// If we've not yet built LLVM, or it's stale, then bust
771+
// the librustc_llvm cache. That will always work, even though it
772+
// may mean that on the next non-check build we'll need to rebuild
773+
// librustc_llvm. But if LLVM is stale, that'll be a tiny amount
774+
// of work comparitively, and we'd likely need to rebuild it anyway,
775+
// so that's okay.
776+
if crate::native::prebuilt_llvm_config(self, target).is_err() {
777+
cargo.env("RUST_CHECK", "1");
778+
}
771779
}
772780

773781
let stage = if compiler.stage == 0 && self.local_rebuild {

src/bootstrap/compile.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,13 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: Interne
517517
// librustc_llvm and librustc_codegen_llvm.
518518
//
519519
// Note that this is disabled if LLVM itself is disabled or we're in a check
520-
// build, where if we're in a check build there's no need to build all of
521-
// LLVM and such.
522-
if builder.config.llvm_enabled() && builder.kind != Kind::Check {
520+
// build. If we are in a check build we still go ahead here presuming we've
521+
// detected that LLVM is alreay built and good to go which helps prevent
522+
// busting caches (e.g. like #71152).
523+
if builder.config.llvm_enabled()
524+
&& (builder.kind != Kind::Check
525+
|| crate::native::prebuilt_llvm_config(builder, target).is_ok())
526+
{
523527
if builder.is_rust_llvm(target) {
524528
cargo.env("LLVM_RUSTLLVM", "1");
525529
}

src/bootstrap/native.rs

+71-45
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,72 @@ use crate::util::{self, exe};
2424
use crate::GitRepo;
2525
use build_helper::up_to_date;
2626

27+
pub struct Meta {
28+
stamp: HashStamp,
29+
build_llvm_config: PathBuf,
30+
out_dir: PathBuf,
31+
root: String,
32+
}
33+
34+
// This returns whether we've already previously built LLVM.
35+
//
36+
// It's used to avoid busting caches during x.py check -- if we've already built
37+
// LLVM, it's fine for us to not try to avoid doing so.
38+
//
39+
// This will return the llvm-config if it can get it (but it will not build it
40+
// if not).
41+
pub fn prebuilt_llvm_config(
42+
builder: &Builder<'_>,
43+
target: Interned<String>,
44+
) -> Result<PathBuf, Meta> {
45+
// If we're using a custom LLVM bail out here, but we can only use a
46+
// custom LLVM for the build triple.
47+
if let Some(config) = builder.config.target_config.get(&target) {
48+
if let Some(ref s) = config.llvm_config {
49+
check_llvm_version(builder, s);
50+
return Ok(s.to_path_buf());
51+
}
52+
}
53+
54+
let root = "src/llvm-project/llvm";
55+
let out_dir = builder.llvm_out(target);
56+
let mut llvm_config_ret_dir = builder.llvm_out(builder.config.build);
57+
if !builder.config.build.contains("msvc") || builder.config.ninja {
58+
llvm_config_ret_dir.push("build");
59+
}
60+
llvm_config_ret_dir.push("bin");
61+
62+
let build_llvm_config = llvm_config_ret_dir.join(exe("llvm-config", &*builder.config.build));
63+
64+
let stamp = out_dir.join("llvm-finished-building");
65+
let stamp = HashStamp::new(stamp, builder.in_tree_llvm_info.sha());
66+
67+
if builder.config.llvm_skip_rebuild && stamp.path.exists() {
68+
builder.info(
69+
"Warning: \
70+
Using a potentially stale build of LLVM; \
71+
This may not behave well.",
72+
);
73+
return Ok(build_llvm_config);
74+
}
75+
76+
if stamp.is_done() {
77+
if stamp.hash.is_none() {
78+
builder.info(
79+
"Could not determine the LLVM submodule commit hash. \
80+
Assuming that an LLVM rebuild is not necessary.",
81+
);
82+
builder.info(&format!(
83+
"To force LLVM to rebuild, remove the file `{}`",
84+
stamp.path.display()
85+
));
86+
}
87+
return Ok(build_llvm_config);
88+
}
89+
90+
Err(Meta { stamp, build_llvm_config, out_dir, root: root.into() })
91+
}
92+
2793
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2894
pub struct Llvm {
2995
pub target: Interned<String>,
@@ -46,51 +112,11 @@ impl Step for Llvm {
46112
fn run(self, builder: &Builder<'_>) -> PathBuf {
47113
let target = self.target;
48114

49-
// If we're using a custom LLVM bail out here, but we can only use a
50-
// custom LLVM for the build triple.
51-
if let Some(config) = builder.config.target_config.get(&target) {
52-
if let Some(ref s) = config.llvm_config {
53-
check_llvm_version(builder, s);
54-
return s.to_path_buf();
55-
}
56-
}
57-
58-
let root = "src/llvm-project/llvm";
59-
let out_dir = builder.llvm_out(target);
60-
let mut llvm_config_ret_dir = builder.llvm_out(builder.config.build);
61-
if !builder.config.build.contains("msvc") || builder.config.ninja {
62-
llvm_config_ret_dir.push("build");
63-
}
64-
llvm_config_ret_dir.push("bin");
65-
66-
let build_llvm_config =
67-
llvm_config_ret_dir.join(exe("llvm-config", &*builder.config.build));
68-
69-
let stamp = out_dir.join("llvm-finished-building");
70-
let stamp = HashStamp::new(stamp, builder.in_tree_llvm_info.sha());
71-
72-
if builder.config.llvm_skip_rebuild && stamp.path.exists() {
73-
builder.info(
74-
"Warning: \
75-
Using a potentially stale build of LLVM; \
76-
This may not behave well.",
77-
);
78-
return build_llvm_config;
79-
}
80-
81-
if stamp.is_done() {
82-
if stamp.hash.is_none() {
83-
builder.info(
84-
"Could not determine the LLVM submodule commit hash. \
85-
Assuming that an LLVM rebuild is not necessary.",
86-
);
87-
builder.info(&format!(
88-
"To force LLVM to rebuild, remove the file `{}`",
89-
stamp.path.display()
90-
));
91-
}
92-
return build_llvm_config;
93-
}
115+
let Meta { stamp, build_llvm_config, out_dir, root } =
116+
match prebuilt_llvm_config(builder, target) {
117+
Ok(p) => return p,
118+
Err(m) => m,
119+
};
94120

95121
builder.info(&format!("Building LLVM for {}", target));
96122
t!(stamp.remove());

src/librustc_llvm/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ fn detect_llvm_link() -> (&'static str, &'static str) {
1515
}
1616

1717
fn main() {
18+
println!("cargo:rerun-if-env-changed=RUST_CHECK");
1819
if env::var_os("RUST_CHECK").is_some() {
1920
// If we're just running `check`, there's no need for LLVM to be built.
20-
println!("cargo:rerun-if-env-changed=RUST_CHECK");
2121
return;
2222
}
2323

0 commit comments

Comments
 (0)