Skip to content

Commit 0e1bd7c

Browse files
committed
rustc_codegen_ssa: Refactor ArchiveEntry to include entry kind
1 parent 4f84d9f commit 0e1bd7c

2 files changed

Lines changed: 60 additions & 42 deletions

File tree

compiler/rustc_codegen_ssa/src/back/archive.rs

Lines changed: 41 additions & 21 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;
@@ -309,12 +309,12 @@ fn find_binutils_dlltool(sess: &Session) -> OsString {
309309
}
310310

311311
pub trait ArchiveBuilder {
312-
fn add_file(&mut self, path: &Path);
312+
fn add_file(&mut self, path: &Path, kind: ArchiveEntryKind);
313313

314314
fn add_archive(
315315
&mut self,
316316
archive: &Path,
317-
skip: Option<Box<dyn FnMut(&str, Option<&RmetaLink>) -> bool + 'static>>,
317+
skip: Option<Box<dyn FnMut(&str, ArchiveEntryKind) -> bool + 'static>>,
318318
) -> io::Result<()>;
319319

320320
fn build(self: Box<Self>, output: &Path) -> bool;
@@ -383,12 +383,27 @@ pub struct ArArchiveBuilder<'a> {
383383
entries: Vec<(Vec<u8>, ArchiveEntry)>,
384384
}
385385

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

400+
#[derive(Debug)]
401+
struct ArchiveEntry {
402+
source: ArchiveEntrySource,
403+
#[expect(dead_code)] // used in #155338
404+
kind: ArchiveEntryKind,
405+
}
406+
392407
impl<'a> ArArchiveBuilder<'a> {
393408
pub fn new(sess: &'a Session, object_reader: &'static ObjectReader) -> ArArchiveBuilder<'a> {
394409
ArArchiveBuilder { sess, object_reader, src_archives: vec![], entries: vec![] }
@@ -446,7 +461,7 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
446461
fn add_archive(
447462
&mut self,
448463
archive_path: &Path,
449-
mut skip: Option<Box<dyn FnMut(&str, Option<&RmetaLink>) -> bool + 'static>>,
464+
mut skip: Option<Box<dyn FnMut(&str, ArchiveEntryKind) -> bool + 'static>>,
450465
) -> io::Result<()> {
451466
let mut archive_path = archive_path.to_path_buf();
452467
if self.sess.target.llvm_target.contains("-apple-macosx")
@@ -462,8 +477,7 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
462477
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
463478
let archive = ArchiveFile::parse(&*archive_map)
464479
.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));
480+
let metadata_link = rmeta_link::read(&archive, &archive_map, &archive_path);
467481
let archive_index = self.src_archives.len();
468482

469483
if let Some(expected_kind) =
@@ -483,17 +497,23 @@ 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 = skip.as_mut().is_some_and(|f| f(&file_name, kind));
487509
if !drop {
488-
if entry.is_thin() {
510+
let source = if entry.is_thin() {
489511
let member_path = archive_path.parent().unwrap().join(Path::new(&file_name));
490-
self.entries.push((file_name.into_bytes(), ArchiveEntry::File(member_path)));
512+
ArchiveEntrySource::File(member_path)
491513
} else {
492-
self.entries.push((
493-
file_name.into_bytes(),
494-
ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
495-
));
496-
}
514+
ArchiveEntrySource::Archive { archive_index, file_range: entry.file_range() }
515+
};
516+
self.entries.push((file_name.into_bytes(), ArchiveEntry { source, kind }));
497517
}
498518
}
499519

@@ -502,10 +522,10 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
502522
}
503523

504524
/// Adds an arbitrary file to this archive
505-
fn add_file(&mut self, file: &Path) {
525+
fn add_file(&mut self, file: &Path, kind: ArchiveEntryKind) {
506526
self.entries.push((
507527
file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),
508-
ArchiveEntry::File(file.to_owned()),
528+
ArchiveEntry { source: ArchiveEntrySource::File(file.to_owned()), kind },
509529
));
510530
}
511531

@@ -539,8 +559,8 @@ impl<'a> ArArchiveBuilder<'a> {
539559

540560
for (entry_name, entry) in self.entries {
541561
let data =
542-
match entry {
543-
ArchiveEntry::FromArchive { archive_index, file_range } => {
562+
match entry.source {
563+
ArchiveEntrySource::Archive { archive_index, file_range } => {
544564
let src_archive = &self.src_archives[archive_index];
545565
let archive_data = &src_archive.1;
546566
let start = file_range.0 as usize;
@@ -563,7 +583,7 @@ impl<'a> ArArchiveBuilder<'a> {
563583

564584
Box::new(data) as Box<dyn AsRef<[u8]>>
565585
}
566-
ArchiveEntry::File(file) => unsafe {
586+
ArchiveEntrySource::File(file) => unsafe {
567587
Box::new(
568588
Mmap::map(File::open(file).map_err(|err| {
569589
io_error_context("failed to open object file", err)

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 19 additions & 21 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::{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
}
@@ -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+
Some(Box::new(move |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

@@ -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+
Some(Box::new(move |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

0 commit comments

Comments
 (0)