From d1a01193ed918196d067b16c3e06b3defbf06af3 Mon Sep 17 00:00:00 2001 From: Connor Wakamo Date: Sun, 15 Sep 2024 17:31:27 -0700 Subject: [PATCH] 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 . (cherry picked from commit b1573ef55119629057a57591916ff4d6d7d5af9e) --- .../Toolchains/DarwinToolchain.swift | 25 +++++++++++++++---- .../macosx/prebuilt-modules/10.15/.empty-file | 0 .../swift/macosx/prebuilt-modules/.empty-file | 0 Tests/SwiftDriverTests/SwiftDriverTests.swift | 24 ++++++++++++++++-- 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 TestInputs/PrebuiltModules-macOS10.15.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/10.15/.empty-file create mode 100644 TestInputs/PrebuiltModules-macOSUnversioned.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/.empty-file 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 } }