diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index e47e98299..5fc55be3f 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -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 @@ -72,9 +84,8 @@ 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)); } @@ -82,7 +93,7 @@ pub(crate) fn build_sysroot( 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); @@ -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); diff --git a/build_system/tests.rs b/build_system/tests.rs index 8de419a0c..731712b63 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -137,6 +137,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); diff --git a/scripts/cargo-clif.rs b/scripts/cargo-clif.rs index 1e14f41d4..00072fbc4 100644 --- a/scripts/cargo-clif.rs +++ b/scripts/cargo-clif.rs @@ -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()); diff --git a/scripts/rustc-clif.rs b/scripts/rustc-clif.rs index a27b9983b..b25db5575 100644 --- a/scripts/rustc-clif.rs +++ b/scripts/rustc-clif.rs @@ -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::>(); 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()