Skip to content

Commit dd1525a

Browse files
committed
Auto merge of rust-lang#86015 - jyn514:revert-revert, r=Mark-Simulacrum
Move LLVM submodule updates back to native.rs Time to find more bugs! The first commit is a straight revert of rust-lang#85647, the second is a fix for https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/x.2Epy.20always.20updates.20LLVM.20submodule/near/240113320 and rust-lang#82653 (comment). I haven't been able to replicate rust-lang#82653 (comment).
2 parents e6b4c25 + 73a40ac commit dd1525a

File tree

4 files changed

+91
-13
lines changed

4 files changed

+91
-13
lines changed

src/bootstrap/bootstrap.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -993,28 +993,20 @@ def update_submodules(self):
993993
).decode(default_encoding).splitlines()]
994994
filtered_submodules = []
995995
submodules_names = []
996-
llvm_checked_out = os.path.exists(os.path.join(self.rust_root, "src/llvm-project/.git"))
997-
external_llvm_provided = self.get_toml('llvm-config') or self.downloading_llvm()
998-
llvm_needed = not self.get_toml('codegen-backends', 'rust') \
999-
or "llvm" in self.get_toml('codegen-backends', 'rust')
1000996
for module in submodules:
997+
# This is handled by native::Llvm in rustbuild, not here
1001998
if module.endswith("llvm-project"):
1002-
# Don't sync the llvm-project submodule if an external LLVM was
1003-
# provided, if we are downloading LLVM or if the LLVM backend is
1004-
# not being built. Also, if the submodule has been initialized
1005-
# already, sync it anyways so that it doesn't mess up contributor
1006-
# pull requests.
1007-
if external_llvm_provided or not llvm_needed:
1008-
if self.get_toml('lld') != 'true' and not llvm_checked_out:
1009-
continue
999+
continue
10101000
check = self.check_submodule(module, slow_submodules)
10111001
filtered_submodules.append((module, check))
10121002
submodules_names.append(module)
10131003
recorded = subprocess.Popen(["git", "ls-tree", "HEAD"] + submodules_names,
10141004
cwd=self.rust_root, stdout=subprocess.PIPE)
10151005
recorded = recorded.communicate()[0].decode(default_encoding).strip().splitlines()
1006+
# { filename: hash }
10161007
recorded_submodules = {}
10171008
for data in recorded:
1009+
# [mode, kind, hash, filename]
10181010
data = data.split()
10191011
recorded_submodules[data[3]] = data[2]
10201012
for module in filtered_submodules:

src/bootstrap/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ impl Build {
483483
job::setup(self);
484484
}
485485

486+
// If the LLVM submodule has been initialized already, sync it unconditionally. This avoids
487+
// contributors checking in a submodule change by accident.
488+
if self.in_tree_llvm_info.is_git() {
489+
native::update_llvm_submodule(self);
490+
}
491+
486492
if let Subcommand::Format { check, paths } = &self.config.cmd {
487493
return format::format(self, *check, &paths);
488494
}

src/bootstrap/native.rs

+80-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use build_helper::{output, t};
2121
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
2222
use crate::config::TargetSelection;
2323
use crate::util::{self, exe};
24-
use crate::GitRepo;
24+
use crate::{Build, GitRepo};
2525
use build_helper::up_to_date;
2626

2727
pub struct Meta {
@@ -91,6 +91,82 @@ pub fn prebuilt_llvm_config(
9191
Err(Meta { stamp, build_llvm_config, out_dir, root: root.into() })
9292
}
9393

94+
// modified from `check_submodule` and `update_submodule` in bootstrap.py
95+
pub(crate) fn update_llvm_submodule(build: &Build) {
96+
let llvm_project = &Path::new("src").join("llvm-project");
97+
98+
fn dir_is_empty(dir: &Path) -> bool {
99+
t!(std::fs::read_dir(dir)).next().is_none()
100+
}
101+
102+
// NOTE: The check for the empty directory is here because when running x.py
103+
// the first time, the llvm submodule won't be checked out. Check it out
104+
// now so we can build it.
105+
if !build.in_tree_llvm_info.is_git() && !dir_is_empty(&build.config.src.join(llvm_project)) {
106+
return;
107+
}
108+
109+
// check_submodule
110+
if build.config.fast_submodules {
111+
let checked_out_hash = output(
112+
Command::new("git")
113+
.args(&["rev-parse", "HEAD"])
114+
.current_dir(build.config.src.join(llvm_project)),
115+
);
116+
// update_submodules
117+
let recorded = output(
118+
Command::new("git")
119+
.args(&["ls-tree", "HEAD"])
120+
.arg(llvm_project)
121+
.current_dir(&build.config.src),
122+
);
123+
let actual_hash = recorded
124+
.split_whitespace()
125+
.nth(2)
126+
.unwrap_or_else(|| panic!("unexpected output `{}`", recorded));
127+
128+
// update_submodule
129+
if actual_hash == checked_out_hash.trim_end() {
130+
// already checked out
131+
return;
132+
}
133+
}
134+
135+
println!("Updating submodule {}", llvm_project.display());
136+
build.run(
137+
Command::new("git")
138+
.args(&["submodule", "-q", "sync"])
139+
.arg(llvm_project)
140+
.current_dir(&build.config.src),
141+
);
142+
143+
// Try passing `--progress` to start, then run git again without if that fails.
144+
let update = |progress: bool| {
145+
let mut git = Command::new("git");
146+
git.args(&["submodule", "update", "--init", "--recursive"]);
147+
if progress {
148+
git.arg("--progress");
149+
}
150+
git.arg(llvm_project).current_dir(&build.config.src);
151+
git
152+
};
153+
// NOTE: doesn't use `try_run` because this shouldn't print an error if it fails.
154+
if !update(true).status().map_or(false, |status| status.success()) {
155+
build.run(&mut update(false));
156+
}
157+
158+
build.run(
159+
Command::new("git")
160+
.args(&["reset", "-q", "--hard"])
161+
.current_dir(build.config.src.join(llvm_project)),
162+
);
163+
build.run(
164+
Command::new("git")
165+
.args(&["clean", "-qdfx"])
166+
.current_dir(build.config.src.join(llvm_project)),
167+
);
168+
}
169+
94170
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
95171
pub struct Llvm {
96172
pub target: TargetSelection,
@@ -128,6 +204,9 @@ impl Step for Llvm {
128204
Err(m) => m,
129205
};
130206

207+
if !builder.config.dry_run {
208+
update_llvm_submodule(builder);
209+
}
131210
if builder.config.llvm_link_shared
132211
&& (target.contains("windows") || target.contains("apple-darwin"))
133212
{

src/build_helper/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub fn make(host: &str) -> PathBuf {
130130
}
131131
}
132132

133+
#[track_caller]
133134
pub fn output(cmd: &mut Command) -> String {
134135
let output = match cmd.stderr(Stdio::inherit()).output() {
135136
Ok(status) => status,

0 commit comments

Comments
 (0)