@@ -15,19 +15,12 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
15
15
use rustc_session:: cstore:: { DllCallingConvention , DllImport } ;
16
16
use rustc_session:: Session ;
17
17
18
- struct ArchiveConfig < ' a > {
19
- pub sess : & ' a Session ,
20
- pub dst : PathBuf ,
21
- pub src : Option < PathBuf > ,
22
- }
23
-
24
18
/// Helper for adding many files to an archive.
25
19
#[ must_use = "must call build() to finish building the archive" ]
26
20
pub struct LlvmArchiveBuilder < ' a > {
27
- config : ArchiveConfig < ' a > ,
28
- removals : Vec < String > ,
21
+ sess : & ' a Session ,
22
+ dst : PathBuf ,
29
23
additions : Vec < Addition > ,
30
- src_archive : Option < Option < ArchiveRO > > ,
31
24
}
32
25
33
26
enum Addition {
@@ -50,10 +43,6 @@ fn is_relevant_child(c: &Child<'_>) -> bool {
50
43
}
51
44
}
52
45
53
- fn archive_config < ' a > ( sess : & ' a Session , output : & Path , input : Option < & Path > ) -> ArchiveConfig < ' a > {
54
- ArchiveConfig { sess, dst : output. to_path_buf ( ) , src : input. map ( |p| p. to_path_buf ( ) ) }
55
- }
56
-
57
46
/// Map machine type strings to values of LLVM's MachineTypes enum.
58
47
fn llvm_machine_type ( cpu : & str ) -> LLVMMachineType {
59
48
match cpu {
@@ -68,37 +57,8 @@ fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
68
57
impl < ' a > ArchiveBuilder < ' a > for LlvmArchiveBuilder < ' a > {
69
58
/// Creates a new static archive, ready for modifying the archive specified
70
59
/// by `config`.
71
- fn new ( sess : & ' a Session , output : & Path , input : Option < & Path > ) -> LlvmArchiveBuilder < ' a > {
72
- let config = archive_config ( sess, output, input) ;
73
- LlvmArchiveBuilder {
74
- config,
75
- removals : Vec :: new ( ) ,
76
- additions : Vec :: new ( ) ,
77
- src_archive : None ,
78
- }
79
- }
80
-
81
- /// Removes a file from this archive
82
- fn remove_file ( & mut self , file : & str ) {
83
- self . removals . push ( file. to_string ( ) ) ;
84
- }
85
-
86
- /// Lists all files in an archive
87
- fn src_files ( & mut self ) -> Vec < String > {
88
- if self . src_archive ( ) . is_none ( ) {
89
- return Vec :: new ( ) ;
90
- }
91
-
92
- let archive = self . src_archive . as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) ;
93
-
94
- archive
95
- . iter ( )
96
- . filter_map ( |child| child. ok ( ) )
97
- . filter ( is_relevant_child)
98
- . filter_map ( |child| child. name ( ) )
99
- . filter ( |name| !self . removals . iter ( ) . any ( |x| x == name) )
100
- . map ( |name| name. to_owned ( ) )
101
- . collect ( )
60
+ fn new ( sess : & ' a Session , output : & Path ) -> LlvmArchiveBuilder < ' a > {
61
+ LlvmArchiveBuilder { sess, dst : output. to_path_buf ( ) , additions : Vec :: new ( ) }
102
62
}
103
63
104
64
fn add_archive < F > ( & mut self , archive : & Path , skip : F ) -> io:: Result < ( ) >
@@ -129,13 +89,10 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
129
89
130
90
/// Combine the provided files, rlibs, and native libraries into a single
131
91
/// `Archive`.
132
- fn build ( mut self ) {
133
- let kind = self . llvm_archive_kind ( ) . unwrap_or_else ( |kind| {
134
- self . config . sess . fatal ( & format ! ( "Don't know how to build archive of type: {}" , kind) )
135
- } ) ;
136
-
137
- if let Err ( e) = self . build_with_llvm ( kind) {
138
- self . config . sess . fatal ( & format ! ( "failed to build archive: {}" , e) ) ;
92
+ fn build ( mut self ) -> bool {
93
+ match self . build_with_llvm ( ) {
94
+ Ok ( any_members) => any_members,
95
+ Err ( e) => self . sess . fatal ( & format ! ( "failed to build archive: {}" , e) ) ,
139
96
}
140
97
}
141
98
@@ -151,7 +108,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
151
108
output_path. with_extension ( "lib" )
152
109
} ;
153
110
154
- let target = & self . config . sess . target ;
111
+ let target = & self . sess . target ;
155
112
let mingw_gnu_toolchain = target. vendor == "pc"
156
113
&& target. os == "windows"
157
114
&& target. env == "gnu"
@@ -160,7 +117,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
160
117
let import_name_and_ordinal_vector: Vec < ( String , Option < u16 > ) > = dll_imports
161
118
. iter ( )
162
119
. map ( |import : & DllImport | {
163
- if self . config . sess . target . arch == "x86" {
120
+ if self . sess . target . arch == "x86" {
164
121
(
165
122
LlvmArchiveBuilder :: i686_decorated_name ( import, mingw_gnu_toolchain) ,
166
123
import. ordinal ,
@@ -197,11 +154,11 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
197
154
match std:: fs:: write ( & def_file_path, def_file_content) {
198
155
Ok ( _) => { }
199
156
Err ( e) => {
200
- self . config . sess . fatal ( & format ! ( "Error writing .DEF file: {}" , e) ) ;
157
+ self . sess . fatal ( & format ! ( "Error writing .DEF file: {}" , e) ) ;
201
158
}
202
159
} ;
203
160
204
- let dlltool = find_binutils_dlltool ( self . config . sess ) ;
161
+ let dlltool = find_binutils_dlltool ( self . sess ) ;
205
162
let result = std:: process:: Command :: new ( dlltool)
206
163
. args ( [
207
164
"-d" ,
@@ -215,9 +172,9 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
215
172
216
173
match result {
217
174
Err ( e) => {
218
- self . config . sess . fatal ( & format ! ( "Error calling dlltool: {}" , e) ) ;
175
+ self . sess . fatal ( & format ! ( "Error calling dlltool: {}" , e) ) ;
219
176
}
220
- Ok ( output) if !output. status . success ( ) => self . config . sess . fatal ( & format ! (
177
+ Ok ( output) if !output. status . success ( ) => self . sess . fatal ( & format ! (
221
178
"Dlltool could not create import library: {}\n {}" ,
222
179
String :: from_utf8_lossy( & output. stdout) ,
223
180
String :: from_utf8_lossy( & output. stderr)
@@ -263,13 +220,13 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
263
220
output_path_z. as_ptr ( ) ,
264
221
ffi_exports. as_ptr ( ) ,
265
222
ffi_exports. len ( ) ,
266
- llvm_machine_type ( & self . config . sess . target . arch ) as u16 ,
267
- !self . config . sess . target . is_like_msvc ,
223
+ llvm_machine_type ( & self . sess . target . arch ) as u16 ,
224
+ !self . sess . target . is_like_msvc ,
268
225
)
269
226
} ;
270
227
271
228
if result == crate :: llvm:: LLVMRustResult :: Failure {
272
- self . config . sess . fatal ( & format ! (
229
+ self . sess . fatal ( & format ! (
273
230
"Error creating import library for {}: {}" ,
274
231
lib_name,
275
232
llvm:: last_error( ) . unwrap_or( "unknown LLVM error" . to_string( ) )
@@ -278,7 +235,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
278
235
} ;
279
236
280
237
self . add_archive ( & output_path, |_| false ) . unwrap_or_else ( |e| {
281
- self . config . sess . fatal ( & format ! (
238
+ self . sess . fatal ( & format ! (
282
239
"failed to add native library {}: {}" ,
283
240
output_path. display( ) ,
284
241
e
@@ -288,46 +245,19 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
288
245
}
289
246
290
247
impl < ' a > LlvmArchiveBuilder < ' a > {
291
- fn src_archive ( & mut self ) -> Option < & ArchiveRO > {
292
- if let Some ( ref a) = self . src_archive {
293
- return a. as_ref ( ) ;
294
- }
295
- let src = self . config . src . as_ref ( ) ?;
296
- self . src_archive = Some ( ArchiveRO :: open ( src) . ok ( ) ) ;
297
- self . src_archive . as_ref ( ) . unwrap ( ) . as_ref ( )
298
- }
299
-
300
- fn llvm_archive_kind ( & self ) -> Result < ArchiveKind , & str > {
301
- let kind = & * self . config . sess . target . archive_format ;
302
- kind. parse ( ) . map_err ( |_| kind)
303
- }
248
+ fn build_with_llvm ( & mut self ) -> io:: Result < bool > {
249
+ let kind = & * self . sess . target . archive_format ;
250
+ let kind = kind. parse :: < ArchiveKind > ( ) . map_err ( |_| kind) . unwrap_or_else ( |kind| {
251
+ self . sess . fatal ( & format ! ( "Don't know how to build archive of type: {}" , kind) )
252
+ } ) ;
304
253
305
- fn build_with_llvm ( & mut self , kind : ArchiveKind ) -> io:: Result < ( ) > {
306
- let removals = mem:: take ( & mut self . removals ) ;
307
254
let mut additions = mem:: take ( & mut self . additions ) ;
308
255
let mut strings = Vec :: new ( ) ;
309
256
let mut members = Vec :: new ( ) ;
310
257
311
- let dst = CString :: new ( self . config . dst . to_str ( ) . unwrap ( ) ) ?;
258
+ let dst = CString :: new ( self . dst . to_str ( ) . unwrap ( ) ) ?;
312
259
313
260
unsafe {
314
- if let Some ( archive) = self . src_archive ( ) {
315
- for child in archive. iter ( ) {
316
- let child = child. map_err ( string_to_io_error) ?;
317
- let Some ( child_name) = child. name ( ) else { continue } ;
318
- if removals. iter ( ) . any ( |r| r == child_name) {
319
- continue ;
320
- }
321
-
322
- let name = CString :: new ( child_name) ?;
323
- members. push ( llvm:: LLVMRustArchiveMemberNew (
324
- ptr:: null ( ) ,
325
- name. as_ptr ( ) ,
326
- Some ( child. raw ) ,
327
- ) ) ;
328
- strings. push ( name) ;
329
- }
330
- }
331
261
for addition in & mut additions {
332
262
match addition {
333
263
Addition :: File { path, name_in_archive } => {
@@ -389,7 +319,7 @@ impl<'a> LlvmArchiveBuilder<'a> {
389
319
} ;
390
320
Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
391
321
} else {
392
- Ok ( ( ) )
322
+ Ok ( !members . is_empty ( ) )
393
323
} ;
394
324
for member in members {
395
325
llvm:: LLVMRustArchiveMemberFree ( member) ;
0 commit comments