Skip to content

Commit 0afc774

Browse files
committed
unify bin_helpers and dylib utility modules
Signed-off-by: onur-ozkan <[email protected]>
1 parent 036b38c commit 0afc774

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

src/bootstrap/src/utils/dylib.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/bootstrap/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ pub(crate) mod cache;
66
pub(crate) mod cc_detect;
77
pub(crate) mod change_tracker;
88
pub(crate) mod channel;
9-
pub(crate) mod dylib;
109
pub(crate) mod exec;
1110
pub(crate) mod helpers;
1211
pub(crate) mod job;
1312
#[cfg(feature = "build-metrics")]
1413
pub(crate) mod metrics;
1514
pub(crate) mod render_tests;
15+
pub(crate) mod shared_helpers;
1616
pub(crate) mod tarball;

src/bootstrap/src/utils/bin_helpers.rs renamed to src/bootstrap/src/utils/shared_helpers.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,65 @@
1-
//! This file is meant to be included directly from bootstrap shims to avoid a
2-
//! dependency on the bootstrap library. This reduces the binary size and
3-
//! improves compilation time by reducing the linking time.
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)]
48

59
use std::env;
10+
use std::ffi::OsString;
611
use std::fs::OpenOptions;
712
use std::io::Write;
813
use std::process::Command;
914
use std::str::FromStr;
1015

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+
#[allow(dead_code)]
48+
pub fn exe(name: &str, target: &str) -> String {
49+
if target.contains("windows") {
50+
format!("{name}.exe")
51+
} else if target.contains("uefi") {
52+
format!("{name}.efi")
53+
} else {
54+
name.to_string()
55+
}
56+
}
57+
1158
/// Parses the value of the "RUSTC_VERBOSE" environment variable and returns it as a `usize`.
1259
/// If it was not defined, returns 0 by default.
1360
///
1461
/// Panics if "RUSTC_VERBOSE" is defined with the value that is not an unsigned integer.
15-
pub(crate) fn parse_rustc_verbose() -> usize {
62+
pub fn parse_rustc_verbose() -> usize {
1663
match env::var("RUSTC_VERBOSE") {
1764
Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
1865
Err(_) => 0,
@@ -22,7 +69,7 @@ pub(crate) fn parse_rustc_verbose() -> usize {
2269
/// Parses the value of the "RUSTC_STAGE" environment variable and returns it as a `String`.
2370
///
2471
/// If "RUSTC_STAGE" was not set, the program will be terminated with 101.
25-
pub(crate) fn parse_rustc_stage() -> String {
72+
pub fn parse_rustc_stage() -> String {
2673
env::var("RUSTC_STAGE").unwrap_or_else(|_| {
2774
// Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead.
2875
eprintln!("rustc shim: FATAL: RUSTC_STAGE was not set");
@@ -35,7 +82,7 @@ pub(crate) fn parse_rustc_stage() -> String {
3582
///
3683
/// Before writing it, replaces user-specific values to create generic dumps for cross-environment
3784
/// comparisons.
38-
pub(crate) fn maybe_dump(dump_name: String, cmd: &Command) {
85+
pub fn maybe_dump(dump_name: String, cmd: &Command) {
3986
if let Ok(dump_dir) = env::var("DUMP_BOOTSTRAP_SHIMS") {
4087
let dump_file = format!("{dump_dir}/{dump_name}");
4188

0 commit comments

Comments
 (0)