@@ -21,7 +21,7 @@ use rustc_target::spec::Arch;
2121use tracing:: trace;
2222
2323use super :: metadata:: { create_compressed_metadata_file, search_for_section} ;
24- use super :: rmeta_link:: { self , RmetaLink } ;
24+ use super :: rmeta_link;
2525use crate :: common;
2626// Public for ArchiveBuilderBuilder::extract_bundled_libs
2727pub use crate :: errors:: ExtractBundledLibsError ;
@@ -309,12 +309,12 @@ fn find_binutils_dlltool(sess: &Session) -> OsString {
309309}
310310
311311pub 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+
392407impl < ' 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)
0 commit comments