@@ -13,56 +13,62 @@ import lib::llvm::{False, llvm, mk_object_file, mk_section_iter};
13
13
import util:: { filesearch} ;
14
14
import io:: writer_util;
15
15
16
+ export os;
17
+ export ctxt;
16
18
export load_library_crate;
17
19
export list_file_metadata;
18
20
export note_linkage_attrs;
19
21
export crate_name_from_metas;
20
22
export metadata_matches;
23
+ export meta_section_name;
21
24
22
- fn load_library_crate ( sess : session:: session , ident : ast:: ident , span : span ,
23
- metas : [ @ast:: meta_item ] , hash : str )
24
- -> { ident: str , data: @[ u8] } {
25
-
25
+ enum os {
26
+ os_macos,
27
+ os_win32,
28
+ os_linux,
29
+ os_freebsd
30
+ }
26
31
27
- alt find_library_crate ( sess, span, metas, hash) {
32
+ type ctxt = {
33
+ sess : session ,
34
+ span : span ,
35
+ ident: ast:: ident ,
36
+ metas : [ @ast:: meta_item ] ,
37
+ hash : str ,
38
+ os : os ,
39
+ static : bool
40
+ } ;
41
+
42
+ fn load_library_crate ( cx : ctxt ) -> { ident: str , data: @[ u8] } {
43
+ alt find_library_crate ( cx) {
28
44
some ( t) { ret t; }
29
45
none {
30
- sess. span_fatal ( span, #fmt[ "can't find crate for '%s'" , ident] ) ;
46
+ cx. sess . span_fatal (
47
+ cx. span , #fmt[ "can't find crate for '%s'" , cx. ident ] ) ;
31
48
}
32
49
}
33
50
}
34
51
35
- fn find_library_crate ( sess : session:: session , span : span ,
36
- metas : [ @ast:: meta_item ] , hash : str )
37
- -> option < { ident: str , data : @[ u8 ] } > {
38
-
39
- attr:: require_unique_names ( sess. diagnostic ( ) , metas) ;
40
- let metas = metas;
41
-
42
- let nn = default_native_lib_naming ( sess, sess. opts . static ) ;
43
- find_library_crate_aux ( sess, span, nn,
44
- metas, hash, sess. filesearch )
52
+ fn find_library_crate ( cx : ctxt ) -> option < { ident: str , data : @[ u8 ] } > {
53
+ attr:: require_unique_names ( cx. sess . diagnostic ( ) , cx. metas ) ;
54
+ find_library_crate_aux ( cx, libname ( cx) , cx. sess . filesearch )
45
55
}
46
56
47
- fn default_native_lib_naming ( sess : session:: session , static : bool ) ->
48
- { prefix : str , suffix : str } {
49
- if static { ret { prefix : "lib" , suffix : ".rlib" } ; }
50
- alt sess. targ_cfg . os {
51
- session:: os_win32 { ret { prefix : "" , suffix : ".dll" } ; }
52
- session:: os_macos { ret { prefix : "lib" , suffix : ".dylib" } ; }
53
- session:: os_linux { ret { prefix : "lib" , suffix : ".so" } ; }
54
- session:: os_freebsd { ret { prefix : "lib" , suffix : ".so" } ; }
57
+ fn libname ( cx : ctxt ) -> { prefix : str , suffix : str } {
58
+ if cx. static { ret { prefix : "lib" , suffix : ".rlib" } ; }
59
+ alt cx. os {
60
+ os_win32 { ret { prefix : "" , suffix : ".dll" } ; }
61
+ os_macos { ret { prefix : "lib" , suffix : ".dylib" } ; }
62
+ os_linux { ret { prefix : "lib" , suffix : ".so" } ; }
63
+ os_freebsd { ret { prefix : "lib" , suffix : ".so" } ; }
55
64
}
56
65
}
57
66
58
- fn find_library_crate_aux ( sess : session:: session ,
59
- span : span ,
67
+ fn find_library_crate_aux ( cx : ctxt ,
60
68
nn : { prefix : str , suffix : str } ,
61
- metas : [ @ast:: meta_item ] ,
62
- hash : str ,
63
69
filesearch : filesearch:: filesearch ) ->
64
70
option < { ident: str , data : @[ u8 ] } > {
65
- let crate_name = crate_name_from_metas ( metas) ;
71
+ let crate_name = crate_name_from_metas ( cx . metas ) ;
66
72
let prefix: str = nn. prefix + crate_name + "-" ;
67
73
let suffix: str = nn. suffix ;
68
74
@@ -76,9 +82,9 @@ fn find_library_crate_aux(sess: session::session,
76
82
option:: none :: < ( ) >
77
83
} else {
78
84
#debug ( "%s is a candidate" , path) ;
79
- alt get_metadata_section ( sess , path) {
85
+ alt get_metadata_section ( cx . os , path) {
80
86
option:: some ( cvec) {
81
- if !crate_matches ( cvec, metas, hash) {
87
+ if !crate_matches ( cvec, cx . metas , cx . hash ) {
82
88
#debug ( "skipping %s, metadata doesn't match" , path) ;
83
89
option:: none :: < ( ) >
84
90
} else {
@@ -100,15 +106,15 @@ fn find_library_crate_aux(sess: session::session,
100
106
} else if matches. len ( ) == 1 u {
101
107
some ( matches[ 0 ] )
102
108
} else {
103
- sess. span_err (
104
- span, #fmt ( "multiple matching crates for `%s`" , crate_name) ) ;
105
- sess. note ( "candidates:" ) ;
109
+ cx . sess . span_err (
110
+ cx . span , #fmt ( "multiple matching crates for `%s`" , crate_name) ) ;
111
+ cx . sess . note ( "candidates:" ) ;
106
112
for matches. each { |match|
107
- sess. note( #fmt( "path: %s" , match. ident) ) ;
113
+ cx . sess. note( #fmt( "path: %s" , match. ident) ) ;
108
114
let attrs = decoder:: get_crate_attributes( match . data) ;
109
- note_linkage_attrs ( sess, attrs) ;
115
+ note_linkage_attrs ( cx . sess , attrs) ;
110
116
}
111
- sess. abort_if_errors ( ) ;
117
+ cx . sess . abort_if_errors ( ) ;
112
118
none
113
119
}
114
120
}
@@ -166,7 +172,7 @@ fn metadata_matches(extern_metas: [@ast::meta_item],
166
172
ret true;
167
173
}
168
174
169
- fn get_metadata_section ( sess : session :: session ,
175
+ fn get_metadata_section ( os : os ,
170
176
filename : str ) -> option < @[ u8 ] > unsafe {
171
177
let mb = str:: as_c_str ( filename, { |buf|
172
178
llvm:: LLVMRustCreateMemoryBufferWithContentsOfFile ( buf)
@@ -180,7 +186,7 @@ fn get_metadata_section(sess: session::session,
180
186
while llvm:: LLVMIsSectionIteratorAtEnd ( of. llof , si. llsi ) == False {
181
187
let name_buf = llvm:: LLVMGetSectionName ( si. llsi ) ;
182
188
let name = unsafe { str:: unsafe:: from_c_str ( name_buf) } ;
183
- if str:: eq ( name, sess . targ_cfg . target_strs . meta_sect_name ) {
189
+ if str:: eq ( name, meta_section_name ( os ) ) {
184
190
let cbuf = llvm:: LLVMGetSectionContents ( si. llsi ) ;
185
191
let csz = llvm:: LLVMGetSectionSize ( si. llsi ) as uint ;
186
192
unsafe {
@@ -193,9 +199,18 @@ fn get_metadata_section(sess: session::session,
193
199
ret option:: none :: < @[ u8 ] > ;
194
200
}
195
201
202
+ fn meta_section_name ( os : os ) -> str {
203
+ alt os {
204
+ os_macos { "__DATA,__note.rustc" }
205
+ os_win32 { ".note.rustc" }
206
+ os_linux { ".note.rustc" }
207
+ os_freebsd { ".note.rustc" }
208
+ }
209
+ }
210
+
196
211
// A diagnostic function for dumping crate metadata to an output stream
197
- fn list_file_metadata ( sess : session :: session , path : str , out : io:: writer ) {
198
- alt get_metadata_section ( sess , path) {
212
+ fn list_file_metadata ( os : os , path : str , out : io:: writer ) {
213
+ alt get_metadata_section ( os , path) {
199
214
option:: some ( bytes) { decoder:: list_crate_metadata ( bytes, out) ; }
200
215
option:: none {
201
216
out. write_str ( "could not find metadata in " + path + ".\n " ) ;
0 commit comments