Inline ArchiveConfig struct into LlvmArchiveBuilder
This commit is contained in:
parent
4e02a9281d
commit
70e084aa21
1 changed files with 21 additions and 28 deletions
|
@ -15,16 +15,12 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
|
||||||
use rustc_session::cstore::{DllCallingConvention, DllImport};
|
use rustc_session::cstore::{DllCallingConvention, DllImport};
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
|
|
||||||
struct ArchiveConfig<'a> {
|
|
||||||
pub sess: &'a Session,
|
|
||||||
pub dst: PathBuf,
|
|
||||||
pub src: Option<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Helper for adding many files to an archive.
|
/// Helper for adding many files to an archive.
|
||||||
#[must_use = "must call build() to finish building the archive"]
|
#[must_use = "must call build() to finish building the archive"]
|
||||||
pub struct LlvmArchiveBuilder<'a> {
|
pub struct LlvmArchiveBuilder<'a> {
|
||||||
config: ArchiveConfig<'a>,
|
sess: &'a Session,
|
||||||
|
dst: PathBuf,
|
||||||
|
src: Option<PathBuf>,
|
||||||
removals: Vec<String>,
|
removals: Vec<String>,
|
||||||
additions: Vec<Addition>,
|
additions: Vec<Addition>,
|
||||||
src_archive: Option<Option<ArchiveRO>>,
|
src_archive: Option<Option<ArchiveRO>>,
|
||||||
|
@ -50,10 +46,6 @@ fn is_relevant_child(c: &Child<'_>) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn archive_config<'a>(sess: &'a Session, output: &Path, input: Option<&Path>) -> ArchiveConfig<'a> {
|
|
||||||
ArchiveConfig { sess, dst: output.to_path_buf(), src: input.map(|p| p.to_path_buf()) }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map machine type strings to values of LLVM's MachineTypes enum.
|
/// Map machine type strings to values of LLVM's MachineTypes enum.
|
||||||
fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
|
fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
|
||||||
match cpu {
|
match cpu {
|
||||||
|
@ -69,9 +61,10 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
/// Creates a new static archive, ready for modifying the archive specified
|
/// Creates a new static archive, ready for modifying the archive specified
|
||||||
/// by `config`.
|
/// by `config`.
|
||||||
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> LlvmArchiveBuilder<'a> {
|
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> LlvmArchiveBuilder<'a> {
|
||||||
let config = archive_config(sess, output, input);
|
|
||||||
LlvmArchiveBuilder {
|
LlvmArchiveBuilder {
|
||||||
config,
|
sess,
|
||||||
|
dst: output.to_path_buf(),
|
||||||
|
src: input.map(|p| p.to_path_buf()),
|
||||||
removals: Vec::new(),
|
removals: Vec::new(),
|
||||||
additions: Vec::new(),
|
additions: Vec::new(),
|
||||||
src_archive: None,
|
src_archive: None,
|
||||||
|
@ -131,11 +124,11 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
/// `Archive`.
|
/// `Archive`.
|
||||||
fn build(mut self) {
|
fn build(mut self) {
|
||||||
let kind = self.llvm_archive_kind().unwrap_or_else(|kind| {
|
let kind = self.llvm_archive_kind().unwrap_or_else(|kind| {
|
||||||
self.config.sess.fatal(&format!("Don't know how to build archive of type: {}", kind))
|
self.sess.fatal(&format!("Don't know how to build archive of type: {}", kind))
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Err(e) = self.build_with_llvm(kind) {
|
if let Err(e) = self.build_with_llvm(kind) {
|
||||||
self.config.sess.fatal(&format!("failed to build archive: {}", e));
|
self.sess.fatal(&format!("failed to build archive: {}", e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +144,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
output_path.with_extension("lib")
|
output_path.with_extension("lib")
|
||||||
};
|
};
|
||||||
|
|
||||||
let target = &self.config.sess.target;
|
let target = &self.sess.target;
|
||||||
let mingw_gnu_toolchain = target.vendor == "pc"
|
let mingw_gnu_toolchain = target.vendor == "pc"
|
||||||
&& target.os == "windows"
|
&& target.os == "windows"
|
||||||
&& target.env == "gnu"
|
&& target.env == "gnu"
|
||||||
|
@ -160,7 +153,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
let import_name_and_ordinal_vector: Vec<(String, Option<u16>)> = dll_imports
|
let import_name_and_ordinal_vector: Vec<(String, Option<u16>)> = dll_imports
|
||||||
.iter()
|
.iter()
|
||||||
.map(|import: &DllImport| {
|
.map(|import: &DllImport| {
|
||||||
if self.config.sess.target.arch == "x86" {
|
if self.sess.target.arch == "x86" {
|
||||||
(
|
(
|
||||||
LlvmArchiveBuilder::i686_decorated_name(import, mingw_gnu_toolchain),
|
LlvmArchiveBuilder::i686_decorated_name(import, mingw_gnu_toolchain),
|
||||||
import.ordinal,
|
import.ordinal,
|
||||||
|
@ -197,11 +190,11 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
match std::fs::write(&def_file_path, def_file_content) {
|
match std::fs::write(&def_file_path, def_file_content) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.config.sess.fatal(&format!("Error writing .DEF file: {}", e));
|
self.sess.fatal(&format!("Error writing .DEF file: {}", e));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let dlltool = find_binutils_dlltool(self.config.sess);
|
let dlltool = find_binutils_dlltool(self.sess);
|
||||||
let result = std::process::Command::new(dlltool)
|
let result = std::process::Command::new(dlltool)
|
||||||
.args([
|
.args([
|
||||||
"-d",
|
"-d",
|
||||||
|
@ -215,9 +208,9 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.config.sess.fatal(&format!("Error calling dlltool: {}", e));
|
self.sess.fatal(&format!("Error calling dlltool: {}", e));
|
||||||
}
|
}
|
||||||
Ok(output) if !output.status.success() => self.config.sess.fatal(&format!(
|
Ok(output) if !output.status.success() => self.sess.fatal(&format!(
|
||||||
"Dlltool could not create import library: {}\n{}",
|
"Dlltool could not create import library: {}\n{}",
|
||||||
String::from_utf8_lossy(&output.stdout),
|
String::from_utf8_lossy(&output.stdout),
|
||||||
String::from_utf8_lossy(&output.stderr)
|
String::from_utf8_lossy(&output.stderr)
|
||||||
|
@ -263,13 +256,13 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
output_path_z.as_ptr(),
|
output_path_z.as_ptr(),
|
||||||
ffi_exports.as_ptr(),
|
ffi_exports.as_ptr(),
|
||||||
ffi_exports.len(),
|
ffi_exports.len(),
|
||||||
llvm_machine_type(&self.config.sess.target.arch) as u16,
|
llvm_machine_type(&self.sess.target.arch) as u16,
|
||||||
!self.config.sess.target.is_like_msvc,
|
!self.sess.target.is_like_msvc,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
if result == crate::llvm::LLVMRustResult::Failure {
|
if result == crate::llvm::LLVMRustResult::Failure {
|
||||||
self.config.sess.fatal(&format!(
|
self.sess.fatal(&format!(
|
||||||
"Error creating import library for {}: {}",
|
"Error creating import library for {}: {}",
|
||||||
lib_name,
|
lib_name,
|
||||||
llvm::last_error().unwrap_or("unknown LLVM error".to_string())
|
llvm::last_error().unwrap_or("unknown LLVM error".to_string())
|
||||||
|
@ -278,7 +271,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
self.add_archive(&output_path, |_| false).unwrap_or_else(|e| {
|
self.add_archive(&output_path, |_| false).unwrap_or_else(|e| {
|
||||||
self.config.sess.fatal(&format!(
|
self.sess.fatal(&format!(
|
||||||
"failed to add native library {}: {}",
|
"failed to add native library {}: {}",
|
||||||
output_path.display(),
|
output_path.display(),
|
||||||
e
|
e
|
||||||
|
@ -292,13 +285,13 @@ impl<'a> LlvmArchiveBuilder<'a> {
|
||||||
if let Some(ref a) = self.src_archive {
|
if let Some(ref a) = self.src_archive {
|
||||||
return a.as_ref();
|
return a.as_ref();
|
||||||
}
|
}
|
||||||
let src = self.config.src.as_ref()?;
|
let src = self.src.as_ref()?;
|
||||||
self.src_archive = Some(ArchiveRO::open(src).ok());
|
self.src_archive = Some(ArchiveRO::open(src).ok());
|
||||||
self.src_archive.as_ref().unwrap().as_ref()
|
self.src_archive.as_ref().unwrap().as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn llvm_archive_kind(&self) -> Result<ArchiveKind, &str> {
|
fn llvm_archive_kind(&self) -> Result<ArchiveKind, &str> {
|
||||||
let kind = &*self.config.sess.target.archive_format;
|
let kind = &*self.sess.target.archive_format;
|
||||||
kind.parse().map_err(|_| kind)
|
kind.parse().map_err(|_| kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -308,7 +301,7 @@ impl<'a> LlvmArchiveBuilder<'a> {
|
||||||
let mut strings = Vec::new();
|
let mut strings = Vec::new();
|
||||||
let mut members = Vec::new();
|
let mut members = Vec::new();
|
||||||
|
|
||||||
let dst = CString::new(self.config.dst.to_str().unwrap())?;
|
let dst = CString::new(self.dst.to_str().unwrap())?;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
if let Some(archive) = self.src_archive() {
|
if let Some(archive) = self.src_archive() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue