Skip to content

Commit 5727711

Browse files
committed
Rework support library API
- Use builders as thin wrappers around `Command`. - Remove dependency on `shell_words` since it is no longer needed.
1 parent f30b778 commit 5727711

File tree

5 files changed

+114
-42
lines changed

5 files changed

+114
-42
lines changed

Cargo.lock

-9
Original file line numberDiff line numberDiff line change
@@ -3316,9 +3316,6 @@ dependencies = [
33163316
[[package]]
33173317
name = "run_make_support"
33183318
version = "0.0.0"
3319-
dependencies = [
3320-
"shell-words",
3321-
]
33223319

33233320
[[package]]
33243321
name = "rust-demangler"
@@ -5032,12 +5029,6 @@ version = "0.1.5"
50325029
source = "registry+https://github.com/rust-lang/crates.io-index"
50335030
checksum = "45bb67a18fa91266cc7807181f62f9178a6873bfad7dc788c42e6430db40184f"
50345031

5035-
[[package]]
5036-
name = "shell-words"
5037-
version = "1.1.0"
5038-
source = "registry+https://github.com/rust-lang/crates.io-index"
5039-
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
5040-
50415032
[[package]]
50425033
name = "shlex"
50435034
version = "1.3.0"

src/tools/run-make-support/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7-
shell-words = "1.1"

src/tools/run-make-support/src/lib.rs

+63-24
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,14 @@ use std::env;
22
use std::path::PathBuf;
33
use std::process::{Command, Output};
44

5-
pub fn aux_build(args: &str) -> Output {
6-
let (words, mut cmd) = build_common(args);
7-
cmd.arg("--crate-type=lib");
8-
for word in words {
9-
cmd.arg(word);
10-
}
11-
let output = cmd.output().unwrap();
12-
if !output.status.success() {
13-
handle_failed_output(&format!("{:?}", cmd), output);
14-
}
15-
output
16-
}
17-
18-
fn build_common(args: &str) -> (Vec<String>, Command) {
5+
fn setup_common_build_cmd() -> Command {
196
let rustc = env::var("RUSTC").unwrap();
20-
let words = shell_words::split(args).expect("failed to parse arguments");
217
let mut cmd = Command::new(rustc);
228
cmd.arg("--out-dir")
239
.arg(env::var("TMPDIR").unwrap())
2410
.arg("-L")
2511
.arg(env::var("TMPDIR").unwrap());
26-
(words, cmd)
12+
cmd
2713
}
2814

2915
fn handle_failed_output(cmd: &str, output: Output) -> ! {
@@ -33,19 +19,72 @@ fn handle_failed_output(cmd: &str, output: Output) -> ! {
3319
std::process::exit(1)
3420
}
3521

36-
pub fn rustc(args: &str) -> Output {
37-
let (words, mut cmd) = build_common(args);
38-
for word in words {
39-
cmd.arg(word);
22+
pub fn rustc() -> RustcInvocationBuilder {
23+
RustcInvocationBuilder::new()
24+
}
25+
26+
pub fn aux_build() -> AuxBuildInvocationBuilder {
27+
AuxBuildInvocationBuilder::new()
28+
}
29+
30+
#[derive(Debug)]
31+
pub struct RustcInvocationBuilder {
32+
cmd: Command,
33+
}
34+
35+
impl RustcInvocationBuilder {
36+
fn new() -> Self {
37+
let cmd = setup_common_build_cmd();
38+
Self { cmd }
4039
}
41-
let output = cmd.output().unwrap();
42-
if !output.status.success() {
43-
handle_failed_output(&format!("{:?}", cmd), output);
40+
41+
pub fn arg(&mut self, arg: &str) -> &mut RustcInvocationBuilder {
42+
self.cmd.arg(arg);
43+
self
44+
}
45+
46+
pub fn run(&mut self) -> Output {
47+
let output = self.cmd.output().unwrap();
48+
if !output.status.success() {
49+
handle_failed_output(&format!("{:?}", self.cmd), output);
50+
}
51+
output
52+
}
53+
}
54+
55+
#[derive(Debug)]
56+
pub struct AuxBuildInvocationBuilder {
57+
cmd: Command,
58+
}
59+
60+
impl AuxBuildInvocationBuilder {
61+
fn new() -> Self {
62+
let mut cmd = setup_common_build_cmd();
63+
cmd.arg("--crate-type=lib");
64+
Self { cmd }
65+
}
66+
67+
pub fn arg(&mut self, arg: &str) -> &mut AuxBuildInvocationBuilder {
68+
self.cmd.arg(arg);
69+
self
70+
}
71+
72+
pub fn run(&mut self) -> Output {
73+
let output = self.cmd.output().unwrap();
74+
if !output.status.success() {
75+
handle_failed_output(&format!("{:?}", self.cmd), output);
76+
}
77+
output
4478
}
45-
output
4679
}
4780

4881
fn run_common(bin_name: &str) -> (Command, Output) {
82+
let bin_name = if std::env::var("TARGET").unwrap().contains("windows") {
83+
format!("{}.exe", bin_name)
84+
} else {
85+
bin_name.to_owned()
86+
};
87+
4988
let mut bin_path = PathBuf::new();
5089
bin_path.push(std::env::var("TMPDIR").unwrap());
5190
bin_path.push(&bin_name);

tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22

33
extern crate run_make_support;
44

5+
use std::path::PathBuf;
6+
57
use run_make_support::{aux_build, rustc};
68

79
fn main() {
8-
aux_build("--emit=metadata stable.rs");
9-
let output = rustc(&format!(
10-
"--emit=metadata --extern stable={}/libstable.rmeta main.rs",
11-
env!("TMPDIR")
12-
));
10+
aux_build()
11+
.arg("--emit=metadata")
12+
.arg("stable.rs")
13+
.run();
14+
let mut stable_path = PathBuf::from(env!("TMPDIR"));
15+
stable_path.push("libstable.rmeta");
16+
let output = rustc()
17+
.arg("--emit=metadata")
18+
.arg("--extern")
19+
.arg(&format!("stable={}", &stable_path.to_string_lossy()))
20+
.arg("main.rs")
21+
.run();
1322

1423
let stderr = String::from_utf8_lossy(&output.stderr);
1524
let version = include_str!(concat!(env!("S"), "/src/version"));

tests/run-make/a-b-a-linker-guard/rmake.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,43 @@ extern crate run_make_support;
55
use run_make_support::{run, run_fail, rustc};
66

77
fn main() {
8-
rustc("a.rs --cfg x -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=legacy");
9-
rustc("b.rs -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=legacy");
8+
rustc()
9+
.arg("a.rs")
10+
.arg("--cfg")
11+
.arg("x")
12+
.arg("-C")
13+
.arg("prefer-dynamic")
14+
.arg("-Z")
15+
.arg("unstable-options")
16+
.arg("-C")
17+
.arg("symbol-mangling-version=legacy")
18+
.run();
19+
20+
rustc()
21+
.arg("b.rs")
22+
.arg("--cfg")
23+
.arg("x")
24+
.arg("-C")
25+
.arg("prefer-dynamic")
26+
.arg("-Z")
27+
.arg("unstable-options")
28+
.arg("-C")
29+
.arg("symbol-mangling-version=legacy")
30+
.run();
31+
1032
run("b");
11-
rustc("a.rs --cfg y -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=legacy");
33+
34+
rustc()
35+
.arg("a.rs")
36+
.arg("--cfg")
37+
.arg("y")
38+
.arg("-C")
39+
.arg("prefer-dynamic")
40+
.arg("-Z")
41+
.arg("unstable-options")
42+
.arg("-C")
43+
.arg("symbol-mangling-version=legacy")
44+
.run();
45+
1246
run_fail("b");
1347
}

0 commit comments

Comments
 (0)