Skip to content

Commit 7c93154

Browse files
committed
Move output argument from ArchiveBuilder::new to .build()
1 parent 48316df commit 7c93154

File tree

5 files changed

+29
-44
lines changed

5 files changed

+29
-44
lines changed

compiler/rustc_codegen_cranelift/src/archive.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ enum ArchiveEntry {
1919

2020
pub(crate) struct ArArchiveBuilder<'a> {
2121
sess: &'a Session,
22-
dst: PathBuf,
2322
use_gnu_style_archive: bool,
2423
no_builtin_ranlib: bool,
2524

@@ -30,10 +29,9 @@ pub(crate) struct ArArchiveBuilder<'a> {
3029
}
3130

3231
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
33-
fn new(sess: &'a Session, output: &Path) -> Self {
32+
fn new(sess: &'a Session) -> Self {
3433
ArArchiveBuilder {
3534
sess,
36-
dst: output.to_path_buf(),
3735
use_gnu_style_archive: sess.target.archive_format == "gnu",
3836
// FIXME fix builtin ranlib on macOS
3937
no_builtin_ranlib: sess.target.is_like_osx,
@@ -74,7 +72,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
7472
Ok(())
7573
}
7674

77-
fn build(mut self) -> bool {
75+
fn build(mut self, output: &Path) -> bool {
7876
enum BuilderKind {
7977
Bsd(ar::Builder<File>),
8078
Gnu(ar::GnuBuilder<File>),
@@ -163,7 +161,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
163161
let mut builder = if self.use_gnu_style_archive {
164162
BuilderKind::Gnu(
165163
ar::GnuBuilder::new(
166-
File::create(&self.dst).unwrap_or_else(|err| {
164+
File::create(output).unwrap_or_else(|err| {
167165
sess.fatal(&format!(
168166
"error opening destination during archive building: {}",
169167
err
@@ -178,7 +176,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
178176
} else {
179177
BuilderKind::Bsd(
180178
ar::Builder::new(
181-
File::create(&self.dst).unwrap_or_else(|err| {
179+
File::create(output).unwrap_or_else(|err| {
182180
sess.fatal(&format!(
183181
"error opening destination during archive building: {}",
184182
err
@@ -209,7 +207,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
209207

210208
// Run ranlib to be able to link the archive
211209
let status = std::process::Command::new(ranlib)
212-
.arg(self.dst)
210+
.arg(output)
213211
.status()
214212
.expect("Couldn't run ranlib");
215213

compiler/rustc_codegen_gcc/src/archive.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_session::cstore::DllImport;
88

99
struct ArchiveConfig<'a> {
1010
sess: &'a Session,
11-
dst: PathBuf,
1211
use_native_ar: bool,
1312
use_gnu_style_archive: bool,
1413
}
@@ -31,10 +30,9 @@ pub struct ArArchiveBuilder<'a> {
3130
}
3231

3332
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
34-
fn new(sess: &'a Session, output: &Path) -> Self {
33+
fn new(sess: &'a Session) -> Self {
3534
let config = ArchiveConfig {
3635
sess,
37-
dst: output.to_path_buf(),
3836
use_native_ar: false,
3937
// FIXME test for linux and System V derivatives instead
4038
use_gnu_style_archive: sess.target.options.archive_format == "gnu",
@@ -77,7 +75,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
7775
Ok(())
7876
}
7977

80-
fn build(mut self) -> bool {
78+
fn build(mut self, output: &Path) -> bool {
8179
use std::process::Command;
8280

8381
fn add_file_using_ar(archive: &Path, file: &Path) {
@@ -97,17 +95,17 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
9795
}
9896

9997
let mut builder = if self.config.use_native_ar {
100-
BuilderKind::NativeAr(&self.config.dst)
98+
BuilderKind::NativeAr(output)
10199
} else if self.config.use_gnu_style_archive {
102100
BuilderKind::Gnu(ar::GnuBuilder::new(
103-
File::create(&self.config.dst).unwrap(),
101+
File::create(output).unwrap(),
104102
self.entries
105103
.iter()
106104
.map(|(name, _)| name.as_bytes().to_vec())
107105
.collect(),
108106
))
109107
} else {
110-
BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
108+
BuilderKind::Bsd(ar::Builder::new(File::create(output).unwrap()))
111109
};
112110

113111
let any_members = !self.entries.is_empty();
@@ -164,10 +162,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
164162
std::mem::drop(builder);
165163

166164
// Run ranlib to be able to link the archive
167-
let status = std::process::Command::new("ranlib")
168-
.arg(self.config.dst)
169-
.status()
170-
.expect("Couldn't run ranlib");
165+
let status =
166+
std::process::Command::new("ranlib").arg(output).status().expect("Couldn't run ranlib");
171167

172168
if !status.success() {
173169
self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));

compiler/rustc_codegen_llvm/src/back/archive.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_session::Session;
1818
#[must_use = "must call build() to finish building the archive"]
1919
pub struct LlvmArchiveBuilder<'a> {
2020
sess: &'a Session,
21-
dst: PathBuf,
2221
additions: Vec<Addition>,
2322
}
2423

@@ -56,8 +55,8 @@ fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
5655
impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
5756
/// Creates a new static archive, ready for modifying the archive specified
5857
/// by `config`.
59-
fn new(sess: &'a Session, output: &Path) -> LlvmArchiveBuilder<'a> {
60-
LlvmArchiveBuilder { sess, dst: output.to_path_buf(), additions: Vec::new() }
58+
fn new(sess: &'a Session) -> LlvmArchiveBuilder<'a> {
59+
LlvmArchiveBuilder { sess, additions: Vec::new() }
6160
}
6261

6362
fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
@@ -88,8 +87,8 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
8887

8988
/// Combine the provided files, rlibs, and native libraries into a single
9089
/// `Archive`.
91-
fn build(mut self) -> bool {
92-
match self.build_with_llvm() {
90+
fn build(mut self, output: &Path) -> bool {
91+
match self.build_with_llvm(output) {
9392
Ok(any_members) => any_members,
9493
Err(e) => self.sess.fatal(&format!("failed to build archive: {}", e)),
9594
}
@@ -241,7 +240,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
241240
}
242241

243242
impl<'a> LlvmArchiveBuilder<'a> {
244-
fn build_with_llvm(&mut self) -> io::Result<bool> {
243+
fn build_with_llvm(&mut self, output: &Path) -> io::Result<bool> {
245244
let kind = &*self.sess.target.archive_format;
246245
let kind = kind.parse::<ArchiveKind>().map_err(|_| kind).unwrap_or_else(|kind| {
247246
self.sess.fatal(&format!("Don't know how to build archive of type: {}", kind))
@@ -251,7 +250,7 @@ impl<'a> LlvmArchiveBuilder<'a> {
251250
let mut strings = Vec::new();
252251
let mut members = Vec::new();
253252

254-
let dst = CString::new(self.dst.to_str().unwrap())?;
253+
let dst = CString::new(output.to_str().unwrap())?;
255254

256255
unsafe {
257256
for addition in &mut additions {

compiler/rustc_codegen_ssa/src/back/archive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ pub(super) fn find_library(
4141
}
4242

4343
pub trait ArchiveBuilder<'a> {
44-
fn new(sess: &'a Session, output: &Path) -> Self;
44+
fn new(sess: &'a Session) -> Self;
4545

4646
fn add_file(&mut self, path: &Path);
4747

4848
fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
4949
where
5050
F: FnMut(&str) -> bool + 'static;
5151

52-
fn build(self) -> bool;
52+
fn build(self, output: &Path) -> bool;
5353

5454
fn sess(&self) -> &Session;
5555

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,9 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
101101
match crate_type {
102102
CrateType::Rlib => {
103103
let _timer = sess.timer("link_rlib");
104-
link_rlib::<B>(
105-
sess,
106-
codegen_results,
107-
RlibFlavor::Normal,
108-
&out_filename,
109-
&path,
110-
)?
111-
.build();
104+
info!("preparing rlib to {:?}", out_filename);
105+
link_rlib::<B>(sess, codegen_results, RlibFlavor::Normal, &path)?
106+
.build(&out_filename);
112107
}
113108
CrateType::Staticlib => {
114109
link_staticlib::<B>(sess, codegen_results, &out_filename, &path)?;
@@ -249,14 +244,11 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
249244
sess: &'a Session,
250245
codegen_results: &CodegenResults,
251246
flavor: RlibFlavor,
252-
out_filename: &Path,
253247
tmpdir: &MaybeTempDir,
254248
) -> Result<B, ErrorGuaranteed> {
255-
info!("preparing rlib to {:?}", out_filename);
256-
257249
let lib_search_paths = archive_search_paths(sess);
258250

259-
let mut ab = <B as ArchiveBuilder>::new(sess, out_filename);
251+
let mut ab = <B as ArchiveBuilder>::new(sess);
260252

261253
let trailing_metadata = match flavor {
262254
RlibFlavor::Normal => {
@@ -451,8 +443,8 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
451443
out_filename: &Path,
452444
tempdir: &MaybeTempDir,
453445
) -> Result<(), ErrorGuaranteed> {
454-
let mut ab =
455-
link_rlib::<B>(sess, codegen_results, RlibFlavor::StaticlibBase, out_filename, tempdir)?;
446+
info!("preparing staticlib to {:?}", out_filename);
447+
let mut ab = link_rlib::<B>(sess, codegen_results, RlibFlavor::StaticlibBase, tempdir)?;
456448
let mut all_native_libs = vec![];
457449

458450
let res = each_linked_rlib(&codegen_results.crate_info, &mut |cnum, path| {
@@ -514,7 +506,7 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
514506
sess.fatal(&e);
515507
}
516508

517-
ab.build();
509+
ab.build(out_filename);
518510

519511
if !all_native_libs.is_empty() {
520512
if sess.opts.prints.contains(&PrintRequest::NativeStaticLibs) {
@@ -2479,7 +2471,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
24792471
let is_builtins = sess.target.no_builtins
24802472
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum);
24812473

2482-
let mut archive = <B as ArchiveBuilder>::new(sess, &dst);
2474+
let mut archive = <B as ArchiveBuilder>::new(sess);
24832475
if let Err(e) = archive.add_archive(cratepath, move |f| {
24842476
if f == METADATA_FILENAME {
24852477
return true;
@@ -2510,7 +2502,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
25102502
}) {
25112503
sess.fatal(&format!("failed to build archive from rlib: {}", e));
25122504
}
2513-
if archive.build() {
2505+
if archive.build(&dst) {
25142506
link_upstream(&dst);
25152507
}
25162508
});

0 commit comments

Comments
 (0)