]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/sanity.rs
fe547a6b151c229bda8408f143448fa7219cf56d
[rust.git] / src / bootstrap / sanity.rs
1 //! Sanity checking performed by rustbuild before actually executing anything.
2 //!
3 //! This module contains the implementation of ensuring that the build
4 //! environment looks reasonable before progressing. This will verify that
5 //! various programs like git and python exist, along with ensuring that all C
6 //! compilers for cross-compiling are found.
7 //!
8 //! In theory if we get past this phase it's a bug if a build fails, but in
9 //! practice that's likely not true!
10
11 use std::collections::HashMap;
12 use std::env;
13 use std::ffi::{OsString, OsStr};
14 use std::fs;
15 use std::path::PathBuf;
16 use std::process::Command;
17
18 use build_helper::output;
19
20 use crate::Build;
21
22 struct Finder {
23     cache: HashMap<OsString, Option<PathBuf>>,
24     path: OsString,
25 }
26
27 impl Finder {
28     fn new() -> Self {
29         Self {
30             cache: HashMap::new(),
31             path: env::var_os("PATH").unwrap_or_default()
32         }
33     }
34
35     fn maybe_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> Option<PathBuf> {
36         let cmd: OsString = cmd.as_ref().into();
37         let path = self.path.clone();
38         self.cache.entry(cmd.clone()).or_insert_with(|| {
39             for path in env::split_paths(&path) {
40                 let target = path.join(&cmd);
41                 let mut cmd_alt = cmd.clone();
42                 cmd_alt.push(".exe");
43                 if target.is_file() || // some/path/git
44                 target.with_extension("exe").exists() || // some/path/git.exe
45                 target.join(&cmd_alt).exists() { // some/path/git/git.exe
46                     return Some(target);
47                 }
48             }
49             None
50         }).clone()
51     }
52
53     fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
54         self.maybe_have(&cmd).unwrap_or_else(|| {
55             panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
56         })
57     }
58 }
59
60 pub fn check(build: &mut Build) {
61     let path = env::var_os("PATH").unwrap_or_default();
62     // On Windows, quotes are invalid characters for filename paths, and if
63     // one is present as part of the PATH then that can lead to the system
64     // being unable to identify the files properly. See
65     // https://github.com/rust-lang/rust/issues/34959 for more details.
66     if cfg!(windows) && path.to_string_lossy().contains('\"') {
67         panic!("PATH contains invalid character '\"'");
68     }
69
70     let mut cmd_finder = Finder::new();
71     // If we've got a git directory we're gonna need git to update
72     // submodules and learn about various other aspects.
73     if build.rust_info.is_git() {
74         cmd_finder.must_have("git");
75     }
76
77     // We need cmake, but only if we're actually building LLVM or sanitizers.
78     let building_llvm = build.hosts.iter()
79         .filter_map(|host| build.config.target_config.get(host))
80         .any(|config| config.llvm_config.is_none());
81     if building_llvm || build.config.sanitizers {
82         cmd_finder.must_have("cmake");
83     }
84
85     // Ninja is currently only used for LLVM itself.
86     if building_llvm {
87         if build.config.ninja {
88             // Some Linux distros rename `ninja` to `ninja-build`.
89             // CMake can work with either binary name.
90             if cmd_finder.maybe_have("ninja-build").is_none() {
91                 cmd_finder.must_have("ninja");
92             }
93         }
94
95         // If ninja isn't enabled but we're building for MSVC then we try
96         // doubly hard to enable it. It was realized in #43767 that the msbuild
97         // CMake generator for MSVC doesn't respect configuration options like
98         // disabling LLVM assertions, which can often be quite important!
99         //
100         // In these cases we automatically enable Ninja if we find it in the
101         // environment.
102         if !build.config.ninja && build.config.build.contains("msvc") {
103             if cmd_finder.maybe_have("ninja").is_some() {
104                 build.config.ninja = true;
105             }
106         }
107     }
108
109     build.config.python = build.config.python.take().map(|p| cmd_finder.must_have(p))
110         .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
111         .or_else(|| cmd_finder.maybe_have("python2.7"))
112         .or_else(|| cmd_finder.maybe_have("python2"))
113         .or_else(|| Some(cmd_finder.must_have("python")));
114
115     build.config.nodejs = build.config.nodejs.take().map(|p| cmd_finder.must_have(p))
116         .or_else(|| cmd_finder.maybe_have("node"))
117         .or_else(|| cmd_finder.maybe_have("nodejs"));
118
119     build.config.gdb = build.config.gdb.take().map(|p| cmd_finder.must_have(p))
120         .or_else(|| cmd_finder.maybe_have("gdb"));
121
122     // We're gonna build some custom C code here and there, host triples
123     // also build some C++ shims for LLVM so we need a C++ compiler.
124     for target in &build.targets {
125         // On emscripten we don't actually need the C compiler to just
126         // build the target artifacts, only for testing. For the sake
127         // of easier bot configuration, just skip detection.
128         if target.contains("emscripten") {
129             continue;
130         }
131
132         if !build.config.dry_run {
133             cmd_finder.must_have(build.cc(*target));
134             if let Some(ar) = build.ar(*target) {
135                 cmd_finder.must_have(ar);
136             }
137         }
138     }
139
140     for host in &build.hosts {
141         if !build.config.dry_run {
142             cmd_finder.must_have(build.cxx(*host).unwrap());
143         }
144     }
145
146     // Externally configured LLVM requires FileCheck to exist
147     let filecheck = build.llvm_filecheck(build.build);
148     if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
149         panic!("FileCheck executable {:?} does not exist", filecheck);
150     }
151
152     for target in &build.targets {
153         // Can't compile for iOS unless we're on macOS
154         if target.contains("apple-ios") &&
155            !build.build.contains("apple-darwin") {
156             panic!("the iOS target is only supported on macOS");
157         }
158
159         if target.contains("-none-") {
160             if build.no_std(*target).is_none() {
161                 let target = build.config.target_config.entry(target.clone())
162                     .or_default();
163
164                 target.no_std = true;
165             }
166
167             if build.no_std(*target) == Some(false) {
168                 panic!("All the *-none-* targets are no-std targets")
169             }
170         }
171
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_default();
179                 target.musl_root = Some("/usr".into());
180             }
181             match build.musl_root(*target) {
182                 Some(root) => {
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());
186                     }
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());
190                     }
191                 }
192                 None => {
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")
196                 }
197             }
198         }
199
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") {
206                 panic!("
207 cmake does not support Visual Studio generators.
208
209 This is likely due to it being an msys/cygwin build of cmake,
210 rather than the required windows version, built using MinGW
211 or Visual Studio.
212
213 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
214 package instead of cmake:
215
216 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
217 ");
218             }
219         }
220     }
221
222     if let Some(ref s) = build.config.ccache {
223         cmd_finder.must_have(s);
224     }
225
226     if build.config.channel == "stable" {
227         let stage0 = t!(fs::read_to_string(build.src.join("src/stage0.txt")));
228         if stage0.contains("\ndev:") {
229             panic!("bootstrapping from a dev compiler in a stable release, but \
230                     should only be bootstrapping from a released compiler!");
231         }
232     }
233 }