Move output argument from ArchiveBuilder::new to .build()
This commit is contained in:
parent
48316dfea1
commit
7c93154a30
5 changed files with 29 additions and 44 deletions
|
@ -19,7 +19,6 @@ enum ArchiveEntry {
|
||||||
|
|
||||||
pub(crate) struct ArArchiveBuilder<'a> {
|
pub(crate) struct ArArchiveBuilder<'a> {
|
||||||
sess: &'a Session,
|
sess: &'a Session,
|
||||||
dst: PathBuf,
|
|
||||||
use_gnu_style_archive: bool,
|
use_gnu_style_archive: bool,
|
||||||
no_builtin_ranlib: bool,
|
no_builtin_ranlib: bool,
|
||||||
|
|
||||||
|
@ -30,10 +29,9 @@ pub(crate) struct ArArchiveBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
fn new(sess: &'a Session, output: &Path) -> Self {
|
fn new(sess: &'a Session) -> Self {
|
||||||
ArArchiveBuilder {
|
ArArchiveBuilder {
|
||||||
sess,
|
sess,
|
||||||
dst: output.to_path_buf(),
|
|
||||||
use_gnu_style_archive: sess.target.archive_format == "gnu",
|
use_gnu_style_archive: sess.target.archive_format == "gnu",
|
||||||
// FIXME fix builtin ranlib on macOS
|
// FIXME fix builtin ranlib on macOS
|
||||||
no_builtin_ranlib: sess.target.is_like_osx,
|
no_builtin_ranlib: sess.target.is_like_osx,
|
||||||
|
@ -74,7 +72,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(mut self) -> bool {
|
fn build(mut self, output: &Path) -> bool {
|
||||||
enum BuilderKind {
|
enum BuilderKind {
|
||||||
Bsd(ar::Builder<File>),
|
Bsd(ar::Builder<File>),
|
||||||
Gnu(ar::GnuBuilder<File>),
|
Gnu(ar::GnuBuilder<File>),
|
||||||
|
@ -163,7 +161,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
let mut builder = if self.use_gnu_style_archive {
|
let mut builder = if self.use_gnu_style_archive {
|
||||||
BuilderKind::Gnu(
|
BuilderKind::Gnu(
|
||||||
ar::GnuBuilder::new(
|
ar::GnuBuilder::new(
|
||||||
File::create(&self.dst).unwrap_or_else(|err| {
|
File::create(output).unwrap_or_else(|err| {
|
||||||
sess.fatal(&format!(
|
sess.fatal(&format!(
|
||||||
"error opening destination during archive building: {}",
|
"error opening destination during archive building: {}",
|
||||||
err
|
err
|
||||||
|
@ -178,7 +176,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
} else {
|
} else {
|
||||||
BuilderKind::Bsd(
|
BuilderKind::Bsd(
|
||||||
ar::Builder::new(
|
ar::Builder::new(
|
||||||
File::create(&self.dst).unwrap_or_else(|err| {
|
File::create(output).unwrap_or_else(|err| {
|
||||||
sess.fatal(&format!(
|
sess.fatal(&format!(
|
||||||
"error opening destination during archive building: {}",
|
"error opening destination during archive building: {}",
|
||||||
err
|
err
|
||||||
|
@ -209,7 +207,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
|
|
||||||
// Run ranlib to be able to link the archive
|
// Run ranlib to be able to link the archive
|
||||||
let status = std::process::Command::new(ranlib)
|
let status = std::process::Command::new(ranlib)
|
||||||
.arg(self.dst)
|
.arg(output)
|
||||||
.status()
|
.status()
|
||||||
.expect("Couldn't run ranlib");
|
.expect("Couldn't run ranlib");
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ use rustc_session::cstore::DllImport;
|
||||||
|
|
||||||
struct ArchiveConfig<'a> {
|
struct ArchiveConfig<'a> {
|
||||||
sess: &'a Session,
|
sess: &'a Session,
|
||||||
dst: PathBuf,
|
|
||||||
use_native_ar: bool,
|
use_native_ar: bool,
|
||||||
use_gnu_style_archive: bool,
|
use_gnu_style_archive: bool,
|
||||||
}
|
}
|
||||||
|
@ -31,10 +30,9 @@ pub struct ArArchiveBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
fn new(sess: &'a Session, output: &Path) -> Self {
|
fn new(sess: &'a Session) -> Self {
|
||||||
let config = ArchiveConfig {
|
let config = ArchiveConfig {
|
||||||
sess,
|
sess,
|
||||||
dst: output.to_path_buf(),
|
|
||||||
use_native_ar: false,
|
use_native_ar: false,
|
||||||
// FIXME test for linux and System V derivatives instead
|
// FIXME test for linux and System V derivatives instead
|
||||||
use_gnu_style_archive: sess.target.options.archive_format == "gnu",
|
use_gnu_style_archive: sess.target.options.archive_format == "gnu",
|
||||||
|
@ -77,7 +75,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(mut self) -> bool {
|
fn build(mut self, output: &Path) -> bool {
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
fn add_file_using_ar(archive: &Path, file: &Path) {
|
fn add_file_using_ar(archive: &Path, file: &Path) {
|
||||||
|
@ -97,17 +95,17 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut builder = if self.config.use_native_ar {
|
let mut builder = if self.config.use_native_ar {
|
||||||
BuilderKind::NativeAr(&self.config.dst)
|
BuilderKind::NativeAr(output)
|
||||||
} else if self.config.use_gnu_style_archive {
|
} else if self.config.use_gnu_style_archive {
|
||||||
BuilderKind::Gnu(ar::GnuBuilder::new(
|
BuilderKind::Gnu(ar::GnuBuilder::new(
|
||||||
File::create(&self.config.dst).unwrap(),
|
File::create(output).unwrap(),
|
||||||
self.entries
|
self.entries
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(name, _)| name.as_bytes().to_vec())
|
.map(|(name, _)| name.as_bytes().to_vec())
|
||||||
.collect(),
|
.collect(),
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
|
BuilderKind::Bsd(ar::Builder::new(File::create(output).unwrap()))
|
||||||
};
|
};
|
||||||
|
|
||||||
let any_members = !self.entries.is_empty();
|
let any_members = !self.entries.is_empty();
|
||||||
|
@ -164,10 +162,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
std::mem::drop(builder);
|
std::mem::drop(builder);
|
||||||
|
|
||||||
// Run ranlib to be able to link the archive
|
// Run ranlib to be able to link the archive
|
||||||
let status = std::process::Command::new("ranlib")
|
let status =
|
||||||
.arg(self.config.dst)
|
std::process::Command::new("ranlib").arg(output).status().expect("Couldn't run ranlib");
|
||||||
.status()
|
|
||||||
.expect("Couldn't run ranlib");
|
|
||||||
|
|
||||||
if !status.success() {
|
if !status.success() {
|
||||||
self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
|
self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
|
||||||
|
|
|
@ -18,7 +18,6 @@ use rustc_session::Session;
|
||||||
#[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> {
|
||||||
sess: &'a Session,
|
sess: &'a Session,
|
||||||
dst: PathBuf,
|
|
||||||
additions: Vec<Addition>,
|
additions: Vec<Addition>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,8 +55,8 @@ fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
|
||||||
impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
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) -> LlvmArchiveBuilder<'a> {
|
fn new(sess: &'a Session) -> LlvmArchiveBuilder<'a> {
|
||||||
LlvmArchiveBuilder { sess, dst: output.to_path_buf(), additions: Vec::new() }
|
LlvmArchiveBuilder { sess, additions: Vec::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
|
fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
|
||||||
|
@ -88,8 +87,8 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
|
|
||||||
/// Combine the provided files, rlibs, and native libraries into a single
|
/// Combine the provided files, rlibs, and native libraries into a single
|
||||||
/// `Archive`.
|
/// `Archive`.
|
||||||
fn build(mut self) -> bool {
|
fn build(mut self, output: &Path) -> bool {
|
||||||
match self.build_with_llvm() {
|
match self.build_with_llvm(output) {
|
||||||
Ok(any_members) => any_members,
|
Ok(any_members) => any_members,
|
||||||
Err(e) => self.sess.fatal(&format!("failed to build archive: {}", e)),
|
Err(e) => self.sess.fatal(&format!("failed to build archive: {}", e)),
|
||||||
}
|
}
|
||||||
|
@ -241,7 +240,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> LlvmArchiveBuilder<'a> {
|
impl<'a> LlvmArchiveBuilder<'a> {
|
||||||
fn build_with_llvm(&mut self) -> io::Result<bool> {
|
fn build_with_llvm(&mut self, output: &Path) -> io::Result<bool> {
|
||||||
let kind = &*self.sess.target.archive_format;
|
let kind = &*self.sess.target.archive_format;
|
||||||
let kind = kind.parse::<ArchiveKind>().map_err(|_| kind).unwrap_or_else(|kind| {
|
let kind = kind.parse::<ArchiveKind>().map_err(|_| kind).unwrap_or_else(|kind| {
|
||||||
self.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))
|
||||||
|
@ -251,7 +250,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.dst.to_str().unwrap())?;
|
let dst = CString::new(output.to_str().unwrap())?;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
for addition in &mut additions {
|
for addition in &mut additions {
|
||||||
|
|
|
@ -41,7 +41,7 @@ pub(super) fn find_library(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ArchiveBuilder<'a> {
|
pub trait ArchiveBuilder<'a> {
|
||||||
fn new(sess: &'a Session, output: &Path) -> Self;
|
fn new(sess: &'a Session) -> Self;
|
||||||
|
|
||||||
fn add_file(&mut self, path: &Path);
|
fn add_file(&mut self, path: &Path);
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ pub trait ArchiveBuilder<'a> {
|
||||||
where
|
where
|
||||||
F: FnMut(&str) -> bool + 'static;
|
F: FnMut(&str) -> bool + 'static;
|
||||||
|
|
||||||
fn build(self) -> bool;
|
fn build(self, output: &Path) -> bool;
|
||||||
|
|
||||||
fn sess(&self) -> &Session;
|
fn sess(&self) -> &Session;
|
||||||
|
|
||||||
|
|
|
@ -101,14 +101,9 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
|
||||||
match crate_type {
|
match crate_type {
|
||||||
CrateType::Rlib => {
|
CrateType::Rlib => {
|
||||||
let _timer = sess.timer("link_rlib");
|
let _timer = sess.timer("link_rlib");
|
||||||
link_rlib::<B>(
|
info!("preparing rlib to {:?}", out_filename);
|
||||||
sess,
|
link_rlib::<B>(sess, codegen_results, RlibFlavor::Normal, &path)?
|
||||||
codegen_results,
|
.build(&out_filename);
|
||||||
RlibFlavor::Normal,
|
|
||||||
&out_filename,
|
|
||||||
&path,
|
|
||||||
)?
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
CrateType::Staticlib => {
|
CrateType::Staticlib => {
|
||||||
link_staticlib::<B>(sess, codegen_results, &out_filename, &path)?;
|
link_staticlib::<B>(sess, codegen_results, &out_filename, &path)?;
|
||||||
|
@ -249,14 +244,11 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
|
||||||
sess: &'a Session,
|
sess: &'a Session,
|
||||||
codegen_results: &CodegenResults,
|
codegen_results: &CodegenResults,
|
||||||
flavor: RlibFlavor,
|
flavor: RlibFlavor,
|
||||||
out_filename: &Path,
|
|
||||||
tmpdir: &MaybeTempDir,
|
tmpdir: &MaybeTempDir,
|
||||||
) -> Result<B, ErrorGuaranteed> {
|
) -> Result<B, ErrorGuaranteed> {
|
||||||
info!("preparing rlib to {:?}", out_filename);
|
|
||||||
|
|
||||||
let lib_search_paths = archive_search_paths(sess);
|
let lib_search_paths = archive_search_paths(sess);
|
||||||
|
|
||||||
let mut ab = <B as ArchiveBuilder>::new(sess, out_filename);
|
let mut ab = <B as ArchiveBuilder>::new(sess);
|
||||||
|
|
||||||
let trailing_metadata = match flavor {
|
let trailing_metadata = match flavor {
|
||||||
RlibFlavor::Normal => {
|
RlibFlavor::Normal => {
|
||||||
|
@ -451,8 +443,8 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
|
||||||
out_filename: &Path,
|
out_filename: &Path,
|
||||||
tempdir: &MaybeTempDir,
|
tempdir: &MaybeTempDir,
|
||||||
) -> Result<(), ErrorGuaranteed> {
|
) -> Result<(), ErrorGuaranteed> {
|
||||||
let mut ab =
|
info!("preparing staticlib to {:?}", out_filename);
|
||||||
link_rlib::<B>(sess, codegen_results, RlibFlavor::StaticlibBase, out_filename, tempdir)?;
|
let mut ab = link_rlib::<B>(sess, codegen_results, RlibFlavor::StaticlibBase, tempdir)?;
|
||||||
let mut all_native_libs = vec![];
|
let mut all_native_libs = vec![];
|
||||||
|
|
||||||
let res = each_linked_rlib(&codegen_results.crate_info, &mut |cnum, path| {
|
let res = each_linked_rlib(&codegen_results.crate_info, &mut |cnum, path| {
|
||||||
|
@ -514,7 +506,7 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
|
||||||
sess.fatal(&e);
|
sess.fatal(&e);
|
||||||
}
|
}
|
||||||
|
|
||||||
ab.build();
|
ab.build(out_filename);
|
||||||
|
|
||||||
if !all_native_libs.is_empty() {
|
if !all_native_libs.is_empty() {
|
||||||
if sess.opts.prints.contains(&PrintRequest::NativeStaticLibs) {
|
if sess.opts.prints.contains(&PrintRequest::NativeStaticLibs) {
|
||||||
|
@ -2479,7 +2471,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
|
||||||
let is_builtins = sess.target.no_builtins
|
let is_builtins = sess.target.no_builtins
|
||||||
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum);
|
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum);
|
||||||
|
|
||||||
let mut archive = <B as ArchiveBuilder>::new(sess, &dst);
|
let mut archive = <B as ArchiveBuilder>::new(sess);
|
||||||
if let Err(e) = archive.add_archive(cratepath, move |f| {
|
if let Err(e) = archive.add_archive(cratepath, move |f| {
|
||||||
if f == METADATA_FILENAME {
|
if f == METADATA_FILENAME {
|
||||||
return true;
|
return true;
|
||||||
|
@ -2510,7 +2502,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
|
||||||
}) {
|
}) {
|
||||||
sess.fatal(&format!("failed to build archive from rlib: {}", e));
|
sess.fatal(&format!("failed to build archive from rlib: {}", e));
|
||||||
}
|
}
|
||||||
if archive.build() {
|
if archive.build(&dst) {
|
||||||
link_upstream(&dst);
|
link_upstream(&dst);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue