Auto merge of #98989 - dpaoliello:rawdylibbin, r=michaelwoerister

Enable raw-dylib for bin crates

Fixes #93842

When `raw-dylib` is used in a `bin` crate, we need to collect all of the `raw-dylib` functions, generate the import library and add that to the linker command line.

I also changed the tests so that 1) the C++ dlls are created after the Rust dlls, thus there is no chance of accidentally using them in the Rust linking process and 2) disabled generating import libraries when building with MSVC.
This commit is contained in:
bors 2022-07-26 01:47:34 +00:00
commit daaae25022
11 changed files with 108 additions and 53 deletions

View file

@ -11,7 +11,6 @@ use std::str;
use crate::llvm::archive_ro::{ArchiveRO, Child};
use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport};
use rustc_codegen_ssa::back::archive::ArchiveBuilder;
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_session::cstore::{DllCallingConvention, DllImport};
use rustc_session::Session;
@ -96,19 +95,23 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
}
}
fn inject_dll_import_lib(
&mut self,
fn sess(&self) -> &Session {
self.sess
}
fn create_dll_import_lib(
sess: &Session,
lib_name: &str,
dll_imports: &[DllImport],
tmpdir: &MaybeTempDir,
) {
tmpdir: &Path,
) -> PathBuf {
let output_path = {
let mut output_path: PathBuf = tmpdir.as_ref().to_path_buf();
let mut output_path: PathBuf = tmpdir.to_path_buf();
output_path.push(format!("{}_imports", lib_name));
output_path.with_extension("lib")
};
let target = &self.sess.target;
let target = &sess.target;
let mingw_gnu_toolchain = target.vendor == "pc"
&& target.os == "windows"
&& target.env == "gnu"
@ -117,7 +120,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
let import_name_and_ordinal_vector: Vec<(String, Option<u16>)> = dll_imports
.iter()
.map(|import: &DllImport| {
if self.sess.target.arch == "x86" {
if sess.target.arch == "x86" {
(
LlvmArchiveBuilder::i686_decorated_name(import, mingw_gnu_toolchain),
import.ordinal,
@ -134,8 +137,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
// that loaded but crashed with an AV upon calling one of the imported
// functions. Therefore, use binutils to create the import library instead,
// by writing a .DEF file to the temp dir and calling binutils's dlltool.
let def_file_path =
tmpdir.as_ref().join(format!("{}_imports", lib_name)).with_extension("def");
let def_file_path = tmpdir.join(format!("{}_imports", lib_name)).with_extension("def");
let def_file_content = format!(
"EXPORTS\n{}",
@ -154,11 +156,11 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
match std::fs::write(&def_file_path, def_file_content) {
Ok(_) => {}
Err(e) => {
self.sess.fatal(&format!("Error writing .DEF file: {}", e));
sess.fatal(&format!("Error writing .DEF file: {}", e));
}
};
let dlltool = find_binutils_dlltool(self.sess);
let dlltool = find_binutils_dlltool(sess);
let result = std::process::Command::new(dlltool)
.args([
"-d",
@ -172,9 +174,9 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
match result {
Err(e) => {
self.sess.fatal(&format!("Error calling dlltool: {}", e));
sess.fatal(&format!("Error calling dlltool: {}", e));
}
Ok(output) if !output.status.success() => self.sess.fatal(&format!(
Ok(output) if !output.status.success() => sess.fatal(&format!(
"Dlltool could not create import library: {}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
@ -220,13 +222,13 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
output_path_z.as_ptr(),
ffi_exports.as_ptr(),
ffi_exports.len(),
llvm_machine_type(&self.sess.target.arch) as u16,
!self.sess.target.is_like_msvc,
llvm_machine_type(&sess.target.arch) as u16,
!sess.target.is_like_msvc,
)
};
if result == crate::llvm::LLVMRustResult::Failure {
self.sess.fatal(&format!(
sess.fatal(&format!(
"Error creating import library for {}: {}",
lib_name,
llvm::last_error().unwrap_or("unknown LLVM error".to_string())
@ -234,13 +236,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
}
};
self.add_archive(&output_path, |_| false).unwrap_or_else(|e| {
self.sess.fatal(&format!(
"failed to add native library {}: {}",
output_path.display(),
e
));
});
output_path
}
}