1//! Sanity checking performed by bootstrap before actually executing anything.
2//!
3//! This module contains the implementation of ensuring that the build
4//! environment looks reasonable before progressing. This will verify that
5//! various programs like git and python exist, along with ensuring that all C
6//! compilers for cross-compiling are found.
7//!
8//! In theory if we get past this phase it's a bug if a build fails, but in
9//! practice that's likely not true!
1011use std::collections::{HashMap, HashSet};
12use std::ffi::{OsStr, OsString};
13use std::path::PathBuf;
14use std::{env, fs};
1516use crate::Build;
17#[cfg(not(test))]
18use crate::builder::Builder;
19use crate::builder::Kind;
20#[cfg(not(test))]
21use crate::core::build_steps::tool;
22use crate::core::config::Target;
23use crate::utils::exec::command;
2425pub struct Finder {
26 cache: HashMap<OsString, Option<PathBuf>>,
27 path: OsString,
28}
2930// During sanity checks, we search for target names to determine if they exist in the compiler's built-in
31// target list (`rustc --print target-list`). While a target name may be present in the stage2 compiler,
32// it might not yet be included in stage0. In such cases, we handle the targets missing from stage0 in this list.
33//
34// Targets can be removed from this list once they are present in the stage0 compiler (usually by updating the beta compiler of the bootstrap).
35const STAGE0_MISSING_TARGETS: &[&str] = &[
36// just a dummy comment so the list doesn't get onelined
37"wasm32-wali-linux-musl",
38];
3940/// Minimum version threshold for libstdc++ required when using prebuilt LLVM
41/// from CI (with`llvm.download-ci-llvm` option).
42#[cfg(not(test))]
43const LIBSTDCXX_MIN_VERSION_THRESHOLD: usize = 8;
4445impl Finder {
46pub fn new() -> Self {
47Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
48 }
4950pub fn maybe_have<S: Into<OsString>>(&mut self, cmd: S) -> Option<PathBuf> {
51let cmd: OsString = cmd.into();
52let path = &self.path;
53self.cache
54 .entry(cmd.clone())
55 .or_insert_with(|| {
56for path in env::split_paths(path) {
57let target = path.join(&cmd);
58let mut cmd_exe = cmd.clone();
59 cmd_exe.push(".exe");
6061if target.is_file() // some/path/git
62|| path.join(&cmd_exe).exists() // some/path/git.exe
63|| target.join(&cmd_exe).exists()
64// some/path/git/git.exe
65{
66return Some(target);
67 }
68 }
69None70 })
71 .clone()
72 }
7374pub fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
75self.maybe_have(&cmd).unwrap_or_else(|| {
76panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
77 })
78 }
79}
8081pub fn check(build: &mut Build) {
82let mut skip_target_sanity =
83env::var_os("BOOTSTRAP_SKIP_TARGET_SANITY").is_some_and(|s| s == "1" || s == "true");
8485skip_target_sanity |= build.config.cmd.kind() == Kind::Check;
8687// Skip target sanity checks when we are doing anything with mir-opt tests or Miri
88let skipped_paths = [OsStr::new("mir-opt"), OsStr::new("miri")];
89skip_target_sanity |= build.config.paths.iter().any(|path| {
90path.components().any(|component| skipped_paths.contains(&component.as_os_str()))
91 });
9293let path = env::var_os("PATH").unwrap_or_default();
94// On Windows, quotes are invalid characters for filename paths, and if
95 // one is present as part of the PATH then that can lead to the system
96 // being unable to identify the files properly. See
97 // https://github.com/rust-lang/rust/issues/34959 for more details.
98if cfg!(windows) && path.to_string_lossy().contains('\"') {
99panic!("PATH contains invalid character '\"'");
100 }
101102let mut cmd_finder = Finder::new();
103// If we've got a git directory we're gonna need git to update
104 // submodules and learn about various other aspects.
105if build.rust_info().is_managed_git_subrepository() {
106cmd_finder.must_have("git");
107 }
108109// Ensure that a compatible version of libstdc++ is available on the system when using `llvm.download-ci-llvm`.
110#[cfg(not(test))]
111if !build.config.dry_run() && !build.build.is_msvc() && build.config.llvm_from_ci {
112let builder = Builder::new(build);
113let libcxx_version = builder.ensure(tool::LibcxxVersionTool { target: build.build });
114115match libcxx_version {
116tool::LibcxxVersion::Gnu(version) => {
117if LIBSTDCXX_MIN_VERSION_THRESHOLD > version {
118eprintln!(
119"\nYour system's libstdc++ version is too old for the `llvm.download-ci-llvm` option."
120);
121eprintln!("Current version detected: '{}'", version);
122eprintln!("Minimum required version: '{}'", LIBSTDCXX_MIN_VERSION_THRESHOLD);
123eprintln!(
124"Consider upgrading libstdc++ or disabling the `llvm.download-ci-llvm` option."
125);
126eprintln!(
127"If you choose to upgrade libstdc++, run `x clean` or delete `build/host/libcxx-version` manually after the upgrade."
128);
129 }
130 }
131tool::LibcxxVersion::Llvm(_) => {
132// FIXME: Handle libc++ version check.
133}
134 }
135 }
136137// We need cmake, but only if we're actually building LLVM or sanitizers.
138let building_llvm = !build.config.llvm_from_ci
139 && build.hosts.iter().any(|host| {
140build.config.llvm_enabled(*host)
141 && build142 .config
143 .target_config
144 .get(host)
145 .map(|config| config.llvm_config.is_none())
146 .unwrap_or(true)
147 });
148149let need_cmake = building_llvm || build.config.any_sanitizers_to_build();
150if need_cmake && cmd_finder.maybe_have("cmake").is_none() {
151eprintln!(
152"
153Couldn't find required command: cmake
154155You should install cmake, or set `download-ci-llvm = true` in the
156`[llvm]` section of `bootstrap.toml` to download LLVM rather
157than building it.
158"
159);
160crate::exit!(1);
161 }
162163build.config.python = build164 .config
165 .python
166 .take()
167 .map(|p| cmd_finder.must_have(p))
168 .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
169.or_else(|| cmd_finder.maybe_have("python"))
170 .or_else(|| cmd_finder.maybe_have("python3"))
171 .or_else(|| cmd_finder.maybe_have("python2"));
172173build.config.nodejs = build174 .config
175 .nodejs
176 .take()
177 .map(|p| cmd_finder.must_have(p))
178 .or_else(|| cmd_finder.maybe_have("node"))
179 .or_else(|| cmd_finder.maybe_have("nodejs"));
180181build.config.npm = build182 .config
183 .npm
184 .take()
185 .map(|p| cmd_finder.must_have(p))
186 .or_else(|| cmd_finder.maybe_have("npm"));
187188build.config.gdb = build189 .config
190 .gdb
191 .take()
192 .map(|p| cmd_finder.must_have(p))
193 .or_else(|| cmd_finder.maybe_have("gdb"));
194195build.config.reuse = build196 .config
197 .reuse
198 .take()
199 .map(|p| cmd_finder.must_have(p))
200 .or_else(|| cmd_finder.maybe_have("reuse"));
201202let stage0_supported_target_list: HashSet<String> = crate::utils::helpers::output(
203command(&build.config.initial_rustc).args(["--print", "target-list"]).as_command_mut(),
204 )
205 .lines()
206 .map(|s| s.to_string())
207 .collect();
208209// We're gonna build some custom C code here and there, host triples
210 // also build some C++ shims for LLVM so we need a C++ compiler.
211for target in &build.targets {
212// On emscripten we don't actually need the C compiler to just
213 // build the target artifacts, only for testing. For the sake
214 // of easier bot configuration, just skip detection.
215if target.contains("emscripten") {
216continue;
217 }
218219// We don't use a C compiler on wasm32
220if target.contains("wasm32") {
221continue;
222 }
223224// skip check for cross-targets
225if skip_target_sanity && target != &build.build {
226continue;
227 }
228229// Ignore fake targets that are only used for unit tests in bootstrap.
230if cfg!(not(test)) && !skip_target_sanity && !build.local_rebuild {
231let mut has_target = false;
232let target_str = target.to_string();
233234let missing_targets_hashset: HashSet<_> =
235 STAGE0_MISSING_TARGETS.iter().map(|t| t.to_string()).collect();
236let duplicated_targets: Vec<_> =
237 stage0_supported_target_list.intersection(&missing_targets_hashset).collect();
238239if !duplicated_targets.is_empty() {
240println!(
241"Following targets supported from the stage0 compiler, please remove them from STAGE0_MISSING_TARGETS list."
242);
243for duplicated_target in duplicated_targets {
244println!(" {duplicated_target}");
245 }
246 std::process::exit(1);
247 }
248249// Check if it's a built-in target.
250has_target |= stage0_supported_target_list.contains(&target_str);
251 has_target |= STAGE0_MISSING_TARGETS.contains(&target_str.as_str());
252253if !has_target {
254// This might also be a custom target, so check the target file that could have been specified by the user.
255if target.filepath().is_some_and(|p| p.exists()) {
256 has_target = true;
257 } else if let Some(custom_target_path) = env::var_os("RUST_TARGET_PATH") {
258let mut target_filename = OsString::from(&target_str);
259// Target filename ends with `.json`.
260target_filename.push(".json");
261262// Recursively traverse through nested directories.
263let walker = walkdir::WalkDir::new(custom_target_path).into_iter();
264for entry in walker.filter_map(|e| e.ok()) {
265 has_target |= entry.file_name() == target_filename;
266 }
267 }
268 }
269270if !has_target {
271panic!(
272"No such target exists in the target list,\n\
273 make sure to correctly specify the location \
274 of the JSON specification file \
275 for custom targets!\n\
276 Use BOOTSTRAP_SKIP_TARGET_SANITY=1 to \
277 bypass this check."
278);
279 }
280 }
281282if !build.config.dry_run() {
283 cmd_finder.must_have(build.cc(*target));
284if let Some(ar) = build.ar(*target) {
285 cmd_finder.must_have(ar);
286 }
287 }
288 }
289290if !build.config.dry_run() {
291for host in &build.hosts {
292 cmd_finder.must_have(build.cxx(*host).unwrap());
293294if build.config.llvm_enabled(*host) {
295// Externally configured LLVM requires FileCheck to exist
296let filecheck = build.llvm_filecheck(build.build);
297if !filecheck.starts_with(&build.out)
298 && !filecheck.exists()
299 && build.config.codegen_tests
300 {
301panic!("FileCheck executable {filecheck:?} does not exist");
302 }
303 }
304 }
305 }
306307for target in &build.targets {
308 build
309 .config
310 .target_config
311 .entry(*target)
312 .or_insert_with(|| Target::from_triple(&target.triple));
313314if (target.contains("-none-") || target.contains("nvptx"))
315 && build.no_std(*target) == Some(false)
316 {
317panic!("All the *-none-* and nvptx* targets are no-std targets")
318 }
319320// skip check for cross-targets
321if skip_target_sanity && target != &build.build {
322continue;
323 }
324325// Make sure musl-root is valid.
326if target.contains("musl") && !target.contains("unikraft") {
327// If this is a native target (host is also musl) and no musl-root is given,
328 // fall back to the system toolchain in /usr before giving up
329if build.musl_root(*target).is_none() && build.is_builder_target(*target) {
330let target = build.config.target_config.entry(*target).or_default();
331 target.musl_root = Some("/usr".into());
332 }
333match build.musl_libdir(*target) {
334Some(libdir) => {
335if fs::metadata(libdir.join("libc.a")).is_err() {
336panic!("couldn't find libc.a in musl libdir: {}", libdir.display());
337 }
338 }
339None => panic!(
340"when targeting MUSL either the rust.musl-root \
341 option or the target.$TARGET.musl-root option must \
342 be specified in bootstrap.toml"
343),
344 }
345 }
346347if need_cmake && target.is_msvc() {
348// There are three builds of cmake on windows: MSVC, MinGW, and
349 // Cygwin. The Cygwin build does not have generators for Visual
350 // Studio, so detect that here and error.
351let out =
352 command("cmake").arg("--help").run_always().run_capture_stdout(build).stdout();
353if !out.contains("Visual Studio") {
354panic!(
355"
356cmake does not support Visual Studio generators.
357358This is likely due to it being an msys/cygwin build of cmake,
359rather than the required windows version, built using MinGW
360or Visual Studio.
361362If you are building under msys2 try installing the mingw-w64-x86_64-cmake
363package instead of cmake:
364365$ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
366"
367);
368 }
369 }
370 }
371372if let Some(ref s) = build.config.ccache {
373cmd_finder.must_have(s);
374 }
375}