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