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 cmd_finder.must_have(build.cc(*target));
144 if let Some(ar) = build.ar(*target) {
145 cmd_finder.must_have(ar);
149 for host in &build.hosts {
150 cmd_finder.must_have(build.cxx(*host).unwrap());
152 // The msvc hosts don't use jemalloc, turn it off globally to
153 // avoid packaging the dummy liballoc_jemalloc on that platform.
154 if host.contains("msvc") {
155 build.config.use_jemalloc = false;
159 // Externally configured LLVM requires FileCheck to exist
160 let filecheck = build.llvm_filecheck(build.build);
161 if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
162 panic!("FileCheck executable {:?} does not exist", filecheck);
165 for target in &build.targets {
166 // Can't compile for iOS unless we're on macOS
167 if target.contains("apple-ios") &&
168 !build.build.contains("apple-darwin") {
169 panic!("the iOS target is only supported on macOS");
172 // Make sure musl-root is valid
173 if target.contains("musl") {
174 // If this is a native target (host is also musl) and no musl-root is given,
175 // fall back to the system toolchain in /usr before giving up
176 if build.musl_root(*target).is_none() && build.config.build == *target {
177 let target = build.config.target_config.entry(target.clone())
178 .or_insert(Default::default());
179 target.musl_root = Some("/usr".into());
181 match build.musl_root(*target) {
183 if fs::metadata(root.join("lib/libc.a")).is_err() {
184 panic!("couldn't find libc.a in musl dir: {}",
185 root.join("lib").display());
187 if fs::metadata(root.join("lib/libunwind.a")).is_err() {
188 panic!("couldn't find libunwind.a in musl dir: {}",
189 root.join("lib").display());
193 panic!("when targeting MUSL either the rust.musl-root \
194 option or the target.$TARGET.musl-root option must \
195 be specified in config.toml")
200 if target.contains("msvc") {
201 // There are three builds of cmake on windows: MSVC, MinGW, and
202 // Cygwin. The Cygwin build does not have generators for Visual
203 // Studio, so detect that here and error.
204 let out = output(Command::new("cmake").arg("--help"));
205 if !out.contains("Visual Studio") {
207 cmake does not support Visual Studio generators.
209 This is likely due to it being an msys/cygwin build of cmake,
210 rather than the required windows version, built using MinGW
213 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
214 package instead of cmake:
216 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
222 let run = |cmd: &mut Command| {
223 cmd.output().map(|output| {
224 String::from_utf8_lossy(&output.stdout)
225 .lines().next().unwrap_or_else(|| {
226 panic!("{:?} failed {:?}", cmd, output)
230 build.lldb_version = run(Command::new("lldb").arg("--version")).ok();
231 if build.lldb_version.is_some() {
232 build.lldb_python_dir = run(Command::new("lldb").arg("-P")).ok();
235 if let Some(ref s) = build.config.ccache {
236 cmd_finder.must_have(s);
239 if build.config.channel == "stable" {
240 let mut stage0 = String::new();
241 t!(t!(File::open(build.src.join("src/stage0.txt")))
242 .read_to_string(&mut stage0));
243 if stage0.contains("\ndev:") {
244 panic!("bootstrapping from a dev compiler in a stable release, but \
245 should only be bootstrapping from a released compiler!");