Skip to content

Commit 0bef691

Browse files
committed
Allow using a cranelift backend built into rustc itself
This avoids building cranelift twice in rust's CI and is a lot easier than trying to make building of codegen backends work from within a cargo invocation done by rust's build system.
1 parent 39bfad7 commit 0bef691

File tree

8 files changed

+96
-48
lines changed

8 files changed

+96
-48
lines changed

build_system/abi_cafe.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use std::path::Path;
2-
31
use super::build_sysroot;
42
use super::path::Dirs;
53
use super::prepare::GitRepo;
64
use super::utils::{spawn_and_wait, CargoProject, Compiler};
7-
use super::SysrootKind;
5+
use super::{CodegenBackend, SysrootKind};
86

97
static ABI_CAFE_REPO: GitRepo =
108
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");
@@ -15,7 +13,7 @@ pub(crate) fn run(
1513
channel: &str,
1614
sysroot_kind: SysrootKind,
1715
dirs: &Dirs,
18-
cg_clif_dylib: &Path,
16+
cg_clif_dylib: &CodegenBackend,
1917
rustup_toolchain_name: Option<&str>,
2018
bootstrap_host_compiler: &Compiler,
2119
) {
@@ -41,7 +39,14 @@ pub(crate) fn run(
4139
cmd.arg("--pairs");
4240
cmd.args(pairs);
4341
cmd.arg("--add-rustc-codegen-backend");
44-
cmd.arg(format!("cgclif:{}", cg_clif_dylib.display()));
42+
match cg_clif_dylib {
43+
CodegenBackend::Local(path) => {
44+
cmd.arg(format!("cgclif:{}", path.display()));
45+
}
46+
CodegenBackend::Builtin(name) => {
47+
cmd.arg(format!("cgclif:{name}"));
48+
}
49+
}
4550
cmd.current_dir(ABI_CAFE.source_dir(dirs));
4651

4752
spawn_and_wait(cmd);

build_system/build_sysroot.rs

+33-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::process::{self, Command};
55
use super::path::{Dirs, RelPath};
66
use super::rustc_info::{get_file_name, get_rustc_version};
77
use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler};
8-
use super::SysrootKind;
8+
use super::{CodegenBackend, SysrootKind};
99

1010
static DIST_DIR: RelPath = RelPath::DIST;
1111
static BIN_DIR: RelPath = RelPath::DIST.join("bin");
@@ -15,7 +15,7 @@ pub(crate) fn build_sysroot(
1515
dirs: &Dirs,
1616
channel: &str,
1717
sysroot_kind: SysrootKind,
18-
cg_clif_dylib_src: &Path,
18+
cg_clif_dylib_src: &CodegenBackend,
1919
bootstrap_host_compiler: &Compiler,
2020
rustup_toolchain_name: Option<&str>,
2121
target_triple: String,
@@ -28,17 +28,23 @@ pub(crate) fn build_sysroot(
2828

2929
let is_native = bootstrap_host_compiler.triple == target_triple;
3030

31-
// Copy the backend
32-
let cg_clif_dylib_path = if cfg!(windows) {
33-
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
34-
// binaries.
35-
BIN_DIR
36-
} else {
37-
LIB_DIR
38-
}
39-
.to_path(dirs)
40-
.join(cg_clif_dylib_src.file_name().unwrap());
41-
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
31+
let cg_clif_dylib_path = match cg_clif_dylib_src {
32+
CodegenBackend::Local(src_path) => {
33+
// Copy the backend
34+
let cg_clif_dylib_path = if cfg!(windows) {
35+
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
36+
// binaries.
37+
BIN_DIR
38+
} else {
39+
LIB_DIR
40+
}
41+
.to_path(dirs)
42+
.join(src_path.file_name().unwrap());
43+
try_hard_link(src_path, &cg_clif_dylib_path);
44+
CodegenBackend::Local(cg_clif_dylib_path)
45+
}
46+
CodegenBackend::Builtin(name) => CodegenBackend::Builtin(name.clone()),
47+
};
4248

4349
// Build and copy rustc and cargo wrappers
4450
let wrapper_base_name = get_file_name(&bootstrap_host_compiler.rustc, "____", "bin");
@@ -64,6 +70,9 @@ pub(crate) fn build_sysroot(
6470
.env("RUSTC", &bootstrap_host_compiler.rustc)
6571
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
6672
}
73+
if let CodegenBackend::Builtin(name) = cg_clif_dylib_src {
74+
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", name);
75+
}
6776
spawn_and_wait(build_cargo_wrapper_cmd);
6877
}
6978

@@ -157,15 +166,15 @@ fn build_sysroot_for_triple(
157166
dirs: &Dirs,
158167
channel: &str,
159168
compiler: Compiler,
160-
cg_clif_dylib_path: &Path,
169+
cg_clif_dylib_path: &CodegenBackend,
161170
sysroot_kind: SysrootKind,
162171
) -> SysrootTarget {
163172
match sysroot_kind {
164173
SysrootKind::None => build_rtstartup(dirs, &compiler)
165174
.unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }),
166175
SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler),
167176
SysrootKind::Clif => {
168-
build_clif_sysroot_for_triple(dirs, channel, compiler, &cg_clif_dylib_path)
177+
build_clif_sysroot_for_triple(dirs, channel, compiler, cg_clif_dylib_path)
169178
}
170179
}
171180
}
@@ -209,7 +218,7 @@ fn build_clif_sysroot_for_triple(
209218
dirs: &Dirs,
210219
channel: &str,
211220
mut compiler: Compiler,
212-
cg_clif_dylib_path: &Path,
221+
cg_clif_dylib_path: &CodegenBackend,
213222
) -> SysrootTarget {
214223
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
215224
Err(e) => {
@@ -247,7 +256,14 @@ fn build_clif_sysroot_for_triple(
247256

248257
// Build sysroot
249258
let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
250-
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
259+
match cg_clif_dylib_path {
260+
CodegenBackend::Local(path) => {
261+
rustflags.push_str(&format!(" -Zcodegen-backend={}", path.to_str().unwrap()));
262+
}
263+
CodegenBackend::Builtin(name) => {
264+
rustflags.push_str(&format!(" -Zcodegen-backend={name}"));
265+
}
266+
};
251267
// Necessary for MinGW to find rsbegin.o and rsend.o
252268
rustflags
253269
.push_str(&format!(" --sysroot {}", RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap()));

build_system/main.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ enum SysrootKind {
4747
Llvm,
4848
}
4949

50+
#[derive(Clone, Debug)]
51+
enum CodegenBackend {
52+
Local(PathBuf),
53+
Builtin(String),
54+
}
55+
5056
fn main() {
5157
if env::var("RUST_BACKTRACE").is_err() {
5258
env::set_var("RUST_BACKTRACE", "1");
@@ -84,6 +90,7 @@ fn main() {
8490
let mut sysroot_kind = SysrootKind::Clif;
8591
let mut use_unstable_features = true;
8692
let mut frozen = false;
93+
let mut use_existing_backend = false;
8794
while let Some(arg) = args.next().as_deref() {
8895
match arg {
8996
"--out-dir" => {
@@ -108,6 +115,7 @@ fn main() {
108115
}
109116
"--no-unstable-features" => use_unstable_features = false,
110117
"--frozen" => frozen = true,
118+
"--use-existing-backend" => use_existing_backend = true,
111119
flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
112120
arg => arg_error!("Unexpected argument {}", arg),
113121
}
@@ -176,12 +184,16 @@ fn main() {
176184
env::set_var("RUSTC", "rustc_should_be_set_explicitly");
177185
env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly");
178186

179-
let cg_clif_dylib = build_backend::build_backend(
180-
&dirs,
181-
channel,
182-
&bootstrap_host_compiler,
183-
use_unstable_features,
184-
);
187+
let cg_clif_dylib = if use_existing_backend {
188+
CodegenBackend::Builtin("cranelift".to_owned())
189+
} else {
190+
CodegenBackend::Local(build_backend::build_backend(
191+
&dirs,
192+
channel,
193+
&bootstrap_host_compiler,
194+
use_unstable_features,
195+
))
196+
};
185197
match command {
186198
Command::Prepare => {
187199
// Handled above

build_system/tests.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use super::config;
33
use super::path::{Dirs, RelPath};
44
use super::prepare::GitRepo;
55
use super::utils::{spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler};
6-
use super::SysrootKind;
6+
use super::{CodegenBackend, SysrootKind};
77
use std::env;
88
use std::ffi::OsStr;
99
use std::fs;
10-
use std::path::Path;
1110
use std::process::Command;
1211

1312
static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example");
@@ -235,7 +234,7 @@ pub(crate) fn run_tests(
235234
channel: &str,
236235
sysroot_kind: SysrootKind,
237236
use_unstable_features: bool,
238-
cg_clif_dylib: &Path,
237+
cg_clif_dylib: &CodegenBackend,
239238
bootstrap_host_compiler: &Compiler,
240239
rustup_toolchain_name: Option<&str>,
241240
target_triple: String,

build_system/usage.txt

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ OPTIONS:
3232
--frozen
3333
Require Cargo.lock and cache are up to date
3434

35+
--use-existing-backend
36+
Use the existing Cranelift backend of the rustc with which we built.
37+
Warning: This is meant for use in rust's CI only!
38+
3539
REQUIREMENTS:
3640
* Rustup: By default rustup is used to install the right nightly version. If you don't want to
3741
use rustup, you can manually install the nightly version indicated by rust-toolchain.toml and

scripts/cargo-clif.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ fn main() {
99

1010
let mut rustflags = String::new();
1111
rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
12-
rustflags.push_str(
13-
sysroot
14-
.join(if cfg!(windows) { "bin" } else { "lib" })
15-
.join(
16-
env::consts::DLL_PREFIX.to_string()
17-
+ "rustc_codegen_cranelift"
18-
+ env::consts::DLL_SUFFIX,
19-
)
20-
.to_str()
21-
.unwrap(),
22-
);
12+
if let Some(name) = option_env!("BUILTIN_BACKEND") {
13+
rustflags.push_str(name);
14+
} else {
15+
rustflags.push_str(
16+
sysroot
17+
.join(if cfg!(windows) { "bin" } else { "lib" })
18+
.join(
19+
env::consts::DLL_PREFIX.to_string()
20+
+ "rustc_codegen_cranelift"
21+
+ env::consts::DLL_SUFFIX,
22+
)
23+
.to_str()
24+
.unwrap(),
25+
);
26+
}
2327
rustflags.push_str(" --sysroot ");
2428
rustflags.push_str(sysroot.to_str().unwrap());
2529
env::set_var("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or(String::new()) + &rustflags);

scripts/rustc-clif.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ fn main() {
1515
let mut args = std::env::args_os().skip(1).collect::<Vec<_>>();
1616
args.push(OsString::from("-Cpanic=abort"));
1717
args.push(OsString::from("-Zpanic-abort-tests"));
18-
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
19-
codegen_backend_arg.push(cg_clif_dylib_path);
20-
args.push(codegen_backend_arg);
18+
if let Some(name) = option_env!("BUILTIN_BACKEND") {
19+
args.push(OsString::from(format!("-Zcodegen-backend={name}")))
20+
} else {
21+
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
22+
codegen_backend_arg.push(cg_clif_dylib_path);
23+
args.push(codegen_backend_arg);
24+
}
2125
if !args.contains(&OsString::from("--sysroot")) {
2226
args.push(OsString::from("--sysroot"));
2327
args.push(OsString::from(sysroot.to_str().unwrap()));

scripts/rustdoc-clif.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ fn main() {
1515
let mut args = std::env::args_os().skip(1).collect::<Vec<_>>();
1616
args.push(OsString::from("-Cpanic=abort"));
1717
args.push(OsString::from("-Zpanic-abort-tests"));
18-
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
19-
codegen_backend_arg.push(cg_clif_dylib_path);
20-
args.push(codegen_backend_arg);
18+
if let Some(name) = option_env!("BUILTIN_BACKEND") {
19+
args.push(OsString::from(format!("-Zcodegen-backend={name}")))
20+
} else {
21+
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
22+
codegen_backend_arg.push(cg_clif_dylib_path);
23+
args.push(codegen_backend_arg);
24+
}
2125
if !args.contains(&OsString::from("--sysroot")) {
2226
args.push(OsString::from("--sysroot"));
2327
args.push(OsString::from(sysroot.to_str().unwrap()));

0 commit comments

Comments
 (0)