Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0396f0e

Browse files
committed
Use check_path_modifications for detecting local LLVM changes
1 parent 1d1f248 commit 0396f0e

File tree

6 files changed

+50
-20
lines changed

6 files changed

+50
-20
lines changed

src/bootstrap/src/core/build_steps/gcc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::core::config::TargetSelection;
1717
use crate::utils::build_stamp::{BuildStamp, generate_smart_stamp_hash};
1818
use crate::utils::exec::command;
1919
use crate::utils::helpers::{self, t};
20-
use build_helper::git::PathFreshness;
2120

2221
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
2322
pub struct Gcc {
@@ -97,6 +96,8 @@ pub enum GccBuildStatus {
9796
/// Returns a path to the libgccjit.so file.
9897
#[cfg(not(test))]
9998
fn try_download_gcc(builder: &Builder<'_>, target: TargetSelection) -> Option<PathBuf> {
99+
use build_helper::git::PathFreshness;
100+
100101
// Try to download GCC from CI if configured and available
101102
if !matches!(builder.config.gcc_ci_mode, crate::core::config::GccCiMode::DownloadFromCi) {
102103
return None;

src/bootstrap/src/core/build_steps/llvm.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::path::{Path, PathBuf};
1414
use std::sync::OnceLock;
1515
use std::{env, fs};
1616

17-
use build_helper::git::get_closest_merge_commit;
17+
use build_helper::git::{PathFreshness, check_path_modifications};
1818
#[cfg(feature = "tracing")]
1919
use tracing::instrument;
2020

@@ -181,26 +181,33 @@ pub const LLVM_INVALIDATION_PATHS: &[&str] = &[
181181
"src/version",
182182
];
183183

184-
/// This retrieves the LLVM sha we *want* to use, according to git history.
185-
pub(crate) fn detect_llvm_sha(config: &Config, is_git: bool) -> String {
186-
let llvm_sha = if is_git {
187-
get_closest_merge_commit(Some(&config.src), &config.git_config(), LLVM_INVALIDATION_PATHS)
188-
.unwrap()
184+
/// Detect whether LLVM sources have been modified locally or not.
185+
pub(crate) fn detect_llvm_freshness(config: &Config, is_git: bool) -> PathFreshness {
186+
let freshness = if is_git {
187+
Some(
188+
check_path_modifications(
189+
Some(&config.src),
190+
&config.git_config(),
191+
LLVM_INVALIDATION_PATHS,
192+
config.ci_env(),
193+
)
194+
.unwrap(),
195+
)
189196
} else if let Some(info) = crate::utils::channel::read_commit_info_file(&config.src) {
190-
info.sha.trim().to_owned()
197+
Some(PathFreshness::LastModifiedUpstream { upstream: info.sha.trim().to_owned() })
191198
} else {
192-
"".to_owned()
199+
None
193200
};
194201

195-
if llvm_sha.is_empty() {
202+
let Some(freshness) = freshness else {
196203
eprintln!("error: could not find commit hash for downloading LLVM");
197204
eprintln!("HELP: maybe your repository history is too shallow?");
198205
eprintln!("HELP: consider disabling `download-ci-llvm`");
199206
eprintln!("HELP: or fetch enough history to include one upstream commit");
200207
panic!();
201-
}
208+
};
202209

203-
llvm_sha
210+
freshness
204211
}
205212

206213
/// Returns whether the CI-found LLVM is currently usable.

src/bootstrap/src/core/config/config.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use std::{cmp, env, fs};
1616

1717
use build_helper::ci::CiEnv;
1818
use build_helper::exit;
19-
use build_helper::git::{GitConfig, get_closest_merge_commit, output_result};
19+
use build_helper::git::{
20+
GitConfig, PathFreshness, check_path_modifications, get_closest_merge_commit, output_result,
21+
};
2022
use serde::{Deserialize, Deserializer};
2123
use serde_derive::Deserialize;
2224
#[cfg(feature = "tracing")]
@@ -3264,9 +3266,7 @@ impl Config {
32643266
self.update_submodule("src/llvm-project");
32653267

32663268
// Check for untracked changes in `src/llvm-project` and other important places.
3267-
let has_changes = self
3268-
.last_modified_commit(LLVM_INVALIDATION_PATHS, "download-ci-llvm", true)
3269-
.is_none();
3269+
let has_changes = self.has_changes_from_upstream(LLVM_INVALIDATION_PATHS);
32703270

32713271
// Return false if there are untracked changes, otherwise check if CI LLVM is available.
32723272
if has_changes { false } else { llvm::is_ci_llvm_available_for_target(self, asserts) }
@@ -3297,6 +3297,17 @@ impl Config {
32973297
}
32983298
}
32993299

3300+
/// Returns true if any of the `paths` have been modified locally.
3301+
fn has_changes_from_upstream(&self, paths: &[&str]) -> bool {
3302+
let freshness =
3303+
check_path_modifications(Some(&self.src), &self.git_config(), paths, CiEnv::current())
3304+
.unwrap();
3305+
match freshness {
3306+
PathFreshness::LastModifiedUpstream { .. } => false,
3307+
PathFreshness::HasLocalModifications { .. } => true,
3308+
}
3309+
}
3310+
33003311
/// Returns the last commit in which any of `modified_paths` were changed,
33013312
/// or `None` if there are untracked changes in the working directory and `if_unchanged` is true.
33023313
pub fn last_modified_commit(

src/bootstrap/src/core/download.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,16 +720,21 @@ download-rustc = false
720720
#[cfg(not(test))]
721721
pub(crate) fn maybe_download_ci_llvm(&self) {
722722
use build_helper::exit;
723+
use build_helper::git::PathFreshness;
723724

724-
use crate::core::build_steps::llvm::detect_llvm_sha;
725+
use crate::core::build_steps::llvm::detect_llvm_freshness;
725726
use crate::core::config::check_incompatible_options_for_ci_llvm;
726727

727728
if !self.llvm_from_ci {
728729
return;
729730
}
730731

731732
let llvm_root = self.ci_llvm_root();
732-
let llvm_sha = detect_llvm_sha(self, self.rust_info.is_managed_git_subrepository());
733+
let llvm_sha =
734+
match detect_llvm_freshness(self, self.rust_info.is_managed_git_subrepository()) {
735+
PathFreshness::LastModifiedUpstream { upstream } => upstream,
736+
PathFreshness::HasLocalModifications { upstream } => upstream,
737+
};
733738
let stamp_key = format!("{}{}", llvm_sha, self.llvm_assertions);
734739
let llvm_stamp = BuildStamp::new(&llvm_root).with_prefix("llvm").add_stamp(stamp_key);
735740
if !llvm_stamp.is_up_to_date() && !self.dry_run() {

src/build_helper/src/git.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ pub fn check_path_modifications(
261261
upstream_sha
262262
};
263263

264+
// For local environments, we want to find out if something has changed
265+
// from the latest upstream commit.
266+
// However, that should be equivalent to checking if something has changed
267+
// from the latest upstream commit *that modified `target_paths`*, and
268+
// with this approach we do not need to invoke git an additional time.
264269
if has_changed_since(git_dir, &upstream_sha, target_paths) {
265270
Ok(PathFreshness::HasLocalModifications { upstream: upstream_sha })
266271
} else {

src/build_helper/src/git/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::ci::CiEnv;
2-
use crate::git::{GitConfig, PathFreshness, check_path_modifications};
31
use std::ffi::OsStr;
42
use std::fs::OpenOptions;
53
use std::process::Command;
64

5+
use crate::ci::CiEnv;
6+
use crate::git::{GitConfig, PathFreshness, check_path_modifications};
7+
78
#[test]
89
fn test_pr_ci_unchanged_anywhere() {
910
git_test(|ctx| {

0 commit comments

Comments
 (0)