Skip to content

Commit 4529979

Browse files
committed
Introduce RelPath
1 parent e75dfef commit 4529979

File tree

8 files changed

+165
-164
lines changed

8 files changed

+165
-164
lines changed

build_system/abi_cafe.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ use super::SysrootKind;
99
pub(crate) static ABI_CAFE_REPO: GitRepo =
1010
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");
1111

12-
static ABI_CAFE: CargoProject = CargoProject::git(&ABI_CAFE_REPO, ".", "abi_cafe");
12+
static ABI_CAFE: CargoProject = CargoProject::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe");
1313

1414
pub(crate) fn run(
1515
channel: &str,
1616
sysroot_kind: SysrootKind,
17-
dist_dir: &Path,
1817
cg_clif_dylib: &Path,
1918
host_triple: &str,
2019
target_triple: &str,
@@ -30,14 +29,7 @@ pub(crate) fn run(
3029
}
3130

3231
eprintln!("Building sysroot for abi-cafe");
33-
build_sysroot::build_sysroot(
34-
channel,
35-
sysroot_kind,
36-
dist_dir,
37-
cg_clif_dylib,
38-
host_triple,
39-
target_triple,
40-
);
32+
build_sysroot::build_sysroot(channel, sysroot_kind, cg_clif_dylib, host_triple, target_triple);
4133

4234
eprintln!("Running abi-cafe");
4335

build_system/build_backend.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::env;
22
use std::path::PathBuf;
33

4+
use super::path::RelPath;
45
use super::rustc_info::get_file_name;
56
use super::utils::{is_ci, CargoProject, Compiler};
67

7-
static CG_CLIF: CargoProject = CargoProject::local(".", "cg_clif");
8+
static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
89

910
pub(crate) fn build_backend(
1011
channel: &str,

build_system/build_sysroot.rs

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

5+
use super::path::RelPath;
56
use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
67
use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
78
use super::SysrootKind;
89

10+
static DIST_DIR: RelPath = RelPath::DIST;
11+
static BIN_DIR: RelPath = RelPath::DIST.join("bin");
12+
static LIB_DIR: RelPath = RelPath::DIST.join("lib");
13+
static RUSTLIB_DIR: RelPath = LIB_DIR.join("rustlib");
14+
915
pub(crate) fn build_sysroot(
1016
channel: &str,
1117
sysroot_kind: SysrootKind,
12-
dist_dir: &Path,
1318
cg_clif_dylib_src: &Path,
1419
host_triple: &str,
1520
target_triple: &str,
1621
) {
1722
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
1823

19-
if dist_dir.exists() {
20-
fs::remove_dir_all(dist_dir).unwrap();
21-
}
22-
fs::create_dir_all(dist_dir.join("bin")).unwrap();
23-
fs::create_dir_all(dist_dir.join("lib")).unwrap();
24+
DIST_DIR.ensure_fresh();
25+
BIN_DIR.ensure_exists();
26+
LIB_DIR.ensure_exists();
2427

2528
// Copy the backend
26-
let cg_clif_dylib_path = dist_dir
27-
.join(if cfg!(windows) {
28-
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
29-
// binaries.
30-
"bin"
31-
} else {
32-
"lib"
33-
})
34-
.join(get_file_name("rustc_codegen_cranelift", "dylib"));
29+
let cg_clif_dylib_path = if cfg!(windows) {
30+
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
31+
// binaries.
32+
BIN_DIR
33+
} else {
34+
LIB_DIR
35+
}
36+
.to_path()
37+
.join(get_file_name("rustc_codegen_cranelift", "dylib"));
3538
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
3639

3740
// Build and copy rustc and cargo wrappers
@@ -40,18 +43,17 @@ pub(crate) fn build_sysroot(
4043

4144
let mut build_cargo_wrapper_cmd = Command::new("rustc");
4245
build_cargo_wrapper_cmd
43-
.arg(Path::new("scripts").join(format!("{wrapper}.rs")))
46+
.arg(RelPath::SCRIPTS.to_path().join(&format!("{wrapper}.rs")))
4447
.arg("-o")
45-
.arg(dist_dir.join(wrapper_name))
48+
.arg(DIST_DIR.to_path().join(wrapper_name))
4649
.arg("-g");
4750
spawn_and_wait(build_cargo_wrapper_cmd);
4851
}
4952

5053
let default_sysroot = super::rustc_info::get_default_sysroot();
5154

52-
let rustlib = dist_dir.join("lib").join("rustlib");
53-
let host_rustlib_lib = rustlib.join(host_triple).join("lib");
54-
let target_rustlib_lib = rustlib.join(target_triple).join("lib");
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");
5557
fs::create_dir_all(&host_rustlib_lib).unwrap();
5658
fs::create_dir_all(&target_rustlib_lib).unwrap();
5759

@@ -112,13 +114,7 @@ pub(crate) fn build_sysroot(
112114
}
113115
}
114116
SysrootKind::Clif => {
115-
build_clif_sysroot_for_triple(
116-
channel,
117-
dist_dir,
118-
host_triple,
119-
&cg_clif_dylib_path,
120-
None,
121-
);
117+
build_clif_sysroot_for_triple(channel, host_triple, &cg_clif_dylib_path, None);
122118

123119
if host_triple != target_triple {
124120
// When cross-compiling it is often necessary to manually pick the right linker
@@ -127,13 +123,7 @@ pub(crate) fn build_sysroot(
127123
} else {
128124
None
129125
};
130-
build_clif_sysroot_for_triple(
131-
channel,
132-
dist_dir,
133-
target_triple,
134-
&cg_clif_dylib_path,
135-
linker,
136-
);
126+
build_clif_sysroot_for_triple(channel, target_triple, &cg_clif_dylib_path, linker);
137127
}
138128

139129
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
@@ -142,23 +132,25 @@ pub(crate) fn build_sysroot(
142132
let file = file.unwrap().path();
143133
let filename = file.file_name().unwrap().to_str().unwrap();
144134
if filename.contains("std-") && !filename.contains(".rlib") {
145-
try_hard_link(&file, dist_dir.join("lib").join(file.file_name().unwrap()));
135+
try_hard_link(&file, LIB_DIR.to_path().join(file.file_name().unwrap()));
146136
}
147137
}
148138
}
149139
}
150140
}
151141

