@@ -19,7 +19,7 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};
19
19
use rustc_hir:: lang_items:: LangItem ;
20
20
use rustc_index:: vec:: Idx ;
21
21
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrs ;
22
- use rustc_middle:: middle:: cstore:: { self , EncodedMetadata } ;
22
+ use rustc_middle:: middle:: cstore:: EncodedMetadata ;
23
23
use rustc_middle:: middle:: lang_items;
24
24
use rustc_middle:: mir:: mono:: { CodegenUnit , CodegenUnitNameBuilder , MonoItem } ;
25
25
use rustc_middle:: ty:: layout:: { HasTyCtxt , TyAndLayout } ;
@@ -775,6 +775,22 @@ impl CrateInfo {
775
775
subsystem. to_string ( )
776
776
} ) ;
777
777
778
+ // This list is used when generating the command line to pass through to
779
+ // system linker. The linker expects undefined symbols on the left of the
780
+ // command line to be defined in libraries on the right, not the other way
781
+ // around. For more info, see some comments in the add_used_library function
782
+ // below.
783
+ //
784
+ // In order to get this left-to-right dependency ordering, we use the reverse
785
+ // postorder of all crates putting the leaves at the right-most positions.
786
+ let used_crates = tcx
787
+ . postorder_cnums ( ( ) )
788
+ . iter ( )
789
+ . rev ( )
790
+ . copied ( )
791
+ . filter ( |& cnum| !tcx. dep_kind ( cnum) . macros_only ( ) )
792
+ . collect ( ) ;
793
+
778
794
let mut info = CrateInfo {
779
795
target_cpu,
780
796
exported_symbols,
@@ -785,7 +801,7 @@ impl CrateInfo {
785
801
native_libraries : Default :: default ( ) ,
786
802
used_libraries : tcx. native_libraries ( LOCAL_CRATE ) . iter ( ) . map ( Into :: into) . collect ( ) ,
787
803
crate_name : Default :: default ( ) ,
788
- used_crates : cstore :: used_crates ( tcx ) ,
804
+ used_crates,
789
805
used_crate_source : Default :: default ( ) ,
790
806
lang_item_to_crate : Default :: default ( ) ,
791
807
missing_lang_items : Default :: default ( ) ,
0 commit comments