Skip to content

Commit 32fb825

Browse files
authored
Include rpath for back-deployed concurrency on macOS prior to 12.0. (#3845)
When building an executable or test target for macOS prior to 12.0, add an additional rpath to point into the toolchain's copy of the back-deployed concurrency shared libraries. This allows `swift build` to build executables that can run on the same macOS that predates macOS 12.0. Fixes rdar://problem/85209998 / SR-15445.
1 parent 0ff3b18 commit 32fb825

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,19 @@ public final class ProductBuildDescription {
12911291
let stdlib = buildParameters.toolchain.macosSwiftStdlib
12921292
args += ["-Xlinker", "-rpath", "-Xlinker", stdlib.pathString]
12931293
}
1294+
1295+
// When deploying to macOS prior to macOS 12, add an rpath to the
1296+
// back-deployed concurrency libraries.
1297+
if buildParameters.triple.isDarwin(),
1298+
let macOSSupportedPlatform = product.targets[0].underlyingTarget.getSupportedPlatform(for: .macOS),
1299+
macOSSupportedPlatform.version.major < 12 {
1300+
let backDeployedStdlib = buildParameters.toolchain.macosSwiftStdlib
1301+
.parentDirectory
1302+
.parentDirectory
1303+
.appending(component: "swift-5.5")
1304+
.appending(component: "macosx")
1305+
args += ["-Xlinker", "-rpath", "-Xlinker", backDeployedStdlib.pathString]
1306+
}
12941307
}
12951308

12961309
// Don't link runtime compatibility patch libraries if there are no

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ final class BuildPlanTests: XCTestCase {
147147
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
148148
"@/path/to/build/debug/exe.product/Objects.LinkFileList",
149149
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
150+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
150151
"-target", defaultTargetTriple, "-Xlinker", "-add_ast_path",
151152
"-Xlinker", "/path/to/build/debug/exe.build/exe.swiftmodule", "-Xlinker", "-add_ast_path",
152153
"-Xlinker", "/path/to/build/debug/lib.swiftmodule",
@@ -465,6 +466,7 @@ final class BuildPlanTests: XCTestCase {
465466
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
466467
"@/path/to/build/release/exe.product/Objects.LinkFileList",
467468
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
469+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
468470
"-target", defaultTargetTriple,
469471
])
470472
#else
@@ -802,6 +804,7 @@ final class BuildPlanTests: XCTestCase {
802804
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
803805
"@/path/to/build/debug/exe.product/Objects.LinkFileList",
804806
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
807+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
805808
"-target", defaultTargetTriple,
806809
"-Xlinker", "-add_ast_path", "-Xlinker", "/path/to/build/debug/exe.build/exe.swiftmodule",
807810
])
@@ -967,6 +970,7 @@ final class BuildPlanTests: XCTestCase {
967970
"-Xlinker", "-rpath", "-Xlinker", "@loader_path/../../../",
968971
"@/path/to/build/debug/PkgPackageTests.product/Objects.LinkFileList",
969972
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
973+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
970974
"-target", "\(hostTriple.tripleString(forPlatformVersion: version))",
971975
"-Xlinker", "-add_ast_path", "-Xlinker", "/path/to/build/debug/Foo.swiftmodule",
972976
"-Xlinker", "-add_ast_path", "-Xlinker", "/path/to/build/debug/FooTests.swiftmodule",
@@ -982,6 +986,54 @@ final class BuildPlanTests: XCTestCase {
982986
#endif
983987
}
984988

989+
func testConcurrencyInOS() throws {
990+
let fs = InMemoryFileSystem(emptyFiles:
991+
"/Pkg/Sources/exe/main.swift"
992+
)
993+
994+
let observability = ObservabilitySystem.makeForTesting()
995+
let graph = try loadPackageGraph(
996+
fs: fs,
997+
manifests: [
998+
Manifest.createRootManifest(
999+
name: "Pkg",
1000+
path: .init("/Pkg"),
1001+
platforms: [
1002+
PlatformDescription(name: "macos", version: "12.0"),
1003+
],
1004+
targets: [
1005+
TargetDescription(name: "exe", dependencies: []),
1006+
]),
1007+
],
1008+
observabilityScope: observability.topScope
1009+
)
1010+
XCTAssertNoDiagnostics(observability.diagnostics)
1011+
1012+
let result = BuildPlanResult(plan: try BuildPlan(
1013+
buildParameters: mockBuildParameters(config: .release),
1014+
graph: graph,
1015+
fileSystem: fs,
1016+
observabilityScope: observability.topScope
1017+
))
1018+
1019+
result.checkProductsCount(1)
1020+
result.checkTargetsCount(1)
1021+
1022+
let exe = try result.target(for: "exe").swiftTarget().compileArguments()
1023+
XCTAssertMatch(exe, ["-swift-version", "4", "-O", "-g", .equal(j), "-DSWIFT_PACKAGE", "-module-cache-path", "/path/to/build/release/ModuleCache", .anySequence])
1024+
1025+
#if os(macOS)
1026+
XCTAssertEqual(try result.buildProduct(for: "exe").linkArguments(), [
1027+
"/fake/path/to/swiftc", "-g", "-L", "/path/to/build/release",
1028+
"-o", "/path/to/build/release/exe", "-module-name", "exe", "-emit-executable",
1029+
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
1030+
"@/path/to/build/release/exe.product/Objects.LinkFileList",
1031+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
1032+
"-target", hostTriple.tripleString(forPlatformVersion: "12.0"),
1033+
])
1034+
#endif
1035+
}
1036+
9851037
func testParseAsLibraryFlagForExe() throws {
9861038
let fs = InMemoryFileSystem(emptyFiles:
9871039
// First executable has a single source file not named `main.swift`.
@@ -1082,6 +1134,7 @@ final class BuildPlanTests: XCTestCase {
10821134
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
10831135
"@/path/to/build/debug/exe.product/Objects.LinkFileList",
10841136
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
1137+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
10851138
"-target", defaultTargetTriple,
10861139
"-Xlinker", "-add_ast_path", "-Xlinker", "/path/to/build/debug/exe.build/exe.swiftmodule",
10871140
])
@@ -1188,6 +1241,7 @@ final class BuildPlanTests: XCTestCase {
11881241
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
11891242
"@/path/to/build/debug/Foo.product/Objects.LinkFileList",
11901243
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
1244+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
11911245
"-target", defaultTargetTriple,
11921246
"-Xlinker", "-add_ast_path", "-Xlinker", "/path/to/build/debug/Foo.build/Foo.swiftmodule"
11931247
])
@@ -1200,6 +1254,7 @@ final class BuildPlanTests: XCTestCase {
12001254
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
12011255
"@/path/to/build/debug/Bar-Baz.product/Objects.LinkFileList",
12021256
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
1257+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
12031258
"-target", defaultTargetTriple,
12041259
"-Xlinker", "-add_ast_path", "-Xlinker", "/path/to/build/debug/Bar.swiftmodule"
12051260
])
@@ -1283,6 +1338,7 @@ final class BuildPlanTests: XCTestCase {
12831338
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
12841339
"@/path/to/build/debug/lib.product/Objects.LinkFileList",
12851340
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
1341+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
12861342
"-target", defaultTargetTriple,
12871343
"-Xlinker", "-add_ast_path", "-Xlinker", "/path/to/build/debug/lib.swiftmodule",
12881344
]

0 commit comments

Comments
 (0)