]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/sanity.rs
Make Build.cxx() return a Result instead of panicking
[rust.git] / src / bootstrap / sanity.rs
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.
4 //
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.
10
11 //! Sanity checking performed by rustbuild before actually executing anything.
12 //!
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.
17 //!
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!
20
21 use std::collections::HashSet;
22 use std::env;
23 use std::ffi::{OsStr, OsString};
24 use std::fs;
25 use std::process::Command;
26 use std::path::PathBuf;
27
28 use build_helper::output;
29
30 use Build;
31
32 pub fn check(build: &mut Build) {
33     let mut checked = HashSet::new();
34     let path = env::var_os("PATH").unwrap_or(OsString::new());
35     // On Windows, quotes are invalid characters for filename paths, and if
36     // one is present as part of the PATH then that can lead to the system
37     // being unable to identify the files properly. See
38     // https://github.com/rust-lang/rust/issues/34959 for more details.
39     if cfg!(windows) {
40         if path.to_string_lossy().contains("\"") {
41             panic!("PATH contains invalid character '\"'");
42         }
43     }
44     let have_cmd = |cmd: &OsStr| {
45         for path in env::split_paths(&path) {
46             let target = path.join(cmd);
47             let mut cmd_alt = cmd.to_os_string();
48             cmd_alt.push(".exe");
49             if target.is_file() ||
50                target.with_extension("exe").exists() ||
51                target.join(cmd_alt).exists() {
52                 return Some(target);
53             }
54         }
55         return None;
56     };
57
58     let mut need_cmd = |cmd: &OsStr| {
59         if !checked.insert(cmd.to_owned()) {
60             return
61         }
62         if have_cmd(cmd).is_none() {
63             panic!("\n\ncouldn't find required command: {:?}\n\n", cmd);
64         }
65     };
66
67     // If we've got a git directory we're gona need git to update
68     // submodules and learn about various other aspects.
69     if build.src_is_git {
70         need_cmd("git".as_ref());
71     }
72
73     // We need cmake, but only if we're actually building LLVM or sanitizers.
74     let building_llvm = build.config.host.iter()
75         .filter_map(|host| build.config.target_config.get(host))
76         .any(|config| config.llvm_config.is_none());
77     if building_llvm || build.config.sanitizers {
78         need_cmd("cmake".as_ref());
79     }
80
81     // Ninja is currently only used for LLVM itself.
82     if building_llvm && build.config.ninja {
83         // Some Linux distros rename `ninja` to `ninja-build`.
84         // CMake can work with either binary name.
85         if have_cmd("ninja-build".as_ref()).is_none() {
86             need_cmd("ninja".as_ref());
87         }
88     }
89
90     if build.config.python.is_none() {
91         // set by bootstrap.py
92         if let Some(v) = env::var_os("BOOTSTRAP_PYTHON") {
93             build.config.python = Some(PathBuf::from(v));
94         }
95     }
96     if build.config.python.is_none() {
97         build.config.python = have_cmd("python2.7".as_ref());
98     }
99     if build.config.python.is_none() {
100         build.config.python = have_cmd("python2".as_ref());
101     }
102     if build.config.python.is_none() {
103         need_cmd("python".as_ref());
104         build.config.python = Some("python".into());
105     }
106     need_cmd(build.config.python.as_ref().unwrap().as_ref());
107
108
109     if let Some(ref s) = build.config.nodejs {
110         need_cmd(s.as_ref());
111     } else {
112         // Look for the nodejs command, needed for emscripten testing
113         if let Some(node) = have_cmd("node".as_ref()) {
114             build.config.nodejs = Some(node);
115         } else if let Some(node) = have_cmd("nodejs".as_ref()) {
116             build.config.nodejs = Some(node);
117         }
118     }
119
120     if let Some(ref gdb) = build.config.gdb {
121         need_cmd(gdb.as_ref());
122     } else {
123         build.config.gdb = have_cmd("gdb".as_ref());
124     }
125
126     // We're gonna build some custom C code here and there, host triples
127     // also build some C++ shims for LLVM so we need a C++ compiler.
128     for target in build.config.target.iter() {
129         // On emscripten we don't actually need the C compiler to just
130         // build the target artifacts, only for testing. For the sake
131         // of easier bot configuration, just skip detection.
132         if target.contains("emscripten") {
133             continue;
134         }
135
136         need_cmd(build.cc(target).as_ref());
137         if let Some(ar) = build.ar(target) {
138             need_cmd(ar.as_ref());
139         }
140     }
141     for host in build.config.host.iter() {
142         need_cmd(build.cxx(host).unwrap().as_ref());
143     }
144
145     // The msvc hosts don't use jemalloc, turn it off globally to
146     // avoid packaging the dummy liballoc_jemalloc on that platform.
147     for host in build.config.host.iter() {
148         if host.contains("msvc") {
149             build.config.use_jemalloc = false;
150         }
151     }
152
153     // Externally configured LLVM requires FileCheck to exist
154     let filecheck = build.llvm_filecheck(&build.config.build);
155     if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
156         panic!("FileCheck executable {:?} does not exist", filecheck);
157     }
158
159     for target in build.config.target.iter() {
160         // Can't compile for iOS unless we're on macOS
161         if target.contains("apple-ios") &&
162            !build.config.build.contains("apple-darwin") {
163             panic!("the iOS target is only supported on macOS");
164         }
165
166         // Make sure musl-root is valid if specified
167         if target.contains("musl") && !target.contains("mips") {
168             match build.musl_root(target) {
169                 Some(root) => {
170                     if fs::metadata(root.join("lib/libc.a")).is_err() {
171                         panic!("couldn't find libc.a in musl dir: {}",
172                                root.join("lib").display());
173                     }
174                     if fs::metadata(root.join("lib/libunwind.a")).is_err() {
175                         panic!("couldn't find libunwind.a in musl dir: {}",
176                                root.join("lib").display());
177                     }
178                 }
179                 None => {
180                     panic!("when targeting MUSL either the rust.musl-root \
181                             option or the target.$TARGET.musl-root option must \
182                             be specified in config.toml")
183                 }
184             }
185         }
186
187         if target.contains("msvc") {
188             // There are three builds of cmake on windows: MSVC, MinGW, and
189             // Cygwin. The Cygwin build does not have generators for Visual
190             // Studio, so detect that here and error.
191             let out = output(Command::new("cmake").arg("--help"));
192             if !out.contains("Visual Studio") {
193                 panic!("
194 cmake does not support Visual Studio generators.
195
196 This is likely due to it being an msys/cygwin build of cmake,
197 rather than the required windows version, built using MinGW
198 or Visual Studio.
199
200 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
201 package instead of cmake:
202
203 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
204 ");
205             }
206         }
207     }
208
209     for host in build.flags.host.iter() {
210         if !build.config.host.contains(host) {
211             panic!("specified host `{}` is not in the ./configure list", host);
212         }
213     }
214     for target in build.flags.target.iter() {
215         if !build.config.target.contains(target) {
216             panic!("specified target `{}` is not in the ./configure list",
217                    target);
218         }
219     }
220
221     let run = |cmd: &mut Command| {
222         cmd.output().map(|output| {
223             String::from_utf8_lossy(&output.stdout)
224                    .lines().next().unwrap()
225                    .to_string()
226         })
227     };
228     build.lldb_version = run(Command::new("lldb").arg("--version")).ok();
229     if build.lldb_version.is_some() {
230         build.lldb_python_dir = run(Command::new("lldb").arg("-P")).ok();
231     }
232
233     if let Some(ref s) = build.config.ccache {
234         need_cmd(s.as_ref());
235     }
236 }