Move output argument from ArchiveBuilder::new to .build()

This commit is contained in:
bjorn3 2022-07-28 08:39:19 +00:00
parent 48316dfea1
commit 7c93154a30
5 changed files with 29 additions and 44 deletions

View File

@ -19,7 +19,6 @@ enum ArchiveEntry {
pub(crate) struct ArArchiveBuilder<'a> {
sess: &'a Session,
dst: PathBuf,
use_gnu_style_archive: bool,
no_builtin_ranlib: bool,
@ -30,10 +29,9 @@ pub(crate) struct ArArchiveBuilder<'a> {
}
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
fn new(sess: &'a Session, output: &Path) -> Self {
fn new(sess: &'a Session) -> Self {
ArArchiveBuilder {
sess,
dst: output.to_path_buf(),
use_gnu_style_archive: sess.target.archive_format == "gnu",
// FIXME fix builtin ranlib on macOS
no_builtin_ranlib: sess.target.is_like_osx,
@ -74,7 +72,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
Ok(())
}
fn build(mut self) -> bool {
fn build(mut self, output: &Path) -> bool {
enum BuilderKind {
Bsd(ar::Builder<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 {
BuilderKind::Gnu(
ar::GnuBuilder::new(
File::create(&self.dst).unwrap_or_else(|err| {
File::create(output).unwrap_or_else(|err| {
sess.fatal(&format!(
"error opening destination during archive building: {}",
err
@ -178,7 +176,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
} else {
BuilderKind::Bsd(
ar::Builder::new(
File::create(&self.dst).unwrap_or_else(|err| {
File::create(output).unwrap_or_else(|err| {
sess.fatal(&format!(
"error opening destination during archive building: {}",
err
@ -209,7 +207,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
// Run ranlib to be able to link the archive
let status = std::process::Command::new(ranlib)
.arg(self.dst)
.arg(output)
.status()
.expect("Couldn't run ranlib");

View File

@ -8,7 +8,6 @@ use rustc_session::cstore::DllImport;
struct ArchiveConfig<'a> {
sess: &'a Session,
dst: PathBuf,
use_native_ar: bool,
use_gnu_style_archive: bool,
}
@ -31,10 +30,9 @@ pub struct 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 {
sess,
dst: output.to_path_buf(),
use_native_ar: false,
// FIXME test for linux and System V derivatives instead
use_gnu_style_archive: sess.target.options.archive_format == "gnu",
@ -77,7 +75,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
Ok(())
}
fn build(mut self) -> bool {
fn build(mut self, output: &Path) -> bool {
use std::process::Command;
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 {
BuilderKind::NativeAr(&self.config.dst)
BuilderKind::NativeAr(output)
} else if self.config.use_gnu_style_archive {
BuilderKind::Gnu(ar::GnuBuilder::new(
File::create(&self.config.dst).unwrap(),
File::create(output).unwrap(),
self.entries
.iter()
.map(|(name, _)| name.as_bytes().to_vec())
.collect(),
))
} 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();
@ -164,10 +162,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
std::mem::drop(builder);
// Run ranlib to be able to link the archive
let status = std::process::Command::new("ranlib")
.arg(self.config.dst)
.status()
.expect("Couldn't run ranlib");
let status =
std::process::Command::new("ranlib").arg(output).status().expect("Couldn't run ranlib");
if !status.success() {
self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));

View File

@ -18,7 +18,6 @@ use rustc_session::Session;
#[must_use = "must call build() to finish building the archive"]
pub struct LlvmArchiveBuilder<'a> {
sess: &'a Session,
dst: PathBuf,
additions: Vec<Addition>,
}
@ -56,8 +55,8 @@ fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
/// Creates a new static archive, ready for modifying the archive specified
/// by `config`.
fn new(sess: &'a Session, output: &Path) -> LlvmArchiveBuilder<'a> {
LlvmArchiveBuilder { sess, dst: output.to_path_buf(), additions: Vec::new() }
fn new(sess: &'a Session) -> LlvmArchiveBuilder<'a> {
LlvmArchiveBuilder { sess, additions: Vec::new() }
}
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
/// `Archive`.
fn build(mut self) -> bool {
match self.build_with_llvm() {
fn build(mut self, output: &Path) -> bool {
match self.build_with_llvm(output) {
Ok(any_members) => any_members,
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> {
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 = kind.parse::<ArchiveKind>().map_err(|_| kind).unwrap_or_else(|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 members = Vec::new();
let dst = CString::new(self.dst.to_str().unwrap())?;
let dst = CString::new(output.to_str().unwrap())?;
unsafe {
for addition in &mut additions {

View File

@ -41,7 +41,7 @@ pub(super) fn find_library(
}
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);
@ -49,7 +49,7 @@ pub trait ArchiveBuilder<'a> {
where
F: FnMut(&str) -> bool + 'static;
fn build(self) -> bool;
fn build(self, output: &Path) -> bool;
fn sess(&self) -> &Session;

View File

@ -101,14 +101,9 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
match crate_type {
CrateType::Rlib => {
let _timer = sess.timer("link_rlib");
link_rlib::<B>(
sess,
codegen_results,
RlibFlavor::Normal,
&out_filename,
&path,
)?
.build();
info!("preparing rlib to {:?}", out_filename);
link_rlib::<B>(sess, codegen_results, RlibFlavor::Normal, &path)?
.build(&out_filename);
}
CrateType::Staticlib => {
link_staticlib::<B>(sess, codegen_results, &out_filename, &path)?;
@ -249,14 +244,11 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
sess: &'a Session,
codegen_results: &CodegenResults,
flavor: RlibFlavor,
out_filename: &Path,
tmpdir: &MaybeTempDir,
) -> Result<B, ErrorGuaranteed> {
info!("preparing rlib to {:?}", out_filename);
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 {
RlibFlavor::Normal => {
@ -451,8 +443,8 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
out_filename: &Path,
tempdir: &MaybeTempDir,
) -> Result<(), ErrorGuaranteed> {
let mut ab =
link_rlib::<B>(sess, codegen_results, RlibFlavor::StaticlibBase, out_filename, tempdir)?;
info!("preparing staticlib to {:?}", out_filename);
let mut ab = link_rlib::<B>(sess, codegen_results, RlibFlavor::StaticlibBase, tempdir)?;
let mut all_native_libs = vec![];
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);
}
ab.build();
ab.build(out_filename);
if !all_native_libs.is_empty() {
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
|| !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 f == METADATA_FILENAME {
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));
}
if archive.build() {
if archive.build(&dst) {
link_upstream(&dst);
}
});