Skip to content

Commit 67d3467

Browse files
authored
Unrolled build for #157263
Rollup merge of #157263 - petrochenkov:arentry, r=bjorn3 rustc_codegen_ssa: Refactor `ArchiveEntry` to include entry kind Needed for #155338 in particular. r? @bjorn3
2 parents 042c759 + d39ecc0 commit 67d3467

2 files changed

Lines changed: 76 additions & 55 deletions

File tree

compiler/rustc_codegen_ssa/src/back/archive.rs

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_target::spec::Arch;
2121
use tracing::trace;
2222

2323
use super::metadata::{create_compressed_metadata_file, search_for_section};
24-
use super::rmeta_link::{self, RmetaLink};
24+
use super::rmeta_link;
2525
use crate::common;
2626
// Public for ArchiveBuilderBuilder::extract_bundled_libs
2727
pub use crate::errors::ExtractBundledLibsError;
@@ -308,14 +308,15 @@ fn find_binutils_dlltool(sess: &Session) -> OsString {
308308
tool_name
309309
}
310310

311+
pub enum AddArchiveKind<'a> {
312+
Rlib(/*skip*/ &'a dyn Fn(&str, ArchiveEntryKind) -> bool),
313+
Other,
314+
}
315+
311316
pub trait ArchiveBuilder {
312-
fn add_file(&mut self, path: &Path);
317+
fn add_file(&mut self, path: &Path, kind: ArchiveEntryKind);
313318

314-
fn add_archive(
315-
&mut self,
316-
archive: &Path,
317-
skip: Option<Box<dyn FnMut(&str, Option<&RmetaLink>) -> bool + 'static>>,
318-
) -> io::Result<()>;
319+
fn add_archive(&mut self, archive: &Path, kind: AddArchiveKind<'_>) -> io::Result<()>;
319320

320321
fn build(self: Box<Self>, output: &Path) -> bool;
321322
}
@@ -383,12 +384,27 @@ pub struct ArArchiveBuilder<'a> {
383384
entries: Vec<(Vec<u8>, ArchiveEntry)>,
384385
}
385386

