@@ -143,16 +143,44 @@ impl CStore {
143
143
self . used_link_args . with_mut ( |s| s. clear ( ) ) ;
144
144
}
145
145
146
+ // This method is used when generating the command line to pass through to
147
+ // system linker. The linker expects undefined symbols on the left of the
148
+ // command line to be defined in libraries on the right, not the other way
149
+ // around. For more info, see some comments in the add_used_library function
150
+ // below.
151
+ //
152
+ // In order to get this left-to-right dependency ordering, we perform a
153
+ // topological sort of all crates putting the leaves at the right-most
154
+ // positions.
146
155
pub fn get_used_crates ( & self , prefer : LinkagePreference )
147
156
-> Vec < ( ast:: CrateNum , Option < Path > ) > {
157
+ let mut ordering = Vec :: new ( ) ;
158
+ fn visit ( cstore : & CStore , cnum : ast:: CrateNum ,
159
+ ordering : & mut Vec < ast:: CrateNum > ) {
160
+ if ordering. as_slice ( ) . contains ( & cnum) { return }
161
+ let meta = cstore. get_crate_data ( cnum) ;
162
+ for ( _, & dep) in meta. cnum_map . borrow ( ) . get ( ) . iter ( ) {
163
+ visit ( cstore, dep, ordering) ;
164
+ }
165
+ ordering. push ( cnum) ;
166
+ } ;
167
+ for ( & num, _) in self . metas . borrow ( ) . get ( ) . iter ( ) {
168
+ visit ( self , num, & mut ordering) ;
169
+ }
170
+ ordering. as_mut_slice ( ) . reverse ( ) ;
171
+ let ordering = ordering. as_slice ( ) ;
148
172
let used_crate_sources = self . used_crate_sources . borrow ( ) ;
149
- used_crate_sources. get ( )
173
+ let mut libs = used_crate_sources. get ( )
150
174
. iter ( )
151
175
. map ( |src| ( src. cnum , match prefer {
152
176
RequireDynamic => src. dylib . clone ( ) ,
153
177
RequireStatic => src. rlib . clone ( ) ,
154
178
} ) )
155
- . collect ( )
179
+ . collect ( ) ;
180
+ libs. sort_by ( |& ( a, _) , & ( b, _) | {
181
+ ordering. position_elem ( & a) . cmp ( & ordering. position_elem ( & b) )
182
+ } ) ;
183
+ libs
156
184
}
157
185
158
186
pub fn add_used_library ( & self , lib : ~str , kind : NativeLibaryKind ) {
0 commit comments