@@ -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 } ;
@@ -769,6 +769,22 @@ impl CrateInfo {
769
769
subsystem. to_string ( )
770
770
} ) ;
771
771
772
+ // This list is used when generating the command line to pass through to
773
+ // system linker. The linker expects undefined symbols on the left of the
774
+ // command line to be defined in libraries on the right, not the other way
775
+ // around. For more info, see some comments in the add_used_library function
776
+ // below.
777
+ //
778
+ // In order to get this left-to-right dependency ordering, we use the reverse
779
+ // postorder of all crates putting the leaves at the right-most positions.
780
+ let used_crates = tcx
781
+ . postorder_cnums ( ( ) )
782
+ . iter ( )
783
+ . rev ( )
784
+ . copied ( )
785
+ . filter ( |& cnum| !tcx. dep_kind ( cnum) . macros_only ( ) )
786
+ . collect ( ) ;
787
+
772
788
let mut info = CrateInfo {
773
789
local_crate_name,
774
790
panic_runtime : None ,
@@ -778,7 +794,7 @@ impl CrateInfo {
778
794
native_libraries : Default :: default ( ) ,
779
795
used_libraries : tcx. native_libraries ( LOCAL_CRATE ) . iter ( ) . map ( Into :: into) . collect ( ) ,
780
796
crate_name : Default :: default ( ) ,
781
- used_crates : cstore :: used_crates ( tcx ) ,
797
+ used_crates,
782
798
used_crate_source : Default :: default ( ) ,
783
799
lang_item_to_crate : Default :: default ( ) ,
784
800
missing_lang_items : Default :: default ( ) ,
0 commit comments