@@ -1390,20 +1390,49 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
1390
1390
Arguments.push_back (context.Args .MakeArgString (Target));
1391
1391
}
1392
1392
1393
+ bool staticExecutable = false ;
1394
+ bool staticStdlib = false ;
1395
+
1396
+ if (context.Args .hasFlag (options::OPT_static_executable,
1397
+ options::OPT_no_static_executable,
1398
+ false )) {
1399
+ staticExecutable = true ;
1400
+ } else if (context.Args .hasFlag (options::OPT_static_stdlib,
1401
+ options::OPT_no_static_stdlib,
1402
+ false )) {
1403
+ staticStdlib = true ;
1404
+ }
1405
+
1406
+ SmallString<128 > SharedRuntimeLibPath;
1407
+ SmallString<128 > StaticRuntimeLibPath;
1408
+ // Path to swift_begin.o and swift_end.o.
1409
+ SmallString<128 > ObjectLibPath;
1410
+ getRuntimeLibraryPath (SharedRuntimeLibPath, context.Args , *this );
1411
+
1412
+ // -static-stdlib uses the static lib path for libswiftCore but
1413
+ // the shared lib path for swift_begin.o and swift_end.o.
1414
+ if (staticExecutable || staticStdlib) {
1415
+ getRuntimeStaticLibraryPath (StaticRuntimeLibPath, context.Args , *this );
1416
+ }
1417
+
1418
+ if (staticExecutable) {
1419
+ ObjectLibPath = StaticRuntimeLibPath;
1420
+ } else {
1421
+ ObjectLibPath = SharedRuntimeLibPath;
1422
+ }
1423
+
1393
1424
// Add the runtime library link path, which is platform-specific and found
1394
1425
// relative to the compiler.
1395
- llvm::SmallString<128 > RuntimeLibPath;
1396
- getRuntimeLibraryPath (RuntimeLibPath, context.Args , *this );
1397
- if (shouldProvideRPathToLinker ()) {
1426
+ if (!staticExecutable && shouldProvideRPathToLinker ()) {
1398
1427
// FIXME: We probably shouldn't be adding an rpath here unless we know
1399
1428
// ahead of time the standard library won't be copied.
1400
1429
Arguments.push_back (" -Xlinker" );
1401
1430
Arguments.push_back (" -rpath" );
1402
1431
Arguments.push_back (" -Xlinker" );
1403
- Arguments.push_back (context.Args .MakeArgString (RuntimeLibPath ));
1432
+ Arguments.push_back (context.Args .MakeArgString (SharedRuntimeLibPath ));
1404
1433
}
1405
1434
1406
- auto PreInputObjectPath = getPreInputObjectPath (RuntimeLibPath );
1435
+ auto PreInputObjectPath = getPreInputObjectPath (ObjectLibPath );
1407
1436
if (!PreInputObjectPath.empty ()) {
1408
1437
Arguments.push_back (context.Args .MakeArgString (PreInputObjectPath));
1409
1438
}
@@ -1421,11 +1450,8 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
1421
1450
1422
1451
// Link the standard library.
1423
1452
Arguments.push_back (" -L" );
1424
- if (context.Args .hasFlag (options::OPT_static_executable,
1425
- options::OPT_no_static_executable,
1426
- false )) {
1427
- SmallString<128 > StaticRuntimeLibPath;
1428
- getRuntimeStaticLibraryPath (StaticRuntimeLibPath, context.Args , *this );
1453
+
1454
+ if (staticExecutable) {
1429
1455
Arguments.push_back (context.Args .MakeArgString (StaticRuntimeLibPath));
1430
1456
1431
1457
SmallString<128 > linkFilePath = StaticRuntimeLibPath;
@@ -1438,11 +1464,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
1438
1464
llvm::report_fatal_error (" -static-executable not supported on this platform" );
1439
1465
}
1440
1466
}
1441
- else if (context.Args .hasFlag (options::OPT_static_stdlib,
1442
- options::OPT_no_static_stdlib,
1443
- false )) {
1444
- SmallString<128 > StaticRuntimeLibPath;
1445
- getRuntimeStaticLibraryPath (StaticRuntimeLibPath, context.Args , *this );
1467
+ else if (staticStdlib) {
1446
1468
Arguments.push_back (context.Args .MakeArgString (StaticRuntimeLibPath));
1447
1469
1448
1470
SmallString<128 > linkFilePath = StaticRuntimeLibPath;
@@ -1455,7 +1477,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
1455
1477
}
1456
1478
}
1457
1479
else {
1458
- Arguments.push_back (context.Args .MakeArgString (RuntimeLibPath ));
1480
+ Arguments.push_back (context.Args .MakeArgString (SharedRuntimeLibPath ));
1459
1481
Arguments.push_back (" -lswiftCore" );
1460
1482
}
1461
1483
@@ -1464,7 +1486,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
1464
1486
Arguments.push_back (context.Args .MakeArgString (" --target=" + getTriple ().str ()));
1465
1487
1466
1488
if (context.Args .hasArg (options::OPT_profile_generate)) {
1467
- SmallString<128 > LibProfile (RuntimeLibPath );
1489
+ SmallString<128 > LibProfile (SharedRuntimeLibPath );
1468
1490
llvm::sys::path::remove_filename (LibProfile); // remove platform name
1469
1491
llvm::sys::path::append (LibProfile, " clang" , " lib" );
1470
1492
@@ -1488,7 +1510,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
1488
1510
1489
1511
// Just before the output option, allow GenericUnix toolchains to add
1490
1512
// additional inputs.
1491
- auto PostInputObjectPath = getPostInputObjectPath (RuntimeLibPath );
1513
+ auto PostInputObjectPath = getPostInputObjectPath (ObjectLibPath );
1492
1514
if (!PostInputObjectPath.empty ()) {
1493
1515
Arguments.push_back (context.Args .MakeArgString (PostInputObjectPath));
1494
1516
}
0 commit comments