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