Skip to content

Commit 9098474

Browse files
committed
create shared_helpers::parse_value_from_args
Signed-off-by: onur-ozkan <[email protected]>
1 parent 0afc774 commit 9098474

File tree

5 files changed

+69
-27
lines changed

5 files changed

+69
-27
lines changed

src/bootstrap/src/bin/rustc.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,24 @@ use std::path::{Path, PathBuf};
2020
use std::process::{Child, Command};
2121
use std::time::Instant;
2222

23-
use dylib_util::{dylib_path, dylib_path_var, exe};
23+
use shared_helpers::{
24+
dylib_path, dylib_path_var, exe, maybe_dump, parse_rustc_stage, parse_rustc_verbose,
25+
parse_value_from_args,
26+
};
2427

25-
#[path = "../utils/bin_helpers.rs"]
26-
mod bin_helpers;
27-
28-
#[path = "../utils/dylib.rs"]
29-
mod dylib_util;
28+
#[path = "../utils/shared_helpers.rs"]
29+
mod shared_helpers;
3030

3131
fn main() {
3232
let orig_args = env::args_os().skip(1).collect::<Vec<_>>();
3333
let mut args = orig_args.clone();
34-
let arg =
35-
|name| orig_args.windows(2).find(|args| args[0] == name).and_then(|args| args[1].to_str());
3634

37-
let stage = bin_helpers::parse_rustc_stage();
38-
let verbose = bin_helpers::parse_rustc_verbose();
35+
let stage = parse_rustc_stage();
36+
let verbose = parse_rustc_verbose();
3937

4038
// Detect whether or not we're a build script depending on whether --target
4139
// is passed (a bit janky...)
42-
let target = arg("--target");
40+
let target = parse_value_from_args(&orig_args, "--target");
4341
let version = args.iter().find(|w| &**w == "-vV");
4442

4543
// Use a different compiler for build scripts, since there may not yet be a
@@ -102,7 +100,7 @@ fn main() {
102100
cmd.args(&args).env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
103101

104102
// Get the name of the crate we're compiling, if any.
105-
let crate_name = arg("--crate-name");
103+
let crate_name = parse_value_from_args(&orig_args, "--crate-name");
106104

107105
if let Some(crate_name) = crate_name {
108106
if let Some(target) = env::var_os("RUSTC_TIME") {
@@ -143,10 +141,11 @@ fn main() {
143141
cmd.arg("-C").arg("panic=abort");
144142
}
145143

144+
let crate_type = parse_value_from_args(&orig_args, "--crate-type");
146145
// `-Ztls-model=initial-exec` must not be applied to proc-macros, see
147146
// issue https://github.com/rust-lang/rust/issues/100530
148147
if env::var("RUSTC_TLS_MODEL_INITIAL_EXEC").is_ok()
149-
&& arg("--crate-type") != Some("proc-macro")
148+
&& crate_type != Some("proc-macro")
150149
&& !matches!(crate_name, Some("proc_macro2" | "quote" | "syn" | "synstructure"))
151150
{
152151
cmd.arg("-Ztls-model=initial-exec");
@@ -251,7 +250,7 @@ fn main() {
251250
eprintln!("{prefix} libdir: {libdir:?}");
252251
}
253252

254-
bin_helpers::maybe_dump(format!("stage{stage}-rustc"), &cmd);
253+
maybe_dump(format!("stage{stage}-rustc"), &cmd);
255254

256255
let start = Instant::now();
257256
let (child, status) = {

src/bootstrap/src/bin/rustdoc.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ use std::env;
66
use std::path::PathBuf;
77
use std::process::Command;
88

9-
use dylib_util::{dylib_path, dylib_path_var};
9+
use shared_helpers::{
10+
dylib_path, dylib_path_var, maybe_dump, parse_rustc_stage, parse_rustc_verbose,
11+
parse_value_from_args,
12+
};
1013

11-
#[path = "../utils/bin_helpers.rs"]
12-
mod bin_helpers;
13-
14-
#[path = "../utils/dylib.rs"]
15-
mod dylib_util;
14+
#[path = "../utils/shared_helpers.rs"]
15+
mod shared_helpers;
1616

1717
fn main() {
1818
let args = env::args_os().skip(1).collect::<Vec<_>>();
1919

20-
let stage = bin_helpers::parse_rustc_stage();
21-
let verbose = bin_helpers::parse_rustc_verbose();
20+
let stage = parse_rustc_stage();
21+
let verbose = parse_rustc_verbose();
2222

2323
let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set");
2424
let libdir = env::var_os("RUSTDOC_LIBDIR").expect("RUSTDOC_LIBDIR was not set");
2525
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
2626

2727
// Detect whether or not we're a build script depending on whether --target
2828
// is passed (a bit janky...)
29-
let target = args.windows(2).find(|w| &*w[0] == "--target").and_then(|w| w[1].to_str());
29+
let target = parse_value_from_args(&args, "--target");
3030

3131
let mut dylib_path = dylib_path();
3232
dylib_path.insert(0, PathBuf::from(libdir.clone()));
@@ -62,7 +62,7 @@ fn main() {
6262
cmd.arg("-Zunstable-options");
6363
cmd.arg("--check-cfg=cfg(bootstrap)");
6464

65-
bin_helpers::maybe_dump(format!("stage{stage}-rustdoc"), &cmd);
65+
maybe_dump(format!("stage{stage}-rustdoc"), &cmd);
6666

6767
if verbose > 1 {
6868
eprintln!(

src/bootstrap/src/utils/helpers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::core::builder::Builder;
1818
use crate::core::config::{Config, TargetSelection};
1919
use crate::LldMode;
2020

21-
pub use crate::utils::dylib::{dylib_path, dylib_path_var};
21+
pub use crate::utils::shared_helpers::{dylib_path, dylib_path_var};
2222

2323
#[cfg(test)]
2424
mod tests;
@@ -50,7 +50,7 @@ macro_rules! t {
5050
pub use t;
5151

5252
pub fn exe(name: &str, target: TargetSelection) -> String {
53-
crate::utils::dylib::exe(name, &target.triple)
53+
crate::utils::shared_helpers::exe(name, &target.triple)
5454
}
5555

5656
/// Returns `true` if the file name given looks like a dynamic library.

src/bootstrap/src/utils/shared_helpers.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub fn dylib_path() -> Vec<std::path::PathBuf> {
4444

4545
/// Given an executable called `name`, return the filename for the
4646
/// executable for a particular target.
47-
#[allow(dead_code)]
4847
pub fn exe(name: &str, target: &str) -> String {
4948
if target.contains("windows") {
5049
format!("{name}.exe")
@@ -95,3 +94,19 @@ pub fn maybe_dump(dump_name: String, cmd: &Command) {
9594
file.write_all(cmd_dump.as_bytes()).expect("Unable to write file");
9695
}
9796
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::parse_value_from_args;
2+
3+
#[test]
4+
fn test_parse_value_from_args() {
5+
let args = vec![
6+
"--stage".into(),
7+
"1".into(),
8+
"--version".into(),
9+
"2".into(),
10+
"--target".into(),
11+
"x86_64-unknown-linux".into(),
12+
];
13+
14+
assert_eq!(parse_value_from_args(args.as_slice(), "--stage").unwrap(), "1");
15+
assert_eq!(parse_value_from_args(args.as_slice(), "--version").unwrap(), "2");
16+
assert_eq!(parse_value_from_args(args.as_slice(), "--target").unwrap(), "x86_64-unknown-linux");
17+
assert!(parse_value_from_args(args.as_slice(), "random-key").is_none());
18+
19+
let args = vec![
20+
"app-name".into(),
21+
"--key".into(),
22+
"value".into(),
23+
"random-value".into(),
24+
"--sysroot=/x/y/z".into(),
25+
];
26+
assert_eq!(parse_value_from_args(args.as_slice(), "--key").unwrap(), "value");
27+
assert_eq!(parse_value_from_args(args.as_slice(), "--sysroot").unwrap(), "/x/y/z");
28+
}

0 commit comments

Comments
 (0)