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