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::HashSet;
23 use std::ffi::{OsStr, OsString};
25 use std::process::Command;
27 use build_helper::output;
31 pub fn check(build: &mut Build) {
32 let mut checked = HashSet::new();
33 let path = env::var_os("PATH").unwrap_or(OsString::new());
34 // On Windows, quotes are invalid characters for filename paths, and if
35 // one is present as part of the PATH then that can lead to the system
36 // being unable to identify the files properly. See
37 // https://github.com/rust-lang/rust/issues/34959 for more details.
39 if path.to_string_lossy().contains("\"") {
40 panic!("PATH contains invalid character '\"'");
43 let have_cmd = |cmd: &OsStr| {
44 for path in env::split_paths(&path) {
45 let target = path.join(cmd);
46 let mut cmd_alt = cmd.to_os_string();
48 if target.is_file() ||
49 target.with_extension("exe").exists() ||
50 target.join(cmd_alt).exists() {
57 let mut need_cmd = |cmd: &OsStr| {
58 if !checked.insert(cmd.to_owned()) {
61 if have_cmd(cmd).is_none() {
62 panic!("\n\ncouldn't find required command: {:?}\n\n", cmd);
66 // If we've got a git directory we're gona need git to update
67 // submodules and learn about various other aspects.
69 need_cmd("git".as_ref());
72 // We need cmake, but only if we're actually building LLVM
73 for host in build.config.host.iter() {
74 if let Some(config) = build.config.target_config.get(host) {
75 if config.llvm_config.is_some() {
79 need_cmd("cmake".as_ref());
80 if build.config.ninja {
81 // Some Linux distros rename `ninja` to `ninja-build`.
82 // CMake can work with either binary name.
83 if have_cmd("ninja-build".as_ref()).is_none() {
84 need_cmd("ninja".as_ref());
90 if build.config.python.is_none() {
91 build.config.python = have_cmd("python2.7".as_ref());
93 if build.config.python.is_none() {
94 build.config.python = have_cmd("python2".as_ref());
96 if build.config.python.is_none() {
97 need_cmd("python".as_ref());
98 build.config.python = Some("python".into());
100 need_cmd(build.config.python.as_ref().unwrap().as_ref());
103 if let Some(ref s) = build.config.nodejs {
104 need_cmd(s.as_ref());
106 // Look for the nodejs command, needed for emscripten testing
107 if let Some(node) = have_cmd("node".as_ref()) {
108 build.config.nodejs = Some(node);
109 } else if let Some(node) = have_cmd("nodejs".as_ref()) {
110 build.config.nodejs = Some(node);
114 if let Some(ref gdb) = build.config.gdb {
115 need_cmd(gdb.as_ref());
117 build.config.gdb = have_cmd("gdb".as_ref());
120 // We're gonna build some custom C code here and there, host triples
121 // also build some C++ shims for LLVM so we need a C++ compiler.
122 for target in build.config.target.iter() {
123 // On emscripten we don't actually need the C compiler to just
124 // build the target artifacts, only for testing. For the sake
125 // of easier bot configuration, just skip detection.
126 if target.contains("emscripten") {
130 need_cmd(build.cc(target).as_ref());
131 if let Some(ar) = build.ar(target) {
132 need_cmd(ar.as_ref());
135 for host in build.config.host.iter() {
136 need_cmd(build.cxx(host).as_ref());
139 // The msvc hosts don't use jemalloc, turn it off globally to
140 // avoid packaging the dummy liballoc_jemalloc on that platform.
141 for host in build.config.host.iter() {
142 if host.contains("msvc") {
143 build.config.use_jemalloc = false;
147 // Externally configured LLVM requires FileCheck to exist
148 let filecheck = build.llvm_filecheck(&build.config.build);
149 if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
150 panic!("FileCheck executable {:?} does not exist", filecheck);
153 for target in build.config.target.iter() {
154 // Can't compile for iOS unless we're on macOS
155 if target.contains("apple-ios") &&
156 !build.config.build.contains("apple-darwin") {
157 panic!("the iOS target is only supported on macOS");
160 // Make sure musl-root is valid if specified
161 if target.contains("musl") && !target.contains("mips") {
162 match build.musl_root(target) {
164 if fs::metadata(root.join("lib/libc.a")).is_err() {
165 panic!("couldn't find libc.a in musl dir: {}",
166 root.join("lib").display());
168 if fs::metadata(root.join("lib/libunwind.a")).is_err() {
169 panic!("couldn't find libunwind.a in musl dir: {}",
170 root.join("lib").display());
174 panic!("when targeting MUSL either the rust.musl-root \
175 option or the target.$TARGET.musl-root option must \
176 be specified in config.toml")
181 if target.contains("msvc") {
182 // There are three builds of cmake on windows: MSVC, MinGW, and
183 // Cygwin. The Cygwin build does not have generators for Visual
184 // Studio, so detect that here and error.
185 let out = output(Command::new("cmake").arg("--help"));
186 if !out.contains("Visual Studio") {
188 cmake does not support Visual Studio generators.
190 This is likely due to it being an msys/cygwin build of cmake,
191 rather than the required windows version, built using MinGW
194 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
195 package instead of cmake:
197 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
203 for host in build.flags.host.iter() {
204 if !build.config.host.contains(host) {
205 panic!("specified host `{}` is not in the ./configure list", host);
208 for target in build.flags.target.iter() {
209 if !build.config.target.contains(target) {
210 panic!("specified target `{}` is not in the ./configure list",
215 let run = |cmd: &mut Command| {
216 cmd.output().map(|output| {
217 String::from_utf8_lossy(&output.stdout)
218 .lines().next().unwrap()
222 build.lldb_version = run(Command::new("lldb").arg("--version")).ok();
223 if build.lldb_version.is_some() {
224 build.lldb_python_dir = run(Command::new("lldb").arg("-P")).ok();
227 if let Some(ref s) = build.config.ccache {
228 need_cmd(s.as_ref());