152-
static STANDARD_LIBRARY: CargoProject = CargoProject::local("build_sysroot", "build_sysroot");
142+
// FIXME move to download/ or dist/
143+
pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = RelPath::BUILD_SYSROOT.join("rustc_version");
144+
pub(crate) static SYSROOT_SRC: RelPath = RelPath::BUILD_SYSROOT.join("sysroot_src");
145+
static STANDARD_LIBRARY: CargoProject = CargoProject::new(&RelPath::BUILD_SYSROOT, "build_sysroot");
153146

154147
fn build_clif_sysroot_for_triple(
155148
channel: &str,
156-
dist_dir: &Path,
157149
triple: &str,
158150
cg_clif_dylib_path: &Path,
159151
linker: Option<&str>,
160152
) {
161-
match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) {
153+
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path()) {
162154
Err(e) => {
163155
eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
164156
eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
@@ -189,7 +181,7 @@ fn build_clif_sysroot_for_triple(
189181
// Build sysroot
190182
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
191183
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
192-
rustflags.push_str(&format!(" --sysroot={}", dist_dir.to_str().unwrap()));
184+
rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path().to_str().unwrap()));
193185
if channel == "release" {
194186
rustflags.push_str(" -Zmir-opt-level=3");
195187
}
@@ -218,7 +210,7 @@ fn build_clif_sysroot_for_triple(
218210
};
219211
try_hard_link(
220212
entry.path(),
221-
dist_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
213+
RUSTLIB_DIR.to_path().join(triple).join("lib").join(entry.file_name()),
222214
);
223215
}
224216
}

build_system/mod.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod abi_cafe;
88
mod build_backend;
99
mod build_sysroot;
1010
mod config;
11+
mod path;
1112
mod prepare;
1213
mod rustc_info;
1314
mod tests;
@@ -133,29 +134,14 @@ pub fn main() {
133134
let cg_clif_dylib = build_backend::build_backend(channel, &host_triple, use_unstable_features);
134135
match command {
135136
Command::Test => {
136-
tests::run_tests(
137-
channel,
138-
sysroot_kind,
139-
&dist_dir,
140-
&cg_clif_dylib,
141-
&host_triple,
142-
&target_triple,
143-
);
137+
tests::run_tests(channel, sysroot_kind, &cg_clif_dylib, &host_triple, &target_triple);
144138

145-
abi_cafe::run(
146-
channel,
147-
sysroot_kind,
148-
&dist_dir,
149-
&cg_clif_dylib,
150-
&host_triple,
151-
&target_triple,
152-
);
139+
abi_cafe::run(channel, sysroot_kind, &cg_clif_dylib, &host_triple, &target_triple);
153140
}
154141
Command::Build => {
155142
build_sysroot::build_sysroot(
156143
channel,
157144
sysroot_kind,
158-
&dist_dir,
159145
&cg_clif_dylib,
160146
&host_triple,
161147
&target_triple,

build_system/path.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::fs;
2+
use std::path::PathBuf;
3+
4+
/*pub(crate) struct Paths {
5+
source_dir: PathBuf,
6+
download_dir: PathBuf,
7+
build_dir: PathBuf,
8+
dist_dir: PathBuf,
9+
}*/
10+
11+
#[doc(hidden)]
12+
#[derive(Debug, Copy, Clone)]
13+
pub(crate) enum PathBase {
14+
Source,
15+
Download,
16+
Build,
17+
Dist,
18+
}
19+
20+
impl PathBase {
21+
fn to_path(self) -> PathBuf {
22+
// FIXME pass in all paths instead
23+
let current_dir = std::env::current_dir().unwrap();
24+
match self {
25+
PathBase::Source => current_dir,
26+
PathBase::Download => current_dir.join("download"),
27+
PathBase::Build => current_dir.join("build"),
28+
PathBase::Dist => current_dir.join("dist"),
29+
}
30+
}
31+
}
32+
33+
#[derive(Debug, Copy, Clone)]
34+
pub(crate) enum RelPath {
35+
Base(PathBase),
36+
Join(&'static RelPath, &'static str),
37+
}
38+
39+
impl RelPath {
40+
pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source);
41+
pub(crate) const DOWNLOAD: RelPath = RelPath::Base(PathBase::Download);
42+
pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build);
43+
pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist);
44+
45+
pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts");
46+
pub(crate) const BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot");
47+
pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches");
48+
49+
pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath {
50+
RelPath::Join(self, suffix)
51+
}
52+
53+
pub(crate) fn to_path(&self) -> PathBuf {
54+
match self {
55+
RelPath::Base(base) => base.to_path(),
56+
RelPath::Join(base, suffix) => base.to_path().join(suffix),
57+
}
58+
}
59+
60+
pub(crate) fn ensure_exists(&self) {
61+
fs::create_dir_all(self.to_path()).unwrap();
62+
}
63+
64+
pub(crate) fn ensure_fresh(&self) {
65+
let path = self.to_path();
66+
if path.exists() {
67+
fs::remove_dir_all(&path).unwrap();
68+
}
69+
fs::create_dir_all(path).unwrap();
70+
}
71+
}

0 commit comments

Comments
 (0)