| //! Sanity checking and tool selection performed by bootstrap. | |
| //! | |
| //! This module ensures that the build environment is correctly set up before | |
| //! executing any build tasks. It verifies required programs exist (like git and | |
| //! cmake when needed), selects some tools based on the environment (like the | |
| //! Python interpreter), and validates that C compilers for cross-compiling are | |
| //! available. | |
| //! | |
| //! In theory if we get past this phase it's a bug if a build fails, but in | |
| //! practice that's likely not true! | |
| use std::collections::{HashMap, HashSet}; | |
| use std::ffi::{OsStr, OsString}; | |
| use std::path::PathBuf; | |
| use std::{env, fs}; | |
| use crate::core::build_steps::tool; | |
| use crate::core::builder::Builder; | |
| use crate::core::config::flags::Subcommand; | |
| use crate::core::config::{CompilerBuiltins, DebuggerPath, Target}; | |
| use crate::core::session::Session; | |
| use crate::utils::exec::command; | |
| use crate::utils::helpers::{self, t}; | |
| pub struct Finder { | |
| cache: HashMap<OsString, Option<PathBuf>>, | |
| path: OsString, | |
| } | |
| /// During sanity checks, we search for target tuples to determine if they exist in the compiler's | |
| /// built-in target list (`rustc --print target-list`). While a target tuple may be present in the | |
| /// in-tree compiler, the stage 0 compiler might not yet know about it (assuming not operating with | |
| /// local-rebuild). In such cases, we handle the targets missing from stage 0 in this list. | |
| /// | |
| /// Targets can be removed from this list during the usual release process bootstrap compiler bumps, | |
| /// when the newly-bumped stage 0 compiler now knows about the formerly-missing targets. | |
| const STAGE0_MISSING_TARGETS: &[&str] = &[ | |
| // just a dummy comment so the list doesn't get onelined | |
| ]; | |
| /// Minimum version threshold for libstdc++ required when using prebuilt LLVM | |
| /// from CI (with`llvm.download-ci-llvm` option). | |
| const LIBSTDCXX_MIN_VERSION_THRESHOLD: usize = 8; | |
| impl Finder { | |
| pub fn new() -> Self { | |
| Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() } | |
| } | |
| pub fn maybe_have<S: Into<OsString>>(&mut self, cmd: S) -> Option<PathBuf> { | |
| let cmd: OsString = cmd.into(); | |
| let path = &self.path; | |
| self.cache | |
| .entry(cmd.clone()) | |
| .or_insert_with(|| { | |
| for path in env::split_paths(path) { | |
| let target = path.join(&cmd); | |
| let mut cmd_exe = cmd.clone(); | |
| cmd_exe.push(".exe"); | |
| if target.is_file() // some/path/git | |
| || path.join(&cmd_exe).exists() // some/path/git.exe | |
| || target.join(&cmd_exe).exists() | |
| // some/path/git/git.exe | |
| { | |
| return Some(target); | |
| } | |
| } | |
| None | |
| }) | |
| .clone() | |
| } | |
| pub fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf { | |
| self.maybe_have(&cmd).unwrap_or_else(|| { | |
| panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref()); | |
| }) | |
| } | |
| } | |
| pub(crate) fn check(sess: &mut Session) { | |
| let mut skip_target_sanity = | |
| env::var_os("BOOTSTRAP_SKIP_TARGET_SANITY").is_some_and(|s| s == "1" || s == "true"); | |
| skip_target_sanity |= matches!(sess.config.cmd, Subcommand::Check { .. }); | |
| // Skip target sanity checks when we are doing anything with mir-opt tests or Miri | |
| let skipped_paths = [OsStr::new("mir-opt"), OsStr::new("miri")]; | |
| skip_target_sanity |= sess.config.paths.iter().any(|path| { | |
| path.components().any(|component| skipped_paths.contains(&component.as_os_str())) | |
| }); | |
| let path = env::var_os("PATH").unwrap_or_default(); | |
| // On Windows, quotes are invalid characters for filename paths, and if | |
| // one is present as part of the PATH then that can lead to the system | |
| // being unable to identify the files properly. See | |
| // https://github.com/rust-lang/rust/issues/34959 for more details. | |
| if cfg!(windows) && path.to_string_lossy().contains('\"') { | |
| panic!("PATH contains invalid character '\"'"); | |
| } | |
| let mut cmd_finder = Finder::new(); | |
| // If we've got a git directory we're gonna need git to update | |
| // submodules and learn about various other aspects. | |
| if sess.rust_info().is_managed_git_subrepository() { | |
| cmd_finder.must_have("git"); | |
| } | |
| // Ensure that a compatible version of libstdc++ is available on the system when using `llvm.download-ci-llvm`. | |
| if cfg!(not(test)) | |
| && !sess.config.dry_run() | |
| && !sess.host_target.is_msvc() | |
| && sess.config.llvm_ci_mode.requests_download_from_ci() | |
| { | |
| let builder = Builder::new(sess); | |
| let libcxx_version = builder.ensure(tool::LibcxxVersionTool { target: sess.host_target }); | |
| match libcxx_version { | |
| tool::LibcxxVersion::Gnu(version) => { | |
| if LIBSTDCXX_MIN_VERSION_THRESHOLD > version { | |
| eprintln!( | |
| "\nYour system's libstdc++ version is too old for the `llvm.download-ci-llvm` option." | |
| ); | |
| eprintln!("Current version detected: '{version}'"); | |
| eprintln!("Minimum required version: '{LIBSTDCXX_MIN_VERSION_THRESHOLD}'"); | |
| eprintln!( | |
| "Consider upgrading libstdc++ or disabling the `llvm.download-ci-llvm` option." | |
| ); | |
| eprintln!( | |
| "If you choose to upgrade libstdc++, run `x clean` or delete `build/host/libcxx-version` manually after the upgrade." | |
| ); | |
| } | |
| } | |
| tool::LibcxxVersion::Llvm(_) => { | |
| // FIXME: Handle libc++ version check. | |
| } | |
| } | |
| } | |
| // We need cmake, but only if we're actually building LLVM or sanitizers. | |
| let building_llvm = !sess.config.llvm_ci_mode.requests_download_from_ci() | |
| && !sess.config.local_rebuild | |
| && sess.hosts.iter().any(|host| { | |
| sess.config.llvm_enabled(*host) | |
| && sess | |
| .config | |
| .target_config | |
| .get(host) | |
| .map(|config| config.llvm_config.is_none()) | |
| .unwrap_or(true) | |
| }); | |
| let need_cmake = building_llvm || sess.config.any_sanitizers_to_build(); | |
| if need_cmake && cmd_finder.maybe_have("cmake").is_none() { | |
| eprintln!( | |
| " | |
| Couldn't find required command: cmake | |
| You should install cmake, or set `download-ci-llvm = true` in the | |
| `[llvm]` section of `bootstrap.toml` to download LLVM rather | |
| than building it. | |
| " | |
| ); | |
| helpers::exit_process(1); | |
| } | |
| sess.config.python = sess | |
| .config | |
| .python | |
| .take() | |
| .map(|p| cmd_finder.must_have(p)) | |
| .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py | |
| .or_else(|| cmd_finder.maybe_have("python")) | |
| .or_else(|| cmd_finder.maybe_have("python3")) | |
| .or_else(|| cmd_finder.maybe_have("python2")); | |
| sess.config.nodejs = sess | |
| .config | |
| .nodejs | |
| .take() | |
| .map(|p| cmd_finder.must_have(p)) | |
| .or_else(|| cmd_finder.maybe_have("node")) | |
| .or_else(|| cmd_finder.maybe_have("nodejs")); | |
| sess.config.yarn = sess | |
| .config | |
| .yarn | |
| .take() | |
| .map(|p| cmd_finder.must_have(p)) | |
| .or_else(|| cmd_finder.maybe_have("yarn")); | |
| sess.config.gdb = sess.config.gdb.take().map(|p| match p { | |
| DebuggerPath::Discover => DebuggerPath::Discover, | |
| DebuggerPath::Path(path) => DebuggerPath::Path(cmd_finder.must_have(path)), | |
| }); | |
| sess.config.reuse = sess | |
| .config | |
| .reuse | |
| .take() | |
| .map(|p| cmd_finder.must_have(p)) | |
| .or_else(|| cmd_finder.maybe_have("reuse")); | |
| let stage0_supported_target_list: HashSet<String> = command(&sess.initial_rustc) | |
| .args(["--print", "target-list"]) | |
| .run_in_dry_run() | |
| .run_capture_stdout(&sess) | |
| .stdout() | |
| .lines() | |
| .map(|s| s.to_string()) | |
| .collect(); | |
| // Compiler tools like `cc` and `ar` are not configured for cross-targets on certain subcommands | |
| // because they are not needed. | |
| // | |
| // See `cc_detect::find` for more details. | |
| let skip_tools_checks = sess.config.dry_run() | |
| || matches!( | |
| sess.config.cmd, | |
| Subcommand::Clean { .. } | |
| | Subcommand::Check { .. } | |
| | Subcommand::Format { .. } | |
| | Subcommand::Setup { .. } | |
| ); | |
| // We're gonna build some custom C code here and there, host triples | |
| // also build some C++ shims for LLVM so we need a C++ compiler. | |
| for target in &sess.targets { | |
| // On emscripten we don't actually need the C compiler to just | |
| // build the target artifacts, only for testing. For the sake | |
| // of easier bot configuration, just skip detection. | |
| if target.contains("emscripten") { | |
| continue; | |
| } | |
| // We don't use a C compiler on wasm32 | |
| if target.contains("wasm32") { | |
| continue; | |
| } | |
| if target.contains("motor") { | |
| continue; | |
| } | |
| // skip check for cross-targets | |
| if skip_target_sanity && target != &sess.host_target { | |
| continue; | |
| } | |
| // Ignore fake targets that are only used for unit tests in bootstrap. | |
| if cfg!(not(test)) && !skip_target_sanity && !sess.local_rebuild { | |
| let mut has_target = false; | |
| let target_str = target.to_string(); | |
| let missing_targets_hashset: HashSet<_> = | |
| STAGE0_MISSING_TARGETS.iter().map(|t| t.to_string()).collect(); | |
| let duplicated_targets: Vec<_> = | |
| stage0_supported_target_list.intersection(&missing_targets_hashset).collect(); | |
| if !duplicated_targets.is_empty() { | |
| println!( | |
| "Following targets supported from the stage0 compiler, please remove them from STAGE0_MISSING_TARGETS list." | |
| ); | |
| for duplicated_target in duplicated_targets { | |
| println!(" {duplicated_target}"); | |
| } | |
| std::process::exit(1); | |
| } | |
| // Check if it's a built-in target. | |
| has_target |= stage0_supported_target_list.contains(&target_str); | |
| has_target |= STAGE0_MISSING_TARGETS.contains(&target_str.as_str()); | |
| if !has_target { | |
| // This might also be a custom target, so check the target file that could have been specified by the user. | |
| if target.filepath().is_some_and(|p| p.exists()) { | |
| has_target = true; | |
| } else if let Some(custom_target_path) = env::var_os("RUST_TARGET_PATH") { | |
| let mut target_filename = OsString::from(&target_str); | |
| // Target filename ends with `.json`. | |
| target_filename.push(".json"); | |
| // Recursively traverse through nested directories. | |
| let walker = walkdir::WalkDir::new(custom_target_path).into_iter(); | |
| for entry in walker.filter_map(|e| e.ok()) { | |
| has_target |= entry.file_name() == target_filename; | |
| } | |
| } | |
| } | |
| if !has_target { | |
| panic!( | |
| "{target_str}: No such target exists in the target list,\n\ | |
| make sure to correctly specify the location \ | |
| of the JSON specification file \ | |
| for custom targets!\n\ | |
| Use BOOTSTRAP_SKIP_TARGET_SANITY=1 to \ | |
| bypass this check." | |
| ); | |
| } | |
| } | |
| if !skip_tools_checks { | |
| cmd_finder.must_have(sess.cc(*target)); | |
| if let Some(ar) = sess.ar(*target) { | |
| cmd_finder.must_have(ar); | |
| } | |
| } | |
| } | |
| if !skip_tools_checks { | |
| for host in &sess.hosts { | |
| cmd_finder.must_have(sess.cxx(*host).unwrap()); | |
| } | |
| } | |
| for target in &sess.config.targets { | |
| sess.config | |
| .target_config | |
| .entry(*target) | |
| .or_insert_with(|| Target::from_triple(&target.triple)); | |
| // compiler-rt c fallbacks for wasm cannot be built with gcc | |
| if target.contains("wasm") | |
| && (*sess.config.optimized_compiler_builtins(*target) | |
| != CompilerBuiltins::BuildRustOnly | |
| || sess.config.rust_std_features.contains("compiler-builtins-c")) | |
| { | |
| let cc_tool = sess.cc_tool(*target); | |
| if !cc_tool.is_like_clang() && !cc_tool.path().ends_with("emcc") { | |
| // emcc works as well | |
| panic!( | |
| "Clang is required to build C code for Wasm targets, got `{}` instead\n\ | |
| this is because compiler-builtins is configured to build C source. Either \ | |
| ensure Clang is used, or adjust this configuration.", | |
| cc_tool.path().display() | |
| ); | |
| } | |
| } | |
| if (target.contains("-none-") || target.contains("nvptx")) | |
| && sess.no_std(*target) == Some(false) | |
| { | |
| panic!("All the *-none-* and nvptx* targets are no-std targets") | |
| } | |
| // skip check for cross-targets | |
| if skip_target_sanity && target != &sess.host_target { | |
| continue; | |
| } | |
| // Make sure musl-root is valid. | |
| if target.contains("musl") && !target.contains("unikraft") { | |
| match sess.musl_libdir(*target) { | |
| Some(libdir) => { | |
| if fs::metadata(libdir.join("libc.a")).is_err() { | |
| panic!("couldn't find libc.a in musl libdir: {}", libdir.display()); | |
| } | |
| } | |
| None => panic!( | |
| "when targeting MUSL either the rust.musl-root \ | |
| option or the target.$TARGET.musl-root option must \ | |
| be specified in bootstrap.toml" | |
| ), | |
| } | |
| } | |
| if need_cmake && target.is_msvc() { | |
| // There are three builds of cmake on windows: MSVC, MinGW, and | |
| // Cygwin. The Cygwin build does not have generators for Visual | |
| // Studio, so detect that here and error. | |
| let out = | |
| command("cmake").arg("--help").run_in_dry_run().run_capture_stdout(&sess).stdout(); | |
| if !out.contains("Visual Studio") { | |
| panic!( | |
| " | |
| cmake does not support Visual Studio generators. | |
| This is likely due to it being an msys/cygwin build of cmake, | |
| rather than the required windows version, built using MinGW | |
| or Visual Studio. | |
| If you are building under msys2 try installing the mingw-w64-x86_64-cmake | |
| package instead of cmake: | |
| $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake | |
| " | |
| ); | |
| } | |
| } | |
| // For testing `wasm32-wasip2`-and-beyond it's required to have | |
| // `wasm-component-ld`. This is enabled by default via `tool_enabled` | |
| // but if it's disabled then double-check it's present on the system. | |
| if target.contains("wasip") | |
| && !target.contains("wasip1") | |
| && !sess.tool_enabled("wasm-component-ld") | |
| { | |
| cmd_finder.must_have("wasm-component-ld"); | |
| } | |
| // aarch64-unknown-linux-pauthtest must use clang | |
| if !skip_tools_checks && target.is_pauthtest() { | |
| let cc_tool = sess.cc_tool(*target); | |
| let linker_path = sess | |
| .linker(*target) | |
| .unwrap_or_else(|| panic!("{} requires an explicit clang linker", target.triple)); | |
| if !cc_tool.is_like_clang() { | |
| panic!( | |
| "Clang is required to build C code for {} target, got:\n\ | |
| cc tool: `{}`,\n\ | |
| linker: `{}`\n", | |
| target.triple, | |
| cc_tool.path().display(), | |
| linker_path.display(), | |
| ); | |
| } | |
| let cc_canon = t!(fs::canonicalize(cc_tool.path())); | |
| let linker_canon = t!(fs::canonicalize(&linker_path)); | |
| if cc_canon != linker_canon { | |
| panic!( | |
| "CC and Linker are expected to be the same for {} target, got:\n\ | |
| CC: `{}`,\n\ | |
| Linker: `{}`\n", | |
| target.triple, | |
| cc_canon.display(), | |
| linker_canon.display(), | |
| ); | |
| } | |
| let output = | |
| command(cc_tool.path()).arg("-dumpversion").run_capture_stdout(&sess).stdout(); | |
| let version_str = output.trim(); | |
| let mut parts = version_str.split('.').map(|s| s.parse::<u32>().unwrap_or(0)); | |
| let major = parts.next().unwrap_or(0); | |
| let minor = parts.next().unwrap_or(0); | |
| let patch = parts.next().unwrap_or(0); | |
| if (major, minor, patch) < (22, 1, 0) { | |
| panic!( | |
| "clang version too old: {} ({} target trequires >= 22.1.0), path: {}", | |
| target.triple, | |
| version_str, | |
| cc_tool.path().display() | |
| ); | |
| } | |
| } | |
| } | |
| if let Some(ref s) = sess.config.ccache { | |
| cmd_finder.must_have(s); | |
| } | |
| } |