Skip to content

Commit a00c7a0

Browse files
authored
Merge pull request rust-lang#1298 from bjorn3/build_system_rework3
Introduce CargoProject type and use it where possible
2 parents f99140e + cdae9ba commit a00c7a0

File tree

7 files changed

+456
-299
lines changed

7 files changed

+456
-299
lines changed

build_system/abi_cafe.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
use std::env;
21
use std::path::Path;
32

43
use super::build_sysroot;
54
use super::config;
6-
use super::prepare;
7-
use super::utils::{cargo_command, spawn_and_wait};
5+
use super::prepare::GitRepo;
6+
use super::utils::{spawn_and_wait, CargoProject, Compiler};
87
use super::SysrootKind;
98

9+
pub(crate) static ABI_CAFE_REPO: GitRepo =
10+
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");
11+
12+
static ABI_CAFE: CargoProject = CargoProject::git(&ABI_CAFE_REPO, ".");
13+
1014
pub(crate) fn run(
1115
channel: &str,
1216
sysroot_kind: SysrootKind,
@@ -36,17 +40,16 @@ pub(crate) fn run(
3640
);
3741

3842
eprintln!("Running abi-cafe");
39-
let abi_cafe_path = prepare::ABI_CAFE.source_dir();
40-
env::set_current_dir(abi_cafe_path.clone()).unwrap();
4143

4244
let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"];
4345

44-
let mut cmd = cargo_command("cargo", "run", Some(target_triple), &abi_cafe_path);
46+
let mut cmd = ABI_CAFE.run(&Compiler::host());
4547
cmd.arg("--");
4648
cmd.arg("--pairs");
4749
cmd.args(pairs);
4850
cmd.arg("--add-rustc-codegen-backend");
4951
cmd.arg(format!("cgclif:{}", cg_clif_dylib.display()));
52+
cmd.current_dir(ABI_CAFE.source_dir());
5053

5154
spawn_and_wait(cmd);
5255
}

build_system/build_backend.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ use std::env;
22
use std::path::PathBuf;
33

44
use super::rustc_info::get_file_name;
5-
use super::utils::{cargo_command, is_ci};
5+
use super::utils::{is_ci, CargoProject, Compiler};
6+
7+
static CG_CLIF: CargoProject = CargoProject::local(".");
68

