1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Sanity checking performed by rustbuild before actually executing anything.
13 //! This module contains the implementation of ensuring that the build
14 //! environment looks reasonable before progressing. This will verify that
15 //! various programs like git and python exist, along with ensuring that all C
16 //! compilers for cross-compiling are found.
18 //! In theory if we get past this phase it's a bug if a build fails, but in
19 //! practice that's likely not true!
21 use std::collections::HashMap;
23 use std::ffi::{OsString, OsStr};
24 use std::fs::{self, File};
26 use std::path::PathBuf;
27 use std::process::Command;
29 use build_helper::output;
34 cache: HashMap<OsString, Option<PathBuf>>,
41 cache: HashMap::new(),
42 path: env::var_os("PATH").unwrap_or_default()
46 fn maybe_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> Option<PathBuf> {
47 let cmd: OsString = cmd.as_ref().into();
48 let path = self.path.clone();
49 self.cache.entry(cmd.clone()).or_insert_with(|| {
50 for path in env::split_paths(&path) {
51 let target = path.join(&cmd);
52 let mut cmd_alt = cmd.clone();
54 if target.is_file() || // some/path/git
55 target.with_extension("exe").exists() || // some/path/git.exe
56 target.join(&cmd_alt).exists() { // some/path/git/git.exe
64 fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
65 self.maybe_have(&cmd).unwrap_or_else(|| {
66 panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
71 pub fn check(build: &mut Build) {
72 let path = env::var_os("PATH").unwrap_or_default();
73 // On Windows, quotes are invalid characters for filename paths, and if
74 // one is present as part of the PATH then that can lead to the system
75 // being unable to identify the files properly. See
76 // https://github.com/rust-lang/rust/issues/34959 for more details.
77 if cfg!(windows) && path.to_string_lossy().contains('\"') {
78 panic!("PATH contains invalid character '\"'");
81 let mut cmd_finder = Finder::new();
82 // If we've got a git directory we're gonna need git to update
83 // submodules and learn about various other aspects.
84 if build.rust_info.is_git() {
85 cmd_finder.must_have("git");
88 // We need cmake, but only if we're actually building LLVM or sanitizers.
89 let building_llvm = build.hosts.iter()
90 .filter_map(|host| build.config.target_config.get(host))
91 .any(|config| config.llvm_config.is_none());
92 if building_llvm || build.config.sanitizers {
93 cmd_finder.must_have("cmake");
96 // Ninja is currently only used for LLVM itself.
98 if build.config.ninja {
99 // Some Linux distros rename `ninja` to `ninja-build`.
100 // CMake can work with either binary name.
101 if cmd_finder.maybe_have("ninja-build").is_none() {
102 cmd_finder.must_have("ninja");
106 // If ninja isn't enabled but we're building for MSVC then we try
107 // doubly hard to enable it. It was realized in #43767 that the msbuild
108 // CMake generator for MSVC doesn't respect configuration options like
109 // disabling LLVM assertions, which can often be quite important!
111 // In these cases we automatically enable Ninja if we find it in the
113 if !build.config.ninja && build.config.build.contains("msvc") {
114 if cmd_finder.maybe_have("ninja").is_some() {
115 build.config.ninja = true;
120 build.config.python = build.config.python.take().map(|p| cmd_finder.must_have(p))
121 .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
122 .or_else(|| cmd_finder.maybe_have("python2.7"))
123 .or_else(|| cmd_finder.maybe_have("python2"))
124 .or_else(|| Some(cmd_finder.must_have("python")));
126 build.config.nodejs = build.config.nodejs.take().map(|p| cmd_finder.must_have(p))
127 .or_else(|| cmd_finder.maybe_have("node"))
128 .or_else(|| cmd_finder.maybe_have("nodejs"));
130 build.config.gdb = build.config.gdb.take().map(|p| cmd_finder.must_have(p))
131 .or_else(|| cmd_finder.maybe_have("gdb"));
133 // We're gonna build some custom C code here and there, host triples
134 // also build some C++ shims for LLVM so we need a C++ compiler.
135 for target in &build.targets {
136 // On emscripten we don't actually need the C compiler to just
137 // build the target artifacts, only for testing. For the sake
138 // of easier bot configuration, just skip detection.
139 if target.contains("emscripten") {
143 if !build.config.dry_run {
144 cmd_finder.must_have(build.cc(*target));
145 if let Some(ar) = build.ar(*target) {
146 cmd_finder.must_have(ar);
151 for host in &build.hosts {
152 if !build.config.dry_run {
153 cmd_finder.must_have(build.cxx(*host).unwrap());
157 // Externally configured LLVM requires FileCheck to exist
158 let filecheck = build.llvm_filecheck(build.build);
159 if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
160 panic!("FileCheck executable {:?} does not exist", filecheck);
163 for target in &build.targets {
164 // Can't compile for iOS unless we're on macOS
165 if target.contains("apple-ios") &&
166 !build.build.contains("apple-darwin") {
167 panic!("the iOS target is only supported on macOS");
170 if target.contains("-none-") {
171 if build.no_std(*target).is_none() {
172 let target = build.config.target_config.entry(target.clone())
175 target.no_std = true;
178 if build.no_std(*target) == Some(false) {
179 panic!("All the *-none-* targets are no-std targets")
183 // Make sure musl-root is valid
184 if target.contains("musl") {
185 // If this is a native target (host is also musl) and no musl-root is given,
186 // fall back to the system toolchain in /usr before giving up
187 if build.musl_root(*target).is_none() && build.config.build == *target {
188 let target = build.config.target_config.entry(target.clone())
190 target.musl_root = Some("/usr".into());
192 match build.musl_root(*target) {
194 if fs::metadata(root.join("lib/libc.a")).is_err() {
195 panic!("couldn't find libc.a in musl dir: {}",
196 root.join("lib").display());
198 if fs::metadata(root.join("lib/libunwind.a")).is_err() {
199 panic!("couldn't find libunwind.a in musl dir: {}",
200 root.join("lib").display());
204 panic!("when targeting MUSL either the rust.musl-root \
205 option or the target.$TARGET.musl-root option must \
206 be specified in config.toml")
211 if target.contains("msvc") {
212 // There are three builds of cmake on windows: MSVC, MinGW, and
213 // Cygwin. The Cygwin build does not have generators for Visual
214 // Studio, so detect that here and error.
215 let out = output(Command::new("cmake").arg("--help"));
216 if !out.contains("Visual Studio") {
218 cmake does not support Visual Studio generators.
220 This is likely due to it being an msys/cygwin build of cmake,
221 rather than the required windows version, built using MinGW
224 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
225 package instead of cmake:
227 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
233 if let Some(ref s) = build.config.ccache {
234 cmd_finder.must_have(s);
237 if build.config.channel == "stable" {
238 let mut stage0 = String::new();
239 t!(t!(File::open(build.src.join("src/stage0.txt")))
240 .read_to_string(&mut stage0));
241 if stage0.contains("\ndev:") {
242 panic!("bootstrapping from a dev compiler in a stable release, but \
243 should only be bootstrapping from a released compiler!");