@@ -1189,6 +1189,74 @@ fn add_post_link_objects(cmd: &mut dyn Linker, sess: &'a Session, crate_type: co
1189
1189
}
1190
1190
}
1191
1191
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.
1192
1260
fn linker_with_args < ' a , B : ArchiveBuilder < ' a > > (
1193
1261
path : & Path ,
1194
1262
flavor : LinkerFlavor ,
@@ -1205,15 +1273,8 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1205
1273
assert ! ( base_cmd. get_args( ) . is_empty( ) || sess. target. target. target_vendor == "uwp" ) ;
1206
1274
let cmd = & mut * codegen_results. linker_info . to_linker ( base_cmd, & sess, flavor, target_cpu) ;
1207
1275
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) ;
1217
1278
1218
1279
if sess. target . target . options . is_like_fuchsia {
1219
1280
let prefix = match sess. opts . debugging_opts . sanitizer {
@@ -1294,16 +1355,19 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1294
1355
cmd. gc_sections ( keep_metadata) ;
1295
1356
}
1296
1357
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
-
1300
1358
if crate_type == config:: CrateType :: Executable {
1301
1359
let mut position_independent_executable = false ;
1302
1360
1303
1361
if t. options . position_independent_executables {
1304
1362
if is_pic ( sess)
1305
1363
&& !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" )
1307
1371
{
1308
1372
position_independent_executable = true ;
1309
1373
}
@@ -1432,35 +1496,18 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1432
1496
cmd. args ( & rpath:: get_rpath_flags ( & mut rpath_config) ) ;
1433
1497
}
1434
1498
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) ;
1438
1500
1439
1501
cmd. finalize ( ) ;
1440
1502
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) ;
1457
1505
1458
1506
// NO-OPT-OUT
1459
1507
add_post_link_objects ( cmd, sess, crate_type) ;
1460
1508
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) ;
1464
1511
1465
1512
cmd. take_cmd ( )
1466
1513
}
0 commit comments