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