Skip to content

Commit

Permalink
Put the codegen backend dylib in the codegen-backends dir of the sysroot
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bjorn3 committed Dec 6, 2024
1 parent 7e0d1b6 commit 6a62f06
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
45 changes: 28 additions & 17 deletions build_system/build_sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,33 @@ pub(crate) fn build_sysroot(

let is_native = bootstrap_host_compiler.triple == target_triple;

let cg_clif_dylib_path = match cg_clif_dylib_src {
let cg_clif_backend_name = match cg_clif_dylib_src {
CodegenBackend::Local(src_path) => {
// Copy the backend
let cg_clif_dylib_path = if cfg!(windows) {
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
// binaries.
dist_dir.join("bin")
} else {
dist_dir.join("lib")
}
.join(src_path.file_name().unwrap());
// Create the codegen-backends dir in the sysroot
let dylib_dir = dist_dir
.join("lib")
.join("rustlib")
.join(&bootstrap_host_compiler.triple)
.join("codegen-backends");
fs::create_dir_all(&dylib_dir).unwrap();

// Copy the backend into the sysroot
let target_dylib_name = get_file_name(
&bootstrap_host_compiler.rustc,
"rustc_codegen_cranelift_local",
"dylib",
)
.replace("cranelift_local", "cranelift-local");
let cg_clif_dylib_path = dylib_dir.join(target_dylib_name);
try_hard_link(src_path, &cg_clif_dylib_path);
CodegenBackend::Local(cg_clif_dylib_path)

// This is using a different name from rustup distributed versions of cg_clif to allow
// switching between a locally built and rustup distributed version and to ensure that
// the rustup distributed version doesn't accidentally gets picked when trying to use
// the locally built version.
"cranelift-local".to_owned()
}
CodegenBackend::Builtin(name) => CodegenBackend::Builtin(name.clone()),
CodegenBackend::Builtin(name) => name.clone(),
};

// Build and copy rustc and cargo wrappers
Expand Down Expand Up @@ -72,17 +84,16 @@ pub(crate) fn build_sysroot(
.env("RUSTC", &bootstrap_host_compiler.rustc)
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
}
if let CodegenBackend::Builtin(name) = cg_clif_dylib_src {
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", name);
}
build_cargo_wrapper_cmd.env("HOST_TUPLE", &bootstrap_host_compiler.triple);
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", &cg_clif_backend_name);
spawn_and_wait(build_cargo_wrapper_cmd);
try_hard_link(wrapper_path, dist_dir.join("bin").join(wrapper_name));
}

let host = build_sysroot_for_triple(
dirs,
bootstrap_host_compiler.clone(),
&cg_clif_dylib_path,
&cg_clif_dylib_src,
sysroot_kind,
);
host.install_into_sysroot(dist_dir);
Expand All @@ -96,7 +107,7 @@ pub(crate) fn build_sysroot(
bootstrap_target_compiler.set_cross_linker_and_runner();
bootstrap_target_compiler
},
&cg_clif_dylib_path,
&cg_clif_dylib_src,
sysroot_kind,
)
.install_into_sysroot(dist_dir);
Expand Down
2 changes: 2 additions & 0 deletions build_system/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
RAND.clean(&runner.dirs);

if runner.is_native {
// FIXME all cargo tests are using both cargo-clif and rustc-clif, making usage of plain
// cargo-clif untested.
let mut test_cmd = RAND.test(&runner.target_compiler, &runner.dirs);
test_cmd.arg("--workspace").arg("--").arg("-q");
spawn_and_wait(test_cmd);
Expand Down
11 changes: 1 addition & 10 deletions scripts/cargo-clif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ fn main() {
}

let mut rustflags = vec!["-Cpanic=abort".to_owned(), "-Zpanic-abort-tests".to_owned()];
if let Some(name) = option_env!("BUILTIN_BACKEND") {
rustflags.push(format!("-Zcodegen-backend={name}"));
} else {
let dylib = sysroot.join(if cfg!(windows) { "bin" } else { "lib" }).join(
env::consts::DLL_PREFIX.to_string()
+ "rustc_codegen_cranelift"
+ env::consts::DLL_SUFFIX,
);
rustflags.push(format!("-Zcodegen-backend={}", dylib.to_str().unwrap()));
}
rustflags.push(concat!("-Zcodegen-backend=", env!("BUILTIN_BACKEND")).to_owned());
rustflags.push("--sysroot".to_owned());
rustflags.push(sysroot.to_str().unwrap().to_owned());

Expand Down
21 changes: 14 additions & 7 deletions scripts/rustc-clif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@ fn main() {
sysroot = sysroot.parent().unwrap();
}

let cg_clif_dylib_path = sysroot.join(if cfg!(windows) { "bin" } else { "lib" }).join(
env::consts::DLL_PREFIX.to_string() + "rustc_codegen_cranelift" + env::consts::DLL_SUFFIX,
);

let passed_args = std::env::args_os().skip(1).collect::<Vec<_>>();
let mut args = vec![];
args.push(OsString::from("-Cpanic=abort"));
args.push(OsString::from("-Zpanic-abort-tests"));
if let Some(name) = option_env!("BUILTIN_BACKEND") {
args.push(OsString::from(format!("-Zcodegen-backend={name}")))
} else {
if passed_args.iter().any(|arg| arg == "-vV" || arg == "--version") {
// Rustc ignores our sysroot when -vV is passed.
let cg_clif_dylib_path = sysroot
.join("lib")
.join("rustlib")
.join(env!("HOST_TUPLE"))
.join("codegen-backends")
.join(
env::consts::DLL_PREFIX.to_string()
+ "rustc_codegen_cranelift-local"
+ env::consts::DLL_SUFFIX,
);
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
codegen_backend_arg.push(cg_clif_dylib_path);
args.push(codegen_backend_arg);
} else {
args.push(OsString::from(concat!("-Zcodegen-backend=", env!("BUILTIN_BACKEND"))))
}
if !passed_args
.iter()
Expand Down

0 comments on commit 6a62f06

Please sign in to comment.