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 or sanitizers.
73 let building_llvm = build.config.host.iter()
74 .filter_map(|host| build.config.target_config.get(host))
75 .any(|config| config.llvm_config.is_none());
76 if building_llvm || build.config.sanitizers {
77 need_cmd("cmake".as_ref());
80 // Ninja is currently only used for LLVM itself.
81 if building_llvm && build.config.ninja {
82 // Some Linux distros rename `ninja` to `ninja-build`.
83 // CMake can work with either binary name.
84 if have_cmd("ninja-build".as_ref()).is_none() {
85 need_cmd("ninja".as_ref());
89 if build.config.python.is_none() {
90 build.config.python = have_cmd("python2.7".as_ref());
92 if build.config.python.is_none() {
93 build.config.python = have_cmd("python2".as_ref());
95 if build.config.python.is_none() {
96 need_cmd("python".as_ref());
97 build.config.python = Some("python".into());
99 need_cmd(build.config.python.as_ref().unwrap().as_ref());
102 if let Some(ref s) = build.config.nodejs {
103 need_cmd(s.as_ref());
105 // Look for the nodejs command, needed for emscripten testing
106 if let Some(node) = have_cmd("node".as_ref()) {
107 build.config.nodejs = Some(node);
108 } else if let Some(node) = have_cmd("nodejs".as_ref()) {
109 build.config.nodejs = Some(node);
113 if let Some(ref gdb) = build.config.gdb {
114 need_cmd(gdb.as_ref());
116 build.config.gdb = have_cmd("gdb".as_ref());
119 // We're gonna build some custom C code here and there, host triples
120 // also build some C++ shims for LLVM so we need a C++ compiler.
121 for target in build.config.target.iter() {
122 // On emscripten we don't actually need the C compiler to just
123 // build the target artifacts, only for testing. For the sake
124 // of easier bot configuration, just skip detection.
125 if target.contains("emscripten") {
129 need_cmd(build.cc(target).as_ref());
130 if let Some(ar) = build.ar(target) {
131 need_cmd(ar.as_ref());
134 for host in build.config.host.iter() {
135 need_cmd(build.cxx(host).as_ref());
138 // The msvc hosts don't use jemalloc, turn it off globally to
139 // avoid packaging the dummy liballoc_jemalloc on that platform.
140 for host in build.config.host.iter() {
141 if host.contains("msvc") {
142 build.config.use_jemalloc = false;
146 // Externally configured LLVM requires FileCheck to exist
147 let filecheck = build.llvm_filecheck(&build.config.build);
148 if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
149 panic!("FileCheck executable {:?} does not exist", filecheck);
152 for target in build.config.target.iter() {
153 // Can't compile for iOS unless we're on macOS
154 if target.contains("apple-ios") &&
155 !build.config.build.contains("apple-darwin") {
156 panic!("the iOS target is only supported on macOS");
159 // Make sure musl-root is valid if specified
160 if target.contains("musl") && !target.contains("mips") {
161 match build.musl_root(target) {
163 if fs::metadata(root.join("lib/libc.a")).is_err() {
164 panic!("couldn't find libc.a in musl dir: {}",
165 root.join("lib").display());
167 if fs::metadata(root.join("lib/libunwind.a")).is_err() {
168 panic!("couldn't find libunwind.a in musl dir: {}",
169 root.join("lib").display());
173 panic!("when targeting MUSL either the rust.musl-root \
174 option or the target.$TARGET.musl-root option must \
175 be specified in config.toml")
180 if target.contains("msvc") {
181 // There are three builds of cmake on windows: MSVC, MinGW, and
182 // Cygwin. The Cygwin build does not have generators for Visual
183 // Studio, so detect that here and error.
184 let out = output(Command::new("cmake").arg("--help"));
185 if !out.contains("Visual Studio") {
187 cmake does not support Visual Studio generators.
189 This is likely due to it being an msys/cygwin build of cmake,
190 rather than the required windows version, built using MinGW
193 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
194 package instead of cmake:
196 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
202 for host in build.flags.host.iter() {
203 if !build.config.host.contains(host) {
204 panic!("specified host `{}` is not in the ./configure list", host);
207 for target in build.flags.target.iter() {
208 if !build.config.target.contains(target) {
209 panic!("specified target `{}` is not in the ./configure list",
214 let run = |cmd: &mut Command| {
215 cmd.output().map(|output| {
216 String::from_utf8_lossy(&output.stdout)
217 .lines().next().unwrap()
221 build.lldb_version = run(Command::new("lldb").arg("--version")).ok();
222 if build.lldb_version.is_some() {
223 build.lldb_python_dir = run(Command::new("lldb").arg("-P")).ok();
226 if let Some(ref s) = build.config.ccache {
227 need_cmd(s.as_ref());