-
Notifications
You must be signed in to change notification settings - Fork 13.4k
unify dylib
and bin_helpers
and create shared_helpers::parse_value_from_args
#127108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
//! This module serves two purposes: | ||
//! 1. It is part of the `utils` module and used in other parts of bootstrap. | ||
//! 2. It is embedded inside bootstrap shims to avoid a dependency on the bootstrap library. | ||
//! Therefore, this module should never use any other bootstrap module. This reduces binary | ||
//! size and improves compilation time by minimizing linking time. | ||
|
||
#![allow(dead_code)] | ||
|
||
use std::env; | ||
use std::ffi::OsString; | ||
use std::fs::OpenOptions; | ||
use std::io::Write; | ||
use std::process::Command; | ||
use std::str::FromStr; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
/// Returns the environment variable which the dynamic library lookup path | ||
/// resides in for this platform. | ||
pub fn dylib_path_var() -> &'static str { | ||
if cfg!(target_os = "windows") { | ||
"PATH" | ||
} else if cfg!(target_vendor = "apple") { | ||
"DYLD_LIBRARY_PATH" | ||
} else if cfg!(target_os = "haiku") { | ||
"LIBRARY_PATH" | ||
} else if cfg!(target_os = "aix") { | ||
"LIBPATH" | ||
} else { | ||
"LD_LIBRARY_PATH" | ||
} | ||
} | ||
|
||
/// Parses the `dylib_path_var()` environment variable, returning a list of | ||
/// paths that are members of this lookup path. | ||
pub fn dylib_path() -> Vec<std::path::PathBuf> { | ||
let var = match std::env::var_os(dylib_path_var()) { | ||
Some(v) => v, | ||
None => return vec![], | ||
}; | ||
std::env::split_paths(&var).collect() | ||
} | ||
|
||
/// Given an executable called `name`, return the filename for the | ||
/// executable for a particular target. | ||
pub fn exe(name: &str, target: &str) -> String { | ||
if target.contains("windows") { | ||
format!("{name}.exe") | ||
} else if target.contains("uefi") { | ||
format!("{name}.efi") | ||
} else { | ||
name.to_string() | ||
} | ||
} | ||
|
||
/// Parses the value of the "RUSTC_VERBOSE" environment variable and returns it as a `usize`. | ||
/// If it was not defined, returns 0 by default. | ||
/// | ||
/// Panics if "RUSTC_VERBOSE" is defined with the value that is not an unsigned integer. | ||
pub fn parse_rustc_verbose() -> usize { | ||
match env::var("RUSTC_VERBOSE") { | ||
Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"), | ||
Err(_) => 0, | ||
} | ||
} | ||
|
||
/// Parses the value of the "RUSTC_STAGE" environment variable and returns it as a `String`. | ||
/// | ||
/// If "RUSTC_STAGE" was not set, the program will be terminated with 101. | ||
pub fn parse_rustc_stage() -> String { | ||
env::var("RUSTC_STAGE").unwrap_or_else(|_| { | ||
// Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead. | ||
eprintln!("rustc shim: FATAL: RUSTC_STAGE was not set"); | ||
eprintln!("rustc shim: NOTE: use `x.py build -vvv` to see all environment variables set by bootstrap"); | ||
std::process::exit(101); | ||
}) | ||
} | ||
|
||
/// Writes the command invocation to a file if `DUMP_BOOTSTRAP_SHIMS` is set during bootstrap. | ||
/// | ||
/// Before writing it, replaces user-specific values to create generic dumps for cross-environment | ||
/// comparisons. | ||
pub fn maybe_dump(dump_name: String, cmd: &Command) { | ||
if let Ok(dump_dir) = env::var("DUMP_BOOTSTRAP_SHIMS") { | ||
let dump_file = format!("{dump_dir}/{dump_name}"); | ||
|
||
let mut file = OpenOptions::new().create(true).append(true).open(dump_file).unwrap(); | ||
|
||
let cmd_dump = format!("{:?}\n", cmd); | ||
let cmd_dump = cmd_dump.replace(&env::var("BUILD_OUT").unwrap(), "${BUILD_OUT}"); | ||
let cmd_dump = cmd_dump.replace(&env::var("CARGO_HOME").unwrap(), "${CARGO_HOME}"); | ||
|
||
file.write_all(cmd_dump.as_bytes()).expect("Unable to write file"); | ||
} | ||
} | ||
|
||
/// Finds `key` and returns its value from the given list of arguments `args`. | ||
pub fn parse_value_from_args<'a>(args: &'a [OsString], key: &str) -> Option<&'a str> { | ||
let mut args = args.iter(); | ||
while let Some(arg) = args.next() { | ||
let arg = arg.to_str().unwrap(); | ||
|
||
if let Some(value) = arg.strip_prefix(&format!("{key}=")) { | ||
return Some(value); | ||
} else if arg == key { | ||
return args.next().map(|v| v.to_str().unwrap()); | ||
} | ||
} | ||
|
||
None | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use super::parse_value_from_args; | ||
|
||
#[test] | ||
fn test_parse_value_from_args() { | ||
let args = vec![ | ||
"--stage".into(), | ||
"1".into(), | ||
"--version".into(), | ||
"2".into(), | ||
"--target".into(), | ||
"x86_64-unknown-linux".into(), | ||
]; | ||
|
||
assert_eq!(parse_value_from_args(args.as_slice(), "--stage").unwrap(), "1"); | ||
assert_eq!(parse_value_from_args(args.as_slice(), "--version").unwrap(), "2"); | ||
assert_eq!(parse_value_from_args(args.as_slice(), "--target").unwrap(), "x86_64-unknown-linux"); | ||
assert!(parse_value_from_args(args.as_slice(), "random-key").is_none()); | ||
|
||
let args = vec![ | ||
"app-name".into(), | ||
"--key".into(), | ||
"value".into(), | ||
"random-value".into(), | ||
"--sysroot=/x/y/z".into(), | ||
]; | ||
assert_eq!(parse_value_from_args(args.as_slice(), "--key").unwrap(), "value"); | ||
assert_eq!(parse_value_from_args(args.as_slice(), "--sysroot").unwrap(), "/x/y/z"); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.