Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit be305c2

Browse files
committed
Introduce cargo_command helper
1 parent c677cba commit be305c2

File tree

6 files changed

+81
-71
lines changed

6 files changed

+81
-71
lines changed

build_system/abi_checker.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::env;
22
use std::path::Path;
3-
use std::process::Command;
43

54
use super::build_sysroot;
65
use super::config;
7-
use super::utils::spawn_and_wait;
6+
use super::utils::{cargo_command, spawn_and_wait};
87
use super::SysrootKind;
98

109
pub(crate) fn run(
@@ -38,14 +37,11 @@ pub(crate) fn run(
3837
eprintln!("Running abi-checker");
3938
let mut abi_checker_path = env::current_dir().unwrap();
4039
abi_checker_path.push("abi-checker");
41-
env::set_current_dir(abi_checker_path.clone()).unwrap();
40+
env::set_current_dir(&abi_checker_path.clone()).unwrap();
4241

4342
let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"];
4443

45-
let mut cmd = Command::new("cargo");
46-
cmd.arg("run");
47-
cmd.arg("--target");
48-
cmd.arg(target_triple);
44+
let mut cmd = cargo_command("cargo", "run", Some(target_triple), &abi_checker_path);
4945
cmd.arg("--");
5046
cmd.arg("--pairs");
5147
cmd.args(pairs);

build_system/build_backend.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use std::env;
2-
use std::path::{Path, PathBuf};
3-
use std::process::Command;
2+
use std::path::PathBuf;
43

54
use super::rustc_info::get_file_name;
6-
use super::utils::is_ci;
5+
use super::utils::{cargo_command, is_ci};
76

87
pub(crate) fn build_backend(
98
channel: &str,
109
host_triple: &str,
1110
use_unstable_features: bool,
1211
) -> PathBuf {
13-
let mut cmd = Command::new("cargo");
14-
cmd.arg("build").arg("--target").arg(host_triple);
12+
let source_dir = std::env::current_dir().unwrap();
13+
let mut cmd = cargo_command("cargo", "build", Some(host_triple), &source_dir);
1514

1615
cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode
1716

@@ -42,7 +41,8 @@ pub(crate) fn build_backend(
4241
eprintln!("[BUILD] rustc_codegen_cranelift");
4342
super::utils::spawn_and_wait(cmd);
4443

45-
Path::new("target")
44+
source_dir
45+
.join("target")
4646
.join(host_triple)
4747
.join(channel)
4848
.join(get_file_name("rustc_codegen_cranelift", "dylib"))

build_system/build_sysroot.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
33
use std::process::{self, Command};
44

55
use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
6-
use super::utils::{spawn_and_wait, try_hard_link};
6+
use super::utils::{cargo_command, spawn_and_wait, try_hard_link};
77
use super::SysrootKind;
88

99
pub(crate) fn build_sysroot(
@@ -185,8 +185,7 @@ fn build_clif_sysroot_for_triple(
185185
}
186186

187187
// Build sysroot
188-
let mut build_cmd = Command::new("cargo");
189-
build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
188+
let mut build_cmd = cargo_command("cargo", "build", Some(triple), Path::new("build_sysroot"));
190189
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
191190
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
192191
if channel == "release" {

build_system/prepare.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55
use std::process::Command;
66

77
use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
8-
use super::utils::{copy_dir_recursively, spawn_and_wait};
8+
use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait};
99

1010
pub(crate) fn prepare() {
1111
prepare_sysroot();
@@ -52,8 +52,7 @@ pub(crate) fn prepare() {
5252
);
5353

5454
eprintln!("[LLVM BUILD] simple-raytracer");
55-
let mut build_cmd = Command::new("cargo");
56-
build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
55+
let build_cmd = cargo_command("cargo", "build", None, Path::new("simple-raytracer"));
5756
spawn_and_wait(build_cmd);
5857
fs::copy(
5958
Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),

build_system/tests.rs

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::build_sysroot;
22
use super::config;
33
use super::rustc_info::get_wrapper_file_name;
4-
use super::utils::{spawn_and_wait, spawn_and_wait_with_input};
4+
use super::utils::{cargo_command, spawn_and_wait, spawn_and_wait_with_input};
55
use build_system::SysrootKind;
66
use std::env;
77
use std::ffi::OsStr;
@@ -218,20 +218,14 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[
218218
const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
219219
TestCase::new("test.rust-random/rand", &|runner| {
220220
runner.in_dir(["rand"], |runner| {
221-
runner.run_cargo(["clean"]);
221+
runner.run_cargo("clean", []);
222222

223223
if runner.host_triple == runner.target_triple {
224224
eprintln!("[TEST] rust-random/rand");
225-
runner.run_cargo(["test", "--workspace"]);
225+
runner.run_cargo("test", ["--workspace"]);
226226
} else {
227227
eprintln!("[AOT] rust-random/rand");
228-
runner.run_cargo([
229-
"build",
230-
"--workspace",
231-
"--target",
232-
&runner.target_triple,
233-
"--tests",
234-
]);
228+
runner.run_cargo("build", ["--workspace", "--tests"]);
235229
}
236230
});
237231
}),
@@ -247,11 +241,19 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
247241
bench_compile.arg("--warmup");
248242
bench_compile.arg("1");
249243
bench_compile.arg("--prepare");
250-
bench_compile.arg(format!("{:?}", runner.cargo_command(["clean"])));
244+
bench_compile.arg(format!("{:?}", runner.cargo_command("clean", [])));
251245

252246
bench_compile.arg("cargo build");
253247

254-
bench_compile.arg(format!("{:?}", runner.cargo_command(["build"])));
248+
let cargo_clif = runner
249+
.root_dir
250+
.clone()
251+
.join("build")
252+
.join(get_wrapper_file_name("cargo-clif", "bin"));
253+
let mut clif_build_cmd = cargo_command(cargo_clif, "build", None, Path::new("."));
254+
clif_build_cmd.env("RUSTFLAGS", &runner.rust_flags);
255+
bench_compile.arg(format!("{:?}", clif_build_cmd));
256+
255257
spawn_and_wait(bench_compile);
256258

257259
eprintln!("[BENCH RUN] ebobby/simple-raytracer");
@@ -265,51 +267,39 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
265267
bench_run.arg(PathBuf::from("./raytracer_cg_clif"));
266268
spawn_and_wait(bench_run);
267269
} else {
268-
runner.run_cargo(["clean"]);
270+
runner.run_cargo("clean", []);
269271
eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)");
270272
eprintln!("[COMPILE] ebobby/simple-raytracer");
271-
runner.run_cargo(["build", "--target", &runner.target_triple]);
273+
runner.run_cargo("build", []);
272274
eprintln!("[BENCH RUN] ebobby/simple-raytracer (skipped)");
273275
}
274276
});
275277
}),
276278
TestCase::new("test.libcore", &|runner| {
277279
runner.in_dir(["build_sysroot", "sysroot_src", "library", "core", "tests"], |runner| {
278-
runner.run_cargo(["clean"]);
280+
runner.run_cargo("clean", []);
279281

280282
if runner.host_triple == runner.target_triple {
281-
runner.run_cargo(["test"]);
283+
runner.run_cargo("test", []);
282284
} else {
283285
eprintln!("Cross-Compiling: Not running tests");
284-
runner.run_cargo(["build", "--target", &runner.target_triple, "--tests"]);
286+
runner.run_cargo("build", ["--tests"]);
285287
}
286288
});
287289
}),
288290
TestCase::new("test.regex-shootout-regex-dna", &|runner| {
289291
runner.in_dir(["regex"], |runner| {
290-
runner.run_cargo(["clean"]);
292+
runner.run_cargo("clean", []);
291293

292294
// newer aho_corasick versions throw a deprecation warning
293295
let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags);
294296

295-
let mut build_cmd = runner.cargo_command([
296-
"build",
297-
"--example",
298-
"shootout-regex-dna",
299-
"--target",
300-
&runner.target_triple,
301-
]);
297+
let mut build_cmd = runner.cargo_command("build", ["--example", "shootout-regex-dna"]);
302298
build_cmd.env("RUSTFLAGS", lint_rust_flags.clone());
303299
spawn_and_wait(build_cmd);
304300

305301
if runner.host_triple == runner.target_triple {
306-
let mut run_cmd = runner.cargo_command([
307-
"run",
308-
"--example",
309-
"shootout-regex-dna",
310-
"--target",
311-
&runner.target_triple,
312-
]);
302+
let mut run_cmd = runner.cargo_command("run", ["--example", "shootout-regex-dna"]);
313303
run_cmd.env("RUSTFLAGS", lint_rust_flags);
314304

315305
let input =
@@ -350,40 +340,42 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
350340
}),
351341
TestCase::new("test.regex", &|runner| {
352342
runner.in_dir(["regex"], |runner| {
353-
runner.run_cargo(["clean"]);
343+
runner.run_cargo("clean", []);
354344

355345
// newer aho_corasick versions throw a deprecation warning
356346
let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags);
357347

358348
if runner.host_triple == runner.target_triple {
359-
let mut run_cmd = runner.cargo_command([
349+
let mut run_cmd = runner.cargo_command(
360350
"test",
361-
"--tests",
362-
"--",
363-
"--exclude-should-panic",
364-
"--test-threads",
365-
"1",
366-
"-Zunstable-options",
367-
"-q",
368-
]);
351+
[
352+
"--tests",
353+
"--",
354+
"--exclude-should-panic",
355+
"--test-threads",
356+
"1",
357+
"-Zunstable-options",
358+
"-q",
359+
],
360+
);
369361
run_cmd.env("RUSTFLAGS", lint_rust_flags);
370362
spawn_and_wait(run_cmd);
371363
} else {
372364
eprintln!("Cross-Compiling: Not running tests");
373365
let mut build_cmd =
374-
runner.cargo_command(["build", "--tests", "--target", &runner.target_triple]);
366+
runner.cargo_command("build", ["--tests", "--target", &runner.target_triple]);
375367
build_cmd.env("RUSTFLAGS", lint_rust_flags.clone());
376368
spawn_and_wait(build_cmd);
377369
}
378370
});
379371
}),
380372
TestCase::new("test.portable-simd", &|runner| {
381373
runner.in_dir(["portable-simd"], |runner| {
382-
runner.run_cargo(["clean"]);
383-
runner.run_cargo(["build", "--all-targets", "--target", &runner.target_triple]);
374+
runner.run_cargo("clean", []);
375+
runner.run_cargo("build", ["--all-targets", "--target", &runner.target_triple]);
384376

385377
if runner.host_triple == runner.target_triple {
386-
runner.run_cargo(["test", "-q"]);
378+
runner.run_cargo("test", ["-q"]);
387379
}
388380
});
389381
}),
@@ -590,25 +582,29 @@ impl TestRunner {
590582
spawn_and_wait(cmd);
591583
}
592584

593-
fn cargo_command<I, S>(&self, args: I) -> Command
585+
fn cargo_command<'a, I>(&self, subcommand: &str, args: I) -> Command
594586
where
595-
I: IntoIterator<Item = S>,
596-
S: AsRef<OsStr>,
587+
I: IntoIterator<Item = &'a str>,
597588
{
598589
let mut cargo_clif = self.root_dir.clone();
599590
cargo_clif.push("build");
600591
cargo_clif.push(get_wrapper_file_name("cargo-clif", "bin"));
601592

602-
let mut cmd = Command::new(cargo_clif);
593+
let mut cmd = cargo_command(
594+
cargo_clif,
595+
subcommand,
596+
if subcommand == "clean" { None } else { Some(&self.target_triple) },
597+
Path::new("."),
598+
);
603599
cmd.args(args);
604600
cmd.env("RUSTFLAGS", &self.rust_flags);
605601
cmd
606602
}
607603

608-
fn run_cargo<'a, I>(&self, args: I)
604+
fn run_cargo<'a, I>(&self, subcommand: &str, args: I)
609605
where
610606
I: IntoIterator<Item = &'a str>,
611607
{
612-
spawn_and_wait(self.cargo_command(args));
608+
spawn_and_wait(self.cargo_command(subcommand, args));
613609
}
614610
}

build_system/utils.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ use std::io::Write;
44
use std::path::Path;
55
use std::process::{self, Command, Stdio};
66

7+
pub(crate) fn cargo_command(
8+
cargo: impl AsRef<Path>,
9+
subcommand: &str,
10+
triple: Option<&str>,
11+
source_dir: &Path,
12+
) -> Command {
13+
let mut cmd = Command::new(cargo.as_ref());
14+
cmd.arg(subcommand)
15+
.arg("--manifest-path")
16+
.arg(source_dir.join("Cargo.toml"))
17+
.arg("--target-dir")
18+
.arg(source_dir.join("target"));
19+
20+
if let Some(triple) = triple {
21+
cmd.arg("--target").arg(triple);
22+
}
23+
24+
cmd
25+
}
26+
727
#[track_caller]
828
pub(crate) fn try_hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
929
let src = src.as_ref();

0 commit comments

Comments
 (0)