79
pub(crate) fn build_backend(
810
channel: &str,
911
host_triple: &str,
1012
use_unstable_features: bool,
1113
) -> PathBuf {
12-
let source_dir = std::env::current_dir().unwrap();
13-
let mut cmd = cargo_command("cargo", "build", Some(host_triple), &source_dir);
14+
let mut cmd = CG_CLIF.build(&Compiler::host());
1415

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

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

44-
source_dir
45+
CG_CLIF
46+
.source_dir()
4547
.join("target")
4648
.join(host_triple)
4749
.join(channel)

build_system/build_sysroot.rs

Lines changed: 9 additions & 4 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::{cargo_command, spawn_and_wait, try_hard_link};
6+
use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
77
use super::SysrootKind;
88

99
pub(crate) fn build_sysroot(
@@ -149,6 +149,8 @@ pub(crate) fn build_sysroot(
149149
}
150150
}
151151

152+
static STANDARD_LIBRARY: CargoProject = CargoProject::local("build_sysroot");
153+
152154
fn build_clif_sysroot_for_triple(
153155
channel: &str,
154156
target_dir: &Path,
@@ -185,19 +187,22 @@ fn build_clif_sysroot_for_triple(
185187
}
186188

187189
// Build sysroot
188-
let mut build_cmd = cargo_command("cargo", "build", Some(triple), Path::new("build_sysroot"));
189190
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
190191
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
191192
rustflags.push_str(&format!(" --sysroot={}", target_dir.to_str().unwrap()));
192193
if channel == "release" {
193-
build_cmd.arg("--release");
194194
rustflags.push_str(" -Zmir-opt-level=3");
195195
}
196196
if let Some(linker) = linker {
197197
use std::fmt::Write;
198198
write!(rustflags, " -Clinker={}", linker).unwrap();
199199
}
200-
build_cmd.env("RUSTFLAGS", rustflags);
200+
let mut compiler = Compiler::with_triple(triple.to_owned());
201+
compiler.rustflags = rustflags;
202+
let mut build_cmd = STANDARD_LIBRARY.build(&compiler);
203+
if channel == "release" {
204+
build_cmd.arg("--release");
205+
}
201206
build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
202207
spawn_and_wait(build_cmd);
203208

build_system/prepare.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +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::{cargo_command, copy_dir_recursively, spawn_and_wait};
9-
10-
pub(crate) const ABI_CAFE: GitRepo =
11-
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");
12-
13-
pub(crate) const RAND: GitRepo =
14-
GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand");
15-
16-
pub(crate) const REGEX: GitRepo =
17-
GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex");
18-
19-
pub(crate) const PORTABLE_SIMD: GitRepo = GitRepo::github(
20-
"rust-lang",
21-
"portable-simd",
22-
"d5cd4a8112d958bd3a252327e0d069a6363249bd",
23-
"portable-simd",
24-
);
25-
26-
pub(crate) const SIMPLE_RAYTRACER: GitRepo = GitRepo::github(
27-
"ebobby",
28-
"simple-raytracer",
29-
"804a7a21b9e673a482797aa289a18ed480e4d813",
30-
"<none>",
31-
);
8+
use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler};
329

3310
pub(crate) fn prepare() {
3411
if Path::new("download").exists() {
@@ -42,22 +19,25 @@ pub(crate) fn prepare() {
4219
eprintln!("[INSTALL] hyperfine");
4320
Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
4421

45-
ABI_CAFE.fetch();
46-
RAND.fetch();
47-
REGEX.fetch();
48-
PORTABLE_SIMD.fetch();
49-
SIMPLE_RAYTRACER.fetch();
22+
super::abi_cafe::ABI_CAFE_REPO.fetch();
23+
super::tests::RAND_REPO.fetch();
24+
super::tests::REGEX_REPO.fetch();
25+
super::tests::PORTABLE_SIMD_REPO.fetch();
26+
super::tests::SIMPLE_RAYTRACER_REPO.fetch();
5027

5128
eprintln!("[LLVM BUILD] simple-raytracer");
52-
let build_cmd = cargo_command("cargo", "build", None, &SIMPLE_RAYTRACER.source_dir());
29+
let host_compiler = Compiler::host();
30+
let build_cmd = super::tests::SIMPLE_RAYTRACER.build(&host_compiler);
5331
spawn_and_wait(build_cmd);
5432
fs::copy(
55-
SIMPLE_RAYTRACER
56-
.source_dir()
57-
.join("target")
33+
super::tests::SIMPLE_RAYTRACER
34+
.target_dir()
35+
.join(&host_compiler.triple)
5836
.join("debug")
5937
.join(get_file_name("main", "bin")),
60-
SIMPLE_RAYTRACER.source_dir().join(get_file_name("raytracer_cg_llvm", "bin")),
38+
super::tests::SIMPLE_RAYTRACER_REPO
39+
.source_dir()
40+
.join(get_file_name("raytracer_cg_llvm", "bin")),
6141
)
6242
.unwrap();
6343
}
@@ -100,7 +80,7 @@ enum GitRepoUrl {
10080
}
10181

10282
impl GitRepo {
103-
const fn github(
83+
pub(crate) const fn github(
10484
user: &'static str,
10585
repo: &'static str,
10686
rev: &'static str,

build_system/rustc_info.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ pub(crate) fn get_host_triple() -> String {
2323
.to_owned()
2424
}
2525

26+
pub(crate) fn get_cargo_path() -> PathBuf {
27+
let cargo_path = Command::new("rustup")
28+
.stderr(Stdio::inherit())
29+
.args(&["which", "cargo"])
30+
.output()
31+
.unwrap()
32+
.stdout;
33+
Path::new(String::from_utf8(cargo_path).unwrap().trim()).to_owned()
34+
}
35+
2636
pub(crate) fn get_rustc_path() -> PathBuf {
2737
let rustc_path = Command::new("rustup")
2838
.stderr(Stdio::inherit())
@@ -33,6 +43,16 @@ pub(crate) fn get_rustc_path() -> PathBuf {
3343
Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
3444
}
3545

46+
pub(crate) fn get_rustdoc_path() -> PathBuf {
47+
let rustc_path = Command::new("rustup")
48+
.stderr(Stdio::inherit())
49+
.args(&["which", "rustdoc"])
50+
.output()
51+
.unwrap()
52+
.stdout;
53+
Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
54+
}
55+
3656
pub(crate) fn get_default_sysroot() -> PathBuf {
3757
let default_sysroot = Command::new("rustc")
3858
.stderr(Stdio::inherit())

0 commit comments

Comments
 (0)