Skip to content

Commit 8d16674

Browse files
committed
Put the codegen backend dylib in the codegen-backends dir of the sysroot
This will make it possible in the future to switch between the Cranelift and LLVM backends in Cargo.toml or .cargo/config.toml even when using a local build of cg_clif.
1 parent 8b11468 commit 8d16674

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

build_system/build_sysroot.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,33 @@ pub(crate) fn build_sysroot(
3131

3232
let is_native = bootstrap_host_compiler.triple == target_triple;
3333

34-
let cg_clif_dylib_path = match cg_clif_dylib_src {
34+
let cg_clif_backend_name = match cg_clif_dylib_src {
3535
CodegenBackend::Local(src_path) => {
36-
// Copy the backend
37-
let cg_clif_dylib_path = dist_dir.join("lib").join(src_path.file_name().unwrap());
36+
// Create the codegen-backends dir in the sysroot
37+
let dylib_dir = dist_dir
38+
.join("lib")
39+
.join("rustlib")
40+
.join(&bootstrap_host_compiler.triple)
41+
.join("codegen-backends");
42+
fs::create_dir_all(&dylib_dir).unwrap();
43+
44+
// Copy the backend into the sysroot
45+
let target_dylib_name = get_file_name(
46+
&bootstrap_host_compiler.rustc,
47+
"rustc_codegen_cranelift_local",
48+
"dylib",
49+
)
50+
.replace("cranelift_local", "cranelift-local");
51+
let cg_clif_dylib_path = dylib_dir.join(target_dylib_name);
3852
try_hard_link(src_path, &cg_clif_dylib_path);
39-
CodegenBackend::Local(cg_clif_dylib_path)
53+
54+
// This is using a different name from rustup distributed versions of cg_clif to allow
55+
// switching between a locally built and rustup distributed version and to ensure that
56+
// the rustup distributed version doesn't accidentally gets picked when trying to use
57+
// the locally built version.
58+
"cranelift-local".to_owned()
4059
}
41-
CodegenBackend::Builtin(name) => CodegenBackend::Builtin(name.clone()),
60+
CodegenBackend::Builtin(name) => name.clone(),
4261
};
4362

4463
// Build and copy rustc and cargo wrappers
@@ -69,17 +88,16 @@ pub(crate) fn build_sysroot(
6988
.env("RUSTC", &bootstrap_host_compiler.rustc)
7089
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
7190
}
72-
if let CodegenBackend::Builtin(name) = cg_clif_dylib_src {
73-
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", name);
74-
}
91+
build_cargo_wrapper_cmd.env("HOST_TUPLE", &bootstrap_host_compiler.triple);
92+
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", &cg_clif_backend_name);
7593
spawn_and_wait(build_cargo_wrapper_cmd);
7694
try_hard_link(wrapper_path, dist_dir.join("bin").join(wrapper_name));
7795
}
7896

7997
let host = build_sysroot_for_triple(
8098
dirs,
8199
bootstrap_host_compiler.clone(),
82-
&cg_clif_dylib_path,
100+
&cg_clif_dylib_src,
83101
sysroot_kind,
84102
panic_unwind_support,
85103
);
@@ -94,7 +112,7 @@ pub(crate) fn build_sysroot(
94112
bootstrap_target_compiler.set_cross_linker_and_runner();
95113
bootstrap_target_compiler
96114
},
97-
&cg_clif_dylib_path,
115+
&cg_clif_dylib_src,
98116
sysroot_kind,
99117
panic_unwind_support,
100118
)

build_system/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
156156
RAND.clean(&runner.dirs);
157157

158158
if runner.is_native {
159+
// FIXME all cargo tests are using both cargo-clif and rustc-clif, making usage of plain
160+
// cargo-clif untested.
159161
let mut test_cmd = RAND.test(&runner.target_compiler, &runner.dirs);
160162
test_cmd.arg("--workspace").arg("--").arg("-q");
161163
spawn_and_wait(test_cmd);

scripts/cargo-clif.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@ fn main() {
1717
rustflags.push("-Cpanic=abort".to_owned());
1818
rustflags.push("-Zpanic-abort-tests".to_owned());
1919
}
20-
if let Some(name) = option_env!("BUILTIN_BACKEND") {
21-
rustflags.push(format!("-Zcodegen-backend={name}"));
22-
} else {
23-
let dylib = sysroot.join("lib").join(
24-
env::consts::DLL_PREFIX.to_string()
25-
+ "rustc_codegen_cranelift"
26-
+ env::consts::DLL_SUFFIX,
27-
);
28-
rustflags.push(format!("-Zcodegen-backend={}", dylib.to_str().unwrap()));
29-
}
20+
rustflags.push(concat!("-Zcodegen-backend=", env!("BUILTIN_BACKEND")).to_owned());
3021
rustflags.push("--sysroot".to_owned());
3122
rustflags.push(sysroot.to_str().unwrap().to_owned());
3223

scripts/rustc-clif.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,13 @@ fn main() {
1111
sysroot = sysroot.parent().unwrap();
1212
}
1313

14-
let cg_clif_dylib_path = sysroot.join("lib").join(
15-
env::consts::DLL_PREFIX.to_string() + "rustc_codegen_cranelift" + env::consts::DLL_SUFFIX,
16-
);
17-
1814
let passed_args = std::env::args_os().skip(1).collect::<Vec<_>>();
1915
let mut args = vec![];
2016
if !cfg!(support_panic_unwind) {
2117
args.push(OsString::from("-Cpanic=abort"));
2218
args.push(OsString::from("-Zpanic-abort-tests"));
2319
}
24-
if let Some(name) = option_env!("BUILTIN_BACKEND") {
25-
args.push(OsString::from(format!("-Zcodegen-backend={name}")))
26-
} else {
27-
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
28-
codegen_backend_arg.push(cg_clif_dylib_path);
29-
args.push(codegen_backend_arg);
30-
}
20+
args.push(OsString::from(concat!("-Zcodegen-backend=", env!("BUILTIN_BACKEND"))));
3121
if !passed_args
3222
.iter()
3323
.any(|arg| arg == "--sysroot" || arg.to_str().is_some_and(|s| s.starts_with("--sysroot=")))

0 commit comments

Comments
 (0)