Enable raw-dylib for binaries

This commit is contained in:
Daniel Paoliello 2022-07-01 13:01:41 -07:00
parent 144227dae9
commit 1f33785ed4
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
}
}