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