Skip to content

Commit 8ca14aa

Browse files
Refactor if-available setting to work in CI
This verifies if the HEAD sha matches with the detected LLVM SHA, and if not, permits usage of the detected LLVM. Otherwise, we fallback on regular non-downloaded LLVM (currently still cached with sccache, though that's still 10+ minutes on macOS).
1 parent 2769d6b commit 8ca14aa

File tree

3 files changed

+79
-44
lines changed

3 files changed

+79
-44
lines changed

src/bootstrap/config.rs

+1-35
Original file line numberDiff line numberDiff line change
@@ -990,41 +990,7 @@ impl Config {
990990
config.llvm_from_ci = match llvm.download_ci_llvm {
991991
Some(StringOrBool::String(s)) => {
992992
assert!(s == "if-available", "unknown option `{}` for download-ci-llvm", s);
993-
// This is currently all tier 1 targets and tier 2 targets with host tools
994-
// (since others may not have CI artifacts)
995-
// https://doc.rust-lang.org/rustc/platform-support.html#tier-1
996-
let supported_platforms = [
997-
// tier 1
998-
"aarch64-unknown-linux-gnu",
999-
"i686-pc-windows-gnu",
1000-
"i686-pc-windows-msvc",
1001-
"i686-unknown-linux-gnu",
1002-
"x86_64-unknown-linux-gnu",
1003-
"x86_64-apple-darwin",
1004-
"x86_64-pc-windows-gnu",
1005-
"x86_64-pc-windows-msvc",
1006-
// tier 2 with host tools
1007-
"aarch64-apple-darwin",
1008-
"aarch64-pc-windows-msvc",
1009-
"aarch64-unknown-linux-musl",
1010-
"arm-unknown-linux-gnueabi",
1011-
"arm-unknown-linux-gnueabihf",
1012-
"armv7-unknown-linux-gnueabihf",
1013-
"mips-unknown-linux-gnu",
1014-
"mips64-unknown-linux-gnuabi64",
1015-
"mips64el-unknown-linux-gnuabi64",
1016-
"mipsel-unknown-linux-gnu",
1017-
"powerpc-unknown-linux-gnu",
1018-
"powerpc64-unknown-linux-gnu",
1019-
"powerpc64le-unknown-linux-gnu",
1020-
"riscv64gc-unknown-linux-gnu",
1021-
"s390x-unknown-linux-gnu",
1022-
"x86_64-unknown-freebsd",
1023-
"x86_64-unknown-illumos",
1024-
"x86_64-unknown-linux-musl",
1025-
"x86_64-unknown-netbsd",
1026-
];
1027-
supported_platforms.contains(&&*config.build.triple)
993+
crate::native::is_ci_llvm_available(&config)
1028994
}
1029995
Some(StringOrBool::Bool(b)) => b,
1030996
None => false,

src/bootstrap/native.rs

+74-9
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,20 @@ pub fn prebuilt_llvm_config(
114114
Err(Meta { stamp, build_llvm_config, out_dir, root: root.into() })
115115
}
116116

117-
pub(crate) fn maybe_download_ci_llvm(builder: &Builder<'_>) {
118-
let config = &builder.config;
119-
if !config.llvm_from_ci {
120-
return;
121-
}
117+
/// This retrieves the LLVM sha we *want* to use, according to git history.
118+
pub(crate) fn detect_llvm_sha(config: &crate::config::Config) -> String {
122119
let mut rev_list = config.git();
123120
rev_list.args(&[
124121
PathBuf::from("rev-list"),
125-
format!("--author={}", builder.config.stage0_metadata.config.git_merge_commit_email).into(),
122+
format!("--author={}", config.stage0_metadata.config.git_merge_commit_email).into(),
126123
"-n1".into(),
127124
"--first-parent".into(),
128125
"HEAD".into(),
129126
"--".into(),
130-
builder.src.join("src/llvm-project"),
131-
builder.src.join("src/bootstrap/download-ci-llvm-stamp"),
127+
config.src.join("src/llvm-project"),
128+
config.src.join("src/bootstrap/download-ci-llvm-stamp"),
132129
// the LLVM shared object file is named `LLVM-12-rust-{version}-nightly`
133-
builder.src.join("src/version"),
130+
config.src.join("src/version"),
134131
]);
135132
let llvm_sha = output(&mut rev_list);
136133
let llvm_sha = llvm_sha.trim();
@@ -143,8 +140,76 @@ pub(crate) fn maybe_download_ci_llvm(builder: &Builder<'_>) {
143140
panic!();
144141
}
145142

143+
llvm_sha.to_owned()
144+
}
145+
146+
/// Returns whether the CI-found LLVM is currently usable.
147+
///
148+
/// This checks both the build triple platform to confirm we're usable at all,
149+
/// and then verifies if the current HEAD matches the detected LLVM SHA head,
150+
/// in which case LLVM is indicated as not available.
151+
pub(crate) fn is_ci_llvm_available(config: &crate::config::Config) -> bool {
152+
// This is currently all tier 1 targets and tier 2 targets with host tools
153+
// (since others may not have CI artifacts)
154+
// https://doc.rust-lang.org/rustc/platform-support.html#tier-1
155+
let supported_platforms = [
156+
// tier 1
157+
"aarch64-unknown-linux-gnu",
158+
"i686-pc-windows-gnu",
159+
"i686-pc-windows-msvc",
160+
"i686-unknown-linux-gnu",
161+
"x86_64-unknown-linux-gnu",
162+
"x86_64-apple-darwin",
163+
"x86_64-pc-windows-gnu",
164+
"x86_64-pc-windows-msvc",
165+
// tier 2 with host tools
166+
"aarch64-apple-darwin",
167+
"aarch64-pc-windows-msvc",
168+
"aarch64-unknown-linux-musl",
169+
"arm-unknown-linux-gnueabi",
170+
"arm-unknown-linux-gnueabihf",
171+
"armv7-unknown-linux-gnueabihf",
172+
"mips-unknown-linux-gnu",
173+
"mips64-unknown-linux-gnuabi64",
174+
"mips64el-unknown-linux-gnuabi64",
175+
"mipsel-unknown-linux-gnu",
176+
"powerpc-unknown-linux-gnu",
177+
"powerpc64-unknown-linux-gnu",
178+
"powerpc64le-unknown-linux-gnu",
179+
"riscv64gc-unknown-linux-gnu",
180+
"s390x-unknown-linux-gnu",
181+
"x86_64-unknown-freebsd",
182+
"x86_64-unknown-illumos",
183+
"x86_64-unknown-linux-musl",
184+
"x86_64-unknown-netbsd",
185+
];
186+
if !supported_platforms.contains(&&*config.build.triple) {
187+
return false;
188+
}
189+
190+
if crate::util::CiEnv::is_ci() {
191+
let llvm_sha = detect_llvm_sha(config);
192+
let head_sha = output(config.git().arg("rev-parse").arg("HEAD"));
193+
let head_sha = head_sha.trim();
194+
if llvm_sha == head_sha {
195+
eprintln!(
196+
"Detected LLVM as non-available: running in CI and modified LLVM in this change"
197+
);
198+
return false;
199+
}
200+
}
201+
202+
true
203+
}
204+
205+
pub(crate) fn maybe_download_ci_llvm(builder: &Builder<'_>) {
206+
let config = &builder.config;
207+
if !config.llvm_from_ci {
208+
return;
209+
}
146210
let llvm_root = config.ci_llvm_root();
147211
let llvm_stamp = llvm_root.join(".llvm-stamp");
212+
let llvm_sha = detect_llvm_sha(&config);
148213
let key = format!("{}{}", llvm_sha, config.llvm_assertions);
149214
if program_out_of_date(&llvm_stamp, &key) && !config.dry_run {
150215
download_ci_llvm(builder, &llvm_sha);

src/bootstrap/util.rs

+4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ impl CiEnv {
258258
}
259259
}
260260

261+
pub fn is_ci() -> bool {
262+
Self::current() != CiEnv::None
263+
}
264+
261265
/// If in a CI environment, forces the command to run with colors.
262266
pub fn force_coloring_in_ci(self, cmd: &mut Command) {
263267
if self != CiEnv::None {

0 commit comments

Comments
 (0)