Skip to content

Commit 5b3bc29

Browse files
committed
Allow testing 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 c87dfd9 commit 5b3bc29

File tree

8 files changed

+101
-48
lines changed

8 files changed

+101
-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");
@@ -65,6 +71,9 @@ pub(crate) fn build_sysroot(
6571
.env("RUSTC", &bootstrap_host_compiler.rustc)
6672
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
6773
}
74+
if let CodegenBackend::Builtin(name) = cg_clif_dylib_src {
75+
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", name);
76+
}
6877
spawn_and_wait(build_cargo_wrapper_cmd);
6978
try_hard_link(wrapper_path, BIN_DIR.to_path(dirs).join(wrapper_name));
7079
}
@@ -159,15 +168,15 @@ fn build_sysroot_for_triple(
159168
dirs: &Dirs,
160169
channel: &str,
161170
compiler: Compiler,
162-
cg_clif_dylib_path: &Path,
171+
cg_clif_dylib_path: &CodegenBackend,
163172
sysroot_kind: SysrootKind,
164173
) -> SysrootTarget {
165174
match sysroot_kind {
166175
SysrootKind::None => build_rtstartup(dirs, &compiler)
167176
.unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }),
168177
SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler),
169178
SysrootKind::Clif => {
170-
build_clif_sysroot_for_triple(dirs, channel, compiler, &cg_clif_dylib_path)
179+
build_clif_sysroot_for_triple(dirs, channel, compiler, cg_clif_dylib_path)
171180
}
172181
}
173182
}
@@ -211,7 +220,7 @@ fn build_clif_sysroot_for_triple(
211220
dirs: &Dirs,
212221
channel: &str,
213222
mut compiler: Compiler,
214-
cg_clif_dylib_path: &Path,
223+
cg_clif_dylib_path: &CodegenBackend,
215224
) -> SysrootTarget {
216225
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
217226
Err(e) => {
@@ -249,7 +258,14 @@ fn build_clif_sysroot_for_triple(
249258

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

build_system/mod.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ pub(crate) enum SysrootKind {
4343
Llvm,
4444
}
4545

46+
#[derive(Clone, Debug)]
47+
pub(crate) enum CodegenBackend {
48+
Local(PathBuf),
49+
Builtin(String),
50+
}
51+
4652
pub(crate) fn main() {
4753
if env::var("RUST_BACKTRACE").is_err() {
4854
env::set_var("RUST_BACKTRACE", "1");
@@ -79,6 +85,7 @@ pub(crate) fn main() {
7985
let mut sysroot_kind = SysrootKind::Clif;
8086
let mut use_unstable_features = true;
8187
let mut frozen = false;
88+
let mut use_backend = None;
8289
while let Some(arg) = args.next().as_deref() {
8390
match arg {
8491
"--out-dir" => {
@@ -98,6 +105,12 @@ pub(crate) fn main() {
98105
}
99106
"--no-unstable-features" => use_unstable_features = false,
100107
"--frozen" => frozen = true,
108+
"--use-backend" => {
109+
use_backend = Some(match args.next() {
110+
Some(name) => name,
111+
None => arg_error!("--use-backend requires argument"),
112+
});
113+
}
101114
flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
102115
arg => arg_error!("Unexpected argument {}", arg),
103116
}
@@ -164,12 +177,16 @@ pub(crate) fn main() {
164177
env::set_var("RUSTC", "rustc_should_be_set_explicitly");
165178
env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly");
166179

167-
let cg_clif_dylib = build_backend::build_backend(
168-
&dirs,
169-
channel,
170-
&bootstrap_host_compiler,
171-
use_unstable_features,
172-
);
180+
let cg_clif_dylib = if let Some(name) = use_backend {
181+
CodegenBackend::Builtin(name)
182+
} else {
183+
CodegenBackend::Local(build_backend::build_backend(
184+
&dirs,
185+
channel,
186+
&bootstrap_host_compiler,
187+
use_unstable_features,
188+
))
189+
};
173190
match command {
174191
Command::Prepare => {
175192
// 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");
@@ -215,7 +214,7 @@ pub(crate) fn run_tests(
215214
dirs: &Dirs,
216215
channel: &str,
217216
sysroot_kind: SysrootKind,
218-
cg_clif_dylib: &Path,
217+
cg_clif_dylib: &CodegenBackend,
219218
bootstrap_host_compiler: &Compiler,
220219
rustup_toolchain_name: Option<&str>,
221220
target_triple: String,

build_system/usage.txt

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ OPTIONS:
2929
--frozen
3030
Require Cargo.lock and cache are up to date
3131

32+
--use-backend NAME
33+
Use the existing Cranelift (or other) backend of the rustc with which we built.
34+
Warning: This is meant for use in rust's CI only!
35+
3236
REQUIREMENTS:
3337
* Rustup: By default rustup is used to install the right nightly version. If you don't want to
3438
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
@@ -12,17 +12,21 @@ fn main() {
1212

1313
let mut rustflags = String::new();
1414
rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
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-
);
15+
if let Some(name) = option_env!("BUILTIN_BACKEND") {
16+
rustflags.push_str(name);
17+
} else {
18+
rustflags.push_str(
19+
sysroot
20+
.join(if cfg!(windows) { "bin" } else { "lib" })
21+
.join(
22+
env::consts::DLL_PREFIX.to_string()
23+
+ "rustc_codegen_cranelift"
24+
+ env::consts::DLL_SUFFIX,
25+
)
26+
.to_str()
27+
.unwrap(),
28+
);
29+
}
2630
rustflags.push_str(" --sysroot ");
2731
rustflags.push_str(sysroot.to_str().unwrap());
2832
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
@@ -19,9 +19,13 @@ fn main() {
1919
let mut args = vec![];
2020
args.push(OsString::from("-Cpanic=abort"));
2121
args.push(OsString::from("-Zpanic-abort-tests"));
22-
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
23-
codegen_backend_arg.push(cg_clif_dylib_path);
24-
args.push(codegen_backend_arg);
22+
if let Some(name) = option_env!("BUILTIN_BACKEND") {
23+
args.push(OsString::from(format!("-Zcodegen-backend={name}")))
24+
} else {
25+
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
26+
codegen_backend_arg.push(cg_clif_dylib_path);
27+
args.push(codegen_backend_arg);
28+
}
2529
if !passed_args.iter().any(|arg| {
2630
arg == "--sysroot" || arg.to_str().map(|s| s.starts_with("--sysroot=")) == Some(true)
2731
}) {

scripts/rustdoc-clif.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ fn main() {
1919
let mut args = vec![];
2020
args.push(OsString::from("-Cpanic=abort"));
2121
args.push(OsString::from("-Zpanic-abort-tests"));
22-
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
23-
codegen_backend_arg.push(cg_clif_dylib_path);
24-
args.push(codegen_backend_arg);
22+
if let Some(name) = option_env!("BUILTIN_BACKEND") {
23+
args.push(OsString::from(format!("-Zcodegen-backend={name}")))
24+
} else {
25+
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
26+
codegen_backend_arg.push(cg_clif_dylib_path);
27+
args.push(codegen_backend_arg);
28+
}
2529
if !passed_args.iter().any(|arg| {
2630
arg == "--sysroot" || arg.to_str().map(|s| s.starts_with("--sysroot=")) == Some(true)
2731
}) {

0 commit comments

Comments
 (0)