Skip to content

Commit d1a0119

Browse files
committed
Only pass a versioned prebuilt-modules for Mac Catalyst if it exists.
Some clients still have unversioned prebuilt modules, but SwiftDriver assumed that the versioned path would always exist for macOS (at least for the purposes of passing it when compiling for Mac Catalyst). This change duplicates some logic from the Swift frontend to check for the existence of the versioned macOS prebuilt modules directory before passing it, optionally stripping off trailing `.0` version components. This change also updates the driver so that it passes a prebuilt modules directory relative to the resource directory rather than relative to the compiler binary, also matching the Swift frontend's behavior. This addresses <rdar://problem/136047054>. (cherry picked from commit b1573ef)
1 parent 8f3f7ba commit d1a0119

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,27 @@ public final class DarwinToolchain: Toolchain {
419419
// doesn't always match the macosx sdk version so the compiler may fail to find
420420
// the prebuilt module in the versioned sub-dir.
421421
if frontendTargetInfo.target.triple.isMacCatalyst {
422+
let resourceDirPath = VirtualPath.lookup(frontendTargetInfo.runtimeResourcePath.path)
423+
let basePrebuiltModulesPath = resourceDirPath.appending(components: "macosx", "prebuilt-modules")
424+
425+
// Ensure we pass a path that exists. This matches logic used in the Swift frontend.
426+
let prebuiltModulesPath: VirtualPath = try {
427+
var versionString = sdkInfo.versionString
428+
repeat {
429+
let versionedPrebuiltModulesPath =
430+
basePrebuiltModulesPath.appending(component: versionString)
431+
if try fileSystem.exists(versionedPrebuiltModulesPath) {
432+
return versionedPrebuiltModulesPath
433+
} else if versionString.hasSuffix(".0") {
434+
versionString.removeLast(2)
435+
} else {
436+
return basePrebuiltModulesPath
437+
}
438+
} while true
439+
}()
440+
422441
commandLine.appendFlag(.prebuiltModuleCachePath)
423-
commandLine.appendPath(try getToolPath(.swiftCompiler).parentDirectory/*bin*/
424-
.parentDirectory/*usr*/
425-
.appending(component: "lib").appending(component: "swift")
426-
.appending(component: "macosx").appending(component: "prebuilt-modules")
427-
.appending(component: sdkInfo.versionString))
442+
commandLine.appendPath(prebuiltModulesPath)
428443
}
429444

430445
// Pass down -clang-target.

TestInputs/PrebuiltModules-macOS10.15.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/10.15/.empty-file

Whitespace-only changes.

TestInputs/PrebuiltModules-macOSUnversioned.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/.empty-file

Whitespace-only changes.

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7029,14 +7029,34 @@ final class SwiftDriverTests: XCTestCase {
70297029
try testInputsPath.appending(component: "mock-sdk.sdk").nativePathString(escaped: false)
70307030

70317031
do {
7032-
var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath],
7032+
let resourceDirPath: String = try testInputsPath.appending(components: "PrebuiltModules-macOS10.15.xctoolchain", "usr", "lib", "swift").nativePathString(escaped: false)
7033+
7034+
var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath, "-resource-dir", resourceDirPath],
7035+
env: envVars)
7036+
let plannedJobs = try driver.planBuild()
7037+
let job = plannedJobs[0]
7038+
XCTAssertTrue(job.commandLine.contains(.flag("-prebuilt-module-cache-path")))
7039+
XCTAssertTrue(job.commandLine.contains { arg in
7040+
if case .path(let curPath) = arg {
7041+
if curPath.basename == "10.15" && curPath.parentDirectory.basename == "prebuilt-modules" && curPath.parentDirectory.parentDirectory.basename == "macosx" {
7042+
return true
7043+
}
7044+
}
7045+
return false
7046+
})
7047+
}
7048+
7049+
do {
7050+
let resourceDirPath: String = try testInputsPath.appending(components: "PrebuiltModules-macOSUnversioned.xctoolchain", "usr", "lib", "swift").nativePathString(escaped: false)
7051+
7052+
var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath, "-resource-dir", resourceDirPath],
70337053
env: envVars)
70347054
let plannedJobs = try driver.planBuild()
70357055
let job = plannedJobs[0]
70367056
XCTAssertTrue(job.commandLine.contains(.flag("-prebuilt-module-cache-path")))
70377057
XCTAssertTrue(job.commandLine.contains { arg in
70387058
if case .path(let curPath) = arg {
7039-
if curPath.basename == "10.15" && curPath.parentDirectory.basename == "prebuilt-modules" {
7059+
if curPath.basename == "prebuilt-modules" && curPath.parentDirectory.basename == "macosx" {
70407060
return true
70417061
}
70427062
}

0 commit comments

Comments
 (0)