diff --git a/Sources/SwiftDriver/Toolchains/DarwinToolchain.swift b/Sources/SwiftDriver/Toolchains/DarwinToolchain.swift index f5a26e92d..18373ec00 100644 --- a/Sources/SwiftDriver/Toolchains/DarwinToolchain.swift +++ b/Sources/SwiftDriver/Toolchains/DarwinToolchain.swift @@ -419,12 +419,27 @@ public final class DarwinToolchain: Toolchain { // doesn't always match the macosx sdk version so the compiler may fail to find // the prebuilt module in the versioned sub-dir. if frontendTargetInfo.target.triple.isMacCatalyst { + let resourceDirPath = VirtualPath.lookup(frontendTargetInfo.runtimeResourcePath.path) + let basePrebuiltModulesPath = resourceDirPath.appending(components: "macosx", "prebuilt-modules") + + // Ensure we pass a path that exists. This matches logic used in the Swift frontend. + let prebuiltModulesPath: VirtualPath = try { + var versionString = sdkInfo.versionString + repeat { + let versionedPrebuiltModulesPath = + basePrebuiltModulesPath.appending(component: versionString) + if try fileSystem.exists(versionedPrebuiltModulesPath) { + return versionedPrebuiltModulesPath + } else if versionString.hasSuffix(".0") { + versionString.removeLast(2) + } else { + return basePrebuiltModulesPath + } + } while true + }() + commandLine.appendFlag(.prebuiltModuleCachePath) - commandLine.appendPath(try getToolPath(.swiftCompiler).parentDirectory/*bin*/ - .parentDirectory/*usr*/ - .appending(component: "lib").appending(component: "swift") - .appending(component: "macosx").appending(component: "prebuilt-modules") - .appending(component: sdkInfo.versionString)) + commandLine.appendPath(prebuiltModulesPath) } // Pass down -clang-target. diff --git a/TestInputs/PrebuiltModules-macOS10.15.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/10.15/.empty-file b/TestInputs/PrebuiltModules-macOS10.15.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/10.15/.empty-file new file mode 100644 index 000000000..e69de29bb diff --git a/TestInputs/PrebuiltModules-macOSUnversioned.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/.empty-file b/TestInputs/PrebuiltModules-macOSUnversioned.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/.empty-file new file mode 100644 index 000000000..e69de29bb diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index dc39711d4..a87c5e617 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -7029,14 +7029,34 @@ final class SwiftDriverTests: XCTestCase { try testInputsPath.appending(component: "mock-sdk.sdk").nativePathString(escaped: false) do { - var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath], + let resourceDirPath: String = try testInputsPath.appending(components: "PrebuiltModules-macOS10.15.xctoolchain", "usr", "lib", "swift").nativePathString(escaped: false) + + var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath, "-resource-dir", resourceDirPath], + env: envVars) + let plannedJobs = try driver.planBuild() + let job = plannedJobs[0] + XCTAssertTrue(job.commandLine.contains(.flag("-prebuilt-module-cache-path"))) + XCTAssertTrue(job.commandLine.contains { arg in + if case .path(let curPath) = arg { + if curPath.basename == "10.15" && curPath.parentDirectory.basename == "prebuilt-modules" && curPath.parentDirectory.parentDirectory.basename == "macosx" { + return true + } + } + return false + }) + } + + do { + let resourceDirPath: String = try testInputsPath.appending(components: "PrebuiltModules-macOSUnversioned.xctoolchain", "usr", "lib", "swift").nativePathString(escaped: false) + + var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-ios13.1-macabi", "foo.swift", "-sdk", mockSDKPath, "-resource-dir", resourceDirPath], env: envVars) let plannedJobs = try driver.planBuild() let job = plannedJobs[0] XCTAssertTrue(job.commandLine.contains(.flag("-prebuilt-module-cache-path"))) XCTAssertTrue(job.commandLine.contains { arg in if case .path(let curPath) = arg { - if curPath.basename == "10.15" && curPath.parentDirectory.basename == "prebuilt-modules" { + if curPath.basename == "prebuilt-modules" && curPath.parentDirectory.basename == "macosx" { return true } }