Skip to content

Commit 82b2d8e

Browse files
committed
Introduce Dirs type to avoid hard coding src and dest locations
1 parent 4529979 commit 82b2d8e

File tree

8 files changed

+207
-158
lines changed

8 files changed

+207
-158
lines changed

build_system/abi_cafe.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::path::Path;
22

33
use super::build_sysroot;
44
use super::config;
5+
use super::path::Dirs;
56
use super::prepare::GitRepo;
67
use super::utils::{spawn_and_wait, CargoProject, Compiler};
78
use super::SysrootKind;
@@ -14,6 +15,7 @@ static ABI_CAFE: CargoProject = CargoProject::new(&ABI_CAFE_REPO.source_dir(), "
1415
pub(crate) fn run(
1516
channel: &str,
1617
sysroot_kind: SysrootKind,
18+
dirs: &Dirs,
1719
cg_clif_dylib: &Path,
1820
host_triple: &str,
1921
target_triple: &str,
@@ -29,19 +31,26 @@ pub(crate) fn run(
2931
}
3032

3133
eprintln!("Building sysroot for abi-cafe");
32-
build_sysroot::build_sysroot(channel, sysroot_kind, cg_clif_dylib, host_triple, target_triple);
34+
build_sysroot::build_sysroot(
35+
dirs,
36+
channel,
37+
sysroot_kind,
38+
cg_clif_dylib,
39+
host_triple,
40+
target_triple,
41+
);
3342

3443
eprintln!("Running abi-cafe");
3544

3645
let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"];
3746

38-
let mut cmd = ABI_CAFE.run(&Compiler::host());
47+
let mut cmd = ABI_CAFE.run(&Compiler::host(), dirs);
3948
cmd.arg("--");
4049
cmd.arg("--pairs");
4150
cmd.args(pairs);
4251
cmd.arg("--add-rustc-codegen-backend");
4352
cmd.arg(format!("cgclif:{}", cg_clif_dylib.display()));
44-
cmd.current_dir(ABI_CAFE.source_dir());
53+
cmd.current_dir(ABI_CAFE.source_dir(dirs));
4554

4655
spawn_and_wait(cmd);
4756
}

build_system/build_backend.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use std::env;
22
use std::path::PathBuf;
33

4-
use super::path::RelPath;
4+
use super::path::{Dirs, RelPath};
55
use super::rustc_info::get_file_name;
66
use super::utils::{is_ci, CargoProject, Compiler};
77

88
static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
99

1010
pub(crate) fn build_backend(
11+
dirs: &Dirs,
1112
channel: &str,
1213
host_triple: &str,
1314
use_unstable_features: bool,
1415
) -> PathBuf {
15-
let mut cmd = CG_CLIF.build(&Compiler::host());
16+
let mut cmd = CG_CLIF.build(&Compiler::host(), dirs);
1617

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

@@ -44,7 +45,7 @@ pub(crate) fn build_backend(
4445
super::utils::spawn_and_wait(cmd);
4546

4647
CG_CLIF
47-
.target_dir()
48+
.target_dir(dirs)
4849
.join(host_triple)
4950
.join(channel)
5051
.join(get_file_name("rustc_codegen_cranelift", "dylib"))

build_system/build_sysroot.rs

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

5-
use super::path::RelPath;
5+
use super::path::{Dirs, RelPath};
66
use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
77
use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
88
use super::SysrootKind;
@@ -13,6 +13,7 @@ static LIB_DIR: RelPath = RelPath::DIST.join("lib");
1313
static RUSTLIB_DIR: RelPath = LIB_DIR.join("rustlib");
1414

1515
pub(crate) fn build_sysroot(
16+
dirs: &Dirs,
1617
channel: &str,
1718
sysroot_kind: SysrootKind,
1819
cg_clif_dylib_src: &Path,
@@ -21,9 +22,9 @@ pub(crate) fn build_sysroot(
2122
) {
2223
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
2324

24-
DIST_DIR.ensure_fresh();
25-
BIN_DIR.ensure_exists();
26-
LIB_DIR.ensure_exists();
25+
DIST_DIR.ensure_fresh(dirs);
26+
BIN_DIR.ensure_exists(dirs);
27+
LIB_DIR.ensure_exists(dirs);
2728

2829
// Copy the backend
2930
let cg_clif_dylib_path = if cfg!(windows) {
@@ -33,7 +34,7 @@ pub(crate) fn build_sysroot(
3334
} else {
3435
LIB_DIR
3536
}
36-
.to_path()
37+
.to_path(dirs)
3738
.join(get_file_name("rustc_codegen_cranelift", "dylib"));
3839
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
3940

@@ -43,17 +44,17 @@ pub(crate) fn build_sysroot(
4344

4445
let mut build_cargo_wrapper_cmd = Command::new("rustc");
4546
build_cargo_wrapper_cmd
46-
.arg(RelPath::SCRIPTS.to_path().join(&format!("{wrapper}.rs")))
47+
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
4748
.arg("-o")
48-
.arg(DIST_DIR.to_path().join(wrapper_name))
49+
.arg(DIST_DIR.to_path(dirs).join(wrapper_name))
4950
.arg("-g");
5051
spawn_and_wait(build_cargo_wrapper_cmd);
5152
}
5253

5354
let default_sysroot = super::rustc_info::get_default_sysroot();
5455

55-
let host_rustlib_lib = RUSTLIB_DIR.to_path().join(host_triple).join("lib");
56-
let target_rustlib_lib = RUSTLIB_DIR.to_path().join(target_triple).join("lib");
56+
let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(host_triple).join("lib");
57+
let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib");
5758
fs::create_dir_all(&host_rustlib_lib).unwrap();
5859
fs::create_dir_all(&target_rustlib_lib).unwrap();
5960

@@ -114,7 +115,7 @@ pub(crate) fn build_sysroot(
114115
}
115116
}
116117
SysrootKind::Clif => {
117-
build_clif_sysroot_for_triple(channel, host_triple, &cg_clif_dylib_path, None);
118+
build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None);
118119

119120
if host_triple != target_triple {
120121
// When cross-compiling it is often necessary to manually pick the right linker
@@ -123,7 +124,13 @@ pub(crate) fn build_sysroot(
123124
} else {
124125
None
125126
};
126-
build_clif_sysroot_for_triple(channel, target_triple, &cg_clif_dylib_path, linker);
127+
build_clif_sysroot_for_triple(
128+
dirs,
129+
channel,
130+
target_triple,
131+
&cg_clif_dylib_path,
132+
linker,
133+
);
127134
}
128135

129136
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
@@ -132,7 +139,7 @@ pub(crate) fn build_sysroot(
132139
let file = file.unwrap().path();
133140
let filename = file.file_name().unwrap().to_str().unwrap();
134141
if filename.contains("std-") && !filename.contains(".rlib") {
135-
try_hard_link(&file, LIB_DIR.to_path().join(file.file_name().unwrap()));
142+
try_hard_link(&file, LIB_DIR.to_path(dirs).join(file.file_name().unwrap()));
136143
}
137144
}
138145
}
@@ -145,12 +152,13 @@ pub(crate) static SYSROOT_SRC: RelPath = RelPath::BUILD_SYSROOT.join("sysroot_sr
145152
static STANDARD_LIBRARY: CargoProject = CargoProject::new(&RelPath::BUILD_SYSROOT, "build_sysroot");
146153

147154
fn build_clif_sysroot_for_triple(
155+
dirs: &Dirs,
148156
channel: &str,
149157
triple: &str,
150158
cg_clif_dylib_path: &Path,
151159
linker: Option<&str>,
152160
) {
153-
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path()) {
161+
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
154162
Err(e) => {
155163
eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
156164
eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
@@ -168,7 +176,7 @@ fn build_clif_sysroot_for_triple(
168176
}
169177
}
170178

171-
let build_dir = STANDARD_LIBRARY.target_dir().join(triple).join(channel);
179+
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel);
172180

173181
if !super::config::get_bool("keep_sysroot") {
174182
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
@@ -181,7 +189,7 @@ fn build_clif_sysroot_for_triple(
181189
// Build sysroot
182190
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
183191
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
184-
rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path().to_str().unwrap()));
192+
rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap()));
185193
if channel == "release" {
186194
rustflags.push_str(" -Zmir-opt-level=3");
187195
}
@@ -191,7 +199,7 @@ fn build_clif_sysroot_for_triple(
191199
}
192200
let mut compiler = Compiler::with_triple(triple.to_owned());
193201
compiler.rustflags = rustflags;
194-
let mut build_cmd = STANDARD_LIBRARY.build(&compiler);
202+
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
195203
if channel == "release" {
196204
build_cmd.arg("--release");
197205
}
@@ -210,7 +218,7 @@ fn build_clif_sysroot_for_triple(
210218
};
211219
try_hard_link(
212220
entry.path(),
213-
RUSTLIB_DIR.to_path().join(triple).join("lib").join(entry.file_name()),
221+
RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()),
214222
);
215223
}
216224
}

build_system/mod.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::env;
2-
use std::path::PathBuf;
32
use std::process;
43

54
use self::utils::is_ci;
@@ -17,12 +16,8 @@ mod utils;
1716
fn usage() {
1817
eprintln!("Usage:");
1918
eprintln!(" ./y.rs prepare");
20-
eprintln!(
21-
" ./y.rs build [--debug] [--sysroot none|clif|llvm] [--dist-dir DIR] [--no-unstable-features]"
22-
);
23-
eprintln!(
24-
" ./y.rs test [--debug] [--sysroot none|clif|llvm] [--dist-dir DIR] [--no-unstable-features]"
25-
);
19+
eprintln!(" ./y.rs build [--debug] [--sysroot none|clif|llvm] [--no-unstable-features]");
20+
eprintln!(" ./y.rs test [--debug] [--sysroot none|clif|llvm] [--no-unstable-features]");
2621
}
2722

2823
macro_rules! arg_error {
@@ -50,13 +45,22 @@ pub fn main() {
5045
env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
5146
env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
5247

53-
std::fs::create_dir_all("build").unwrap();
48+
let current_dir = std::env::current_dir().unwrap();
49+
let dirs = path::Dirs {
50+
source_dir: current_dir.clone(),
51+
download_dir: current_dir.join("download"),
52+
build_dir: current_dir.join("build"),
53+
dist_dir: current_dir.join("dist"),
54+
};
55+
56+
path::RelPath::BUILD.ensure_exists(&dirs);
5457

5558
{
5659
// Make sure we always explicitly specify the target dir
57-
let target = "build/target_dir_should_be_set_explicitly";
58-
env::set_var("CARGO_TARGET_DIR", target);
59-
let _ = std::fs::remove_file(target);
60+
let target =
61+
path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs);
62+
env::set_var("CARGO_TARGET_DIR", &target);
63+
let _ = std::fs::remove_file(&target);
6064
std::fs::File::create(target).unwrap();
6165
}
6266

@@ -71,7 +75,7 @@ pub fn main() {
7175
if args.next().is_some() {
7276
arg_error!("./y.rs prepare doesn't expect arguments");
7377
}
74-
prepare::prepare();
78+
prepare::prepare(&dirs);
7579
process::exit(0);
7680
}
7781
Some("build") => Command::Build,
@@ -84,17 +88,11 @@ pub fn main() {
8488
}
8589
};
8690

