diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber.swift b/Sources/XToolSupport/SwiftPMBuildDescriber.swift index 3cfc986f..96ead30f 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber.swift @@ -158,6 +158,26 @@ struct SwiftPMBuildDescriber { "$(inherited)", "@executable_path/../Frameworks", "@executable_path/../../../../Frameworks", ] } + #if !os(macOS) + if !product.isExtension { + // swiftc links through clang but only passes --sysroot, which clang's + // Darwin driver doesn't use to derive the SDK version for + // LC_BUILD_VERSION.sdk (it falls back to the deployment target). + // Pass -isysroot explicitly so the recorded SDK matches the one we + // linked against. Only the app needs this: extensions link via clang, + // which already receives -isysroot from Swift Build. + // + // LINKER_DRIVER is pinned to swiftc so that -Xclang-linker is always + // valid here: "auto" would pick clang for packages without Swift, + // which doesn't understand the flag (and doesn't need it anyway). + settings[single: "LINKER_DRIVER"] = "swiftc" + settings[.OTHER_LDFLAGS] = [ + "$(inherited)", + "-Xclang-linker", "-isysroot", + "-Xclang-linker", "$(SDKROOT)", + ] + } + #endif return settings } diff --git a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift index c1a52d2f..cb217ba9 100644 --- a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift +++ b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift @@ -219,6 +219,35 @@ struct SwiftPMBuildDescriberTests { #expect(!FileManager.default.fileExists(atPath: root.appending(path: "xtool").path)) } + #if !os(macOS) + @Test func appTargetPassesIsysrootToSwiftDriverLink() throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + try writePlist(["NSExtension": ["NSExtensionPointIdentifier": "com.apple.widgetkit-extension"]], + to: root.appending(path: "Widget.plist")) + let schema = try PackSchema(validating: .init( + version: .v1, orgID: "com.example", product: "App", + extensions: [.init(product: "Widget", infoPath: "Widget.plist")] + )) + let describer = describer(schema: schema, root: root) + let description = try describer.describe(basePIF: pif(root: root, products: ["App", "Widget"])) + let targets = description.workspace.projects.flatMap(\.targets) + let app = try #require(targets.first { $0.id == "xtool:main-app" }) + let widget = try #require(targets.first { $0.id == "xtool:extension:Widget" }) + let expected: [String] = [ + "$(inherited)", "-Xclang-linker", "-isysroot", "-Xclang-linker", "$(SDKROOT)", + ] + for config in app.common.buildConfigs { + expectNoDifference(config.settings[.OTHER_LDFLAGS], expected) + expectNoDifference(config.settings[.LINKER_DRIVER], "swiftc") + } + // extensions link through clang, which already receives -isysroot + for config in widget.common.buildConfigs { + #expect(config.settings[.OTHER_LDFLAGS]?.contains("-isysroot") != true) + } + } + #endif + @Test(arguments: [nil, "App", "Tool"] as [String?]) func selectsLibrariesAlongsideExecutables(expected: String?) async throws { let root = try temporaryDirectory()