]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/sanity.rs
Rollup merge of #66331 - JohnTitor:add-tests, r=Centril
[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, t};
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;
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_exe = cmd.clone();
42                 cmd_exe.push(".exe");
43
44                 if target.is_file()                   // some/path/git
45                     || path.join(&cmd_exe).exists()   // some/path/git.exe
46                     || target.join(&cmd_exe).exists() // some/path/git/git.exe
47                 {
48                     return Some(target);
49                 }
50             }
51             None
52         }).clone()
53     }
54
55     fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
56         self.maybe_have(&cmd).unwrap_or_else(|| {
57             panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
58         })
59     }
60 }
61
62 pub fn check(build: &mut Build) {
63     let path = env::var_os("PATH").unwrap_or_default();
64     // On Windows, quotes are invalid characters for filename paths, and if
65     // one is present as part of the PATH then that can lead to the system
66     // being unable to identify the files properly. See
67     // https://github.com/rust-lang/rust/issues/34959 for more details.
68     if cfg!(windows) && path.to_string_lossy().contains('\"') {
69         panic!("PATH contains invalid character '\"'");
70     }
71
72     let mut cmd_finder = Finder::new();
73     // If we've got a git directory we're gonna need git to update
74     // submodules and learn about various other aspects.
75     if build.rust_info.is_git() {
76         cmd_finder.must_have("git");
77     }
78
79     // We need cmake, but only if we're actually building LLVM or sanitizers.
80     let building_llvm = build.hosts.iter()
81         .map(|host| build.config.target_config
82             .get(host)
83             .map(|config| config.llvm_config.is_none())
84             .unwrap_or(true))
85         .any(|build_llvm_ourselves| build_llvm_ourselves);
86     if building_llvm || build.config.sanitizers {
87         cmd_finder.must_have("cmake");
88     }
89
90     // Ninja is currently only used for LLVM itself.
91     if building_llvm {
92         if build.config.ninja {
93             // Some Linux distros rename `ninja` to `ninja-build`.
94             // CMake can work with either binary name.
95             if cmd_finder.maybe_have("ninja-build").is_none() {
96                 cmd_finder.must_have("ninja");
97             }
98         }
99
100         // If ninja isn't enabled but we're building for MSVC then we try
101         // doubly hard to enable it. It was realized in #43767 that the msbuild
102         // CMake generator for MSVC doesn't respect configuration options like
103         // disabling LLVM assertions, which can often be quite important!
104         //
105         // In these cases we automatically enable Ninja if we find it in the
106         // environment.
107         if !build.config.ninja && build.config.build.contains("msvc") {
108             if cmd_finder.maybe_have("ninja").is_some() {
109                 build.config.ninja = true;
110             }
111         }
112
113         if build.config.lldb_enabled {
114             cmd_finder.must_have("swig");
115             let out = output(Command::new("swig").arg("-version"));
116             if !out.contains("SWIG Version 3") && !out.contains("SWIG Version 4") {
117                 panic!("Ensure that Swig 3.x.x or 4.x.x is installed.");
118             }
119         }
120     }
121
122     build.config.python = build.config.python.take().map(|p| cmd_finder.must_have(p))
123         .or_else(|| cmd_finder.maybe_have("python2.7"))
124         .or_else(|| cmd_finder.maybe_have("python2"))
125         .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
126         .or_else(|| Some(cmd_finder.must_have("python")));
127
128     build.config.nodejs = build.config.nodejs.take().map(|p| cmd_finder.must_have(p))
129         .or_else(|| cmd_finder.maybe_have("node"))
130         .or_else(|| cmd_finder.maybe_have("nodejs"));
131
132     build.config.gdb = build.config.gdb.take().map(|p| cmd_finder.must_have(p))
133         .or_else(|| cmd_finder.maybe_have("gdb"));
134
135     // We're gonna build some custom C code here and there, host triples
136     // also build some C++ shims for LLVM so we need a C++ compiler.
137     for target in &build.targets {
138         // On emscripten we don't actually need the C compiler to just
139         // build the target artifacts, only for testing. For the sake
140         // of easier bot configuration, just skip detection.
141         if target.contains("emscripten") {
142             continue;
143         }
144
145         // We don't use a C compiler on wasm32
146         if target.contains("wasm32") {
147             continue;
148         }
149
150         if !build.config.dry_run {
151             cmd_finder.must_have(build.cc(*target));
152             if let Some(ar) = build.ar(*target) {
153                 cmd_finder.must_have(ar);
154             }
155         }
156     }
157
158     for host in &build.hosts {
159         if !build.config.dry_run {
160             cmd_finder.must_have(build.cxx(*host).unwrap());
161         }
162     }
163
164     // Externally configured LLVM requires FileCheck to exist
165     let filecheck = build.llvm_filecheck(build.build);
166     if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
167         panic!("FileCheck executable {:?} does not exist", filecheck);
168     }
169
170     for target in &build.targets {
171         // Can't compile for iOS unless we're on macOS
172         if target.contains("apple-ios") &&
173            !build.build.contains("apple-darwin") {
174             panic!("the iOS target is only supported on macOS");
175         }
176
177         if target.contains("-none-") || target.contains("nvptx") {
178             if build.no_std(*target).is_none() {
179                 let target = build.config.target_config.entry(target.clone())
180                     .or_default();
181
182                 target.no_std = true;
183             }
184
185             if build.no_std(*target) == Some(false) {
186                 panic!("All the *-none-* and nvptx* targets are no-std targets")
187             }
188         }
189
190         // Make sure musl-root is valid
191         if target.contains("musl") {
192             // If this is a native target (host is also musl) and no musl-root is given,
193             // fall back to the system toolchain in /usr before giving up
194             if build.musl_root(*target).is_none() && build.config.build == *target {
195                 let target = build.config.target_config.entry(target.clone())
196                     .or_default();
197                 target.musl_root = Some("/usr".into());
198             }
199             match build.musl_root(*target) {
200                 Some(root) => {
201                     if fs::metadata(root.join("lib/libc.a")).is_err() {
202                         panic!("couldn't find libc.a in musl dir: {}",
203                                root.join("lib").display());
204                     }
205                 }
206                 None => {
207                     panic!("when targeting MUSL either the rust.musl-root \
208                             option or the target.$TARGET.musl-root option must \
209                             be specified in config.toml")
210                 }
211             }
212         }
213
214         if target.contains("msvc") {
215             // There are three builds of cmake on windows: MSVC, MinGW, and
216             // Cygwin. The Cygwin build does not have generators for Visual
217             // Studio, so detect that here and error.
218             let out = output(Command::new("cmake").arg("--help"));
219             if !out.contains("Visual Studio") {
220                 panic!("
221 cmake does not support Visual Studio generators.
222
223 This is likely due to it being an msys/cygwin build of cmake,
224 rather than the required windows version, built using MinGW
225 or Visual Studio.
226
227 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
228 package instead of cmake:
229
230 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
231 ");
232             }
233         }
234     }
235
236     if let Some(ref s) = build.config.ccache {
237         cmd_finder.must_have(s);
238     }
239
240     if build.config.channel == "stable" {
241         let stage0 = t!(fs::read_to_string(build.src.join("src/stage0.txt")));
242         if stage0.contains("\ndev:") {
243             panic!("bootstrapping from a dev compiler in a stable release, but \
244                     should only be bootstrapping from a released compiler!");
245         }
246     }
247 }