Skip to content

Commit adaddb5

Browse files
committed
Auto merge of #98526 - jyn514:download-llvm-outside-checkout, r=Mark-Simulacrum
Allow using `download-ci-llvm = true` outside the git checkout `@bjorn3` noticed that this is already allowed today when download-llvm is disabled, but breaks with it enabled: ``` $ ./rust2/x.py build fatal: not a git repository (or any of the parent directories): .git thread 'main' panicked at 'command did not execute successfully: "git" "rev-list" "[email protected]" "-n1" "--first-parent" "HEAD" "--" "/home/jnelson/rust-lang/rust2/src/llvm-project" "/home/jnelson/rust-lang/rust2/src/bootstrap/download-ci-llvm-stamp" "/home/jnelson/rust-lang/rust2/src/version" expected success, got: exit status: 128', src/bootstrap/native.rs:134:20 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` Support it too for consistency. It's unclear to me when anyone would need to use this, but `@bjorn3` feels we should support it, and it's not much additional effort to get it working.
2 parents e1b348f + 56e42b8 commit adaddb5

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

src/bootstrap/config.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -1126,11 +1126,7 @@ impl Config {
11261126
config.rust_codegen_units_std = rust.codegen_units_std.map(threads_from_config);
11271127
config.rust_profile_use = flags.rust_profile_use.or(rust.profile_use);
11281128
config.rust_profile_generate = flags.rust_profile_generate.or(rust.profile_generate);
1129-
config.download_rustc_commit = download_ci_rustc_commit(
1130-
&config.stage0_metadata,
1131-
rust.download_rustc,
1132-
config.verbose > 0,
1133-
);
1129+
config.download_rustc_commit = download_ci_rustc_commit(&config, rust.download_rustc);
11341130
} else {
11351131
config.rust_profile_use = flags.rust_profile_use;
11361132
config.rust_profile_generate = flags.rust_profile_generate;
@@ -1302,6 +1298,15 @@ impl Config {
13021298
config
13031299
}
13041300

1301+
/// A git invocation which runs inside the source directory.
1302+
///
1303+
/// Use this rather than `Command::new("git")` in order to support out-of-tree builds.
1304+
pub(crate) fn git(&self) -> Command {
1305+
let mut git = Command::new("git");
1306+
git.current_dir(&self.src);
1307+
git
1308+
}
1309+
13051310
/// Try to find the relative path of `bindir`, otherwise return it in full.
13061311
pub fn bindir_relative(&self) -> &Path {
13071312
let bindir = &self.bindir;
@@ -1451,9 +1456,8 @@ fn threads_from_config(v: u32) -> u32 {
14511456

14521457
/// Returns the commit to download, or `None` if we shouldn't download CI artifacts.
14531458
fn download_ci_rustc_commit(
1454-
stage0_metadata: &Stage0Metadata,
1459+
config: &Config,
14551460
download_rustc: Option<StringOrBool>,
1456-
verbose: bool,
14571461
) -> Option<String> {
14581462
// If `download-rustc` is not set, default to rebuilding.
14591463
let if_unchanged = match download_rustc {
@@ -1466,17 +1470,18 @@ fn download_ci_rustc_commit(
14661470
};
14671471

14681472
// Handle running from a directory other than the top level
1469-
let top_level = output(Command::new("git").args(&["rev-parse", "--show-toplevel"]));
1473+
let top_level = output(config.git().args(&["rev-parse", "--show-toplevel"]));
14701474
let top_level = top_level.trim_end();
14711475
let compiler = format!("{top_level}/compiler/");
14721476
let library = format!("{top_level}/library/");
14731477

14741478
// Look for a version to compare to based on the current commit.
14751479
// Only commits merged by bors will have CI artifacts.
14761480
let merge_base = output(
1477-
Command::new("git")
1481+
config
1482+
.git()
14781483
.arg("rev-list")
1479-
.arg(format!("--author={}", stage0_metadata.config.git_merge_commit_email))
1484+
.arg(format!("--author={}", config.stage0_metadata.config.git_merge_commit_email))
14801485
.args(&["-n1", "--first-parent", "HEAD"]),
14811486
);
14821487
let commit = merge_base.trim_end();
@@ -1489,13 +1494,14 @@ fn download_ci_rustc_commit(
14891494
}
14901495

14911496
// Warn if there were changes to the compiler or standard library since the ancestor commit.
1492-
let has_changes = !t!(Command::new("git")
1497+
let has_changes = !t!(config
1498+
.git()
14931499
.args(&["diff-index", "--quiet", &commit, "--", &compiler, &library])
14941500
.status())
14951501
.success();
14961502
if has_changes {
14971503
if if_unchanged {
1498-
if verbose {
1504+
if config.verbose > 0 {
14991505
println!(
15001506
"warning: saw changes to compiler/ or library/ since {commit}; \
15011507
ignoring `download-rustc`"

src/bootstrap/format.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
7272
Err(_) => false,
7373
};
7474
if git_available {
75-
let in_working_tree = match Command::new("git")
75+
let in_working_tree = match build
76+
.config
77+
.git()
7678
.arg("rev-parse")
7779
.arg("--is-inside-work-tree")
7880
.stdout(Stdio::null())
@@ -84,10 +86,7 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
8486
};
8587
if in_working_tree {
8688
let untracked_paths_output = output(
87-
Command::new("git")
88-
.arg("status")
89-
.arg("--porcelain")
90-
.arg("--untracked-files=normal"),
89+
build.config.git().arg("status").arg("--porcelain").arg("--untracked-files=normal"),
9190
);
9291
let untracked_paths = untracked_paths_output
9392
.lines()

src/bootstrap/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,8 @@ impl Build {
643643
return;
644644
}
645645
let output = output(
646-
Command::new("git")
646+
self.config
647+
.git()
647648
.args(&["config", "--file"])
648649
.arg(&self.config.src.join(".gitmodules"))
649650
.args(&["--get-regexp", "path"]),
@@ -1280,12 +1281,12 @@ impl Build {
12801281
// That's our beta number!
12811282
// (Note that we use a `..` range, not the `...` symmetric difference.)
12821283
let count = output(
1283-
Command::new("git")
1284+
self.config
1285+
.git()
12841286
.arg("rev-list")
12851287
.arg("--count")
12861288
.arg("--merges")
1287-
.arg("refs/remotes/origin/master..HEAD")
1288-
.current_dir(&self.src),
1289+
.arg("refs/remotes/origin/master..HEAD"),
12891290
);
12901291
let n = count.trim().parse().unwrap();
12911292
self.prerelease_version.set(Some(n));

src/bootstrap/native.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub(crate) fn maybe_download_ci_llvm(builder: &Builder<'_>) {
118118
if !config.llvm_from_ci {
119119
return;
120120
}
121-
let mut rev_list = Command::new("git");
121+
let mut rev_list = config.git();
122122
rev_list.args(&[
123123
PathBuf::from("rev-list"),
124124
format!("--author={}", builder.config.stage0_metadata.config.git_merge_commit_email).into(),

src/bootstrap/setup.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub fn setup(config: &Config, profile: Profile) {
136136

137137
println!();
138138

139-
t!(install_git_hook_maybe(&config.src));
139+
t!(install_git_hook_maybe(&config));
140140

141141
println!();
142142

@@ -302,7 +302,7 @@ pub fn interactive_path() -> io::Result<Profile> {
302302
}
303303

304304
// install a git hook to automatically run tidy --bless, if they want
305-
fn install_git_hook_maybe(src_path: &Path) -> io::Result<()> {
305+
fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
306306
let mut input = String::new();
307307
println!(
308308
"Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
@@ -328,13 +328,12 @@ undesirable, simply delete the `pre-push` file from .git/hooks."
328328
};
329329

330330
if should_install {
331-
let src = src_path.join("src").join("etc").join("pre-push.sh");
332-
let git = t!(Command::new("git").args(&["rev-parse", "--git-common-dir"]).output().map(
333-
|output| {
331+
let src = config.src.join("src").join("etc").join("pre-push.sh");
332+
let git =
333+
t!(config.git().args(&["rev-parse", "--git-common-dir"]).output().map(|output| {
334334
assert!(output.status.success(), "failed to run `git`");
335335
PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
336-
}
337-
));
336+
}));
338337
let dst = git.join("hooks").join("pre-push");
339338
match fs::hard_link(src, &dst) {
340339
Err(e) => eprintln!(

0 commit comments

Comments
 (0)