Skip to content

Commit f73b0b1

Browse files
authored
Merge pull request #1302 from bjorn3/build_system_rework4
Allow specifying where build artifacts should be written to
2 parents 450257c + b6ac5a3 commit f73b0b1

File tree

12 files changed

+339
-250
lines changed

12 files changed

+339
-250
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
- name: Cache cargo target dir
6464
uses: actions/cache@v3
6565
with:
66-
path: target
66+
path: build/cg_clif
6767
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }}
6868

6969
- name: Install MinGW toolchain and wine
@@ -164,7 +164,7 @@ jobs:
164164
- name: Cache cargo target dir
165165
uses: actions/cache@v3
166166
with:
167-
path: target
167+
path: build/cg_clif
168168
key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }}
169169

170170
- name: Set MinGW as the default toolchain

.github/workflows/rustc.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- name: Cache cargo target dir
2828
uses: actions/cache@v3
2929
with:
30-
path: target
30+
path: build/cg_clif
3131
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }}
3232

3333
- name: Prepare dependencies
@@ -65,7 +65,7 @@ jobs:
6565
- name: Cache cargo target dir
6666
uses: actions/cache@v3
6767
with:
68-
path: target
68+
path: build/cg_clif
6969
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }}
7070

7171
- name: Prepare dependencies

build_system/abi_cafe.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ 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;
89

910
pub(crate) static ABI_CAFE_REPO: GitRepo =
1011
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");
1112

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

