1 //! Sanity checking performed by rustbuild before actually executing anything.
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.
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!
11 use std::collections::HashMap;
13 use std::ffi::{OsStr, OsString};
15 use std::path::PathBuf;
16 use std::process::Command;
18 use build_helper::{output, t};
20 use crate::cache::INTERNER;
21 use crate::config::Target;
25 cache: HashMap<OsString, Option<PathBuf>>,
30 pub fn new() -> Self {
31 Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
34 pub fn maybe_have<S: Into<OsString>>(&mut self, cmd: S) -> Option<PathBuf> {
35 let cmd: OsString = cmd.into();
36 let path = &self.path;
40 for path in env::split_paths(path) {
41 let target = path.join(&cmd);
42 let mut cmd_exe = cmd.clone();
45 if target.is_file() // some/path/git
46 || path.join(&cmd_exe).exists() // some/path/git.exe
47 || target.join(&cmd_exe).exists()
48 // some/path/git/git.exe
58 pub fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
59 self.maybe_have(&cmd).unwrap_or_else(|| {
60 panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
65 pub fn check(build: &mut Build) {
66 let path = env::var_os("PATH").unwrap_or_default();
67 // On Windows, quotes are invalid characters for filename paths, and if
68 // one is present as part of the PATH then that can lead to the system
69 // being unable to identify the files properly. See
70 // https://github.com/rust-lang/rust/issues/34959 for more details.
71 if cfg!(windows) && path.to_string_lossy().contains('\"') {
72 panic!("PATH contains invalid character '\"'");
75 let mut cmd_finder = Finder::new();
76 // If we've got a git directory we're gonna need git to update
77 // submodules and learn about various other aspects.
78 if build.rust_info.is_git() {
79 cmd_finder.must_have("git");
82 // We need cmake, but only if we're actually building LLVM or sanitizers.
83 let building_llvm = build.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm"))
92 .map(|config| config.llvm_config.is_none())
95 .any(|build_llvm_ourselves| build_llvm_ourselves);
96 if building_llvm || build.config.any_sanitizers_enabled() {
97 cmd_finder.must_have("cmake");
100 build.config.python = build
104 .map(|p| cmd_finder.must_have(p))
105 .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
106 .or_else(|| Some(cmd_finder.must_have("python")));
108 build.config.nodejs = build
112 .map(|p| cmd_finder.must_have(p))
113 .or_else(|| cmd_finder.maybe_have("node"))
114 .or_else(|| cmd_finder.maybe_have("nodejs"));
116 build.config.gdb = build
120 .map(|p| cmd_finder.must_have(p))
121 .or_else(|| cmd_finder.maybe_have("gdb"));
123 // We're gonna build some custom C code here and there, host triples
124 // also build some C++ shims for LLVM so we need a C++ compiler.
125 for target in &build.targets {
126 // On emscripten we don't actually need the C compiler to just
127 // build the target artifacts, only for testing. For the sake
128 // of easier bot configuration, just skip detection.
129 if target.contains("emscripten") {
133 // We don't use a C compiler on wasm32
134 if target.contains("wasm32") {
138 if !build.config.dry_run {
139 cmd_finder.must_have(build.cc(*target));
140 if let Some(ar) = build.ar(*target) {
141 cmd_finder.must_have(ar);
146 for host in &build.hosts {
147 if !build.config.dry_run {
148 cmd_finder.must_have(build.cxx(*host).unwrap());
152 if build.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm")) {
153 // Externally configured LLVM requires FileCheck to exist
154 let filecheck = build.llvm_filecheck(build.build);
155 if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
156 panic!("FileCheck executable {:?} does not exist", filecheck);
160 for target in &build.targets {
161 // Can't compile for iOS unless we're on macOS
162 if target.contains("apple-ios") && !build.build.contains("apple-darwin") {
163 panic!("the iOS target is only supported on macOS");
166 build.config.target_config.entry(*target).or_insert(Target::from_triple(&target.triple));
168 if target.contains("-none-") || target.contains("nvptx") {
169 if build.no_std(*target) == Some(false) {
170 panic!("All the *-none-* and nvptx* targets are no-std targets")
174 // Make sure musl-root is valid
175 if target.contains("musl") {
176 // If this is a native target (host is also musl) and no musl-root is given,
177 // fall back to the system toolchain in /usr before giving up
178 if build.musl_root(*target).is_none() && build.config.build == *target {
179 let target = build.config.target_config.entry(*target).or_default();
180 target.musl_root = Some("/usr".into());
182 match build.musl_libdir(*target) {
184 if fs::metadata(libdir.join("libc.a")).is_err() {
185 panic!("couldn't find libc.a in musl libdir: {}", libdir.display());
189 "when targeting MUSL either the rust.musl-root \
190 option or the target.$TARGET.musl-root option must \
191 be specified in config.toml"
196 if target.contains("msvc") {
197 // There are three builds of cmake on windows: MSVC, MinGW, and
198 // Cygwin. The Cygwin build does not have generators for Visual
199 // Studio, so detect that here and error.
200 let out = output(Command::new("cmake").arg("--help"));
201 if !out.contains("Visual Studio") {
204 cmake does not support Visual Studio generators.
206 This is likely due to it being an msys/cygwin build of cmake,
207 rather than the required windows version, built using MinGW
210 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
211 package instead of cmake:
213 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
220 if let Some(ref s) = build.config.ccache {
221 cmd_finder.must_have(s);
224 if build.config.channel == "stable" {
225 let stage0 = t!(fs::read_to_string(build.src.join("src/stage0.txt")));
226 if stage0.contains("\ndev:") {
228 "bootstrapping from a dev compiler in a stable release, but \
229 should only be bootstrapping from a released compiler!"