1use std::ffi::OsStr;
7use std::path::{Path, PathBuf};
8use std::sync::OnceLock;
9use std::thread::panicking;
10use std::time::{Instant, SystemTime, UNIX_EPOCH};
11use std::{env, fs, io, panic, str};
12
13use build_helper::ci::CiEnv;
14use object::read::archive::ArchiveFile;
15pub(crate) use shim_utils::{dylib_path, dylib_path_var};
16
17pub(crate) use self::macros::t;
18use crate::core::builder::{Builder, StepStack};
19use crate::core::config::{BootstrapOverrideLld, Config, TargetSelection};
20use crate::utils::exec::{BootstrapCommand, command};
21
22#[cfg(test)]
23mod tests;
24
25pub struct PanicTracker<'a>(pub &'a panic::Location<'a>);
28
29impl Drop for PanicTracker<'_> {
30 fn drop(&mut self) {
31 if panicking() {
32 eprintln!(
33 "Panic was initiated from {}:{}:{}",
34 self.0.file(),
35 self.0.line(),
36 self.0.column()
37 );
38 }
39 }
40}
41
42mod macros {
43 macro_rules! t {
52 ($e:expr) => {{
53 let _panic_guard = $crate::utils::helpers::PanicTracker(std::panic::Location::caller());
54 match $e {
55 Ok(e) => e,
56 Err(e) => panic!("{} failed with {}", stringify!($e), e),
57 }
58 }};
59 ($e:expr, $extra:expr) => {{
61 let _panic_guard = $crate::utils::helpers::PanicTracker(std::panic::Location::caller());
62 match $e {
63 Ok(e) => e,
64 Err(e) => panic!("{} failed with {} ({:?})", stringify!($e), e, $extra),
65 }
66 }};
67 }
68
69 pub(crate) use t;
70}
71
72pub fn exe(name: &str, target: TargetSelection) -> String {
73 shim_utils::exe(name, &target.triple)
74}
75
76pub fn split_debuginfo(name: impl Into<PathBuf>) -> Option<PathBuf> {
78 let path = name.into();
81 let pdb = path.with_extension("pdb");
82 if pdb.exists() {
83 return Some(pdb);
84 }
85
86 let file_name = pdb.file_name()?.to_str()?.replace("-", "_");
88
89 let pdb: PathBuf = [path.parent()?, Path::new(&file_name)].into_iter().collect();
90 pdb.exists().then_some(pdb)
91}
92
93pub fn is_dylib(path: &Path) -> bool {
95 path.extension().and_then(|ext| ext.to_str()).is_some_and(|ext| {
96 ext == "dylib" || ext == "so" || ext == "dll" || (ext == "a" && is_aix_shared_archive(path))
97 })
98}
99
100pub fn submodule_path_of(builder: &Builder<'_>, path: &str) -> Option<String> {
102 submodule_path_of_paths(builder.submodule_paths(), path)
103}
104
105fn submodule_path_of_paths(submodule_paths: &[String], path: &str) -> Option<String> {
106 let path = Path::new(path);
107 submodule_paths.iter().find_map(|submodule_path| {
108 if path.starts_with(submodule_path) { Some(submodule_path.to_string()) } else { None }
109 })
110}
111
112fn is_aix_shared_archive(path: &Path) -> bool {
113 let file = match fs::File::open(path) {
114 Ok(file) => file,
115 Err(_) => return false,
116 };
117 let reader = object::ReadCache::new(file);
118 let archive = match ArchiveFile::parse(&reader) {
119 Ok(result) => result,
120 Err(_) => return false,
121 };
122
123 archive
124 .members()
125 .filter_map(Result::ok)
126 .any(|entry| String::from_utf8_lossy(entry.name()).contains(".so"))
127}
128
129pub fn is_debug_info(name: &str) -> bool {
131 name.ends_with(".pdb")
133}
134
135pub fn libdir(target: TargetSelection) -> &'static str {
138 if target.is_windows() || target.contains("cygwin") { "bin" } else { "lib" }
139}
140
141pub fn add_dylib_path(path: Vec<PathBuf>, cmd: &mut BootstrapCommand) {
144 let mut list = dylib_path();
145 for path in path {
146 list.insert(0, path);
147 }
148 cmd.env(dylib_path_var(), t!(env::join_paths(list)));
149}
150
151pub struct TimeIt(bool, Instant);
152
153pub fn timeit(builder: &Builder<'_>) -> TimeIt {
155 TimeIt(builder.config.dry_run(), Instant::now())
156}
157
158impl Drop for TimeIt {
159 fn drop(&mut self) {
160 let time = self.1.elapsed();
161 if !self.0 {
162 println!("\tfinished in {}.{:03} seconds", time.as_secs(), time.subsec_millis());
163 }
164 }
165}
166
167pub fn symlink_dir(config: &Config, original: &Path, link: &Path) -> io::Result<()> {
170 if config.dry_run() {
171 return Ok(());
172 }
173 let _ = fs::remove_dir_all(link);
174 return symlink_dir_inner(original, link);
175
176 #[cfg(not(windows))]
177 fn symlink_dir_inner(original: &Path, link: &Path) -> io::Result<()> {
178 use std::os::unix::fs;
179 fs::symlink(original, link)
180 }
181
182 #[cfg(windows)]
183 fn symlink_dir_inner(target: &Path, junction: &Path) -> io::Result<()> {
184 junction::create(target, junction)
185 }
186}
187
188pub fn get_host_target() -> TargetSelection {
190 TargetSelection::from_user(env!("BUILD_TRIPLE"))
191}
192
193pub fn move_file<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
196 match fs::rename(&from, &to) {
197 Err(e) if e.kind() == io::ErrorKind::CrossesDevices => {
198 std::fs::copy(&from, &to)?;
199 std::fs::remove_file(&from)
200 }
201 r => r,
202 }
203}
204
205pub fn forcing_clang_based_tests() -> bool {
206 if let Some(var) = env::var_os("RUSTBUILD_FORCE_CLANG_BASED_TESTS") {
207 match &var.to_string_lossy().to_lowercase()[..] {
208 "1" | "yes" | "on" => true,
209 "0" | "no" | "off" => false,
210 other => {
211 panic!(
213 "Unrecognized option '{other}' set in \
214 RUSTBUILD_FORCE_CLANG_BASED_TESTS"
215 )
216 }
217 }
218 } else {
219 false
220 }
221}
222
223pub fn use_host_linker(target: TargetSelection) -> bool {
224 !(target.contains("emscripten")
227 || target.contains("wasm32")
228 || target.contains("nvptx")
229 || target.contains("fortanix")
230 || target.contains("fuchsia")
231 || target.contains("bpf")
232 || target.contains("switch")
233 || target.contains("l4re"))
234}
235
236pub fn target_supports_cranelift_backend(target: TargetSelection) -> bool {
237 if target.contains("linux") {
238 target.contains("x86_64")
239 || target.contains("aarch64")
240 || target.contains("s390x")
241 || target.contains("riscv64gc")
242 } else if target.contains("darwin") {
243 target.contains("x86_64") || target.contains("aarch64")
244 } else if target.is_windows() {
245 target.contains("x86_64")
246 } else {
247 false
248 }
249}
250
251pub enum TestFilterCategory<'a> {
254 Fullsuite,
256 Arg(&'a str),
259 Uninteresting,
261}
262
263pub fn is_valid_test_suite_arg<'a, P: AsRef<Path>>(
264 path: &'a Path,
265 suite_path: P,
266 builder: &Builder<'_>,
267) -> TestFilterCategory<'a> {
268 let suite_path = suite_path.as_ref();
269 let path = match path.strip_prefix(".") {
270 Ok(p) => p,
271 Err(_) => path,
272 };
273 if !path.starts_with(suite_path) {
274 return TestFilterCategory::Uninteresting;
275 }
276 let abs_path = builder.src.join(path);
277 let exists = abs_path.is_dir() || abs_path.is_file();
278 if !exists {
279 panic!(
280 "Invalid test suite filter \"{}\": file or directory does not exist",
281 abs_path.display()
282 );
283 }
284 match path.strip_prefix(suite_path).ok().and_then(|p| p.to_str()) {
291 Some(s) if !s.is_empty() => TestFilterCategory::Arg(s),
292 _ => TestFilterCategory::Fullsuite,
293 }
294}
295
296pub fn make(host: &str) -> PathBuf {
297 if host.contains("dragonfly")
298 || host.contains("freebsd")
299 || host.contains("netbsd")
300 || host.contains("openbsd")
301 {
302 PathBuf::from("gmake")
303 } else {
304 PathBuf::from("make")
305 }
306}
307
308pub fn mtime(path: &Path) -> SystemTime {
310 fs::metadata(path).and_then(|f| f.modified()).unwrap_or(UNIX_EPOCH)
311}
312
313pub fn up_to_date(src: &Path, dst: &Path) -> bool {
318 if !dst.exists() {
319 return false;
320 }
321 let threshold = mtime(dst);
322 let meta = match fs::metadata(src) {
323 Ok(meta) => meta,
324 Err(e) => panic!("source {src:?} failed to get metadata: {e}"),
325 };
326 if meta.is_dir() {
327 dir_up_to_date(src, threshold)
328 } else {
329 meta.modified().unwrap_or(UNIX_EPOCH) <= threshold
330 }
331}
332
333pub fn unhashed_basename(obj: &Path) -> &str {
338 let basename = obj.file_stem().unwrap().to_str().expect("UTF-8 file name");
339 basename.split_once('-').unwrap().1
340}
341
342fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
343 t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
344 let meta = t!(e.metadata());
345 if meta.is_dir() {
346 dir_up_to_date(&e.path(), threshold)
347 } else {
348 meta.modified().unwrap_or(UNIX_EPOCH) < threshold
349 }
350 })
351}
352
353pub fn get_clang_cl_resource_dir(builder: &Builder<'_>, clang_cl_path: &str) -> PathBuf {
359 let mut builtins_locator = command(clang_cl_path);
362 builtins_locator.args(["/clang:-print-libgcc-file-name", "/clang:--rtlib=compiler-rt"]);
363
364 let clang_rt_builtins = builtins_locator.run_capture_stdout(builder).stdout();
365 let clang_rt_builtins = Path::new(clang_rt_builtins.trim());
366 assert!(
367 clang_rt_builtins.exists(),
368 "`clang-cl` must correctly locate the library runtime directory"
369 );
370
371 let clang_rt_dir = clang_rt_builtins.parent().expect("The clang lib folder should exist");
374 clang_rt_dir.to_path_buf()
375}
376
377fn lld_flag_no_threads(
381 builder: &Builder<'_>,
382 bootstrap_override_lld: BootstrapOverrideLld,
383 is_windows: bool,
384) -> &'static str {
385 static LLD_NO_THREADS: OnceLock<(&'static str, &'static str)> = OnceLock::new();
386
387 let new_flags = ("/threads:1", "--threads=1");
388 let old_flags = ("/no-threads", "--no-threads");
389
390 let (windows_flag, other_flag) = LLD_NO_THREADS.get_or_init(|| {
391 let newer_version = match bootstrap_override_lld {
392 BootstrapOverrideLld::External => {
393 let mut cmd = command("lld");
394 cmd.arg("-flavor").arg("ld").arg("--version");
395 let out = cmd.run_capture_stdout(builder).stdout();
396 match (out.find(char::is_numeric), out.find('.')) {
397 (Some(b), Some(e)) => out.as_str()[b..e].parse::<i32>().ok().unwrap_or(14) > 10,
398 _ => true,
399 }
400 }
401 _ => true,
402 };
403 if newer_version { new_flags } else { old_flags }
404 });
405 if is_windows { windows_flag } else { other_flag }
406}
407
408pub fn dir_is_empty(dir: &Path) -> bool {
409 t!(std::fs::read_dir(dir), dir).next().is_none()
410}
411
412pub fn extract_beta_rev(version: &str) -> Option<String> {
417 let parts = version.splitn(2, "-beta.").collect::<Vec<_>>();
418 parts.get(1).and_then(|s| s.find(' ').map(|p| s[..p].to_string()))
419}
420
421pub enum LldThreads {
422 Yes,
423 No,
424}
425
426pub fn linker_args(
428 builder: &Builder<'_>,
429 target: TargetSelection,
430 lld_threads: LldThreads,
431) -> Vec<String> {
432 let mut args = linker_flags(builder, target, lld_threads);
433
434 if let Some(linker) = builder.linker(target) {
435 args.push(format!("-Clinker={}", linker.display()));
436 }
437
438 args
439}
440
441pub fn linker_flags(
444 builder: &Builder<'_>,
445 target: TargetSelection,
446 lld_threads: LldThreads,
447) -> Vec<String> {
448 let mut args = vec![];
449 if !builder.is_lld_direct_linker(target) && builder.config.bootstrap_override_lld.is_used() {
450 match builder.config.bootstrap_override_lld {
451 BootstrapOverrideLld::External => {
452 args.push("-Clinker-features=+lld".to_string());
453 args.push("-Clink-self-contained=-linker".to_string());
454 args.push("-Zunstable-options".to_string());
455 }
456 BootstrapOverrideLld::SelfContained => {
457 args.push("-Clinker-features=+lld".to_string());
458 args.push("-Clink-self-contained=+linker".to_string());
459 args.push("-Zunstable-options".to_string());
460 }
461 BootstrapOverrideLld::None => unreachable!(),
462 };
463
464 if matches!(lld_threads, LldThreads::No) {
465 args.push(format!(
466 "-Clink-arg=-Wl,{}",
467 lld_flag_no_threads(
468 builder,
469 builder.config.bootstrap_override_lld,
470 target.is_windows()
471 )
472 ));
473 }
474 }
475 args
476}
477
478pub fn add_rustdoc_cargo_linker_args(
479 cmd: &mut BootstrapCommand,
480 builder: &Builder<'_>,
481 target: TargetSelection,
482 lld_threads: LldThreads,
483) {
484 let args = linker_args(builder, target, lld_threads);
485 let mut flags = cmd
486 .get_envs()
487 .find_map(|(k, v)| if k == OsStr::new("RUSTDOCFLAGS") { v } else { None })
488 .unwrap_or_default()
489 .to_os_string();
490 for arg in args {
491 if !flags.is_empty() {
492 flags.push(" ");
493 }
494 flags.push(arg);
495 }
496 if !flags.is_empty() {
497 cmd.env("RUSTDOCFLAGS", flags);
498 }
499}
500
501pub fn hex_encode<T>(input: T) -> String
503where
504 T: AsRef<[u8]>,
505{
506 use std::fmt::Write;
507
508 input.as_ref().iter().fold(String::with_capacity(input.as_ref().len() * 2), |mut acc, &byte| {
509 write!(&mut acc, "{byte:02x}").expect("Failed to write byte to the hex String.");
510 acc
511 })
512}
513
514pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String {
517 let next = match values {
520 Some(values) => {
521 let mut tmp = values.iter().flat_map(|val| [",", "\"", val, "\""]).collect::<String>();
522
523 tmp.insert_str(1, "values(");
524 tmp.push(')');
525 tmp
526 }
527 None => "".to_string(),
528 };
529 format!("--check-cfg=cfg({name}{next})")
530}
531
532#[track_caller]
540pub fn git(source_dir: Option<&Path>) -> BootstrapCommand {
541 let mut git = command("git");
542 git.cached();
544
545 if let Some(source_dir) = source_dir {
546 git.current_dir(source_dir);
547 git.env_remove("GIT_DIR");
550 git.env_remove("GIT_WORK_TREE")
554 .env_remove("GIT_INDEX_FILE")
555 .env_remove("GIT_OBJECT_DIRECTORY")
556 .env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES");
557 }
558
559 git
560}
561
562pub fn set_file_times<P: AsRef<Path>>(path: P, times: fs::FileTimes) -> io::Result<()> {
564 let f = if cfg!(windows) {
567 fs::File::options().write(true).open(path)?
568 } else {
569 fs::File::open(path)?
570 };
571 f.set_times(times)
572}
573
574pub(crate) fn exit_process(code: i32) -> ! {
582 if cfg!(test) {
584 panic!("status code: {code}");
585 } else {
586 if CiEnv::is_ci() {
589 let bootstrap_args =
591 std::env::args().skip(1).map(|a| a.to_string()).collect::<Vec<_>>().join(" ");
592 eprintln!("Bootstrap failed while executing `{bootstrap_args}`");
593 eprintln!("Currently active steps:");
594 StepStack::with_current(|stack| {
595 for step in stack.get_active_steps() {
596 eprintln!("{} at {}", step.info, step.location);
597 }
598 });
599 }
600
601 std::process::exit(code);
603 }
604}
605
606pub fn fail(s: &str) -> ! {
607 eprintln!("\n\n{s}\n\n");
608 exit_process(1);
609}