Skip to content

Commit 6bcfe30

Browse files
Align Clang resource directory lookup with Clang driver logic
Previously, we used `Triple.platformName(conflatingDarwin: true)` to derive the OS directory under <ResourceDir>/lib/, which was documented as “the name clang uses”. However, this was in fact closer to what Swift itself uses for resource directories, and it diverged from Clang’s behavior in some cases. For example, for wasm32-unknown-wasip1-threads, Swift uses "wasi" as the OS name, but Clang uses "wasip1", matching the result of Triple::getOS, in their resource directory lookup. This patch aligns the behavior with Clang’s logic for looking up the compiler resource directory libraries.
1 parent f7b8007 commit 6bcfe30

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift

+1-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ extension Toolchain {
2626
for targetInfo: FrontendTargetInfo,
2727
parsedOptions: inout ParsedOptions
2828
) throws -> VirtualPath {
29-
var platform = targetInfo.target.triple.platformName(conflatingDarwin: true)!
30-
// compiler-rt moved these Android sanitizers into `lib/linux/` a couple
31-
// years ago, llvm/llvm-project@a68ccba, so look for them there instead.
32-
if platform == "android" {
33-
platform = "linux"
34-
}
29+
let platform = targetInfo.target.triple.clangOSLibName
3530

3631
// NOTE(compnerd) Windows uses the per-target runtime directory for the
3732
// Windows runtimes. This should also be done for the other platforms, but

Sources/SwiftDriver/Utilities/Triple+Platforms.swift

+22-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,28 @@ extension Triple {
303303
}
304304
}
305305

306-
/// The platform name, i.e. the name clang uses to identify this target in its
306+
307+
/// The "os" component of the Clang compiler resource library directory (`<ResourceDir>/lib/<OSName>`).
308+
/// Must be kept in sync with Clang driver:
309+
/// https://github.com/llvm/llvm-project/blob/llvmorg-20.1.4/clang/lib/Driver/ToolChain.cpp#L690
310+
@_spi(Testing) public var clangOSLibName: String {
311+
guard let os else {
312+
return osName
313+
}
314+
if os.isDarwin {
315+
return "darwin"
316+
}
317+
318+
switch os {
319+
case .freeBSD: return "freebsd"
320+
case .netbsd: return "netbsd"
321+
case .openbsd: return "openbsd"
322+
case .aix: return "aix"
323+
default: return osName
324+
}
325+
}
326+
327+
/// The platform name, i.e. the name Swift uses to identify this target in its
307328
/// resource directory.
308329
///
309330
/// - Parameter conflatingDarwin: If true, all Darwin platforms will be

Tests/SwiftDriverTests/TripleTests.swift

+9
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,15 @@ final class TripleTests: XCTestCase {
13171317
shouldHaveJetPacks: true)
13181318
}
13191319

1320+
func testClangOSLibName() {
1321+
XCTAssertEqual("darwin", Triple("x86_64-apple-macosx").clangOSLibName)
1322+
XCTAssertEqual("darwin", Triple("arm64-apple-ios13.0").clangOSLibName)
1323+
XCTAssertEqual("linux", Triple("aarch64-unknown-linux-android24").clangOSLibName)
1324+
XCTAssertEqual("wasi", Triple("wasm32-unknown-wasi").clangOSLibName)
1325+
XCTAssertEqual("wasip1", Triple("wasm32-unknown-wasip1-threads").clangOSLibName)
1326+
XCTAssertEqual("none", Triple("arm64-unknown-none").clangOSLibName)
1327+
}
1328+
13201329
func testToolchainSelection() {
13211330
let diagnostics = DiagnosticsEngine()
13221331
struct None { }

0 commit comments

Comments
 (0)