From 1f33785ed4a4774c5d68c78a13a24f9faf0dac55 Mon Sep 17 00:00:00 2001 From: Daniel Paoliello Date: Fri, 1 Jul 2022 13:01:41 -0700 Subject: Enable raw-dylib for binaries --- compiler/rustc_codegen_ssa/src/back/archive.rs | 29 +++++++++++++++++++++++++- compiler/rustc_codegen_ssa/src/back/link.rs | 27 +++++++++++++++++------- 2 files changed, 48 insertions(+), 8 deletions(-) (limited to 'compiler/rustc_codegen_ssa/src') diff --git a/compiler/rustc_codegen_ssa/src/back/archive.rs b/compiler/rustc_codegen_ssa/src/back/archive.rs index 1cb8d342381..53550b049db 100644 --- a/compiler/rustc_codegen_ssa/src/back/archive.rs +++ b/compiler/rustc_codegen_ssa/src/back/archive.rs @@ -51,10 +51,37 @@ pub trait ArchiveBuilder<'a> { fn build(self) -> bool; + fn sess(&self) -> &Session; + + /// Creates a DLL Import Library . + /// and returns the path on disk to that import library. + /// This functions doesn't take `self` so that it can be called from + /// `linker_with_args`, which is specialized on `ArchiveBuilder` but + /// doesn't take or create an instance of that type. + fn create_dll_import_lib( + sess: &Session, + lib_name: &str, + dll_imports: &[DllImport], + tmpdir: &Path, + ) -> PathBuf; + + /// Creates a DLL Import Library + /// and adds it to the current compilation's set of archives. fn inject_dll_import_lib( &mut self, lib_name: &str, dll_imports: &[DllImport], tmpdir: &MaybeTempDir, - ); + ) { + let output_path = + Self::create_dll_import_lib(self.sess(), lib_name, dll_imports, tmpdir.as_ref()); + + self.add_archive(&output_path, |_| false).unwrap_or_else(|e| { + self.sess().fatal(&format!( + "failed to add native library {}: {}", + output_path.display(), + e + )); + }); + } } diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 878a670cba3..a8513a426c6 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -120,7 +120,7 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>( &out_filename, codegen_results, path.as_ref(), - ); + )?; } } if sess.opts.json_artifact_notifications { @@ -650,7 +650,7 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>( out_filename: &Path, codegen_results: &CodegenResults, tmpdir: &Path, -) { +) -> Result<(), ErrorGuaranteed> { info!("preparing {:?} to {:?}", crate_type, out_filename); let (linker_path, flavor) = linker_and_flavor(sess); let mut cmd = linker_with_args::( @@ -661,7 +661,7 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>( tmpdir, out_filename, codegen_results, - ); + )?; linker::disable_localization(&mut cmd); @@ -1000,6 +1000,8 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>( (Strip::None, _) => {} } } + + Ok(()) } // Temporarily support both -Z strip and -C strip @@ -1848,7 +1850,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>( tmpdir: &Path, out_filename: &Path, codegen_results: &CodegenResults, -) -> Command { +) -> Result { let crt_objects_fallback = crt_objects_fallback(sess, crate_type); let cmd = &mut *super::linker::get_linker( sess, @@ -1955,6 +1957,18 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>( add_upstream_native_libraries(cmd, sess, codegen_results); } + // Link with the import library generated for any raw-dylib functions. + for (raw_dylib_name, raw_dylib_imports) in + collate_raw_dylibs(sess, &codegen_results.crate_info.used_libraries)? + { + cmd.add_object(&B::create_dll_import_lib( + sess, + &raw_dylib_name, + &raw_dylib_imports, + tmpdir, + )); + } + // Library linking above uses some global state for things like `-Bstatic`/`-Bdynamic` to make // command line shorter, reset it to default here before adding more libraries. cmd.reset_per_library_state(); @@ -1998,7 +2012,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>( // to it and remove the option. add_post_link_args(cmd, sess, flavor); - cmd.take_cmd() + Ok(cmd.take_cmd()) } fn add_order_independent_options( @@ -2222,8 +2236,7 @@ fn add_local_native_libraries( } } NativeLibKind::RawDylib => { - // FIXME(#58713): Proper handling for raw dylibs. - bug!("raw_dylib feature not yet implemented"); + // Ignore RawDylib here, they are handled separately in linker_with_args(). } } } -- cgit 1.4.1-3-g733a5