bootstrap/core/
sanity.rs

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!
10
11use std::collections::{HashMap, HashSet};
12use std::ffi::{OsStr, OsString};
13use std::path::PathBuf;
14use std::{env, fs};
15
16#[cfg(not(test))]
17use crate::builder::Builder;
18use crate::builder::Kind;
19#[cfg(not(test))]
20use crate::core::build_steps::tool;
21use crate::core::config::Target;
22use crate::utils::exec::command;
23use crate::{Build, Subcommand};
24
25pub struct Finder {
26    cache: HashMap<OsString, Option<PathBuf>>,
27    path: OsString,
28}
29
30// 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    "armv7a-vex-v5",
37    // just a dummy comment so the list doesn't get onelined
38    "aarch64_be-unknown-none-softfloat",
39];
40
41/// Minimum version threshold for libstdc++ required when using prebuilt LLVM
42/// from CI (with`llvm.download-ci-llvm` option).
43#[cfg(not(test))]
44const LIBSTDCXX_MIN_VERSION_THRESHOLD: usize = 8;
45
46impl Finder {
47    pub fn new() -> Self {
48        Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
49    }
50
51    pub fn maybe_have<S: Into<OsString>>(&mut self, cmd: S) -> Option<PathBuf> {
52        let cmd: OsString = cmd.into();
53        let path = &self.path;
54        self.cache
55            .entry(cmd.clone())
56            .or_insert_with(|| {
57                for path in env::split_paths(path) {
58                    let target = path.join(&cmd);
59                    let mut cmd_exe = cmd.clone();
60                    cmd_exe.push(".exe");
61
62                    if target.is_file()                   // some/path/git
63                    || path.join(&cmd_exe).exists()   // some/path/git.exe
64                    || target.join(&cmd_exe).exists()
65                    // some/path/git/git.exe
66                    {
67                        return Some(target);
68                    }
69                }
70                None
71            })
72            .clone()
73    }
74
75    pub fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
76        self.maybe_have(&cmd).unwrap_or_else(|| {
77            panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
78        })
79    }
80}
81
82pub fn check(build: &mut Build) {
83    let mut skip_target_sanity =
84        env::var_os("BOOTSTRAP_SKIP_TARGET_SANITY").is_some_and(|s| s == "1" || s == "true");
85
86    skip_target_sanity |= build.config.cmd.kind() == Kind::Check;
87
88    // Skip target sanity checks when we are doing anything with mir-opt tests or Miri
89    let skipped_paths = [OsStr::new("mir-opt"), OsStr::new("miri")];
90    skip_target_sanity |= build.config.paths.iter().any(|path| {
91        path.components().any(|component| skipped_paths.contains(&component.as_os_str()))
92    });
93
94    let path = env::var_os("PATH").unwrap_or_default();
95    // On Windows, quotes are invalid characters for filename paths, and if
96    // one is present as part of the PATH then that can lead to the system
97    // being unable to identify the files properly. See
98    // https://github.com/rust-lang/rust/issues/34959 for more details.
99    if cfg!(windows) && path.to_string_lossy().contains('\"') {
100        panic!("PATH contains invalid character '\"'");
101    }
102
103    let mut cmd_finder = Finder::new();
104    // If we've got a git directory we're gonna need git to update
105    // submodules and learn about various other aspects.
106    if build.rust_info().is_managed_git_subrepository() {
107        cmd_finder.must_have("git");
108    }
109
110    // Ensure that a compatible version of libstdc++ is available on the system when using `llvm.download-ci-llvm`.
111    #[cfg(not(test))]
112    if !build.config.dry_run() && !build.host_target.is_msvc() && build.config.llvm_from_ci {
113        let builder = Builder::new(build);
114        let libcxx_version = builder.ensure(tool::LibcxxVersionTool { target: build.host_target });
115
116        match libcxx_version {
117            tool::LibcxxVersion::Gnu(version) => {
118                if LIBSTDCXX_MIN_VERSION_THRESHOLD > version {
119                    eprintln!(
120                        "\nYour system's libstdc++ version is too old for the `llvm.download-ci-llvm` option."
121                    );
122                    eprintln!("Current version detected: '{version}'");
123                    eprintln!("Minimum required version: '{LIBSTDCXX_MIN_VERSION_THRESHOLD}'");
124                    eprintln!(
125                        "Consider upgrading libstdc++ or disabling the `llvm.download-ci-llvm` option."
126                    );
127                    eprintln!(
128                        "If you choose to upgrade libstdc++, run `x clean` or delete `build/host/libcxx-version` manually after the upgrade."
129                    );
130                }
131            }
132            tool::LibcxxVersion::Llvm(_) => {
133                // FIXME: Handle libc++ version check.
134            }
135        }
136    }
137
138    // We need cmake, but only if we're actually building LLVM or sanitizers.
139    let building_llvm = !build.config.llvm_from_ci
140        && build.hosts.iter().any(|host| {
141            build.config.llvm_enabled(*host)
142                && build
143                    .config
144                    .target_config
145                    .get(host)
146                    .map(|config| config.llvm_config.is_none())
147                    .unwrap_or(true)
148        });
149
150    let need_cmake = building_llvm || build.config.any_sanitizers_to_build();
151    if need_cmake && cmd_finder.maybe_have("cmake").is_none() {
152        eprintln!(
153            "
154Couldn't find required command: cmake
155
156You should install cmake, or set `download-ci-llvm = true` in the
157`[llvm]` section of `bootstrap.toml` to download LLVM rather
158than building it.
159"
160        );
161        crate::exit!(1);
162    }
163
164    build.config.python = build
165        .config
166        .python
167        .take()
168        .map(|p| cmd_finder.must_have(p))
169        .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
170        .or_else(|| cmd_finder.maybe_have("python"))
171        .or_else(|| cmd_finder.maybe_have("python3"))
172        .or_else(|| cmd_finder.maybe_have("python2"));
173
174    build.config.nodejs = build
175        .config
176        .nodejs
177        .take()
178        .map(|p| cmd_finder.must_have(p))
179        .or_else(|| cmd_finder.maybe_have("node"))
180        .or_else(|| cmd_finder.maybe_have("nodejs"));
181
182    build.config.npm = build
183        .config
184        .npm
185        .take()
186        .map(|p| cmd_finder.must_have(p))
187        .or_else(|| cmd_finder.maybe_have("npm"));
188
189    build.config.gdb = build
190        .config
191        .gdb
192        .take()
193        .map(|p| cmd_finder.must_have(p))
194        .or_else(|| cmd_finder.maybe_have("gdb"));
195
196    build.config.reuse = build
197        .config
198        .reuse
199        .take()
200        .map(|p| cmd_finder.must_have(p))
201        .or_else(|| cmd_finder.maybe_have("reuse"));
202
203    let stage0_supported_target_list: HashSet<String> = command(&build.config.initial_rustc)
204        .args(["--print", "target-list"])
205        .run_in_dry_run()
206        .run_capture_stdout(&build)
207        .stdout()
208        .lines()
209        .map(|s| s.to_string())
210        .collect();
211
212    // Compiler tools like `cc` and `ar` are not configured for cross-targets on certain subcommands
213    // because they are not needed.
214    //
215    // See `cc_detect::find` for more details.
216    let skip_tools_checks = build.config.dry_run()
217        || matches!(
218            build.config.cmd,
219            Subcommand::Clean { .. }
220                | Subcommand::Check { .. }
221                | Subcommand::Format { .. }
222                | Subcommand::Setup { .. }
223        );
224
225    // We're gonna build some custom C code here and there, host triples
226    // also build some C++ shims for LLVM so we need a C++ compiler.
227    for target in &build.targets {
228        // On emscripten we don't actually need the C compiler to just
229        // build the target artifacts, only for testing. For the sake
230        // of easier bot configuration, just skip detection.
231        if target.contains("emscripten") {
232            continue;
233        }
234
235        // We don't use a C compiler on wasm32
236        if target.contains("wasm32") {
237            continue;
238        }
239
240        // skip check for cross-targets
241        if skip_target_sanity && target != &build.host_target {
242            continue;
243        }
244
245        // Ignore fake targets that are only used for unit tests in bootstrap.
246        if cfg!(not(test)) && !skip_target_sanity && !build.local_rebuild {
247            let mut has_target = false;
248            let target_str = target.to_string();
249
250            let missing_targets_hashset: HashSet<_> =
251                STAGE0_MISSING_TARGETS.iter().map(|t| t.to_string()).collect();
252            let duplicated_targets: Vec<_> =
253                stage0_supported_target_list.intersection(&missing_targets_hashset).collect();
254
255            if !duplicated_targets.is_empty() {
256                println!(
257                    "Following targets supported from the stage0 compiler, please remove them from STAGE0_MISSING_TARGETS list."
258                );
259                for duplicated_target in duplicated_targets {
260                    println!("  {duplicated_target}");
261                }
262                std::process::exit(1);
263            }
264
265            // Check if it's a built-in target.
266            has_target |= stage0_supported_target_list.contains(&target_str);
267            has_target |= STAGE0_MISSING_TARGETS.contains(&target_str.as_str());
268
269            if !has_target {
270                // This might also be a custom target, so check the target file that could have been specified by the user.
271                if target.filepath().is_some_and(|p| p.exists()) {
272                    has_target = true;
273                } else if let Some(custom_target_path) = env::var_os("RUST_TARGET_PATH") {
274                    let mut target_filename = OsString::from(&target_str);
275                    // Target filename ends with `.json`.
276                    target_filename.push(".json");
277
278                    // Recursively traverse through nested directories.
279                    let walker = walkdir::WalkDir::new(custom_target_path).into_iter();
280                    for entry in walker.filter_map(|e| e.ok()) {
281                        has_target |= entry.file_name() == target_filename;
282                    }
283                }
284            }
285
286            if !has_target {
287                panic!(
288                    "No such target exists in the target list,\n\
289                     make sure to correctly specify the location \
290                     of the JSON specification file \
291                     for custom targets!\n\
292                     Use BOOTSTRAP_SKIP_TARGET_SANITY=1 to \
293                     bypass this check."
294                );
295            }
296        }
297
298        if !skip_tools_checks {
299            cmd_finder.must_have(build.cc(*target));
300            if let Some(ar) = build.ar(*target) {
301                cmd_finder.must_have(ar);
302            }
303        }
304    }
305
306    if !skip_tools_checks {
307        for host in &build.hosts {
308            cmd_finder.must_have(build.cxx(*host).unwrap());
309
310            if build.config.llvm_enabled(*host) {
311                // Externally configured LLVM requires FileCheck to exist
312                let filecheck = build.llvm_filecheck(build.host_target);
313                if !filecheck.starts_with(&build.out)
314                    && !filecheck.exists()
315                    && build.config.codegen_tests
316                {
317                    panic!("FileCheck executable {filecheck:?} does not exist");
318                }
319            }
320        }
321    }
322
323    for target in &build.targets {
324        build
325            .config
326            .target_config
327            .entry(*target)
328            .or_insert_with(|| Target::from_triple(&target.triple));
329
330        if (target.contains("-none-") || target.contains("nvptx"))
331            && build.no_std(*target) == Some(false)
332        {
333            panic!("All the *-none-* and nvptx* targets are no-std targets")
334        }
335
336        // skip check for cross-targets
337        if skip_target_sanity && target != &build.host_target {
338            continue;
339        }
340
341        // Make sure musl-root is valid.
342        if target.contains("musl") && !target.contains("unikraft") {
343            match build.musl_libdir(*target) {
344                Some(libdir) => {
345                    if fs::metadata(libdir.join("libc.a")).is_err() {
346                        panic!("couldn't find libc.a in musl libdir: {}", libdir.display());
347                    }
348                }
349                None => panic!(
350                    "when targeting MUSL either the rust.musl-root \
351                            option or the target.$TARGET.musl-root option must \
352                            be specified in bootstrap.toml"
353                ),
354            }
355        }
356
357        if need_cmake && target.is_msvc() {
358            // There are three builds of cmake on windows: MSVC, MinGW, and
359            // Cygwin. The Cygwin build does not have generators for Visual
360            // Studio, so detect that here and error.
361            let out =
362                command("cmake").arg("--help").run_in_dry_run().run_capture_stdout(&build).stdout();
363            if !out.contains("Visual Studio") {
364                panic!(
365                    "
366cmake does not support Visual Studio generators.
367
368This is likely due to it being an msys/cygwin build of cmake,
369rather than the required windows version, built using MinGW
370or Visual Studio.
371
372If you are building under msys2 try installing the mingw-w64-x86_64-cmake
373package instead of cmake:
374
375$ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
376"
377                );
378            }
379        }
380    }
381
382    if let Some(ref s) = build.config.ccache {
383        cmd_finder.must_have(s);
384    }
385}