bootstrap/utils/
helpers.rs

1//! Various utility functions used throughout bootstrap.
2//!
3//! Simple things like testing the various filesystem operations here and there,
4//! not a lot of interesting happenings here unfortunately.
5
6use 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/// A helper macro to `unwrap` a result except also print out details like:
26///
27/// * The file/line of the panic
28/// * The expression that failed
29/// * The error itself
30///
31/// This is currently used judiciously throughout the build system rather than
32/// using a `Result` with `try!`, but this may change one day...
33#[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    // it can show extra info in the second parameter
42    ($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
55/// Returns `true` if the file name given looks like a dynamic library.
56pub 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
62/// Return the path to the containing submodule if available.
63pub 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
87/// Returns `true` if the file name given looks like a debug info file
88pub fn is_debug_info(name: &str) -> bool {
89    // FIXME: consider split debug info on other platforms (e.g., Linux, macOS)
90    name.ends_with(".pdb")
91}
92
93/// Returns the corresponding relative library directory that the compiler's
94/// dylibs will be found in.
95pub fn libdir(target: TargetSelection) -> &'static str {
96    if target.is_windows() { "bin" } else { "lib" }
97}
98
99/// Adds a list of lookup paths to `cmd`'s dynamic library lookup path.
100/// If the dylib_path_var is already set for this cmd, the old value will be overwritten!
101pub 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
111/// Returns an RAII structure that prints out how long it took to drop.
112pub 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
125/// Symlinks two directories, using junctions on Windows and normal symlinks on
126/// Unix.
127pub 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
146/// Rename a file if from and to are in the same filesystem or
147/// copy and remove the file otherwise
148pub 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                // Let's make sure typos don't go unnoticed
165                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    // FIXME: this information should be gotten by checking the linker flavor
178    // of the rustc target
179    !(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    // Since test suite paths are themselves directories, if we don't
225    // specify a directory or file, we'll get an empty string here
226    // (the result of the test suite directory without its suite prefix).
227    // Therefore, we need to filter these out, as only the first --test-args
228    // flag is respected, so providing an empty --test-args conflicts with
229    // any following it.
230    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
236// FIXME: get rid of this function
237pub 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/// Spawn a process and return a closure that will wait for the process
286/// to finish and then return its output. This allows the spawned process
287/// to do work without immediately blocking bootstrap.
288#[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
312/// Returns the last-modified time for `path`, or zero if it doesn't exist.
313pub fn mtime(path: &Path) -> SystemTime {
314    fs::metadata(path).and_then(|f| f.modified()).unwrap_or(UNIX_EPOCH)
315}
316
317/// Returns `true` if `dst` is up to date given that the file or files in `src`
318/// are used to generate it.
319///
320/// Uses last-modified time checks to verify this.
321pub 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
337/// Returns the filename without the hash prefix added by the cc crate.
338///
339/// Since v1.0.78 of the cc crate, object files are prefixed with a 16-character hash
340/// to avoid filename collisions.
341pub 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
357/// Adapted from <https://github.com/llvm/llvm-project/blob/782e91224601e461c019e0a4573bbccc6094fbcd/llvm/cmake/modules/HandleLLVMOptions.cmake#L1058-L1079>
358///
359/// When `clang-cl` is used with instrumentation, we need to add clang's runtime library resource
360/// directory to the linker flags, otherwise there will be linker errors about the profiler runtime
361/// missing. This function returns the path to that directory.
362pub fn get_clang_cl_resource_dir(builder: &Builder<'_>, clang_cl_path: &str) -> PathBuf {
363    // Similar to how LLVM does it, to find clang's library runtime directory:
364    // - we ask `clang-cl` to locate the `clang_rt.builtins` lib.
365    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    // - the profiler runtime will be located in the same directory as the builtins lib, like
376    // `$LLVM_DISTRO_ROOT/lib/clang/$LLVM_VERSION/lib/windows`.
377    let clang_rt_dir = clang_rt_builtins.parent().expect("The clang lib folder should exist");
378    clang_rt_dir.to_path_buf()
379}
380
381/// Returns a flag that configures LLD to use only a single thread.
382/// If we use an external LLD, we need to find out which version is it to know which flag should we
383/// pass to it (LLD older than version 10 had a different flag).
384fn 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
412/// Extract the beta revision from the full version string.
413///
414/// The full version string looks like "a.b.c-beta.y". And we need to extract
415/// the "y" part from the string.
416pub 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
428/// Returns the linker arguments for rustc/rustdoc for the given builder and target.
429pub 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
443/// Returns the linker arguments for rustc/rustdoc for the given builder and target, without the
444/// -Clinker flag.
445pub 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                // FIXME(kobzol): remove this flag once MCP510 gets stabilized
456                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                // FIXME(kobzol): remove this flag once MCP510 gets stabilized
462                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
500/// Converts `T` into a hexadecimal `String`.
501pub 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
513/// Create a `--check-cfg` argument invocation for a given name
514/// and it's values.
515pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String {
516    // Creating a string of the values by concatenating each value:
517    // ',values("tvos","watchos")' or '' (nothing) when there are no values.
518    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/// Prepares `BootstrapCommand` that runs git inside the source directory if given.
532///
533/// Whenever a git invocation is needed, this function should be preferred over
534/// manually building a git `BootstrapCommand`. This approach allows us to manage
535/// bootstrap-specific needs/hacks from a single source, rather than applying them on next to every
536/// git command creation, which is painful to ensure that the required change is applied
537/// on each one of them correctly.
538#[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        // If we are running inside git (e.g. via a hook), `GIT_DIR` is set and takes precedence
545        // over the current dir. Un-set it to make the current dir matter.
546        git.env_remove("GIT_DIR");
547        // Also un-set some other variables, to be on the safe side (based on cargo's
548        // `fetch_with_cli`). In particular un-setting `GIT_INDEX_FILE` is required to fix some odd
549        // misbehavior.
550        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
559/// Sets the file times for a given file at `path`.
560pub fn set_file_times<P: AsRef<Path>>(path: P, times: fs::FileTimes) -> io::Result<()> {
561    // Windows requires file to be writable to modify file times. But on Linux CI the file does not
562    // need to be writable to modify file times and might be read-only.
563    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}