1415
pub(crate) fn run(
1516
channel: &str,
1617
sysroot_kind: SysrootKind,
17-
dist_dir: &Path,
18+
dirs: &Dirs,
1819
cg_clif_dylib: &Path,
1920
host_triple: &str,
2021
target_triple: &str,
@@ -31,9 +32,9 @@ pub(crate) fn run(
3132

3233
eprintln!("Building sysroot for abi-cafe");
3334
build_sysroot::build_sysroot(
35+
dirs,
3436
channel,
3537
sysroot_kind,
36-
dist_dir,
3738
cg_clif_dylib,
3839
host_triple,
3940
target_triple,
@@ -43,13 +44,13 @@ pub(crate) fn run(
4344

4445
let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"];
4546

46-
let mut cmd = ABI_CAFE.run(&Compiler::host());
47+
let mut cmd = ABI_CAFE.run(&Compiler::host(), dirs);
4748
cmd.arg("--");
4849
cmd.arg("--pairs");
4950
cmd.args(pairs);
5051
cmd.arg("--add-rustc-codegen-backend");
5152
cmd.arg(format!("cgclif:{}", cg_clif_dylib.display()));
52-
cmd.current_dir(ABI_CAFE.source_dir());
53+
cmd.current_dir(ABI_CAFE.source_dir(dirs));
5354

5455
spawn_and_wait(cmd);
5556
}

build_system/build_backend.rs

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

4+
use super::path::{Dirs, 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(".");
8+
static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
89

910
pub(crate) fn build_backend(
11+
dirs: &Dirs,
1012
channel: &str,
1113
host_triple: &str,
1214
use_unstable_features: bool,
1315
) -> PathBuf {
14-
let mut cmd = CG_CLIF.build(&Compiler::host());
16+
let mut cmd = CG_CLIF.build(&Compiler::host(), dirs);
1517

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

@@ -43,8 +45,7 @@ pub(crate) fn build_backend(
4345
super::utils::spawn_and_wait(cmd);
4446

4547
CG_CLIF
46-
.source_dir()
47-
.join("target")
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: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
use std::fs;
2-
use std::path::{Path, PathBuf};
2+
use std::path::Path;
33
use std::process::{self, Command};
44

5+
use super::path::{Dirs, 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(
16+
dirs: &Dirs,
1017
channel: &str,
1118
sysroot_kind: SysrootKind,
12-
dist_dir: &Path,
1319
cg_clif_dylib_src: &Path,
1420
host_triple: &str,
1521
target_triple: &str,
1622
) {
1723
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
1824

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();
25+
DIST_DIR.ensure_fresh(dirs);
26+
BIN_DIR.ensure_exists(dirs);
27+
LIB_DIR.ensure_exists(dirs);
2428

2529
// 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"));
30+
let cg_clif_dylib_path = if cfg!(windows) {
31+
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
32+
// binaries.
33+
BIN_DIR
34+
} else {
35+
LIB_DIR
36+
}
37+
.to_path(dirs)
38+
.join(get_file_name("rustc_codegen_cranelift", "dylib"));
3539
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
3640

3741
// Build and copy rustc and cargo wrappers
@@ -40,18 +44,17 @@ pub(crate) fn build_sysroot(
4044

4145
let mut build_cargo_wrapper_cmd = Command::new("rustc");
4246
build_cargo_wrapper_cmd
43-
.arg(PathBuf::from("scripts").join(format!("{wrapper}.rs")))
47+
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
4448
.arg("-o")
45-
.arg(dist_dir.join(wrapper_name))
49+
.arg(DIST_DIR.to_path(dirs).join(wrapper_name))
4650
.arg("-g");
4751
spawn_and_wait(build_cargo_wrapper_cmd);
4852
}
4953

5054
let default_sysroot = super::rustc_info::get_default_sysroot();
5155

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");
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");
5558
fs::create_dir_all(&host_rustlib_lib).unwrap();
5659
fs::create_dir_all(&target_rustlib_lib).unwrap();
5760

@@ -112,13 +115,7 @@ pub(crate) fn build_sysroot(
112115
}
113116
}
114117
SysrootKind::Clif => {
115-
build_clif_sysroot_for_triple(
116-
channel,
117-
dist_dir,
118-
host_triple,
119-
&cg_clif_dylib_path,
120-
None,
121-
);
118+
build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None);
122119

123120
if host_triple != target_triple {
124121
// When cross-compiling it is often necessary to manually pick the right linker
@@ -128,8 +125,8 @@ pub(crate) fn build_sysroot(
128125
None
129126
};
130127
build_clif_sysroot_for_triple(
128+
dirs,
131129
channel,
132-
dist_dir,
133130
target_triple,
134131
&cg_clif_dylib_path,
135132
linker,
@@ -142,23 +139,26 @@ pub(crate) fn build_sysroot(
142139
let file = file.unwrap().path();
143140
let filename = file.file_name().unwrap().to_str().unwrap();
144141
if filename.contains("std-") && !filename.contains(".rlib") {
145-
try_hard_link(&file, dist_dir.join("lib").join(file.file_name().unwrap()));
142+
try_hard_link(&file, LIB_DIR.to_path(dirs).join(file.file_name().unwrap()));
146143
}
147144
}
148145
}
149146
}
150147
}
151148

152-
static STANDARD_LIBRARY: CargoProject = CargoProject::local("build_sysroot");
149+
// FIXME move to download/ or dist/
150+
pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = RelPath::BUILD_SYSROOT.join("rustc_version");
151+
pub(crate) static SYSROOT_SRC: RelPath = RelPath::BUILD_SYSROOT.join("sysroot_src");
152+
static STANDARD_LIBRARY: CargoProject = CargoProject::new(&RelPath::BUILD_SYSROOT, "build_sysroot");
153153

154154
fn build_clif_sysroot_for_triple(
155+
dirs: &Dirs,
155156
channel: &str,
156-
dist_dir: &Path,
157157
triple: &str,
158158
cg_clif_dylib_path: &Path,
159159
linker: Option<&str>,
160160
) {
161-
match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) {
161+
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
162162
Err(e) => {
163163
eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
164164
eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
@@ -176,7 +176,7 @@ fn build_clif_sysroot_for_triple(
176176
}
177177
}
178178

179-
let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
179+
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel);
180180

181181
if !super::config::get_bool("keep_sysroot") {
182182
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
@@ -189,7 +189,7 @@ fn build_clif_sysroot_for_triple(
189189
// Build sysroot
190190
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
191191
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
192-
rustflags.push_str(&format!(" --sysroot={}", dist_dir.to_str().unwrap()));
192+
rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap()));
193193
if channel == "release" {
194194
rustflags.push_str(" -Zmir-opt-level=3");
195195
}
@@ -199,18 +199,15 @@ fn build_clif_sysroot_for_triple(
199199
}
200200
let mut compiler = Compiler::with_triple(triple.to_owned());
201201
compiler.rustflags = rustflags;
202-
let mut build_cmd = STANDARD_LIBRARY.build(&compiler);
202+
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
203203
if channel == "release" {
204204
build_cmd.arg("--release");
205205
}
206206
build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
207207
spawn_and_wait(build_cmd);
208208

209209
// Copy all relevant files to the sysroot
210-
for entry in
211-
fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
212-
.unwrap()
213-
{
210+
for entry in fs::read_dir(build_dir.join("deps")).unwrap() {
214211
let entry = entry.unwrap();
215212
if let Some(ext) = entry.path().extension() {
216213
if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
@@ -221,7 +218,7 @@ fn build_clif_sysroot_for_triple(
221218
};
222219
try_hard_link(
223220
entry.path(),
224-
dist_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
221+
RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()),
225222
);
226223
}
227224
}

0 commit comments

Comments
 (0)