11use std:: fs:: File ;
22use std:: path:: { Path , PathBuf } ;
33
4- use rustc_codegen_ssa:: back:: archive:: ArchiveBuilder ;
4+ use rustc_codegen_ssa:: back:: archive:: { ArchiveBuilder , ArchiveBuilderBuilder } ;
55use rustc_session:: Session ;
66
77use rustc_session:: cstore:: DllImport ;
88
99struct ArchiveConfig < ' a > {
1010 sess : & ' a Session ,
11- dst : PathBuf ,
1211 use_native_ar : bool ,
1312 use_gnu_style_archive : bool ,
1413}
@@ -22,42 +21,56 @@ enum ArchiveEntry {
2221 File ( PathBuf ) ,
2322}
2423
25- pub struct ArArchiveBuilder < ' a > {
26- config : ArchiveConfig < ' a > ,
27- src_archives : Vec < ( PathBuf , ar:: Archive < File > ) > ,
28- // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
29- // the end of an archive for linkers to not get confused.
30- entries : Vec < ( String , ArchiveEntry ) > ,
31- }
24+ pub struct ArArchiveBuilderBuilder ;
3225
33- impl < ' a > ArchiveBuilder < ' a > for ArArchiveBuilder < ' a > {
34- fn new ( sess : & ' a Session , output : & Path ) -> Self {
26+ impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
27+ fn new_archive_builder < ' a > ( & self , sess : & ' a Session ) -> Box < dyn ArchiveBuilder < ' a > + ' a > {
3528 let config = ArchiveConfig {
3629 sess,
37- dst : output. to_path_buf ( ) ,
3830 use_native_ar : false ,
3931 // FIXME test for linux and System V derivatives instead
4032 use_gnu_style_archive : sess. target . options . archive_format == "gnu" ,
4133 } ;
4234
43- ArArchiveBuilder {
35+ Box :: new ( ArArchiveBuilder {
4436 config,
4537 src_archives : vec ! [ ] ,
4638 entries : vec ! [ ] ,
47- }
39+ } )
40+ }
41+
42+ fn create_dll_import_lib (
43+ & self ,
44+ _sess : & Session ,
45+ _lib_name : & str ,
46+ _dll_imports : & [ DllImport ] ,
47+ _tmpdir : & Path ,
48+ ) -> PathBuf {
49+ unimplemented ! ( ) ;
4850 }
51+ }
4952
53+ pub struct ArArchiveBuilder < ' a > {
54+ config : ArchiveConfig < ' a > ,
55+ src_archives : Vec < ( PathBuf , ar:: Archive < File > ) > ,
56+ // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
57+ // the end of an archive for linkers to not get confused.
58+ entries : Vec < ( String , ArchiveEntry ) > ,
59+ }
60+
61+ impl < ' a > ArchiveBuilder < ' a > for ArArchiveBuilder < ' a > {
5062 fn add_file ( & mut self , file : & Path ) {
5163 self . entries . push ( (
5264 file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
5365 ArchiveEntry :: File ( file. to_owned ( ) ) ,
5466 ) ) ;
5567 }
5668
57- fn add_archive < F > ( & mut self , archive_path : & Path , mut skip : F ) -> std:: io:: Result < ( ) >
58- where
59- F : FnMut ( & str ) -> bool + ' static ,
60- {
69+ fn add_archive (
70+ & mut self ,
71+ archive_path : & Path ,
72+ mut skip : Box < dyn FnMut ( & str ) -> bool + ' static > ,
73+ ) -> std:: io:: Result < ( ) > {
6174 let mut archive = ar:: Archive :: new ( std:: fs:: File :: open ( & archive_path) ?) ;
6275 let archive_index = self . src_archives . len ( ) ;
6376
@@ -77,7 +90,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
7790 Ok ( ( ) )
7891 }
7992
80- fn build ( mut self ) -> bool {
93+ fn build ( mut self : Box < Self > , output : & Path ) -> bool {
8194 use std:: process:: Command ;
8295
8396 fn add_file_using_ar ( archive : & Path , file : & Path ) {
@@ -97,17 +110,17 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
97110 }
98111
99112 let mut builder = if self . config . use_native_ar {
100- BuilderKind :: NativeAr ( & self . config . dst )
113+ BuilderKind :: NativeAr ( output )
101114 } else if self . config . use_gnu_style_archive {
102115 BuilderKind :: Gnu ( ar:: GnuBuilder :: new (
103- File :: create ( & self . config . dst ) . unwrap ( ) ,
116+ File :: create ( output ) . unwrap ( ) ,
104117 self . entries
105118 . iter ( )
106119 . map ( |( name, _) | name. as_bytes ( ) . to_vec ( ) )
107120 . collect ( ) ,
108121 ) )
109122 } else {
110- BuilderKind :: Bsd ( ar:: Builder :: new ( File :: create ( & self . config . dst ) . unwrap ( ) ) )
123+ BuilderKind :: Bsd ( ar:: Builder :: new ( File :: create ( output ) . unwrap ( ) ) )
111124 } ;
112125
113126 let any_members = !self . entries . is_empty ( ) ;
@@ -164,28 +177,13 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
164177 std:: mem:: drop ( builder) ;
165178
166179 // 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" ) ;
180+ let status =
181+ std:: process:: Command :: new ( "ranlib" ) . arg ( output) . status ( ) . expect ( "Couldn't run ranlib" ) ;
171182
172183 if !status. success ( ) {
173184 self . config . sess . fatal ( & format ! ( "Ranlib exited with code {:?}" , status. code( ) ) ) ;
174185 }
175186
176187 any_members
177188 }
178-
179- fn sess ( & self ) -> & Session {
180- self . config . sess
181- }
182-
183- fn create_dll_import_lib (
184- _sess : & Session ,
185- _lib_name : & str ,
186- _dll_imports : & [ DllImport ] ,
187- _tmpdir : & Path ,
188- ) -> PathBuf {
189- unimplemented ! ( ) ;
190- }
191189}
0 commit comments