|
| 1 | +//! This module serves two purposes: |
| 2 | +//! 1. It is part of the `utils` module and used in other parts of bootstrap. |
| 3 | +//! 2. It is embedded inside bootstrap shims to avoid a dependency on the bootstrap library. |
| 4 | +//! Therefore, this module should never use any other bootstrap module. This reduces binary |
| 5 | +//! size and improves compilation time by minimizing linking time. |
| 6 | +
|
| 7 | +#![allow(dead_code)] |
| 8 | + |
| 9 | +use std::env; |
| 10 | +use std::ffi::OsString; |
| 11 | +use std::fs::OpenOptions; |
| 12 | +use std::io::Write; |
| 13 | +use std::process::Command; |
| 14 | +use std::str::FromStr; |
| 15 | + |
| 16 | +#[cfg(test)] |
| 17 | +mod tests; |
| 18 | + |
| 19 | +/// Returns the environment variable which the dynamic library lookup path |
| 20 | +/// resides in for this platform. |
| 21 | +pub fn dylib_path_var() -> &'static str { |
| 22 | + if cfg!(target_os = "windows") { |
| 23 | + "PATH" |
| 24 | + } else if cfg!(target_vendor = "apple") { |
| 25 | + "DYLD_LIBRARY_PATH" |
| 26 | + } else if cfg!(target_os = "haiku") { |
| 27 | + "LIBRARY_PATH" |
| 28 | + } else if cfg!(target_os = "aix") { |
| 29 | + "LIBPATH" |
| 30 | + } else { |
| 31 | + "LD_LIBRARY_PATH" |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Parses the `dylib_path_var()` environment variable, returning a list of |
| 36 | +/// paths that are members of this lookup path. |
| 37 | +pub fn dylib_path() -> Vec<std::path::PathBuf> { |
| 38 | + let var = match std::env::var_os(dylib_path_var()) { |
| 39 | + Some(v) => v, |
| 40 | + None => return vec![], |
| 41 | + }; |
| 42 | + std::env::split_paths(&var).collect() |
| 43 | +} |
| 44 | + |
| 45 | +/// Given an executable called `name`, return the filename for the |
| 46 | +/// executable for a particular target. |
| 47 | +pub fn exe(name: &str, target: &str) -> String { |
| 48 | + if target.contains("windows") { |
| 49 | + format!("{name}.exe") |
| 50 | + } else if target.contains("uefi") { |
| 51 | + format!("{name}.efi") |
| 52 | + } else { |
| 53 | + name.to_string() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// Parses the value of the "RUSTC_VERBOSE" environment variable and returns it as a `usize`. |
| 58 | +/// If it was not defined, returns 0 by default. |
| 59 | +/// |
| 60 | +/// Panics if "RUSTC_VERBOSE" is defined with the value that is not an unsigned integer. |
| 61 | +pub fn parse_rustc_verbose() -> usize { |
| 62 | + match env::var("RUSTC_VERBOSE") { |
| 63 | + Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"), |
| 64 | + Err(_) => 0, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Parses the value of the "RUSTC_STAGE" environment variable and returns it as a `String`. |
| 69 | +/// |
| 70 | +/// If "RUSTC_STAGE" was not set, the program will be terminated with 101. |
| 71 | +pub fn parse_rustc_stage() -> String { |
| 72 | + env::var("RUSTC_STAGE").unwrap_or_else(|_| { |
| 73 | + // Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead. |
| 74 | + eprintln!("rustc shim: FATAL: RUSTC_STAGE was not set"); |
| 75 | + eprintln!("rustc shim: NOTE: use `x.py build -vvv` to see all environment variables set by bootstrap"); |
| 76 | + std::process::exit(101); |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +/// Writes the command invocation to a file if `DUMP_BOOTSTRAP_SHIMS` is set during bootstrap. |
| 81 | +/// |
| 82 | +/// Before writing it, replaces user-specific values to create generic dumps for cross-environment |
| 83 | +/// comparisons. |
| 84 | +pub fn maybe_dump(dump_name: String, cmd: &Command) { |
| 85 | + if let Ok(dump_dir) = env::var("DUMP_BOOTSTRAP_SHIMS") { |
| 86 | + let dump_file = format!("{dump_dir}/{dump_name}"); |
| 87 | + |
| 88 | + let mut file = OpenOptions::new().create(true).append(true).open(dump_file).unwrap(); |
| 89 | + |
| 90 | + let cmd_dump = format!("{:?}\n", cmd); |
| 91 | + let cmd_dump = cmd_dump.replace(&env::var("BUILD_OUT").unwrap(), "${BUILD_OUT}"); |
| 92 | + let cmd_dump = cmd_dump.replace(&env::var("CARGO_HOME").unwrap(), "${CARGO_HOME}"); |
| 93 | + |
| 94 | + file.write_all(cmd_dump.as_bytes()).expect("Unable to write file"); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// Finds `key` and returns its value from the given list of arguments `args`. |
| 99 | +pub fn parse_value_from_args<'a>(args: &'a [OsString], key: &str) -> Option<&'a str> { |
| 100 | + let mut args = args.iter(); |
| 101 | + while let Some(arg) = args.next() { |
| 102 | + let arg = arg.to_str().unwrap(); |
| 103 | + |
| 104 | + if let Some(value) = arg.strip_prefix(&format!("{key}=")) { |
| 105 | + return Some(value); |
| 106 | + } else if arg == key { |
| 107 | + return args.next().map(|v| v.to_str().unwrap()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + None |
| 112 | +} |
0 commit comments