@@ -43,8 +43,7 @@ use self::unit_dependencies::UnitDep;
4343pub use crate :: core:: compiler:: unit:: { Unit , UnitInterner } ;
4444use crate :: core:: manifest:: TargetSourcePath ;
4545use crate :: core:: profiles:: { Lto , PanicStrategy , Profile } ;
46- use crate :: core:: Feature ;
47- use crate :: core:: { PackageId , Target } ;
46+ use crate :: core:: { Feature , InternedString , PackageId , Target } ;
4847use crate :: util:: errors:: { self , CargoResult , CargoResultExt , Internal , ProcessError } ;
4948use crate :: util:: machine_message:: Message ;
5049use crate :: util:: paths;
@@ -894,8 +893,7 @@ fn build_deps_args<'a, 'cfg>(
894893 } ) ;
895894 }
896895
897- // Create Vec since mutable cx is needed in closure below.
898- let deps = Vec :: from ( cx. unit_deps ( unit) ) ;
896+ let deps = cx. unit_deps ( unit) ;
899897
900898 // If there is not one linkable target but should, rustc fails later
901899 // on if there is an `extern crate` for it. This may turn into a hard
@@ -922,23 +920,14 @@ fn build_deps_args<'a, 'cfg>(
922920
923921 let mut unstable_opts = false ;
924922
925- if let Some ( sysroot) = cx. files ( ) . layout ( unit. kind ) . sysroot ( ) {
926- if !unit. kind . is_host ( ) {
927- cmd. arg ( "--sysroot" ) . arg ( sysroot) ;
928- }
929- }
930-
931923 for dep in deps {
932- if !unit. is_std && dep. unit . is_std {
933- // Dependency to sysroot crate uses --sysroot.
934- continue ;
935- }
936924 if dep. unit . mode . is_run_custom_build ( ) {
937925 cmd. env ( "OUT_DIR" , & cx. files ( ) . build_script_out_dir ( & dep. unit ) ) ;
938926 }
939- if dep. unit . target . linkable ( ) && !dep. unit . mode . is_doc ( ) {
940- link_to ( cmd, cx, unit, & dep, & mut unstable_opts) ?;
941- }
927+ }
928+
929+ for arg in extern_args ( cx, unit, & mut unstable_opts) ? {
930+ cmd. arg ( arg) ;
942931 }
943932
944933 // This will only be set if we're already using a feature
@@ -948,37 +937,51 @@ fn build_deps_args<'a, 'cfg>(
948937 }
949938
950939 return Ok ( ( ) ) ;
940+ }
951941
952- fn link_to < ' a , ' cfg > (
953- cmd : & mut ProcessBuilder ,
954- cx : & mut Context < ' a , ' cfg > ,
955- current : & Unit < ' a > ,
956- dep : & UnitDep < ' a > ,
957- need_unstable_opts : & mut bool ,
958- ) -> CargoResult < ( ) > {
942+ /// Generates a list of `--extern` arguments.
943+ pub fn extern_args < ' a > (
944+ cx : & Context < ' a , ' _ > ,
945+ unit : & Unit < ' a > ,
946+ unstable_opts : & mut bool ,
947+ ) -> CargoResult < Vec < OsString > > {
948+ let mut result = Vec :: new ( ) ;
949+ let deps = cx. unit_deps ( unit) ;
950+
951+ // Closure to add one dependency to `result`.
952+ let mut link_to = |dep : & UnitDep < ' a > ,
953+ extern_crate_name : InternedString ,
954+ noprelude : bool |
955+ -> CargoResult < ( ) > {
959956 let mut value = OsString :: new ( ) ;
960- value. push ( dep. extern_crate_name . as_str ( ) ) ;
957+ let mut opts = Vec :: new ( ) ;
958+ if unit
959+ . pkg
960+ . manifest ( )
961+ . features ( )
962+ . require ( Feature :: public_dependency ( ) )
963+ . is_ok ( )
964+ && !dep. public
965+ {
966+ opts. push ( "priv" ) ;
967+ * unstable_opts = true ;
968+ }
969+ if noprelude {
970+ opts. push ( "noprelude" ) ;
971+ * unstable_opts = true ;
972+ }
973+ if !opts. is_empty ( ) {
974+ value. push ( opts. join ( "," ) ) ;
975+ value. push ( ":" ) ;
976+ }
977+ value. push ( extern_crate_name. as_str ( ) ) ;
961978 value. push ( "=" ) ;
962979
963980 let mut pass = |file| {
964981 let mut value = value. clone ( ) ;
965982 value. push ( file) ;
966-
967- if current
968- . pkg
969- . manifest ( )
970- . features ( )
971- . require ( Feature :: public_dependency ( ) )
972- . is_ok ( )
973- && !dep. public
974- {
975- cmd. arg ( "--extern-private" ) ;
976- * need_unstable_opts = true ;
977- } else {
978- cmd. arg ( "--extern" ) ;
979- }
980-
981- cmd. arg ( & value) ;
983+ result. push ( OsString :: from ( "--extern" ) ) ;
984+ result. push ( value) ;
982985 } ;
983986
984987 let outputs = cx. outputs ( & dep. unit ) ?;
@@ -987,7 +990,7 @@ fn build_deps_args<'a, 'cfg>(
987990 _ => None ,
988991 } ) ;
989992
990- if cx. only_requires_rmeta ( current , & dep. unit ) {
993+ if cx. only_requires_rmeta ( unit , & dep. unit ) {
991994 let ( output, _rmeta) = outputs
992995 . find ( |( _output, rmeta) | * rmeta)
993996 . expect ( "failed to find rlib dep for pipelined dep" ) ;
@@ -1000,7 +1003,15 @@ fn build_deps_args<'a, 'cfg>(
10001003 }
10011004 }
10021005 Ok ( ( ) )
1006+ } ;
1007+
1008+ for dep in deps {
1009+ if dep. unit . target . linkable ( ) && !dep. unit . mode . is_doc ( ) {
1010+ link_to ( & dep, dep. extern_crate_name , dep. noprelude ) ?;
1011+ }
10031012 }
1013+
1014+ Ok ( result)
10041015}
10051016
10061017fn envify ( s : & str ) -> String {
0 commit comments