]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/sanity.rs
Rollup merge of #107710 - ehuss:update-strip-ansi-escapes, r=Mark-Simulacrum
[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::{OsStr, OsString};
14 use std::fs;
15 use std::path::PathBuf;
16 use std::process::Command;
17
18 use crate::cache::INTERNER;
19 use crate::config::Target;
20 use crate::util::output;
21 use crate::Build;
22
23 pub struct Finder {
24     cache: HashMap<OsString, Option<PathBuf>>,
25     path: OsString,
26 }
27
28 impl Finder {
29     pub fn new() -> Self {
30         Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
31     }
32
33     pub fn maybe_have<S: Into<OsString>>(&mut self, cmd: S) -> Option<PathBuf> {
34         let cmd: OsString = cmd.into();
35         let path = &self.path;
36         self.cache
37             .entry(cmd.clone())
38             .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()
47                     // some/path/git/git.exe
48                     {
49                         return Some(target);
50                     }
51                 }
52                 None
53             })
54             .clone()
55     }
56
57     pub fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
58         self.maybe_have(&cmd).unwrap_or_else(|| {
59             panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
60         })
61     }
62 }
63
64 pub fn check(build: &mut Build) {
65     let path = env::var_os("PATH").unwrap_or_default();
66     // On Windows, quotes are invalid characters for filename paths, and if
67     // one is present as part of the PATH then that can lead to the system
68     // being unable to identify the files properly. See
69     // https://github.com/rust-lang/rust/issues/34959 for more details.
70     if cfg!(windows) && path.to_string_lossy().contains('\"') {
71         panic!("PATH contains invalid character '\"'");
72     }
73
74     let mut cmd_finder = Finder::new();
75     // If we've got a git directory we're gonna need git to update
76     // submodules and learn about various other aspects.
77     if build.rust_info().is_managed_git_subrepository() {
78         cmd_finder.must_have("git");
79     }
80
81     // We need cmake, but only if we're actually building LLVM or sanitizers.
82     let building_llvm = build.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm"))
83         && build
84             .hosts
85             .iter()
86             .map(|host| {
87                 build
88                     .config
89                     .target_config
90                     .get(host)
91                     .map(|config| config.llvm_config.is_none())
92                     .unwrap_or(true)
93             })
94             .any(|build_llvm_ourselves| build_llvm_ourselves);
95     let need_cmake = building_llvm || build.config.any_sanitizers_enabled();
96     if need_cmake {
97         if cmd_finder.maybe_have("cmake").is_none() {
98             eprintln!(
99                 "
100 Couldn't find required command: cmake
101
102 You should install cmake, or set `download-ci-llvm = true` in the
103 `[llvm]` section section of `config.toml` to download LLVM rather
104 than building it.
105 "
106             );
107             crate::detail_exit(1);
108         }
109     }
110
111     build.config.python = build
112         .config
113         .python
114         .take()
115         .map(|p| cmd_finder.must_have(p))
116         .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
117         .or_else(|| cmd_finder.maybe_have("python"))
118         .or_else(|| cmd_finder.maybe_have("python3"))
119         .or_else(|| cmd_finder.maybe_have("python2"));
120
121     build.config.nodejs = build
122         .config
123         .nodejs
124         .take()
125         .map(|p| cmd_finder.must_have(p))
126         .or_else(|| cmd_finder.maybe_have("node"))
127         .or_else(|| cmd_finder.maybe_have("nodejs"));
128
129     build.config.npm = build
130         .config
131         .npm
132         .take()
133         .map(|p| cmd_finder.must_have(p))
134         .or_else(|| cmd_finder.maybe_have("npm"));
135
136     build.config.gdb = build
137         .config
138         .gdb
139         .take()
140         .map(|p| cmd_finder.must_have(p))
141         .or_else(|| cmd_finder.maybe_have("gdb"));
142
143     build.config.reuse = build
144         .config
145         .reuse
146         .take()
147         .map(|p| cmd_finder.must_have(p))
148         .or_else(|| cmd_finder.maybe_have("reuse"));
149
150     // We're gonna build some custom C code here and there, host triples
151     // also build some C++ shims for LLVM so we need a C++ compiler.
152     for target in &build.targets {
153         // On emscripten we don't actually need the C compiler to just
154         // build the target artifacts, only for testing. For the sake
155         // of easier bot configuration, just skip detection.
156         if target.contains("emscripten") {
157             continue;
158         }
159
160         // We don't use a C compiler on wasm32
161         if target.contains("wasm32") {
162             continue;
163         }
164
165         // Some environments don't want or need these tools, such as when testing Miri.
166         // FIXME: it would be better to refactor this code to split necessary setup from pure sanity
167         // checks, and have a regular flag for skipping the latter. Also see
168         // <https://github.com/rust-lang/rust/pull/103569#discussion_r1008741742>.
169         if env::var_os("BOOTSTRAP_SKIP_TARGET_SANITY").is_some() {
170             continue;
171         }
172
173         if !build.config.dry_run() {
174             cmd_finder.must_have(build.cc(*target));
175             if let Some(ar) = build.ar(*target) {
176                 cmd_finder.must_have(ar);
177             }
178         }
179     }
180
181     for host in &build.hosts {
182         if !build.config.dry_run() {
183             cmd_finder.must_have(build.cxx(*host).unwrap());
184         }
185     }
186
187     if build.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm")) {
188         // Externally configured LLVM requires FileCheck to exist
189         let filecheck = build.llvm_filecheck(build.build);
190         if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
191             panic!("FileCheck executable {:?} does not exist", filecheck);
192         }
193     }
194
195     for target in &build.targets {
196         build
197             .config
198             .target_config
199             .entry(*target)
200             .or_insert_with(|| Target::from_triple(&target.triple));
201
202         if target.contains("-none-") || target.contains("nvptx") {
203             if build.no_std(*target) == Some(false) {
204                 panic!("All the *-none-* and nvptx* targets are no-std targets")
205             }
206         }
207
208         // Make sure musl-root is valid
209         if target.contains("musl") {
210             // If this is a native target (host is also musl) and no musl-root is given,
211             // fall back to the system toolchain in /usr before giving up
212             if build.musl_root(*target).is_none() && build.config.build == *target {
213                 let target = build.config.target_config.entry(*target).or_default();
214                 target.musl_root = Some("/usr".into());
215             }
216             match build.musl_libdir(*target) {
217                 Some(libdir) => {
218                     if fs::metadata(libdir.join("libc.a")).is_err() {
219                         panic!("couldn't find libc.a in musl libdir: {}", libdir.display());
220                     }
221                 }
222                 None => panic!(
223                     "when targeting MUSL either the rust.musl-root \
224                             option or the target.$TARGET.musl-root option must \
225                             be specified in config.toml"
226                 ),
227             }
228         }
229
230         // Some environments don't want or need these tools, such as when testing Miri.
231         // FIXME: it would be better to refactor this code to split necessary setup from pure sanity
232         // checks, and have a regular flag for skipping the latter. Also see
233         // <https://github.com/rust-lang/rust/pull/103569#discussion_r1008741742>.
234         if env::var_os("BOOTSTRAP_SKIP_TARGET_SANITY").is_some() {
235             continue;
236         }
237
238         if need_cmake && target.contains("msvc") {
239             // There are three builds of cmake on windows: MSVC, MinGW, and
240             // Cygwin. The Cygwin build does not have generators for Visual
241             // Studio, so detect that here and error.
242             let out = output(Command::new("cmake").arg("--help"));
243             if !out.contains("Visual Studio") {
244                 panic!(
245                     "
246 cmake does not support Visual Studio generators.
247
248 This is likely due to it being an msys/cygwin build of cmake,
249 rather than the required windows version, built using MinGW
250 or Visual Studio.
251
252 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
253 package instead of cmake:
254
255 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
256 "
257                 );
258             }
259         }
260     }
261
262     if let Some(ref s) = build.config.ccache {
263         cmd_finder.must_have(s);
264     }
265 }