1use std::ffi::OsStr;
7use std::path::{Path, PathBuf};
8use std::process::{Command, Stdio};
9use std::sync::OnceLock;
10use std::time::{Instant, SystemTime, UNIX_EPOCH};
11use std::{env, fs, io, str};
12
13use build_helper::util::fail;
14use object::read::archive::ArchiveFile;
15
16use crate::LldMode;
17use crate::core::builder::Builder;
18use crate::core::config::{Config, TargetSelection};
19use crate::utils::exec::{BootstrapCommand, command};
20pub use crate::utils::shared_helpers::{dylib_path, dylib_path_var};
21
22#[cfg(test)]
23mod tests;
24
25#[macro_export]
34macro_rules! t {
35 ($e:expr) => {
36 match $e {
37 Ok(e) => e,
38 Err(e) => panic!("{} failed with {}", stringify!($e), e),
39 }
40 };
41 ($e:expr, $extra:expr) => {
43 match $e {
44 Ok(e) => e,
45 Err(e) => panic!("{} failed with {} ({:?})", stringify!($e), e, $extra),
46 }
47 };
48}
49
50pub use t;
51pub fn exe(name: &str, target: TargetSelection) -> String {
52 crate::utils::shared_helpers::exe(name, &target.triple)
53}
54
55pub fn is_dylib(path: &Path) -> bool {
57 path.extension().and_then(|ext| ext.to_str()).is_some_and(|ext| {
58 ext == "dylib" || ext == "so" || ext == "dll" || (ext == "a" && is_aix_shared_archive(path))
59 })
60}
61
62pub fn submodule_path_of(builder: &Builder<'_>, path: &str) -> Option<String> {
64 let submodule_paths = build_helper::util::parse_gitmodules(&builder.src);
65 submodule_paths.iter().find_map(|submodule_path| {
66 if path.starts_with(submodule_path) { Some(submodule_path.to_string()) } else { None }
67 })
68}
69
70fn is_aix_shared_archive(path: &Path) -> bool {
71 let file = match fs::File::open(path) {
72 Ok(file) => file,
73 Err(_) => return false,
74 };
75 let reader = object::ReadCache::new(file);
76 let archive = match ArchiveFile::parse(&reader) {
77 Ok(result) => result,
78 Err(_) => return false,
79 };
80
81 archive
82 .members()
83 .filter_map(Result::ok)
84 .any(|entry| String::from_utf8_lossy(entry.name()).contains(".so"))
85}
86
87pub fn is_debug_info(name: &str) -> bool {
89 name.ends_with(".pdb")
91}
92
93pub fn libdir(target: TargetSelection) -> &'static str {
96 if target.is_windows() { "bin" } else { "lib" }
97}
98
99pub fn add_dylib_path(path: Vec<PathBuf>, cmd: &mut BootstrapCommand) {
102 let mut list = dylib_path();
103 for path in path {
104 list.insert(0, path);
105 }
106 cmd.env(dylib_path_var(), t!(env::join_paths(list)));
107}
108
109pub struct TimeIt(bool, Instant);
110
111pub fn timeit(builder: &Builder<'_>) -> TimeIt {
113 TimeIt(builder.config.dry_run(), Instant::now())
114}
115
116impl Drop for TimeIt {
117 fn drop(&mut self) {
118 let time = self.1.elapsed();
119 if !self.0 {
120 println!("\tfinished in {}.{:03} seconds", time.as_secs(), time.subsec_millis());
121 }
122 }
123}
124
125pub fn symlink_dir(config: &Config, original: &Path, link: &Path) -> io::Result<()> {
128 if config.dry_run() {
129 return Ok(());
130 }
131 let _ = fs::remove_dir_all(link);
132 return symlink_dir_inner(original, link);
133
134 #[cfg(not(windows))]
135 fn symlink_dir_inner(original: &Path, link: &Path) -> io::Result<()> {
136 use std::os::unix::fs;
137 fs::symlink(original, link)
138 }
139
140 #[cfg(windows)]
141 fn symlink_dir_inner(target: &Path, junction: &Path) -> io::Result<()> {
142 junction::create(target, junction)
143 }
144}
145
146pub fn move_file<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
149 match fs::rename(&from, &to) {
150 Err(e) if e.kind() == io::ErrorKind::CrossesDevices => {
151 std::fs::copy(&from, &to)?;
152 std::fs::remove_file(&from)
153 }
154 r => r,
155 }
156}
157
158pub fn forcing_clang_based_tests() -> bool {
159 if let Some(var) = env::var_os("RUSTBUILD_FORCE_CLANG_BASED_TESTS") {
160 match &var.to_string_lossy().to_lowercase()[..] {
161 "1" | "yes" | "on" => true,
162 "0" | "no" | "off" => false,
163 other => {
164 panic!(
166 "Unrecognized option '{other}' set in \
167 RUSTBUILD_FORCE_CLANG_BASED_TESTS"
168 )
169 }
170 }
171 } else {
172 false
173 }
174}
175
176pub fn use_host_linker(target: TargetSelection) -> bool {
177 !(target.contains("emscripten")
180 || target.contains("wasm32")
181 || target.contains("nvptx")
182 || target.contains("fortanix")
183 || target.contains("fuchsia")
184 || target.contains("bpf")
185 || target.contains("switch"))
186}
187
188pub fn target_supports_cranelift_backend(target: TargetSelection) -> bool {
189 if target.contains("linux") {
190 target.contains("x86_64")
191 || target.contains("aarch64")
192 || target.contains("s390x")
193 || target.contains("riscv64gc")
194 } else if target.contains("darwin") {
195 target.contains("x86_64") || target.contains("aarch64")
196 } else if target.is_windows() {
197 target.contains("x86_64")
198 } else {
199 false
200 }
201}
202
203pub fn is_valid_test_suite_arg<'a, P: AsRef<Path>>(
204 path: &'a Path,
205 suite_path: P,
206 builder: &Builder<'_>,
207) -> Option<&'a str> {
208 let suite_path = suite_path.as_ref();
209 let path = match path.strip_prefix(".") {
210 Ok(p) => p,
211 Err(_) => path,
212 };
213 if !path.starts_with(suite_path) {
214 return None;
215 }
216 let abs_path = builder.src.join(path);
217 let exists = abs_path.is_dir() || abs_path.is_file();
218 if !exists {
219 panic!(
220 "Invalid test suite filter \"{}\": file or directory does not exist",
221 abs_path.display()
222 );
223 }
224 match path.strip_prefix(suite_path).ok().and_then(|p| p.to_str()) {
231 Some(s) if !s.is_empty() => Some(s),
232 _ => None,
233 }
234}
235
236pub fn check_run(cmd: &mut BootstrapCommand, print_cmd_on_fail: bool) -> bool {
238 let status = match cmd.as_command_mut().status() {
239 Ok(status) => status,
240 Err(e) => {
241 println!("failed to execute command: {cmd:?}\nERROR: {e}");
242 return false;
243 }
244 };
245 if !status.success() && print_cmd_on_fail {
246 println!(
247 "\n\ncommand did not execute successfully: {cmd:?}\n\
248 expected success, got: {status}\n\n"
249 );
250 }
251 status.success()
252}
253
254pub fn make(host: &str) -> PathBuf {
255 if host.contains("dragonfly")
256 || host.contains("freebsd")
257 || host.contains("netbsd")
258 || host.contains("openbsd")
259 {
260 PathBuf::from("gmake")
261 } else {
262 PathBuf::from("make")
263 }
264}
265
266#[track_caller]
267pub fn output(cmd: &mut Command) -> String {
268 #[cfg(feature = "tracing")]
269 let _run_span = crate::trace_cmd!(cmd);
270
271 let output = match cmd.stderr(Stdio::inherit()).output() {
272 Ok(status) => status,
273 Err(e) => fail(&format!("failed to execute command: {cmd:?}\nERROR: {e}")),
274 };
275 if !output.status.success() {
276 panic!(
277 "command did not execute successfully: {:?}\n\
278 expected success, got: {}",
279 cmd, output.status
280 );
281 }
282 String::from_utf8(output.stdout).unwrap()
283}
284
285#[track_caller]
289pub fn start_process(cmd: &mut Command) -> impl FnOnce() -> String {
290 let child = match cmd.stderr(Stdio::inherit()).stdout(Stdio::piped()).spawn() {
291 Ok(child) => child,
292 Err(e) => fail(&format!("failed to execute command: {cmd:?}\nERROR: {e}")),
293 };
294
295 let command = format!("{:?}", cmd);
296
297 move || {
298 let output = child.wait_with_output().unwrap();
299
300 if !output.status.success() {
301 panic!(
302 "command did not execute successfully: {}\n\
303 expected success, got: {}",
304 command, output.status
305 );
306 }
307
308 String::from_utf8(output.stdout).unwrap()
309 }
310}
311
312pub fn mtime(path: &Path) -> SystemTime {
314 fs::metadata(path).and_then(|f| f.modified()).unwrap_or(UNIX_EPOCH)
315}
316
317pub fn up_to_date(src: &Path, dst: &Path) -> bool {
322 if !dst.exists() {
323 return false;
324 }
325 let threshold = mtime(dst);
326 let meta = match fs::metadata(src) {
327 Ok(meta) => meta,
328 Err(e) => panic!("source {src:?} failed to get metadata: {e}"),
329 };
330 if meta.is_dir() {
331 dir_up_to_date(src, threshold)
332 } else {
333 meta.modified().unwrap_or(UNIX_EPOCH) <= threshold
334 }
335}
336
337pub fn unhashed_basename(obj: &Path) -> &str {
342 let basename = obj.file_stem().unwrap().to_str().expect("UTF-8 file name");
343 basename.split_once('-').unwrap().1
344}
345
346fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
347 t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
348 let meta = t!(e.metadata());
349 if meta.is_dir() {
350 dir_up_to_date(&e.path(), threshold)
351 } else {
352 meta.modified().unwrap_or(UNIX_EPOCH) < threshold
353 }
354 })
355}
356
357pub fn get_clang_cl_resource_dir(builder: &Builder<'_>, clang_cl_path: &str) -> PathBuf {
363 let mut builtins_locator = command(clang_cl_path);
366 builtins_locator.args(["/clang:-print-libgcc-file-name", "/clang:--rtlib=compiler-rt"]);
367
368 let clang_rt_builtins = builtins_locator.run_capture_stdout(builder).stdout();
369 let clang_rt_builtins = Path::new(clang_rt_builtins.trim());
370 assert!(
371 clang_rt_builtins.exists(),
372 "`clang-cl` must correctly locate the library runtime directory"
373 );
374
375 let clang_rt_dir = clang_rt_builtins.parent().expect("The clang lib folder should exist");
378 clang_rt_dir.to_path_buf()
379}
380
381fn lld_flag_no_threads(builder: &Builder<'_>, lld_mode: LldMode, is_windows: bool) -> &'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 lld_mode {
392 LldMode::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 let count = parts.get(1).and_then(|s| s.find(' ').map(|p| s[..p].to_string()));
419
420 count
421}
422
423pub enum LldThreads {
424 Yes,
425 No,
426}
427
428pub fn linker_args(
430 builder: &Builder<'_>,
431 target: TargetSelection,
432 lld_threads: LldThreads,
433) -> Vec<String> {
434 let mut args = linker_flags(builder, target, lld_threads);
435
436 if let Some(linker) = builder.linker(target) {
437 args.push(format!("-Clinker={}", linker.display()));
438 }
439
440 args
441}
442
443pub fn linker_flags(
446 builder: &Builder<'_>,
447 target: TargetSelection,
448 lld_threads: LldThreads,
449) -> Vec<String> {
450 let mut args = vec![];
451 if !builder.is_lld_direct_linker(target) && builder.config.lld_mode.is_used() {
452 match builder.config.lld_mode {
453 LldMode::External => {
454 args.push("-Clinker-flavor=gnu-lld-cc".to_string());
455 args.push("-Zunstable-options".to_string());
457 }
458 LldMode::SelfContained => {
459 args.push("-Clinker-flavor=gnu-lld-cc".to_string());
460 args.push("-Clink-self-contained=+linker".to_string());
461 args.push("-Zunstable-options".to_string());
463 }
464 LldMode::Unused => unreachable!(),
465 };
466
467 if matches!(lld_threads, LldThreads::No) {
468 args.push(format!(
469 "-Clink-arg=-Wl,{}",
470 lld_flag_no_threads(builder, builder.config.lld_mode, target.is_windows())
471 ));
472 }
473 }
474 args
475}
476
477pub fn add_rustdoc_cargo_linker_args(
478 cmd: &mut BootstrapCommand,
479 builder: &Builder<'_>,
480 target: TargetSelection,
481 lld_threads: LldThreads,
482) {
483 let args = linker_args(builder, target, lld_threads);
484 let mut flags = cmd
485 .get_envs()
486 .find_map(|(k, v)| if k == OsStr::new("RUSTDOCFLAGS") { v } else { None })
487 .unwrap_or_default()
488 .to_os_string();
489 for arg in args {
490 if !flags.is_empty() {
491 flags.push(" ");
492 }
493 flags.push(arg);
494 }
495 if !flags.is_empty() {
496 cmd.env("RUSTDOCFLAGS", flags);
497 }
498}
499
500pub fn hex_encode<T>(input: T) -> String
502where
503 T: AsRef<[u8]>,
504{
505 use std::fmt::Write;
506
507 input.as_ref().iter().fold(String::with_capacity(input.as_ref().len() * 2), |mut acc, &byte| {
508 write!(&mut acc, "{:02x}", byte).expect("Failed to write byte to the hex String.");
509 acc
510 })
511}
512
513pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String {
516 let next = match values {
519 Some(values) => {
520 let mut tmp = values.iter().flat_map(|val| [",", "\"", val, "\""]).collect::<String>();
521
522 tmp.insert_str(1, "values(");
523 tmp.push(')');
524 tmp
525 }
526 None => "".to_string(),
527 };
528 format!("--check-cfg=cfg({name}{next})")
529}
530
531#[track_caller]
539pub fn git(source_dir: Option<&Path>) -> BootstrapCommand {
540 let mut git = command("git");
541
542 if let Some(source_dir) = source_dir {
543 git.current_dir(source_dir);
544 git.env_remove("GIT_DIR");
547 git.env_remove("GIT_WORK_TREE")
551 .env_remove("GIT_INDEX_FILE")
552 .env_remove("GIT_OBJECT_DIRECTORY")
553 .env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES");
554 }
555
556 git
557}
558
559pub fn set_file_times<P: AsRef<Path>>(path: P, times: fs::FileTimes) -> io::Result<()> {
561 let f = if cfg!(windows) {
564 fs::File::options().write(true).open(path)?
565 } else {
566 fs::File::open(path)?
567 };
568 f.set_times(times)
569}