Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Sources/XToolSupport/SwiftPMBuildDescriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
29 changes: 29 additions & 0 deletions Tests/XToolTests/SwiftPMBuildDescriberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down