1use std::collections::{HashMap, HashSet};
12use std::ffi::{OsStr, OsString};
13use std::path::PathBuf;
14use std::{env, fs};
15
16#[cfg(not(test))]
17use crate::builder::Builder;
18use crate::builder::Kind;
19#[cfg(not(test))]
20use crate::core::build_steps::tool;
21use crate::core::config::Target;
22use crate::utils::exec::command;
23use crate::{Build, Subcommand};
24
25pub struct Finder {
26 cache: HashMap<OsString, Option<PathBuf>>,
27 path: OsString,
28}
29
30const STAGE0_MISSING_TARGETS: &[&str] = &[
36 ];
38
39#[cfg(not(test))]
42const LIBSTDCXX_MIN_VERSION_THRESHOLD: usize = 8;
43
44impl Finder {
45 pub fn new() -> Self {
46 Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
47 }
48
49 pub fn maybe_have<S: Into<OsString>>(&mut self, cmd: S) -> Option<PathBuf> {
50 let cmd: OsString = cmd.into();
51 let path = &self.path;
52 self.cache
53 .entry(cmd.clone())
54 .or_insert_with(|| {
55 for path in env::split_paths(path) {
56 let target = path.join(&cmd);
57 let mut cmd_exe = cmd.clone();
58 cmd_exe.push(".exe");
59
60 if target.is_file() || path.join(&cmd_exe).exists() || target.join(&cmd_exe).exists()
63 {
65 return Some(target);
66 }
67 }
68 None
69 })
70 .clone()
71 }
72
73 pub fn must_have<S: AsRef<OsStr>>(&mut self, cmd: S) -> PathBuf {
74 self.maybe_have(&cmd).unwrap_or_else(|| {
75 panic!("\n\ncouldn't find required command: {:?}\n\n", cmd.as_ref());
76 })
77 }
78}
79
80pub fn check(build: &mut Build) {
81 let mut skip_target_sanity =
82 env::var_os("BOOTSTRAP_SKIP_TARGET_SANITY").is_some_and(|s| s == "1" || s == "true");
83
84 skip_target_sanity |= build.config.cmd.kind() == Kind::Check;
85
86 let skipped_paths = [OsStr::new("mir-opt"), OsStr::new("miri")];
88 skip_target_sanity |= build.config.paths.iter().any(|path| {
89 path.components().any(|component| skipped_paths.contains(&component.as_os_str()))
90 });
91
92 let path = env::var_os("PATH").unwrap_or_default();
93 if cfg!(windows) && path.to_string_lossy().contains('\"') {
98 panic!("PATH contains invalid character '\"'");
99 }
100
101 let mut cmd_finder = Finder::new();
102 if build.rust_info().is_managed_git_subrepository() {
105 cmd_finder.must_have("git");
106 }
107
108 #[cfg(not(test))]
110 if !build.config.dry_run() && !build.build.is_msvc() && build.config.llvm_from_ci {
111 let builder = Builder::new(build);
112 let libcxx_version = builder.ensure(tool::LibcxxVersionTool { target: build.build });
113
114 match libcxx_version {
115 tool::LibcxxVersion::Gnu(version) => {
116 if LIBSTDCXX_MIN_VERSION_THRESHOLD > version {
117 eprintln!(
118 "\nYour system's libstdc++ version is too old for the `llvm.download-ci-llvm` option."
119 );
120 eprintln!("Current version detected: '{version}'");
121 eprintln!("Minimum required version: '{LIBSTDCXX_MIN_VERSION_THRESHOLD}'");
122 eprintln!(
123 "Consider upgrading libstdc++ or disabling the `llvm.download-ci-llvm` option."
124 );
125 eprintln!(
126 "If you choose to upgrade libstdc++, run `x clean` or delete `build/host/libcxx-version` manually after the upgrade."
127 );
128 }
129 }
130 tool::LibcxxVersion::Llvm(_) => {
131 }
133 }
134 }
135
136 let building_llvm = !build.config.llvm_from_ci
138 && build.hosts.iter().any(|host| {
139 build.config.llvm_enabled(*host)
140 && build
141 .config
142 .target_config
143 .get(host)
144 .map(|config| config.llvm_config.is_none())
145 .unwrap_or(true)
146 });
147
148 let need_cmake = building_llvm || build.config.any_sanitizers_to_build();
149 if need_cmake && cmd_finder.maybe_have("cmake").is_none() {
150 eprintln!(
151 "
152Couldn't find required command: cmake
153
154You should install cmake, or set `download-ci-llvm = true` in the
155`[llvm]` section of `bootstrap.toml` to download LLVM rather
156than building it.
157"
158 );
159 crate::exit!(1);
160 }
161
162 build.config.python = build
163 .config
164 .python
165 .take()
166 .map(|p| cmd_finder.must_have(p))
167 .or_else(|| env::var_os("BOOTSTRAP_PYTHON").map(PathBuf::from)) .or_else(|| cmd_finder.maybe_have("python"))
169 .or_else(|| cmd_finder.maybe_have("python3"))
170 .or_else(|| cmd_finder.maybe_have("python2"));
171
172 build.config.nodejs = build
173 .config
174 .nodejs
175 .take()
176 .map(|p| cmd_finder.must_have(p))
177 .or_else(|| cmd_finder.maybe_have("node"))
178 .or_else(|| cmd_finder.maybe_have("nodejs"));
179
180 build.config.npm = build
181 .config
182 .npm
183 .take()
184 .map(|p| cmd_finder.must_have(p))
185 .or_else(|| cmd_finder.maybe_have("npm"));
186
187 build.config.gdb = build
188 .config
189 .gdb
190 .take()
191 .map(|p| cmd_finder.must_have(p))
192 .or_else(|| cmd_finder.maybe_have("gdb"));
193
194 build.config.reuse = build
195 .config
196 .reuse
197 .take()
198 .map(|p| cmd_finder.must_have(p))
199 .or_else(|| cmd_finder.maybe_have("reuse"));
200
201 let stage0_supported_target_list: HashSet<String> = crate::utils::helpers::output(
202 command(&build.config.initial_rustc).args(["--print", "target-list"]).as_command_mut(),
203 )
204 .lines()
205 .map(|s| s.to_string())
206 .collect();
207
208 let skip_tools_checks = build.config.dry_run()
213 || matches!(
214 build.config.cmd,
215 Subcommand::Clean { .. }
216 | Subcommand::Check { .. }
217 | Subcommand::Suggest { .. }
218 | Subcommand::Format { .. }
219 | Subcommand::Setup { .. }
220 );
221
222 for target in &build.targets {
225 if target.contains("emscripten") {
229 continue;
230 }
231
232 if target.contains("wasm32") {
234 continue;
235 }
236
237 if skip_target_sanity && target != &build.build {
239 continue;
240 }
241
242 if cfg!(not(test)) && !skip_target_sanity && !build.local_rebuild {
244 let mut has_target = false;
245 let target_str = target.to_string();
246
247 let missing_targets_hashset: HashSet<_> =
248 STAGE0_MISSING_TARGETS.iter().map(|t| t.to_string()).collect();
249 let duplicated_targets: Vec<_> =
250 stage0_supported_target_list.intersection(&missing_targets_hashset).collect();
251
252 if !duplicated_targets.is_empty() {
253 println!(
254 "Following targets supported from the stage0 compiler, please remove them from STAGE0_MISSING_TARGETS list."
255 );
256 for duplicated_target in duplicated_targets {
257 println!(" {duplicated_target}");
258 }
259 std::process::exit(1);
260 }
261
262 has_target |= stage0_supported_target_list.contains(&target_str);
264 has_target |= STAGE0_MISSING_TARGETS.contains(&target_str.as_str());
265
266 if !has_target {
267 if target.filepath().is_some_and(|p| p.exists()) {
269 has_target = true;
270 } else if let Some(custom_target_path) = env::var_os("RUST_TARGET_PATH") {
271 let mut target_filename = OsString::from(&target_str);
272 target_filename.push(".json");
274
275 let walker = walkdir::WalkDir::new(custom_target_path).into_iter();
277 for entry in walker.filter_map(|e| e.ok()) {
278 has_target |= entry.file_name() == target_filename;
279 }
280 }
281 }
282
283 if !has_target {
284 panic!(
285 "No such target exists in the target list,\n\
286 make sure to correctly specify the location \
287 of the JSON specification file \
288 for custom targets!\n\
289 Use BOOTSTRAP_SKIP_TARGET_SANITY=1 to \
290 bypass this check."
291 );
292 }
293 }
294
295 if !skip_tools_checks {
296 cmd_finder.must_have(build.cc(*target));
297 if let Some(ar) = build.ar(*target) {
298 cmd_finder.must_have(ar);
299 }
300 }
301 }
302
303 if !skip_tools_checks {
304 for host in &build.hosts {
305 cmd_finder.must_have(build.cxx(*host).unwrap());
306
307 if build.config.llvm_enabled(*host) {
308 let filecheck = build.llvm_filecheck(build.build);
310 if !filecheck.starts_with(&build.out)
311 && !filecheck.exists()
312 && build.config.codegen_tests
313 {
314 panic!("FileCheck executable {filecheck:?} does not exist");
315 }
316 }
317 }
318 }
319
320 for target in &build.targets {
321 build
322 .config
323 .target_config
324 .entry(*target)
325 .or_insert_with(|| Target::from_triple(&target.triple));
326
327 if (target.contains("-none-") || target.contains("nvptx"))
328 && build.no_std(*target) == Some(false)
329 {
330 panic!("All the *-none-* and nvptx* targets are no-std targets")
331 }
332
333 if skip_target_sanity && target != &build.build {
335 continue;
336 }
337
338 if target.contains("musl") && !target.contains("unikraft") {
340 if build.musl_root(*target).is_none() && build.config.is_host_target(*target) {
343 let target = build.config.target_config.entry(*target).or_default();
344 target.musl_root = Some("/usr".into());
345 }
346 match build.musl_libdir(*target) {
347 Some(libdir) => {
348 if fs::metadata(libdir.join("libc.a")).is_err() {
349 panic!("couldn't find libc.a in musl libdir: {}", libdir.display());
350 }
351 }
352 None => panic!(
353 "when targeting MUSL either the rust.musl-root \
354 option or the target.$TARGET.musl-root option must \
355 be specified in bootstrap.toml"
356 ),
357 }
358 }
359
360 if need_cmake && target.is_msvc() {
361 let out =
365 command("cmake").arg("--help").run_always().run_capture_stdout(build).stdout();
366 if !out.contains("Visual Studio") {
367 panic!(
368 "
369cmake does not support Visual Studio generators.
370
371This is likely due to it being an msys/cygwin build of cmake,
372rather than the required windows version, built using MinGW
373or Visual Studio.
374
375If you are building under msys2 try installing the mingw-w64-x86_64-cmake
376package instead of cmake:
377
378$ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
379"
380 );
381 }
382 }
383 }
384
385 if let Some(ref s) = build.config.ccache {
386 cmd_finder.must_have(s);
387 }
388}