@@ -1189,6 +1189,74 @@ fn add_post_link_objects(cmd: &mut dyn Linker, sess: &'a Session, crate_type: co
11891189 }
11901190}
11911191
1192+ /// Add arbitrary "pre-link" args defined by the target spec or from command line.
1193+ /// FIXME: Determine where exactly these args need to be inserted.
1194+ fn add_pre_link_args (
1195+ cmd : & mut dyn Linker ,
1196+ sess : & ' a Session ,
1197+ flavor : LinkerFlavor ,
1198+ crate_type : config:: CrateType ,
1199+ ) {
1200+ if let Some ( args) = sess. target . target . options . pre_link_args . get ( & flavor) {
1201+ cmd. args ( args) ;
1202+ }
1203+ if let Some ( args) = sess. target . target . options . pre_link_args_crt . get ( & flavor) {
1204+ if sess. crt_static ( Some ( crate_type) ) {
1205+ cmd. args ( args) ;
1206+ }
1207+ }
1208+ cmd. args ( & sess. opts . debugging_opts . pre_link_args ) ;
1209+ }
1210+
1211+ /// Add arbitrary "user defined" args defined from command line and by `#[link_args]` attributes.
1212+ /// FIXME: Determine where exactly these args need to be inserted.
1213+ fn add_user_defined_link_args (
1214+ cmd : & mut dyn Linker ,
1215+ sess : & ' a Session ,
1216+ codegen_results : & CodegenResults ,
1217+ ) {
1218+ cmd. args ( & sess. opts . cg . link_args ) ;
1219+ cmd. args ( & * codegen_results. crate_info . link_args ) ;
1220+ }
1221+
1222+ /// Add arbitrary "late link" args defined by the target spec.
1223+ /// FIXME: Determine where exactly these args need to be inserted.
1224+ fn add_late_link_args (
1225+ cmd : & mut dyn Linker ,
1226+ sess : & ' a Session ,
1227+ flavor : LinkerFlavor ,
1228+ crate_type : config:: CrateType ,
1229+ codegen_results : & CodegenResults ,
1230+ ) {
1231+ if let Some ( args) = sess. target . target . options . late_link_args . get ( & flavor) {
1232+ cmd. args ( args) ;
1233+ }
1234+ let any_dynamic_crate = crate_type == config:: CrateType :: Dylib
1235+ || codegen_results. crate_info . dependency_formats . iter ( ) . any ( |( ty, list) | {
1236+ * ty == crate_type && list. iter ( ) . any ( |& linkage| linkage == Linkage :: Dynamic )
1237+ } ) ;
1238+ if any_dynamic_crate {
1239+ if let Some ( args) = sess. target . target . options . late_link_args_dynamic . get ( & flavor) {
1240+ cmd. args ( args) ;
1241+ }
1242+ } else {
1243+ if let Some ( args) = sess. target . target . options . late_link_args_static . get ( & flavor) {
1244+ cmd. args ( args) ;
1245+ }
1246+ }
1247+ }
1248+
1249+ /// Add arbitrary "post-link" args defined by the target spec.
1250+ /// FIXME: Determine where exactly these args need to be inserted.
1251+ fn add_post_link_args ( cmd : & mut dyn Linker , sess : & ' a Session , flavor : LinkerFlavor ) {
1252+ if let Some ( args) = sess. target . target . options . post_link_args . get ( & flavor) {
1253+ cmd. args ( args) ;
1254+ }
1255+ }
1256+
1257+ /// Produce the linker command line containing linker path and arguments.
1258+ /// `NO-OPT-OUT` marks the arguments that cannot be removed from the command line
1259+ /// by the user without creating a custom target specification.
11921260fn linker_with_args < ' a , B : ArchiveBuilder < ' a > > (
11931261 path : & Path ,
11941262 flavor : LinkerFlavor ,
@@ -1205,15 +1273,8 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
12051273 assert ! ( base_cmd. get_args( ) . is_empty( ) || sess. target. target. target_vendor == "uwp" ) ;
12061274 let cmd = & mut * codegen_results. linker_info . to_linker ( base_cmd, & sess, flavor, target_cpu) ;
12071275
1208- if let Some ( args) = sess. target . target . options . pre_link_args . get ( & flavor) {
1209- cmd. args ( args) ;
1210- }
1211- if let Some ( args) = sess. target . target . options . pre_link_args_crt . get ( & flavor) {
1212- if sess. crt_static ( Some ( crate_type) ) {
1213- cmd. args ( args) ;
1214- }
1215- }
1216- cmd. args ( & sess. opts . debugging_opts . pre_link_args ) ;
1276+ // NO-OPT-OUT
1277+ add_pre_link_args ( cmd, sess, flavor, crate_type) ;
12171278
12181279 if sess. target . target . options . is_like_fuchsia {
12191280 let prefix = match sess. opts . debugging_opts . sanitizer {
@@ -1294,16 +1355,19 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
12941355 cmd. gc_sections ( keep_metadata) ;
12951356 }
12961357
1297- let attr_link_args = codegen_results. crate_info . link_args . iter ( ) ;
1298- let user_link_args = sess. opts . cg . link_args . iter ( ) . chain ( attr_link_args) ;
1299-
13001358 if crate_type == config:: CrateType :: Executable {
13011359 let mut position_independent_executable = false ;
13021360
13031361 if t. options . position_independent_executables {
13041362 if is_pic ( sess)
13051363 && !sess. crt_static ( Some ( crate_type) )
1306- && !user_link_args. clone ( ) . any ( |x| x == "-static" )
1364+ && !sess
1365+ . opts
1366+ . cg
1367+ . link_args
1368+ . iter ( )
1369+ . chain ( & * codegen_results. crate_info . link_args )
1370+ . any ( |x| x == "-static" )
13071371 {
13081372 position_independent_executable = true ;
13091373 }
@@ -1432,35 +1496,18 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
14321496 cmd. args ( & rpath:: get_rpath_flags ( & mut rpath_config) ) ;
14331497 }
14341498
1435- // Finally add all the linker arguments provided on the command line along
1436- // with any #[link_args] attributes found inside the crate
1437- cmd. args ( user_link_args) ;
1499+ add_user_defined_link_args ( cmd, sess, codegen_results) ;
14381500
14391501 cmd. finalize ( ) ;
14401502
1441- if let Some ( args) = sess. target . target . options . late_link_args . get ( & flavor) {
1442- cmd. args ( args) ;
1443- }
1444- let any_dynamic_crate = crate_type == config:: CrateType :: Dylib
1445- || codegen_results. crate_info . dependency_formats . iter ( ) . any ( |( ty, list) | {
1446- * ty == crate_type && list. iter ( ) . any ( |& linkage| linkage == Linkage :: Dynamic )
1447- } ) ;
1448- if any_dynamic_crate {
1449- if let Some ( args) = sess. target . target . options . late_link_args_dynamic . get ( & flavor) {
1450- cmd. args ( args) ;
1451- }
1452- } else {
1453- if let Some ( args) = sess. target . target . options . late_link_args_static . get ( & flavor) {
1454- cmd. args ( args) ;
1455- }
1456- }
1503+ // NO-OPT-OUT
1504+ add_late_link_args ( cmd, sess, flavor, crate_type, codegen_results) ;
14571505
14581506 // NO-OPT-OUT
14591507 add_post_link_objects ( cmd, sess, crate_type) ;
14601508
1461- if let Some ( args) = sess. target . target . options . post_link_args . get ( & flavor) {
1462- cmd. args ( args) ;
1463- }
1509+ // NO-OPT-OUT
1510+ add_post_link_args ( cmd, sess, flavor) ;
14641511
14651512 cmd. take_cmd ( )
14661513}
0 commit comments