bootstrap/core/
sanity.rs

1//! Sanity checking and tool selection performed by bootstrap.
2//!
3//! This module ensures that the build environment is correctly set up before
4//! executing any build tasks. It verifies required programs exist (like git and
5//! cmake when needed), selects some tools based on the environment (like the
6//! Python interpreter), and validates that C compilers for cross-compiling are
7//! available.
8//!
9//! In theory if we get past this phase it's a bug if a build fails, but in
10//! practice that's likely not true!
11
12use std::collections::{HashMap, HashSet};
13use std::ffi::{OsStr, OsString};
14use std::path::PathBuf;
15use std::{env, fs};
16
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::{CompilerBuiltins, Target};
23use crate::utils::exec::command;
24use crate::{Build, Subcommand};
25
26pub struct Finder {
27    cache: HashMap<OsString, Option<PathBuf>>,
28    path: OsString,
29}
30
31/// During sanity checks, we search for target tuples to determine if they exist in the compiler's
32/// built-in target list (`rustc --print target-list`). While a target tuple may be present in the
33/// in-tree compiler, the stage 0 compiler might not yet know about it (assuming not operating with
34/// local-rebuild). In such cases, we handle the targets missing from stage 0 in this list.
35///
36/// Targets can be removed from this list during the usual release process bootstrap compiler bumps,
37/// when the newly-bumped stage 0 compiler now knows about the formerly-missing targets.
38const STAGE0_MISSING_TARGETS: &[&str] = &[
39    // just a dummy comment so the list doesn't get onelined
40    "riscv64im-unknown-none-elf",
41];
42
43/// 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;
47
48impl Finder {
49    pub fn new() -> Self {
50        Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
51    }
52
53    pub fn maybe_have<S: Into<OsString>>(&mut self, cmd: S) -> Option<PathBuf> {
54        let cmd: OsString = cmd.into();
55        let path = &self.path;
56        self.cache
57            .entry(cmd.clone())
58            .or_insert_with(|| {
59                for path in env::split_paths(path) {
60                    let target = path.join(&cmd);
61                    let mut cmd_exe = cmd.clone();
62                    cmd_exe.push(".exe");
63
64                    if 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                    {
69                        return Some(target);
70                    }
71                }
72                None
73            })
74            .clone()
75    }
76
77    pub fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
78        self.maybe_have(&cmd).unwrap_or_else(|| {
79            panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
80        })
81    }
82}
83
84pub fn check(build: &mut Build) {
85    let mut skip_target_sanity =
86        env::var_os("BOOTSTRAP_SKIP_TARGET_SANITY").is_some_and(|s| s == "1" || s == "true");
87
88    skip_target_sanity |= build.config.cmd.kind() == Kind::Check;
89
90    // Skip target sanity checks when we are doing anything with mir-opt tests or Miri
91    let 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    });
95
96    let 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.
101    if cfg!(windows) && path.to_string_lossy().contains('\"') {
102        panic!("PATH contains invalid character '\"'");
103    }
104
105    let 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.
108    if build.rust_info().is_managed_git_subrepository() {
109        cmd_finder.must_have("git");
110    }
111
112    // Ensure that a compatible version of libstdc++ is available on the system when using `llvm.download-ci-llvm`.
113    #[cfg(not(test))]
114    if !build.config.dry_run() && !build.host_target.is_msvc() && build.config.llvm_from_ci {
115        let builder = Builder::new(build);
116        let libcxx_version = builder.ensure(tool::LibcxxVersionTool { target: build.host_target });
117
118        match libcxx_version {
119            tool::LibcxxVersion::Gnu(version) => {
120                if LIBSTDCXX_MIN_VERSION_THRESHOLD > version {
121                    eprintln!(
122                        "\nYour system's libstdc++ version is too old for the `llvm.download-ci-llvm` option."
123                    );
124                    eprintln!("Current version detected: '{version}'");
125                    eprintln!("Minimum required version: '{LIBSTDCXX_MIN_VERSION_THRESHOLD}'");
126                    eprintln!(
127                        "Consider upgrading libstdc++ or disabling the `llvm.download-ci-llvm` option."
128                    );
129                    eprintln!(
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    }
139
140    // We need cmake, but only if we're actually building LLVM or sanitizers.
141    let building_llvm = !build.config.llvm_from_ci
142        && !build.config.local_rebuild
143        && build.hosts.iter().any(|host| {
144            build.config.llvm_enabled(*host)
145                && build
146                    .config
147                    .target_config
148                    .get(host)
149                    .map(|config| config.llvm_config.is_none())
150                    .unwrap_or(true)
151        });
152
153    let need_cmake = building_llvm || build.config.any_sanitizers_to_build();
154    if need_cmake && cmd_finder.maybe_have("cmake").is_none() {
155        eprintln!(
156            "
157Couldn't find required command: cmake
158
159You should install cmake, or set `download-ci-llvm = true` in the
160`[llvm]` section of `bootstrap.toml` to download LLVM rather
161than building it.
162"
163        );
164        crate::exit!(1);
165    }
166
167    build.config.python = build
168        .config
169        .python
170        .take()
171        .map(|p| cmd_finder.must_have(p))
172        .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
173        .or_else(|| cmd_finder.maybe_have("python"))
174        .or_else(|| cmd_finder.maybe_have("python3"))
175        .or_else(|| cmd_finder.maybe_have("python2"));
176
177    build.config.nodejs = build
178        .config
179        .nodejs
180        .take()
181        .map(|p| cmd_finder.must_have(p))
182        .or_else(|| cmd_finder.maybe_have("node"))
183        .or_else(|| cmd_finder.maybe_have("nodejs"));
184
185    build.config.yarn = build
186        .config
187        .yarn
188        .take()
189        .map(|p| cmd_finder.must_have(p))
190        .or_else(|| cmd_finder.maybe_have("yarn"));
191
192    build.config.gdb = build
193        .config
194        .gdb
195        .take()
196        .map(|p| cmd_finder.must_have(p))
197        .or_else(|| cmd_finder.maybe_have("gdb"));
198
199    build.config.reuse = build
200        .config
201        .reuse
202        .take()
203        .map(|p| cmd_finder.must_have(p))
204        .or_else(|| cmd_finder.maybe_have("reuse"));
205
206    let stage0_supported_target_list: HashSet<String> = command(&build.config.initial_rustc)
207        .args(["--print", "target-list"])
208        .run_in_dry_run()
209        .run_capture_stdout(&build)
210        .stdout()
211        .lines()
212        .map(|s| s.to_string())
213        .collect();
214
215    // Compiler tools like `cc` and `ar` are not configured for cross-targets on certain subcommands
216    // because they are not needed.
217    //
218    // See `cc_detect::find` for more details.
219    let skip_tools_checks = build.config.dry_run()
220        || matches!(
221            build.config.cmd,
222            Subcommand::Clean { .. }
223                | Subcommand::Check { .. }
224                | Subcommand::Format { .. }
225                | Subcommand::Setup { .. }
226        );
227
228    // We're gonna build some custom C code here and there, host triples
229    // also build some C++ shims for LLVM so we need a C++ compiler.
230    for target in &build.targets {
231        // On emscripten we don't actually need the C compiler to just
232        // build the target artifacts, only for testing. For the sake
233        // of easier bot configuration, just skip detection.
234        if target.contains("emscripten") {
235            continue;
236        }
237
238        // We don't use a C compiler on wasm32
239        if target.contains("wasm32") {
240            continue;
241        }
242
243        if target.contains("motor") {
244            continue;
245        }
246
247        // skip check for cross-targets
248        if skip_target_sanity && target != &build.host_target {
249            continue;
250        }
251
252        // Ignore fake targets that are only used for unit tests in bootstrap.
253        if cfg!(not(test)) && !skip_target_sanity && !build.local_rebuild {
254            let mut has_target = false;
255            let target_str = target.to_string();
256
257            let missing_targets_hashset: HashSet<_> =
258                STAGE0_MISSING_TARGETS.iter().map(|t| t.to_string()).collect();
259            let duplicated_targets: Vec<_> =
260                stage0_supported_target_list.intersection(&missing_targets_hashset).collect();
261
262            if !duplicated_targets.is_empty() {
263                println!(
264                    "Following targets supported from the stage0 compiler, please remove them from STAGE0_MISSING_TARGETS list."
265                );
266                for duplicated_target in duplicated_targets {
267                    println!("  {duplicated_target}");
268                }
269                std::process::exit(1);
270            }
271
272            // Check if it's a built-in target.
273            has_target |= stage0_supported_target_list.contains(&target_str);
274            has_target |= STAGE0_MISSING_TARGETS.contains(&target_str.as_str());
275
276            if !has_target {
277                // This might also be a custom target, so check the target file that could have been specified by the user.
278                if target.filepath().is_some_and(|p| p.exists()) {
279                    has_target = true;
280                } else if let Some(custom_target_path) = env::var_os("RUST_TARGET_PATH") {
281                    let mut target_filename = OsString::from(&target_str);
282                    // Target filename ends with `.json`.
283                    target_filename.push(".json");
284
285                    // Recursively traverse through nested directories.
286                    let walker = walkdir::WalkDir::new(custom_target_path).into_iter();
287                    for entry in walker.filter_map(|e| e.ok()) {
288                        has_target |= entry.file_name() == target_filename;
289                    }
290                }
291            }
292
293            if !has_target {
294                panic!(
295                    "{target_str}: No such target exists in the target list,\n\
296                     make sure to correctly specify the location \
297                     of the JSON specification file \
298                     for custom targets!\n\
299                     Use BOOTSTRAP_SKIP_TARGET_SANITY=1 to \
300                     bypass this check."
301                );
302            }
303        }
304
305        if !skip_tools_checks {
306            cmd_finder.must_have(build.cc(*target));
307            if let Some(ar) = build.ar(*target) {
308                cmd_finder.must_have(ar);
309            }
310        }
311    }
312
313    if !skip_tools_checks {
314        for host in &build.hosts {
315            cmd_finder.must_have(build.cxx(*host).unwrap());
316
317            if build.config.llvm_enabled(*host) {
318                // Externally configured LLVM requires FileCheck to exist
319                let filecheck = build.llvm_filecheck(build.host_target);
320                if !filecheck.starts_with(&build.out)
321                    && !filecheck.exists()
322                    && build.config.codegen_tests
323                {
324                    panic!("FileCheck executable {filecheck:?} does not exist");
325                }
326            }
327        }
328    }
329
330    for target in &build.targets {
331        build
332            .config
333            .target_config
334            .entry(*target)
335            .or_insert_with(|| Target::from_triple(&target.triple));
336
337        // compiler-rt c fallbacks for wasm cannot be built with gcc
338        if target.contains("wasm")
339            && (*build.config.optimized_compiler_builtins(*target)
340                != CompilerBuiltins::BuildRustOnly
341                || build.config.rust_std_features.contains("compiler-builtins-c"))
342        {
343            let cc_tool = build.cc_tool(*target);
344            if !cc_tool.is_like_clang() && !cc_tool.path().ends_with("emcc") {
345                // emcc works as well
346                panic!(
347                    "Clang is required to build C code for Wasm targets, got `{}` instead\n\
348                    this is because compiler-builtins is configured to build C source. Either \
349                    ensure Clang is used, or adjust this configuration.",
350                    cc_tool.path().display()
351                );
352            }
353        }
354
355        if (target.contains("-none-") || target.contains("nvptx"))
356            && build.no_std(*target) == Some(false)
357        {
358            panic!("All the *-none-* and nvptx* targets are no-std targets")
359        }
360
361        // skip check for cross-targets
362        if skip_target_sanity && target != &build.host_target {
363            continue;
364        }
365
366        // Make sure musl-root is valid.
367        if target.contains("musl") && !target.contains("unikraft") {
368            match build.musl_libdir(*target) {
369                Some(libdir) => {
370                    if fs::metadata(libdir.join("libc.a")).is_err() {
371                        panic!("couldn't find libc.a in musl libdir: {}", libdir.display());
372                    }
373                }
374                None => panic!(
375                    "when targeting MUSL either the rust.musl-root \
376                            option or the target.$TARGET.musl-root option must \
377                            be specified in bootstrap.toml"
378                ),
379            }
380        }
381
382        if need_cmake && target.is_msvc() {
383            // There are three builds of cmake on windows: MSVC, MinGW, and
384            // Cygwin. The Cygwin build does not have generators for Visual
385            // Studio, so detect that here and error.
386            let out =
387                command("cmake").arg("--help").run_in_dry_run().run_capture_stdout(&build).stdout();
388            if !out.contains("Visual Studio") {
389                panic!(
390                    "
391cmake does not support Visual Studio generators.
392
393This is likely due to it being an msys/cygwin build of cmake,
394rather than the required windows version, built using MinGW
395or Visual Studio.
396
397If you are building under msys2 try installing the mingw-w64-x86_64-cmake
398package instead of cmake:
399
400$ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
401"
402                );
403            }
404        }
405
406        // For testing `wasm32-wasip2`-and-beyond it's required to have
407        // `wasm-component-ld`. This is enabled by default via `tool_enabled`
408        // but if it's disabled then double-check it's present on the system.
409        if target.contains("wasip")
410            && !target.contains("wasip1")
411            && !build.tool_enabled("wasm-component-ld")
412        {
413            cmd_finder.must_have("wasm-component-ld");
414        }
415    }
416
417    if let Some(ref s) = build.config.ccache {
418        cmd_finder.must_have(s);
419    }
420}