@@ -7,14 +7,13 @@ use std::path::{Path, PathBuf};
77use std:: ptr;
88use std:: str;
99
10- use crate :: back:: bytecode:: RLIB_BYTECODE_EXTENSION ;
1110use crate :: llvm:: archive_ro:: { ArchiveRO , Child } ;
1211use crate :: llvm:: { self , ArchiveKind } ;
13- use crate :: metadata :: METADATA_FILENAME ;
14- use rustc_codegen_ssa:: back:: archive:: find_library;
12+ use rustc_codegen_ssa :: { METADATA_FILENAME , RLIB_BYTECODE_EXTENSION } ;
13+ use rustc_codegen_ssa:: back:: archive:: { ArchiveBuilder , find_library} ;
1514use rustc:: session:: Session ;
1615
17- pub struct ArchiveConfig < ' a > {
16+ struct ArchiveConfig < ' a > {
1817 pub sess : & ' a Session ,
1918 pub dst : PathBuf ,
2019 pub src : Option < PathBuf > ,
@@ -23,7 +22,7 @@ pub struct ArchiveConfig<'a> {
2322
2423/// Helper for adding many files to an archive.
2524#[ must_use = "must call build() to finish building the archive" ]
26- pub struct ArchiveBuilder < ' a > {
25+ pub struct LlvmArchiveBuilder < ' a > {
2726 config : ArchiveConfig < ' a > ,
2827 removals : Vec < String > ,
2928 additions : Vec < Addition > ,
@@ -49,11 +48,26 @@ fn is_relevant_child(c: &Child<'_>) -> bool {
4948 }
5049}
5150
52- impl < ' a > ArchiveBuilder < ' a > {
51+ fn archive_config < ' a > ( sess : & ' a Session ,
52+ output : & Path ,
53+ input : Option < & Path > ) -> ArchiveConfig < ' a > {
54+ use rustc_codegen_ssa:: back:: link:: archive_search_paths;
55+ ArchiveConfig {
56+ sess,
57+ dst : output. to_path_buf ( ) ,
58+ src : input. map ( |p| p. to_path_buf ( ) ) ,
59+ lib_search_paths : archive_search_paths ( sess) ,
60+ }
61+ }
62+
63+ impl < ' a > ArchiveBuilder < ' a > for LlvmArchiveBuilder < ' a > {
5364 /// Creates a new static archive, ready for modifying the archive specified
5465 /// by `config`.
55- pub fn new ( config : ArchiveConfig < ' a > ) -> ArchiveBuilder < ' a > {
56- ArchiveBuilder {
66+ fn new ( sess : & ' a Session ,
67+ output : & Path ,
68+ input : Option < & Path > ) -> LlvmArchiveBuilder < ' a > {
69+ let config = archive_config ( sess, output, input) ;
70+ LlvmArchiveBuilder {
5771 config,
5872 removals : Vec :: new ( ) ,
5973 additions : Vec :: new ( ) ,
@@ -63,12 +77,12 @@ impl<'a> ArchiveBuilder<'a> {
6377 }
6478
6579 /// Removes a file from this archive
66- pub fn remove_file ( & mut self , file : & str ) {
80+ fn remove_file ( & mut self , file : & str ) {
6781 self . removals . push ( file. to_string ( ) ) ;
6882 }
6983
7084 /// Lists all files in an archive
71- pub fn src_files ( & mut self ) -> Vec < String > {
85+ fn src_files ( & mut self ) -> Vec < String > {
7286 if self . src_archive ( ) . is_none ( ) {
7387 return Vec :: new ( )
7488 }
@@ -84,18 +98,9 @@ impl<'a> ArchiveBuilder<'a> {
8498 . collect ( )
8599 }
86100
87- fn src_archive ( & mut self ) -> Option < & ArchiveRO > {
88- if let Some ( ref a) = self . src_archive {
89- return a. as_ref ( )
90- }
91- let src = self . config . src . as_ref ( ) ?;
92- self . src_archive = Some ( ArchiveRO :: open ( src) . ok ( ) ) ;
93- self . src_archive . as_ref ( ) . unwrap ( ) . as_ref ( )
94- }
95-
96101 /// Adds all of the contents of a native library to this archive. This will
97102 /// search in the relevant locations for a library named `name`.
98- pub fn add_native_library ( & mut self , name : & str ) {
103+ fn add_native_library ( & mut self , name : & str ) {
99104 let location = find_library ( name, & self . config . lib_search_paths ,
100105 self . config . sess ) ;
101106 self . add_archive ( & location, |_| false ) . unwrap_or_else ( |e| {
@@ -109,7 +114,7 @@ impl<'a> ArchiveBuilder<'a> {
109114 ///
110115 /// This ignores adding the bytecode from the rlib, and if LTO is enabled
111116 /// then the object file also isn't added.
112- pub fn add_rlib ( & mut self ,
117+ fn add_rlib ( & mut self ,
113118 rlib : & Path ,
114119 name : & str ,
115120 lto : bool ,
@@ -141,23 +146,8 @@ impl<'a> ArchiveBuilder<'a> {
141146 } )
142147 }
143148
144- fn add_archive < F > ( & mut self , archive : & Path , skip : F )
145- -> io:: Result < ( ) >
146- where F : FnMut ( & str ) -> bool + ' static
147- {
148- let archive = match ArchiveRO :: open ( archive) {
149- Ok ( ar) => ar,
150- Err ( e) => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , e) ) ,
151- } ;
152- self . additions . push ( Addition :: Archive {
153- archive,
154- skip : Box :: new ( skip) ,
155- } ) ;
156- Ok ( ( ) )
157- }
158-
159149 /// Adds an arbitrary file to this archive
160- pub fn add_file ( & mut self , file : & Path ) {
150+ fn add_file ( & mut self , file : & Path ) {
161151 let name = file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
162152 self . additions . push ( Addition :: File {
163153 path : file. to_path_buf ( ) ,
@@ -167,13 +157,13 @@ impl<'a> ArchiveBuilder<'a> {
167157
168158 /// Indicate that the next call to `build` should update all symbols in
169159 /// the archive (equivalent to running 'ar s' over it).
170- pub fn update_symbols ( & mut self ) {
160+ fn update_symbols ( & mut self ) {
171161 self . should_update_symbols = true ;
172162 }
173163
174164 /// Combine the provided files, rlibs, and native libraries into a single
175165 /// `Archive`.
176- pub fn build ( & mut self ) {
166+ fn build ( mut self ) {
177167 let kind = self . llvm_archive_kind ( ) . unwrap_or_else ( |kind|
178168 self . config . sess . fatal ( & format ! ( "Don't know how to build archive of type: {}" , kind) ) ) ;
179169
@@ -182,6 +172,32 @@ impl<'a> ArchiveBuilder<'a> {
182172 }
183173
184174 }
175+ }
176+
177+ impl < ' a > LlvmArchiveBuilder < ' a > {
178+ fn src_archive ( & mut self ) -> Option < & ArchiveRO > {
179+ if let Some ( ref a) = self . src_archive {
180+ return a. as_ref ( )
181+ }
182+ let src = self . config . src . as_ref ( ) ?;
183+ self . src_archive = Some ( ArchiveRO :: open ( src) . ok ( ) ) ;
184+ self . src_archive . as_ref ( ) . unwrap ( ) . as_ref ( )
185+ }
186+
187+ fn add_archive < F > ( & mut self , archive : & Path , skip : F )
188+ -> io:: Result < ( ) >
189+ where F : FnMut ( & str ) -> bool + ' static
190+ {
191+ let archive = match ArchiveRO :: open ( archive) {
192+ Ok ( ar) => ar,
193+ Err ( e) => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , e) ) ,
194+ } ;
195+ self . additions . push ( Addition :: Archive {
196+ archive,
197+ skip : Box :: new ( skip) ,
198+ } ) ;
199+ Ok ( ( ) )
200+ }
185201
186202 fn llvm_archive_kind ( & self ) -> Result < ArchiveKind , & str > {
187203 let kind = & * self . config . sess . target . target . options . archive_format ;
0 commit comments