Skip to content

Commit dc4cb3d

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 994c8cf commit dc4cb3d

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
@@ -30,14 +30,33 @@ pub(crate) fn build_sysroot(
3030

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

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

4362
// Build and copy rustc and cargo wrappers
@@ -65,17 +84,16 @@ pub(crate) fn build_sysroot(
6584
.env("RUSTC", &bootstrap_host_compiler.rustc)
6685
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
6786
}
68-
if let CodegenBackend::Builtin(name) = cg_clif_dylib_src {
69-
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", name);
70-
}
87+
build_cargo_wrapper_cmd.env("HOST_TUPLE", &bootstrap_host_compiler.triple);
88+
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", &cg_clif_backend_name);
7189
spawn_and_wait(build_cargo_wrapper_cmd);
7290
try_hard_link(wrapper_path, dist_dir.join("bin").join(wrapper_name));
7391
}
7492

7593
let host = build_sysroot_for_triple(
7694
dirs,
7795
bootstrap_host_compiler.clone(),
78-
&cg_clif_dylib_path,
96+
&cg_clif_dylib_src,
7997
sysroot_kind,
8098
);
8199
host.install_into_sysroot(dist_dir);
@@ -89,7 +107,7 @@ pub(crate) fn build_sysroot(
89107
bootstrap_target_compiler.set_cross_linker_and_runner();
90108
bootstrap_target_compiler
91109
},
92-
&cg_clif_dylib_path,
110+
&cg_clif_dylib_src,
93111
sysroot_kind,
94112
)
95113
.install_into_sysroot(dist_dir);

build_system/tests.rs

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

139139
if runner.is_native {
140+
// FIXME all cargo tests are using both cargo-clif and rustc-clif, making usage of plain
141+
// cargo-clif untested.
140142
let mut test_cmd = RAND.test(&runner.target_compiler, &runner.dirs);
141143
test_cmd.arg("--workspace").arg("--").arg("-q");
142144
spawn_and_wait(test_cmd);

scripts/cargo-clif.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ fn main() {
1313
}
1414

1515
let mut rustflags = vec!["-Cpanic=abort".to_owned(), "-Zpanic-abort-tests".to_owned()];
16-
if let Some(name) = option_env!("BUILTIN_BACKEND") {
17-
rustflags.push(format!("-Zcodegen-backend={name}"));
18-
} else {
19-
let dylib = sysroot.join("lib").join(
20-
env::consts::DLL_PREFIX.to_string()
21-
+ "rustc_codegen_cranelift"
22-
+ env::consts::DLL_SUFFIX,
23-
);
24-
rustflags.push(format!("-Zcodegen-backend={}", dylib.to_str().unwrap()));
25-
}
16+
rustflags.push(concat!("-Zcodegen-backend=", env!("BUILTIN_BACKEND")).to_owned());
2617
rustflags.push("--sysroot".to_owned());
2718
rustflags.push(sysroot.to_str().unwrap().to_owned());
2819

scripts/rustc-clif.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,11 @@ 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
args.push(OsString::from("-Cpanic=abort"));
2117
args.push(OsString::from("-Zpanic-abort-tests"));
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-
}
18+
args.push(OsString::from(concat!("-Zcodegen-backend=", env!("BUILTIN_BACKEND"))));
2919
if !passed_args
3020
.iter()
3121
.any(|arg| arg == "--sysroot" || arg.to_str().is_some_and(|s| s.starts_with("--sysroot=")))

0 commit comments

Comments
 (0)