Skip to content

Commit b1573ef

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>.
1 parent 0155ae0 commit b1573ef

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

+20-5
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,27 @@ public final class DarwinToolchain: Toolchain {
410410
// doesn't always match the macosx sdk version so the compiler may fail to find
411411
// the prebuilt module in the versioned sub-dir.
412412
if frontendTargetInfo.target.triple.isMacCatalyst {
413+
let resourceDirPath = VirtualPath.lookup(frontendTargetInfo.runtimeResourcePath.path)
414+
let basePrebuiltModulesPath = resourceDirPath.appending(components: "macosx", "prebuilt-modules")
415+
416+
// Ensure we pass a path that exists. This matches logic used in the Swift frontend.
417+
let prebuiltModulesPath: VirtualPath = try {
418+
var versionString = sdkInfo.versionString
419+
repeat {
420+
let versionedPrebuiltModulesPath =
421+
basePrebuiltModulesPath.appending(component: versionString)
422+
if try fileSystem.exists(versionedPrebuiltModulesPath) {
423+
return versionedPrebuiltModulesPath
424+
} else if versionString.hasSuffix(".0") {
425+
versionString.removeLast(2)
426+
} else {
427+
return basePrebuiltModulesPath
428+
}
429+
} while true
430+
}()
431+
413432
commandLine.appendFlag(.prebuiltModuleCachePath)
414-
commandLine.appendPath(try getToolPath(.swiftCompiler).parentDirectory/*bin*/
415-
.parentDirectory/*usr*/
416-
.appending(component: "lib").appending(component: "swift")
417-
.appending(component: "macosx").appending(component: "prebuilt-modules")
418-
.appending(component: sdkInfo.versionString))
433+
commandLine.appendPath(prebuiltModulesPath)
419434
}
420435

421436
// 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

+22-2
Original file line numberDiff line numberDiff line change
@@ -7172,14 +7172,34 @@ final class SwiftDriverTests: XCTestCase {
71727172
try testInputsPath.appending(component: "mock-sdk.sdk").nativePathString(escaped: false)
71737173

71747174
do {
7175-
var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath],
7175+
let resourceDirPath: String = try testInputsPath.appending(components: "PrebuiltModules-macOS10.15.xctoolchain", "usr", "lib", "swift").nativePathString(escaped: false)
7176+
7177+
var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath, "-resource-dir", resourceDirPath],
7178+
env: envVars)
7179+
let plannedJobs = try driver.planBuild()
7180+
let job = plannedJobs[0]
7181+
XCTAssertTrue(job.commandLine.contains(.flag("-prebuilt-module-cache-path")))
7182+
XCTAssertTrue(job.commandLine.contains { arg in
7183+
if case .path(let curPath) = arg {
7184+
if curPath.basename == "10.15" && curPath.parentDirectory.basename == "prebuilt-modules" && curPath.parentDirectory.parentDirectory.basename == "macosx" {
7185+
return true
7186+
}
7187+
}
7188+
return false
7189+
})
7190+
}
7191+
7192+
do {
7193+
let resourceDirPath: String = try testInputsPath.appending(components: "PrebuiltModules-macOSUnversioned.xctoolchain", "usr", "lib", "swift").nativePathString(escaped: false)
7194+
7195+
var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath, "-resource-dir", resourceDirPath],
71767196
env: envVars)
71777197
let plannedJobs = try driver.planBuild()
71787198
let job = plannedJobs[0]
71797199
XCTAssertTrue(job.commandLine.contains(.flag("-prebuilt-module-cache-path")))
71807200
XCTAssertTrue(job.commandLine.contains { arg in
71817201
if case .path(let curPath) = arg {
7182-
if curPath.basename == "10.15" && curPath.parentDirectory.basename == "prebuilt-modules" {
7202+
if curPath.basename == "prebuilt-modules" && curPath.parentDirectory.basename == "macosx" {
71837203
return true
71847204
}
71857205
}

0 commit comments

Comments
 (0)