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