Skip to content
Merged
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
92 changes: 85 additions & 7 deletions Sources/PackLib/AppleBuildDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public struct AppleBuildDestination: Sendable, Codable, Equatable {
case .iosDevice: "ios"
}
}

public var clangRuntimeSuffix: String {
switch self {
case .iosSimulator: "iossim"
case .iosDevice: "ios"
}
}
}

public let triple: String
Expand All @@ -37,6 +44,7 @@ public struct AppleBuildDestination: Sendable, Codable, Equatable {
public let toolsetBinPath: String?
public let swiftResourcesPath: String?
public let swiftPlatformLibPath: String?
public let clangResourceDirPath: String?

public var isSimulator: Bool {
platform == .iosSimulator
Expand Down Expand Up @@ -94,7 +102,8 @@ public struct AppleBuildDestination: Sendable, Codable, Equatable {
sdkRootPath: paths.sdkRoot?.path,
toolsetBinPath: paths.toolsetBin?.path,
swiftResourcesPath: paths.swiftResources?.path,
swiftPlatformLibPath: paths.swiftPlatformLib?.path
swiftPlatformLibPath: paths.swiftPlatformLib?.path,
clangResourceDirPath: paths.clangResourceDir?.path
)
}

Expand Down Expand Up @@ -122,6 +131,7 @@ public struct AppleBuildDestination: Sendable, Codable, Equatable {
let toolsetBin: URL?
let swiftResources: URL?
let swiftPlatformLib: URL?
let clangResourceDir: URL?
}

private struct SwiftSDKManifest: Decodable {
Expand All @@ -141,7 +151,13 @@ public struct AppleBuildDestination: Sendable, Codable, Equatable {
#if os(macOS)
return resolveMacOSHostSDKPaths(platform: platform)
#else
return ResolvedSDKPaths(sdkRoot: nil, toolsetBin: nil, swiftResources: nil, swiftPlatformLib: nil)
return ResolvedSDKPaths(
sdkRoot: nil,
toolsetBin: nil,
swiftResources: nil,
swiftPlatformLib: nil,
clangResourceDir: nil
)
#endif
}
let bundle = darwinSDK.bundle
Expand Down Expand Up @@ -186,12 +202,55 @@ public struct AppleBuildDestination: Sendable, Codable, Equatable {
)
let swiftPlatformLib = swiftLibCandidate.dirExists ? swiftLibCandidate : nil

let clangResourceDir = resolveClangResourceDir(
bundle: bundle,
swiftResources: swiftResources,
platform: platform
)

return ResolvedSDKPaths(
sdkRoot: sdkRoot,
toolsetBin: toolsetBin,
swiftResources: swiftResources,
swiftPlatformLib: swiftPlatformLib
swiftPlatformLib: swiftPlatformLib,
clangResourceDir: clangResourceDir
)
}

private static func resolveClangResourceDir(
bundle: URL,
swiftResources: URL?,
platform: Platform
) -> URL? {
let rtRelativePath = "lib/darwin/libclang_rt.\(platform.clangRuntimeSuffix).a"
func hasRuntimeArchive(_ dir: URL) -> Bool {
FileManager.default.fileExists(atPath: dir.appendingPathComponent(rtRelativePath).path)
}

if let swiftClang = swiftResources?.appendingPathComponent("clang", isDirectory: true),
hasRuntimeArchive(swiftClang) {
return swiftClang.standardizedFileURL
}

let toolchainClangRoot = bundle.appendingPathComponent(
"Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang",
isDirectory: true
)
if let versions = try? FileManager.default.contentsOfDirectory(
at: toolchainClangRoot,
includingPropertiesForKeys: nil
) {
let candidates = versions
.filter(\.dirExists)
.sorted { $0.lastPathComponent > $1.lastPathComponent }
if let matched = candidates.first(where: hasRuntimeArchive) {
return matched.standardizedFileURL
}
}
if hasRuntimeArchive(toolchainClangRoot) {
return toolchainClangRoot.standardizedFileURL
}
return nil
}

#if os(macOS)
Expand All @@ -207,12 +266,24 @@ public struct AppleBuildDestination: Sendable, Codable, Equatable {
try proc.run()
proc.waitUntilExit()
guard proc.terminationStatus == 0 else {
return ResolvedSDKPaths(sdkRoot: nil, toolsetBin: nil, swiftResources: nil, swiftPlatformLib: nil)
return ResolvedSDKPaths(
sdkRoot: nil,
toolsetBin: nil,
swiftResources: nil,
swiftPlatformLib: nil,
clangResourceDir: nil
)
}
let out = String(decoding: pipe.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)
.trimmingCharacters(in: .whitespacesAndNewlines)
guard !out.isEmpty else {
return ResolvedSDKPaths(sdkRoot: nil, toolsetBin: nil, swiftResources: nil, swiftPlatformLib: nil)
return ResolvedSDKPaths(
sdkRoot: nil,
toolsetBin: nil,
swiftResources: nil,
swiftPlatformLib: nil,
clangResourceDir: nil
)
}
let sdkURL = URL(fileURLWithPath: out, isDirectory: true)
let devDir = sdkURL
Expand All @@ -226,10 +297,17 @@ public struct AppleBuildDestination: Sendable, Codable, Equatable {
sdkRoot: sdkURL.dirExists ? sdkURL : nil,
toolsetBin: nil,
swiftResources: nil,
swiftPlatformLib: swiftLib.dirExists ? swiftLib : nil
swiftPlatformLib: swiftLib.dirExists ? swiftLib : nil,
clangResourceDir: nil
)
} catch {
return ResolvedSDKPaths(sdkRoot: nil, toolsetBin: nil, swiftResources: nil, swiftPlatformLib: nil)
return ResolvedSDKPaths(
sdkRoot: nil,
toolsetBin: nil,
swiftResources: nil,
swiftPlatformLib: nil,
clangResourceDir: nil
)
}
}
#endif
Expand Down
12 changes: 12 additions & 0 deletions Sources/PackLib/OriginalHostCompiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public struct OriginalHostCompiler: Sendable {
// Clang needs the linker flavor as well as the path to the SDK trampoline.
// Darwin SDK metadata lookup in clang uses -isysroot, while swiftc forwards -sdk as --sysroot.
args += ["-Xclang-linker", "-isysroot", "-Xclang-linker", sdkRoot]
if let clangResourceDir = request.destination.clangResourceDirPath,
!hasCustomClangLinkerResourceDir(in: request.extraSwiftFlags) {
// Darwin compiler-rt archives (libclang_rt.iossim.a / libclang_rt.ios.a, e.g.
// __isPlatformVersionAtLeast) live under the Apple SDK Clang resource directory.
args += ["-Xclang-linker", "-resource-dir", "-Xclang-linker", clangResourceDir]
}
let selectors = ["-use-ld", "-ld-path", "--ld-path", "-fuse-ld"]
let hasCustomLinker = request.extraSwiftFlags.contains { flag in
selectors.contains { flag == $0 || flag.hasPrefix($0 + "=") }
Expand All @@ -141,6 +147,12 @@ public struct OriginalHostCompiler: Sendable {
return args
}

private static func hasCustomClangLinkerResourceDir(in flags: [String]) -> Bool {
zip(flags, flags.dropFirst()).contains { first, second in
first == "-Xclang-linker" && (second == "-resource-dir" || second.hasPrefix("-resource-dir="))
}
}

public func compile(_ request: Request) async throws -> URL {
guard !request.swiftSources.isEmpty || !request.objcSources.isEmpty else {
throw StringError("Original host build requires at least one Swift or Objective-C source file.")
Expand Down
40 changes: 40 additions & 0 deletions Tests/XToolTests/SDKBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,46 @@ struct SDKBuilderTests {
#expect(flags.allSatisfy { custom.contains($0) })
#expect(selection.allSatisfy { !custom.contains($0) })
}

let suffix = triple.contains("simulator") ? "iossim" : "ios"
let clangRes = fixture.output.appending(path: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang")
let darwinRT = clangRes.appending(path: "lib/darwin")
try FileManager.default.createDirectory(at: darwinRT, withIntermediateDirectories: true)
try Data("!<arch>\n".utf8).write(to: darwinRT.appending(path: "libclang_rt.\(suffix).a"))

let withRT = try AppleBuildDestination.resolve(triple: triple, minimumOS: "16.0", darwinSDK: sdk)
#expect(withRT.clangResourceDirPath == clangRes.standardizedFileURL.path)
func argumentsWithRT(_ flags: [String] = []) -> [String] {
let request = OriginalHostCompiler.Request(
moduleName: "Host", executableName: "Host", destination: withRT,
configuration: .release, swiftSources: [fixture.root.appending(path: "Host.swift")],
extraSwiftFlags: flags, outputExecutableURL: fixture.root.appending(path: "Host")
)
return OriginalHostCompiler.buildSwiftcArguments(for: request, sdkRoot: fixture.root.path, objcObjects: [])
}
let rtArgs = argumentsWithRT()
let rtForwarded = zip(rtArgs, rtArgs.dropFirst()).filter { $0.0 == "-Xclang-linker" }.map { $0.1 }
#expect(rtForwarded == ["-isysroot", fixture.root.path, "-resource-dir", clangRes.standardizedFileURL.path])

for overrideFlags in [
["-Xclang-linker", "-resource-dir", "-Xclang-linker", "/custom/clang"],
["-Xclang-linker", "-resource-dir=/custom/clang"],
] {
let overridden = argumentsWithRT(overrideFlags)
let overriddenForwarded = zip(overridden, overridden.dropFirst())
.filter { $0.0 == "-Xclang-linker" }
.map { $0.1 }
#expect(!overriddenForwarded.contains(clangRes.standardizedFileURL.path))
}

// Removing swift/clang runtime falls back to usr/lib/clang/<version>.
try FileManager.default.removeItem(at: darwinRT)
let versionedClang = fixture.output.appending(path: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17")
let versionedRT = versionedClang.appending(path: "lib/darwin")
try FileManager.default.createDirectory(at: versionedRT, withIntermediateDirectories: true)
try Data("!<arch>\n".utf8).write(to: versionedRT.appending(path: "libclang_rt.\(suffix).a"))
let fallbackRT = try AppleBuildDestination.resolve(triple: triple, minimumOS: "16.0", darwinSDK: sdk)
#expect(fallbackRT.clangResourceDirPath == versionedClang.standardizedFileURL.path)
}

@Test func originalHostUsesSlimSDKSwiftResources() async throws {
Expand Down