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