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