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