Skip to content

Commit 8fe7701

Browse files
authored
Rollup merge of #127108 - onur-ozkan:bin-helper, r=Kobzol
unify `dylib` and `bin_helpers` and create `shared_helpers::parse_value_from_args` `dylib` and `bin_helpers` were already used in similar logic. This PR unifies them under a `shared_helpers` module that is utilized by both the bootstrap library and shims. Additionally, created `parse_value_from_args` with a unit test. This helps avoid code duplication in shims and can also be used in the bootstrap library if needed (which is the case in one of `@Kobzol's` tasks). r? `@Kobzol`
2 parents a4e92bf + 9098474 commit 8fe7701

File tree

8 files changed

+166
-117
lines changed

8 files changed

+166
-117
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/bin_helpers.rs

-50
This file was deleted.

src/bootstrap/src/utils/dylib.rs

-40
This file was deleted.

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;
@@ -51,7 +51,7 @@ use crate::utils::exec::BootstrapCommand;
5151
pub use t;
5252

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

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

src/bootstrap/src/utils/mod.rs

+1-1
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;
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
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)