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