Skip to content

Commit 6a62f06

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 7e0d1b6 commit 6a62f06

File tree

4 files changed

+45
-34
lines changed

4 files changed

+45
-34
lines changed

build_system/build_sysroot.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +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 = if cfg!(windows) {
37-
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
38-
// binaries.
39-
dist_dir.join("bin")
40-
} else {
41-
dist_dir.join("lib")
42-
}
43-
.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);
4451
try_hard_link(src_path, &cg_clif_dylib_path);
45-
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()
4658
}
47-
CodegenBackend::Builtin(name) => CodegenBackend::Builtin(name.clone()),
59+
CodegenBackend::Builtin(name) => name.clone(),
4860
};
4961

5062
// Build and copy rustc and cargo wrappers
@@ -72,17 +84,16 @@ pub(crate) fn build_sysroot(
7284
.env("RUSTC", &bootstrap_host_compiler.rustc)
7385
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
7486
}
75-
if let CodegenBackend::Builtin(name) = cg_clif_dylib_src {
76-
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", name);
77-
}
87+
build_cargo_wrapper_cmd.env("HOST_TUPLE", &bootstrap_host_compiler.triple);
88+
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", &cg_clif_backend_name);
7889
spawn_and_wait(build_cargo_wrapper_cmd);
7990
try_hard_link(wrapper_path, dist_dir.join("bin").join(wrapper_name));
8091
}
8192

8293
let host = build_sysroot_for_triple(
8394
dirs,
8495
bootstrap_host_compiler.clone(),
85-
&cg_clif_dylib_path,
96+
&cg_clif_dylib_src,
8697
sysroot_kind,
8798
);
8899
host.install_into_sysroot(dist_dir);
@@ -96,7 +107,7 @@ pub(crate) fn build_sysroot(
96107
bootstrap_target_compiler.set_cross_linker_and_runner();
97108
bootstrap_target_compiler
98109
},
99-
&cg_clif_dylib_path,
110+
&cg_clif_dylib_src,
100111
sysroot_kind,
101112
)
102113
.install_into_sysroot(dist_dir);

build_system/tests.rs

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

146146
if runner.is_native {
147+
// FIXME all cargo tests are using both cargo-clif and rustc-clif, making usage of plain
148+
// cargo-clif untested.
147149
let mut test_cmd = RAND.test(&runner.target_compiler, &runner.dirs);
148150
test_cmd.arg("--workspace").arg("--").arg("-q");
149151
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(if cfg!(windows) { "bin" } else { "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: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,27 @@ fn main() {
1111
sysroot = sysroot.parent().unwrap();
1212
}
1313

14-
let cg_clif_dylib_path = sysroot.join(if cfg!(windows) { "bin" } else { "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 {
18+
if passed_args.iter().any(|arg| arg == "-vV" || arg == "--version") {
19+
// Rustc ignores our sysroot when -vV is passed.
20+
let cg_clif_dylib_path = sysroot
21+
.join("lib")
22+
.join("rustlib")
23+
.join(env!("HOST_TUPLE"))
24+
.join("codegen-backends")
25+
.join(
26+
env::consts::DLL_PREFIX.to_string()
27+
+ "rustc_codegen_cranelift-local"
28+
+ env::consts::DLL_SUFFIX,
29+
);
2530
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
2631
codegen_backend_arg.push(cg_clif_dylib_path);
2732
args.push(codegen_backend_arg);
33+
} else {
34+
args.push(OsString::from(concat!("-Zcodegen-backend=", env!("BUILTIN_BACKEND"))))
2835
}
2936
if !passed_args
3037
.iter()

0 commit comments

Comments
 (0)