387+
#[derive(Clone, Copy, PartialEq, Debug)]
388+
pub enum ArchiveEntryKind {
389+
/// Object file produced from Rust code.
390+
RustObj,
391+
/// Anything else, introduce new variants as needed.
392+
Other,
393+
}
394+
386395
#[derive(Debug)]
387-
enum ArchiveEntry {
388-
FromArchive { archive_index: usize, file_range: (u64, u64) },
396+
enum ArchiveEntrySource {
397+
Archive { archive_index: usize, file_range: (u64, u64) },
389398
File(PathBuf),
390399
}
391400

401+
#[derive(Debug)]
402+
struct ArchiveEntry {
403+
source: ArchiveEntrySource,
404+
#[expect(dead_code)] // used in #155338
405+
kind: ArchiveEntryKind,
406+
}
407+
392408
impl<'a> ArArchiveBuilder<'a> {
393409
pub fn new(sess: &'a Session, object_reader: &'static ObjectReader) -> ArArchiveBuilder<'a> {
394410
ArArchiveBuilder { sess, object_reader, src_archives: vec![], entries: vec![] }
@@ -443,11 +459,7 @@ pub fn try_extract_macho_fat_archive(
443459
}
444460

445461
impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
446-
fn add_archive(
447-
&mut self,
448-
archive_path: &Path,
449-
mut skip: Option<Box<dyn FnMut(&str, Option<&RmetaLink>) -> bool + 'static>>,
450-
) -> io::Result<()> {
462+
fn add_archive(&mut self, archive_path: &Path, ar_kind: AddArchiveKind<'_>) -> io::Result<()> {
451463
let mut archive_path = archive_path.to_path_buf();
452464
if self.sess.target.llvm_target.contains("-apple-macosx")
453465
&& let Some(new_archive_path) = try_extract_macho_fat_archive(self.sess, &archive_path)?
@@ -462,8 +474,10 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
462474
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
463475
let archive = ArchiveFile::parse(&*archive_map)
464476
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
465-
let metadata_link =
466-
skip.as_ref().and_then(|_| rmeta_link::read(&archive, &archive_map, &archive_path));
477+
let metadata_link = match ar_kind {
478+
AddArchiveKind::Rlib(..) => rmeta_link::read(&archive, &archive_map, &archive_path),
479+
AddArchiveKind::Other => None,
480+
};
467481
let archive_index = self.src_archives.len();
468482

469483
if let Some(expected_kind) =
@@ -483,17 +497,26 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
483497
let entry = entry.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
484498
let file_name = String::from_utf8(entry.name().to_vec())
485499
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
486-
let drop = skip.as_mut().is_some_and(|f| f(&file_name, metadata_link.as_ref()));
500+
let kind = if metadata_link
501+
.as_ref()
502+
.is_some_and(|m| m.rust_object_files.iter().any(|f| f == &file_name))
503+
{
504+
ArchiveEntryKind::RustObj
505+
} else {
506+
ArchiveEntryKind::Other
507+
};
508+
let drop = match ar_kind {
509+
AddArchiveKind::Rlib(skip) => skip(&file_name, kind),
510+
AddArchiveKind::Other => false,
511+
};
487512
if !drop {
488-
if entry.is_thin() {
513+
let source = if entry.is_thin() {
489514
let member_path = archive_path.parent().unwrap().join(Path::new(&file_name));
490-
self.entries.push((file_name.into_bytes(), ArchiveEntry::File(member_path)));
515+
ArchiveEntrySource::File(member_path)
491516
} else {
492-
self.entries.push((
493-
file_name.into_bytes(),
494-
ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
495-
));
496-
}
517+
ArchiveEntrySource::Archive { archive_index, file_range: entry.file_range() }
518+
};
519+
self.entries.push((file_name.into_bytes(), ArchiveEntry { source, kind }));
497520
}
498521
}
499522

@@ -502,10 +525,10 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
502525
}
503526

504527
/// Adds an arbitrary file to this archive
505-
fn add_file(&mut self, file: &Path) {
528+
fn add_file(&mut self, file: &Path, kind: ArchiveEntryKind) {
506529
self.entries.push((
507530
file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),
508-
ArchiveEntry::File(file.to_owned()),
531+
ArchiveEntry { source: ArchiveEntrySource::File(file.to_owned()), kind },
509532
));
510533
}
511534

@@ -539,8 +562,8 @@ impl<'a> ArArchiveBuilder<'a> {
539562

540563
for (entry_name, entry) in self.entries {
541564
let data =
542-
match entry {
543-
ArchiveEntry::FromArchive { archive_index, file_range } => {
565+
match entry.source {
566+
ArchiveEntrySource::Archive { archive_index, file_range } => {
544567
let src_archive = &self.src_archives[archive_index];
545568
let archive_data = &src_archive.1;
546569
let start = file_range.0 as usize;
@@ -563,7 +586,7 @@ impl<'a> ArArchiveBuilder<'a> {
563586

564587
Box::new(data) as Box<dyn AsRef<[u8]>>
565588
}
566-
ArchiveEntry::File(file) => unsafe {
589+
ArchiveEntrySource::File(file) => unsafe {
567590
Box::new(
568591
Mmap::map(File::open(file).map_err(|err| {
569592
io_error_context("failed to open object file", err)

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc_target::spec::{
5353
};
5454
use tracing::{debug, info, warn};
5555

56-
use super::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
56+
use super::archive::{AddArchiveKind, ArchiveBuilder, ArchiveBuilderBuilder, ArchiveEntryKind};
5757
use super::command::Command;
5858
use super::linker::{self, Linker};
5959
use super::metadata::{MetadataPosition, create_wrapper_file};
@@ -342,11 +342,11 @@ fn link_rlib<'a>(
342342
// normal linkers for the platform. Sometimes this is not possible however.
343343
// If it is possible however, placing the metadata object first improves
344344
// performance of getting metadata from rlibs.
345-
ab.add_file(&metadata);
345+
ab.add_file(&metadata, ArchiveEntryKind::Other);
346346
// Place the rmeta-link member immediately after metadata so consumers
347347
// can find it without iterating the whole archive.
348348
if let Some(file) = &metadata_link_file {
349-
ab.add_file(file);
349+
ab.add_file(file, ArchiveEntryKind::Other);
350350
}
351351
None
352352
}
@@ -359,15 +359,15 @@ fn link_rlib<'a>(
359359

360360
for m in &compiled_modules.modules {
361361
if let Some(obj) = m.object.as_ref() {
362-
ab.add_file(obj);
362+
ab.add_file(obj, ArchiveEntryKind::RustObj);
363363
}
364364

365365
if let Some(obj) = m.global_asm_object.as_ref() {
366-
ab.add_file(obj);
366+
ab.add_file(obj, ArchiveEntryKind::RustObj);
367367
}
368368

369369
if let Some(dwarf_obj) = m.dwarf_object.as_ref() {
370-
ab.add_file(dwarf_obj);
370+
ab.add_file(dwarf_obj, ArchiveEntryKind::Other);
371371
}
372372
}
373373

@@ -376,10 +376,10 @@ fn link_rlib<'a>(
376376
RlibFlavor::StaticlibBase => {
377377
if let Some(m) = &compiled_modules.allocator_module {
378378
if let Some(obj) = &m.object {
379-
ab.add_file(obj);
379+
ab.add_file(obj, ArchiveEntryKind::RustObj);
380380
}
381381
if let Some(obj) = &m.global_asm_object {
382-
ab.add_file(obj);
382+
ab.add_file(obj, ArchiveEntryKind::RustObj);
383383
}
384384
}
385385
}
@@ -419,7 +419,7 @@ fn link_rlib<'a>(
419419
packed_bundled_libs.push(wrapper_file);
420420
} else {
421421
let path = find_native_static_library(lib.name.as_str(), lib.verbatim, sess);
422-
ab.add_archive(&path, None).unwrap_or_else(|error| {
422+
ab.add_archive(&path, AddArchiveKind::Other).unwrap_or_else(|error| {
423423
sess.dcx().emit_fatal(errors::AddNativeLibrary { library_path: path, error })
424424
});
425425
}
@@ -436,7 +436,7 @@ fn link_rlib<'a>(
436436
tmpdir.as_ref(),
437437
true,
438438
) {
439-
ab.add_archive(&output_path, None).unwrap_or_else(|error| {
439+
ab.add_archive(&output_path, AddArchiveKind::Other).unwrap_or_else(|error| {
440440
sess.dcx()
441441
.emit_fatal(errors::AddNativeLibrary { library_path: output_path, error });
442442
});
@@ -469,18 +469,18 @@ fn link_rlib<'a>(
469469
//
470470
// Basically, all this means is that this code should not move above the
471471
// code above.
472-
ab.add_file(&trailing_metadata);
472+
ab.add_file(&trailing_metadata, ArchiveEntryKind::Other);
473473
// Place the rmeta-link member immediately after metadata so consumers can
474474
// find it without iterating the whole archive.
475475
if let Some(file) = &metadata_link_file {
476-
ab.add_file(file);
476+
ab.add_file(file, ArchiveEntryKind::Other);
477477
}
478478
}
479479

480480
// Add all bundled static native library dependencies.
481481
// Archives added to the end of .rlib archive, see comment above for the reason.
482482
for lib in packed_bundled_libs {
483-
ab.add_file(&lib)
483+
ab.add_file(&lib, ArchiveEntryKind::Other)
484484
}
485485

486486
ab
@@ -529,16 +529,14 @@ fn link_staticlib(
529529
let bundled_libs: FxIndexSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
530530
ab.add_archive(
531531
path,
532-
Some(Box::new(move |fname: &str, metadata_link| {
532+
AddArchiveKind::Rlib(&|fname: &str, entry_kind| {
533533
// Ignore metadata and rmeta-link files.
534534
if fname == METADATA_FILENAME || fname == rmeta_link::FILENAME {
535535
return true;
536536
}
537537

538538
// Don't include Rust objects if LTO is enabled.
539-
if lto
540-
&& metadata_link.is_some_and(|m| m.rust_object_files.iter().any(|f| f == fname))
541-
{
539+
if lto && entry_kind == ArchiveEntryKind::RustObj {
542540
return true;
543541
}
544542

@@ -548,7 +546,7 @@ fn link_staticlib(
548546
}
549547

550548
false
551-
})),
549+
}),
552550
)
553551
.unwrap();
554552

@@ -559,7 +557,7 @@ fn link_staticlib(
559557
for filename in relevant_libs.iter() {
560558
let joined = tempdir.as_ref().join(filename.as_str());
561559
let path = joined.as_path();
562-
ab.add_archive(path, None).unwrap();
560+
ab.add_archive(path, AddArchiveKind::Other).unwrap();
563561
}
564562

565563
all_native_libs.extend(crate_info.native_libraries[&cnum].iter().cloned());
@@ -1266,7 +1264,7 @@ fn link_natively(
12661264

12671265
if should_archive {
12681266
let mut ab = archive_builder_builder.new_archive_builder(sess);
1269-
ab.add_file(temp_filename);
1267+
ab.add_file(temp_filename, ArchiveEntryKind::Other);
12701268
ab.build(out_filename);
12711269
}
12721270
}
@@ -3242,19 +3240,19 @@ fn add_static_crate(
32423240
let mut archive = archive_builder_builder.new_archive_builder(sess);
32433241
if let Err(error) = archive.add_archive(
32443242
cratepath,
3245-
Some(Box::new(move |f, metadata_link| {
3243+
AddArchiveKind::Rlib(&|f, entry_kind| {
32463244
if f == METADATA_FILENAME || f == rmeta_link::FILENAME {
32473245
return true;
32483246
}
32493247

3250-
let is_rust_object =
3251-
metadata_link.is_some_and(|m| m.rust_object_files.iter().any(|rf| rf == f));
3252-
32533248
// If we're performing LTO and this is a rust-generated object
32543249
// file, then we don't need the object file as it's part of the
32553250
// LTO module. Note that `#![no_builtins]` is excluded from LTO,
32563251
// though, so we let that object file slide.
3257-
if upstream_rust_objects_already_included && is_rust_object && is_builtins {
3252+
if upstream_rust_objects_already_included
3253+
&& entry_kind == ArchiveEntryKind::RustObj
3254+
&& is_builtins
3255+
{
32583256
return true;
32593257
}
32603258

@@ -3268,7 +3266,7 @@ fn add_static_crate(
32683266
}
32693267

32703268
false
3271-
})),
3269+
}),
32723270
) {
32733271
sess.dcx()
32743272
.emit_fatal(errors::RlibArchiveBuildFailure { path: cratepath.clone(), error });

0 commit comments

Comments
 (0)