]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/sanity.rs
Rollup merge of #76069 - pickfire:patch-16, r=jyn514
[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 build_helper::{output, t};
19
20 use crate::config::Target;
21 use crate::Build;
22
23 struct Finder {
24     cache: HashMap<OsString, Option<PathBuf>>,
25     path: OsString,
26 }
27
28 impl Finder {
29     fn new() -> Self {
30         Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
31     }
32
33     fn maybe_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> Option<PathBuf> {
34         let cmd: OsString = cmd.as_ref().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     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_git() {
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
83         .hosts
84         .iter()
85         .map(|host| {
86             build
87                 .config
88                 .target_config
89                 .get(host)
90                 .map(|config| config.llvm_config.is_none())
91                 .unwrap_or(true)
92         })
93         .any(|build_llvm_ourselves| build_llvm_ourselves);
94     if building_llvm || build.config.sanitizers {
95         cmd_finder.must_have("cmake");
96     }
97
98     // Ninja is currently only used for LLVM itself.
99     if building_llvm {
100         if build.config.ninja {
101             // Some Linux distros rename `ninja` to `ninja-build`.
102             // CMake can work with either binary name.
103             if cmd_finder.maybe_have("ninja-build").is_none()
104                 && cmd_finder.maybe_have("ninja").is_none()
105             {
106                 eprintln!(
107                     "
108 Couldn't find required command: ninja
109 You should install ninja, or set ninja=false in config.toml
110 "
111                 );
112                 std::process::exit(1);
113             }
114         }
115
116         // If ninja isn't enabled but we're building for MSVC then we try
117         // doubly hard to enable it. It was realized in #43767 that the msbuild
118         // CMake generator for MSVC doesn't respect configuration options like
119         // disabling LLVM assertions, which can often be quite important!
120         //
121         // In these cases we automatically enable Ninja if we find it in the
122         // environment.
123         if !build.config.ninja && build.config.build.contains("msvc") {
124             if cmd_finder.maybe_have("ninja").is_some() {
125                 build.config.ninja = true;
126             }
127         }
128     }
129
130     build.config.python = build
131         .config
132         .python
133         .take()
134         .map(|p| cmd_finder.must_have(p))
135         .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) // set by bootstrap.py
136         .or_else(|| Some(cmd_finder.must_have("python")));
137
138     build.config.nodejs = build
139         .config
140         .nodejs
141         .take()
142         .map(|p| cmd_finder.must_have(p))
143         .or_else(|| cmd_finder.maybe_have("node"))
144         .or_else(|| cmd_finder.maybe_have("nodejs"));
145
146     build.config.gdb = build
147         .config
148         .gdb
149         .take()
150         .map(|p| cmd_finder.must_have(p))
151         .or_else(|| cmd_finder.maybe_have("gdb"));
152
153     // We're gonna build some custom C code here and there, host triples
154     // also build some C++ shims for LLVM so we need a C++ compiler.
155     for target in &build.targets {
156         // On emscripten we don't actually need the C compiler to just
157         // build the target artifacts, only for testing. For the sake
158         // of easier bot configuration, just skip detection.
159         if target.contains("emscripten") {
160             continue;
161         }
162
163         // We don't use a C compiler on wasm32
164         if target.contains("wasm32") {
165             continue;
166         }
167
168         if !build.config.dry_run {
169             cmd_finder.must_have(build.cc(*target));
170             if let Some(ar) = build.ar(*target) {
171                 cmd_finder.must_have(ar);
172             }
173         }
174     }
175
176     for host in &build.hosts {
177         if !build.config.dry_run {
178             cmd_finder.must_have(build.cxx(*host).unwrap());
179         }
180     }
181
182     // Externally configured LLVM requires FileCheck to exist
183     let filecheck = build.llvm_filecheck(build.build);
184     if !filecheck.starts_with(&build.out) && !filecheck.exists() && build.config.codegen_tests {
185         panic!("FileCheck executable {:?} does not exist", filecheck);
186     }
187
188     for target in &build.targets {
189         // Can't compile for iOS unless we're on macOS
190         if target.contains("apple-ios") && !build.build.contains("apple-darwin") {
191             panic!("the iOS target is only supported on macOS");
192         }
193
194         build
195             .config
196             .target_config
197             .entry(target.clone())
198             .or_insert(Target::from_triple(&target.triple));
199
200         if target.contains("-none-") || target.contains("nvptx") {
201             if build.no_std(*target) == Some(false) {
202                 panic!("All the *-none-* and nvptx* targets are no-std targets")
203             }
204         }
205
206         // Make sure musl-root is valid
207         if target.contains("musl") {
208             // If this is a native target (host is also musl) and no musl-root is given,
209             // fall back to the system toolchain in /usr before giving up
210             if build.musl_root(*target).is_none() && build.config.build == *target {
211                 let target = build.config.target_config.entry(target.clone()).or_default();
212                 target.musl_root = Some("/usr".into());
213             }
214             match build.musl_libdir(*target) {
215                 Some(libdir) => {
216                     if fs::metadata(libdir.join("libc.a")).is_err() {
217                         panic!("couldn't find libc.a in musl libdir: {}", libdir.display());
218                     }
219                 }
220                 None => panic!(
221                     "when targeting MUSL either the rust.musl-root \
222                             option or the target.$TARGET.musl-root option must \
223                             be specified in config.toml"
224                 ),
225             }
226         }
227
228         if target.contains("msvc") {
229             // There are three builds of cmake on windows: MSVC, MinGW, and
230             // Cygwin. The Cygwin build does not have generators for Visual
231             // Studio, so detect that here and error.
232             let out = output(Command::new("cmake").arg("--help"));
233             if !out.contains("Visual Studio") {
234                 panic!(
235                     "
236 cmake does not support Visual Studio generators.
237
238 This is likely due to it being an msys/cygwin build of cmake,
239 rather than the required windows version, built using MinGW
240 or Visual Studio.
241
242 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
243 package instead of cmake:
244
245 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
246 "
247                 );
248             }
249         }
250     }
251
252     if let Some(ref s) = build.config.ccache {
253         cmd_finder.must_have(s);
254     }
255
256     if build.config.channel == "stable" {
257         let stage0 = t!(fs::read_to_string(build.src.join("src/stage0.txt")));
258         if stage0.contains("\ndev:") {
259             panic!(
260                 "bootstrapping from a dev compiler in a stable release, but \
261                     should only be bootstrapping from a released compiler!"
262             );
263         }
264     }
265 }