87-
let mut dist_dir = PathBuf::from("dist");
8891
let mut channel = "release";
8992
let mut sysroot_kind = SysrootKind::Clif;
9093
let mut use_unstable_features = true;
9194
while let Some(arg) = args.next().as_deref() {
9295
match arg {
93-
"--dist-dir" => {
94-
dist_dir = PathBuf::from(args.next().unwrap_or_else(|| {
95-
arg_error!("--dist-dir requires argument");
96-
}))
97-
}
9896
"--debug" => channel = "debug",
9997
"--sysroot" => {
10098
sysroot_kind = match args.next().as_deref() {
@@ -110,7 +108,6 @@ pub fn main() {
110108
arg => arg_error!("Unexpected argument {}", arg),
111109
}
112110
}
113-
dist_dir = std::env::current_dir().unwrap().join(dist_dir);
114111

115112
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
116113
host_triple
@@ -131,15 +128,31 @@ pub fn main() {
131128
host_triple.clone()
132129
};
133130

134-
let cg_clif_dylib = build_backend::build_backend(channel, &host_triple, use_unstable_features);
131+
let cg_clif_dylib =
132+
build_backend::build_backend(&dirs, channel, &host_triple, use_unstable_features);
135133
match command {
136134
Command::Test => {
137-
tests::run_tests(channel, sysroot_kind, &cg_clif_dylib, &host_triple, &target_triple);
135+
tests::run_tests(
136+
&dirs,
137+
channel,
138+
sysroot_kind,
139+
&cg_clif_dylib,
140+
&host_triple,
141+
&target_triple,
142+
);
138143

139-
abi_cafe::run(channel, sysroot_kind, &cg_clif_dylib, &host_triple, &target_triple);
144+
abi_cafe::run(
145+
channel,
146+
sysroot_kind,
147+
&dirs,
148+
&cg_clif_dylib,
149+
&host_triple,
150+
&target_triple,
151+
);
140152
}
141153
Command::Build => {
142154
build_sysroot::build_sysroot(
155+
&dirs,
143156
channel,
144157
sysroot_kind,
145158
&cg_clif_dylib,

0 commit comments

Comments
 (0)