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"aarch64-unknown-nto-qnx710_iosock",
38"x86_64-pc-nto-qnx710_iosock",
39"x86_64-pc-nto-qnx800",
40"aarch64-unknown-nto-qnx800",
41];
4243/// Minimum version threshold for libstdc++ required when using prebuilt LLVM
44/// from CI (with`llvm.download-ci-llvm` option).
45#[cfg(not(test))]
46const LIBSTDCXX_MIN_VERSION_THRESHOLD: usize = 8;
4748impl Finder {
49pub fn new() -> Self {
50Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
51 }
5253pub fn maybe_have<S: Into<OsString>>(&mut self, cmd: S) -> Option<PathBuf> {
54let cmd: OsString = cmd.into();
55let path = &self.path;
56self.cache
57 .entry(cmd.clone())
58 .or_insert_with(|| {
59for path in env::split_paths(path) {
60let target = path.join(&cmd);
61let mut cmd_exe = cmd.clone();
62 cmd_exe.push(".exe");
6364if target.is_file() // some/path/git
65|| path.join(&cmd_exe).exists() // some/path/git.exe
66|| target.join(&cmd_exe).exists()
67// some/path/git/git.exe
68{
69return Some(target);
70 }
71 }
72None
73})
74 .clone()
75 }
7677pub fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
78self.maybe_have(&cmd).unwrap_or_else(|| {
79panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
80 })
81 }
82}
8384pub fn check(build: &mut Build) {
85let mut skip_target_sanity =
86 env::var_os("BOOTSTRAP_SKIP_TARGET_SANITY").is_some_and(|s| s == "1" || s == "true");
8788 skip_target_sanity |= build.config.cmd.kind() == Kind::Check;
8990// Skip target sanity checks when we are doing anything with mir-opt tests or Miri
91let skipped_paths = [OsStr::new("mir-opt"), OsStr::new("miri")];
92 skip_target_sanity |= build.config.paths.iter().any(|path| {
93 path.components().any(|component| skipped_paths.contains(&component.as_os_str()))
94 });
9596let path = env::var_os("PATH").unwrap_or_default();
97// On Windows, quotes are invalid characters for filename paths, and if
98 // one is present as part of the PATH then that can lead to the system
99 // being unable to identify the files properly. See
100 // https://github.com/rust-lang/rust/issues/34959 for more details.
101if cfg!(windows) && path.to_string_lossy().contains('\"') {
102panic!("PATH contains invalid character '\"'");
103 }
104105let mut cmd_finder = Finder::new();
106// If we've got a git directory we're gonna need git to update
107 // submodules and learn about various other aspects.
108if build.rust_info().is_managed_git_subrepository() {
109 cmd_finder.must_have("git");
110 }
111112// Ensure that a compatible version of libstdc++ is available on the system when using `llvm.download-ci-llvm`.
113#[cfg(not(test))]
114if !build.config.dry_run() && !build.build.is_msvc() && build.config.llvm_from_ci {
115let builder = Builder::new(build);
116let libcxx_version = builder.ensure(tool::LibcxxVersionTool { target: build.build });
117118match libcxx_version {
119 tool::LibcxxVersion::Gnu(version) => {
120if LIBSTDCXX_MIN_VERSION_THRESHOLD > version {
121eprintln!(
122"\nYour system's libstdc++ version is too old for the `llvm.download-ci-llvm` option."
123);
124eprintln!("Current version detected: '{}'", version);
125eprintln!("Minimum required version: '{}'", LIBSTDCXX_MIN_VERSION_THRESHOLD);
126eprintln!(
127"Consider upgrading libstdc++ or disabling the `llvm.download-ci-llvm` option."
128);
129eprintln!(
130"If you choose to upgrade libstdc++, run `x clean` or delete `build/host/libcxx-version` manually after the upgrade."
131);
132 }
133 }
134 tool::LibcxxVersion::Llvm(_) => {
135// FIXME: Handle libc++ version check.
136}
137 }
138 }
139140// We need cmake, but only if we're actually building LLVM or sanitizers.
141let building_llvm = !build.config.llvm_from_ci
142 && build.hosts.iter().any(|host| {
143 build.config.llvm_enabled(*host)
144 && build
145 .config
146 .target_config
147 .get(host)
148 .map(|config| config.llvm_config.is_none())
149 .unwrap_or(true)
150 });
151152let need_cmake = building_llvm || build.config.any_sanitizers_to_build();
153if need_cmake && cmd_finder.maybe_have("cmake").is_none() {
154eprintln!(
155"
156Couldn't find required command: cmake
157158You should install cmake, or set `download-ci-llvm = true` in the
159`[llvm]` section of `config.toml` to download LLVM rather
160than building it.
161"
162);
163crate::exit!(1);
164 }
165166 build.config.python = build
167 .config
168 .python
169 .take()
170 .map(|p| cmd_finder.must_have(p))
171 .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
172.or_else(|| cmd_finder.maybe_have("python"))
173 .or_else(|| cmd_finder.maybe_have("python3"))
174 .or_else(|| cmd_finder.maybe_have("python2"));
175176 build.config.nodejs = build
177 .config
178 .nodejs
179 .take()
180 .map(|p| cmd_finder.must_have(p))
181 .or_else(|| cmd_finder.maybe_have("node"))
182 .or_else(|| cmd_finder.maybe_have("nodejs"));
183184 build.config.npm = build
185 .config
186 .npm
187 .take()
188 .map(|p| cmd_finder.must_have(p))
189 .or_else(|| cmd_finder.maybe_have("npm"));
190191 build.config.gdb = build
192 .config
193 .gdb
194 .take()
195 .map(|p| cmd_finder.must_have(p))
196 .or_else(|| cmd_finder.maybe_have("gdb"));
197198 build.config.reuse = build
199 .config
200 .reuse
201 .take()
202 .map(|p| cmd_finder.must_have(p))
203 .or_else(|| cmd_finder.maybe_have("reuse"));
204205let stage0_supported_target_list: HashSet<String> = crate::utils::helpers::output(
206 command(&build.config.initial_rustc).args(["--print", "target-list"]).as_command_mut(),
207 )
208 .lines()
209 .map(|s| s.to_string())
210 .collect();
211212// We're gonna build some custom C code here and there, host triples
213 // also build some C++ shims for LLVM so we need a C++ compiler.
214for target in &build.targets {
215// On emscripten we don't actually need the C compiler to just
216 // build the target artifacts, only for testing. For the sake
217 // of easier bot configuration, just skip detection.
218if target.contains("emscripten") {
219continue;
220 }
221222// We don't use a C compiler on wasm32
223if target.contains("wasm32") {
224continue;
225 }
226227// skip check for cross-targets
228if skip_target_sanity && target != &build.build {
229continue;
230 }
231232// Ignore fake targets that are only used for unit tests in bootstrap.
233if cfg!(not(test)) && !skip_target_sanity && !build.local_rebuild {
234let mut has_target = false;
235let target_str = target.to_string();
236237let missing_targets_hashset: HashSet<_> =
238 STAGE0_MISSING_TARGETS.iter().map(|t| t.to_string()).collect();
239let duplicated_targets: Vec<_> =
240 stage0_supported_target_list.intersection(&missing_targets_hashset).collect();
241242if !duplicated_targets.is_empty() {
243println!(
244"Following targets supported from the stage0 compiler, please remove them from STAGE0_MISSING_TARGETS list."
245);
246for duplicated_target in duplicated_targets {
247println!(" {duplicated_target}");
248 }
249 std::process::exit(1);
250 }
251252// Check if it's a built-in target.
253has_target |= stage0_supported_target_list.contains(&target_str);
254 has_target |= STAGE0_MISSING_TARGETS.contains(&target_str.as_str());
255256if !has_target {
257// This might also be a custom target, so check the target file that could have been specified by the user.
258if target.filepath().is_some_and(|p| p.exists()) {
259 has_target = true;
260 } else if let Some(custom_target_path) = env::var_os("RUST_TARGET_PATH") {
261let mut target_filename = OsString::from(&target_str);
262// Target filename ends with `.json`.
263target_filename.push(".json");
264265// Recursively traverse through nested directories.
266let walker = walkdir::WalkDir::new(custom_target_path).into_iter();
267for entry in walker.filter_map(|e| e.ok()) {
268 has_target |= entry.file_name() == target_filename;
269 }
270 }
271 }
272273if !has_target {
274panic!(
275"No such target exists in the target list,\n\
276 make sure to correctly specify the location \
277 of the JSON specification file \
278 for custom targets!\n\
279 Use BOOTSTRAP_SKIP_TARGET_SANITY=1 to \
280 bypass this check."
281);
282 }
283 }
284285if !build.config.dry_run() {
286 cmd_finder.must_have(build.cc(*target));
287if let Some(ar) = build.ar(*target) {
288 cmd_finder.must_have(ar);
289 }
290 }
291 }
292293if !build.config.dry_run() {
294for host in &build.hosts {
295 cmd_finder.must_have(build.cxx(*host).unwrap());
296297if build.config.llvm_enabled(*host) {
298// Externally configured LLVM requires FileCheck to exist
299let filecheck = build.llvm_filecheck(build.build);
300if !filecheck.starts_with(&build.out)
301 && !filecheck.exists()
302 && build.config.codegen_tests
303 {
304panic!("FileCheck executable {filecheck:?} does not exist");
305 }
306 }
307 }
308 }
309310for target in &build.targets {
311 build
312 .config
313 .target_config
314 .entry(*target)
315 .or_insert_with(|| Target::from_triple(&target.triple));
316317if (target.contains("-none-") || target.contains("nvptx"))
318 && build.no_std(*target) == Some(false)
319 {
320panic!("All the *-none-* and nvptx* targets are no-std targets")
321 }
322323// skip check for cross-targets
324if skip_target_sanity && target != &build.build {
325continue;
326 }
327328// Make sure musl-root is valid.
329if target.contains("musl") && !target.contains("unikraft") {
330// If this is a native target (host is also musl) and no musl-root is given,
331 // fall back to the system toolchain in /usr before giving up
332if build.musl_root(*target).is_none() && build.is_builder_target(target) {
333let target = build.config.target_config.entry(*target).or_default();
334 target.musl_root = Some("/usr".into());
335 }
336match build.musl_libdir(*target) {
337Some(libdir) => {
338if fs::metadata(libdir.join("libc.a")).is_err() {
339panic!("couldn't find libc.a in musl libdir: {}", libdir.display());
340 }
341 }
342None => panic!(
343"when targeting MUSL either the rust.musl-root \
344 option or the target.$TARGET.musl-root option must \
345 be specified in config.toml"
346),
347 }
348 }
349350if need_cmake && target.is_msvc() {
351// There are three builds of cmake on windows: MSVC, MinGW, and
352 // Cygwin. The Cygwin build does not have generators for Visual
353 // Studio, so detect that here and error.
354let out =
355 command("cmake").arg("--help").run_always().run_capture_stdout(build).stdout();
356if !out.contains("Visual Studio") {
357panic!(
358"
359cmake does not support Visual Studio generators.
360361This is likely due to it being an msys/cygwin build of cmake,
362rather than the required windows version, built using MinGW
363or Visual Studio.
364365If you are building under msys2 try installing the mingw-w64-x86_64-cmake
366package instead of cmake:
367368$ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
369"
370);
371 }
372 }
373 }
374375if let Some(ref s) = build.config.ccache {
376 cmd_finder.must_have(s);
377 }
378}