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::config::Target;
24 cache: HashMap<OsString, Option<PathBuf>>,
30 Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
33 fn maybe_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> Option<PathBuf> {
34 let cmd: OsString = cmd.as_ref().into();
35 let path = &self.path;
39 for path in env::split_paths(path) {
40 let target = path.join(&cmd);
41 let mut cmd_exe = cmd.clone();
44 if target.is_file() // some/path/git
45 || path.join(&cmd_exe).exists() // some/path/git.exe
46 || target.join(&cmd_exe).exists()
47 // some/path/git/git.exe
57 fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
58 self.maybe_have(&cmd).unwrap_or_else(|| {
59 panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
64 pub fn check(build: &mut Build) {
65 let path = env::var_os("PATH").unwrap_or_default();
66 // On Windows, quotes are invalid characters for filename paths, and if
67 // one is present as part of the PATH then that can lead to the system
68 // being unable to identify the files properly. See
69 // https://github.com/rust-lang/rust/issues/34959 for more details.
70 if cfg!(windows) && path.to_string_lossy().contains('\"') {
71 panic!("PATH contains invalid character '\"'");
74 let mut cmd_finder = Finder::new();
75 // If we've got a git directory we're gonna need git to update
76 // submodules and learn about various other aspects.
77 if build.rust_info.is_git() {
78 cmd_finder.must_have("git");
81 // We need cmake, but only if we're actually building LLVM or sanitizers.
82 let building_llvm = build
90 .map(|config| config.llvm_config.is_none())
93 .any(|build_llvm_ourselves| build_llvm_ourselves);
94 if building_llvm || build.config.sanitizers {
95 cmd_finder.must_have("cmake");
98 // Ninja is currently only used for LLVM itself.
100 if build.config.ninja {
101 // Some Linux distros rename `ninja` to `ninja-build`.
102 // CMake can work with either binary name.
103 if cmd_finder.maybe_have("ninja-build").is_none() {
104 cmd_finder.must_have("ninja");
108 // If ninja isn't enabled but we're building for MSVC then we try
109 // doubly hard to enable it. It was realized in #43767 that the msbuild
110 // CMake generator for MSVC doesn't respect configuration options like
111 // disabling LLVM assertions, which can often be quite important!
113 // In these cases we automatically enable Ninja if we find it in the
115 if !build.config.ninja && build.config.build.contains("msvc") {
116 if cmd_finder.maybe_have("ninja").is_some() {
117 build.config.ninja = true;
121 if build.config.lldb_enabled {
122 cmd_finder.must_have("swig");
123 let out = output(Command::new("swig").arg("-version"));
124 if !out.contains("SWIG Version 3") && !out.contains("SWIG Version 4") {
125 panic!("Ensure that Swig 3.x.x or 4.x.x is installed.");
130 build.config.python = build
134 .map(|p| cmd_finder.must_have(p))
135 .or_else(|| cmd_finder.maybe_have("python2.7"))
136 .or_else(|| cmd_finder.maybe_have("python2"))
137 .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
138 .or_else(|| Some(cmd_finder.must_have("python")));
140 build.config.nodejs = build
144 .map(|p| cmd_finder.must_have(p))
145 .or_else(|| cmd_finder.maybe_have("node"))
146 .or_else(|| cmd_finder.maybe_have("nodejs"));
148 build.config.gdb = build
152 .map(|p| cmd_finder.must_have(p))
153 .or_else(|| cmd_finder.maybe_have("gdb"));
155 // We're gonna build some custom C code here and there, host triples
156 // also build some C++ shims for LLVM so we need a C++ compiler.
157 for target in &build.targets {
158 // On emscripten we don't actually need the C compiler to just
159 // build the target artifacts, only for testing. For the sake
160 // of easier bot configuration, just skip detection.
161 if target.contains("emscripten") {
165 // We don't use a C compiler on wasm32
166 if target.contains("wasm32") {
170 if !build.config.dry_run {
171 cmd_finder.must_have(build.cc(*target));
172 if let Some(ar) = build.ar(*target) {
173 cmd_finder.must_have(ar);
178 for host in &build.hosts {
179 if !build.config.dry_run {
180 cmd_finder.must_have(build.cxx(*host).unwrap());
184 // Externally configured LLVM requires FileCheck to exist
185 let filecheck = build.llvm_filecheck(build.build);
186 if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
187 panic!("FileCheck executable {:?} does not exist", filecheck);
190 for target in &build.targets {
191 // Can't compile for iOS unless we're on macOS
192 if target.contains("apple-ios") && !build.build.contains("apple-darwin") {
193 panic!("the iOS target is only supported on macOS");
196 build.config.target_config.entry(target.clone()).or_insert(Target::from_triple(target));
198 if target.contains("-none-") || target.contains("nvptx") {
199 if build.no_std(*target) == Some(false) {
200 panic!("All the *-none-* and nvptx* targets are no-std targets")
204 // Make sure musl-root is valid
205 if target.contains("musl") {
206 // If this is a native target (host is also musl) and no musl-root is given,
207 // fall back to the system toolchain in /usr before giving up
208 if build.musl_root(*target).is_none() && build.config.build == *target {
209 let target = build.config.target_config.entry(target.clone()).or_default();
210 target.musl_root = Some("/usr".into());
212 match build.musl_root(*target) {
214 if fs::metadata(root.join("lib/libc.a")).is_err() {
215 panic!("couldn't find libc.a in musl dir: {}", root.join("lib").display());
219 "when targeting MUSL either the rust.musl-root \
220 option or the target.$TARGET.musl-root option must \
221 be specified in config.toml"
226 if target.contains("msvc") {
227 // There are three builds of cmake on windows: MSVC, MinGW, and
228 // Cygwin. The Cygwin build does not have generators for Visual
229 // Studio, so detect that here and error.
230 let out = output(Command::new("cmake").arg("--help"));
231 if !out.contains("Visual Studio") {
234 cmake does not support Visual Studio generators.
236 This is likely due to it being an msys/cygwin build of cmake,
237 rather than the required windows version, built using MinGW
240 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
241 package instead of cmake:
243 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
250 if let Some(ref s) = build.config.ccache {
251 cmd_finder.must_have(s);
254 if build.config.channel == "stable" {
255 let stage0 = t!(fs::read_to_string(build.src.join("src/stage0.txt")));
256 if stage0.contains("\ndev:") {
258 "bootstrapping from a dev compiler in a stable release, but \
259 should only be bootstrapping from a released compiler!"