Skip to content

Commit f968345

Browse files
committed
Locate "arclite" library relative to the Clang in the active Xcode
Instead of current toolchain that the driver belongs to. This will match the behaviour in the legacy driver: https://github.com/apple/swift/blob/main/lib/Driver/DarwinToolChains.cpp#L287 Resolves rdar://76035286
1 parent e9f4a0a commit f968345

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ import TSCUtility
1414
import SwiftOptions
1515

1616
extension DarwinToolchain {
17+
internal func findXcodeClangPath() throws -> AbsolutePath? {
18+
let result = try executor.checkNonZeroExit(
19+
args: "xcrun", "-toolchain", "default", "-f", "clang",
20+
environment: env
21+
).spm_chomp()
22+
23+
return result.isEmpty ? nil : AbsolutePath(result)
24+
}
25+
1726
internal func findXcodeClangLibPath(_ additionalPath: String) throws -> AbsolutePath? {
1827
let path = try getToolPath(.swiftCompiler)
1928
.parentDirectory // 'swift'
@@ -24,7 +33,7 @@ extension DarwinToolchain {
2433

2534
// If we don't have a 'lib/arc/' directory, find the "arclite" library
2635
// relative to the Clang in the active Xcode.
27-
if let clangPath = try? getToolPath(.clang) {
36+
if let clangPath = try? findXcodeClangPath() {
2837
return clangPath
2938
.parentDirectory // 'clang'
3039
.parentDirectory // 'bin'

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,18 @@ final class SwiftDriverTests: XCTestCase {
14361436
}
14371437
}
14381438

1439+
private func clangPathInActiveXcode() throws -> AbsolutePath? {
1440+
#if !os(macOS)
1441+
return nil
1442+
#endif
1443+
let process = Process(arguments: ["xcrun", "-toolchain", "default", "-f", "clang"])
1444+
try process.launch()
1445+
let result = try process.waitUntilExit()
1446+
guard result.exitStatus == .terminated(code: EXIT_SUCCESS) else { return nil }
1447+
guard let path = String(bytes: try result.output.get(), encoding: .utf8) else { return nil }
1448+
return path.isEmpty ? nil : AbsolutePath(path.spm_chomp())
1449+
}
1450+
14391451
func testCompatibilityLibs() throws {
14401452
var env = ProcessEnv.vars
14411453
env["SWIFT_DRIVER_TESTS_ENABLE_EXEC_PATH_FALLBACK"] = "1"
@@ -1537,6 +1549,30 @@ final class SwiftDriverTests: XCTestCase {
15371549
XCTAssertTrue(cmd.contains(subsequence: [.flag("-force_load"), .path(.absolute(path5_1iOS))]))
15381550
XCTAssertTrue(cmd.contains(subsequence: [.flag("-force_load"), .path(.absolute(pathDynamicReplacementsiOS))]))
15391551
}
1552+
1553+
// libarclite is only relevant on darwin
1554+
#if os(macOS)
1555+
do {
1556+
// Override executive paths and make sure this does not affect the location of the found
1557+
// libarclite
1558+
env["SWIFT_DRIVER_SWIFTC_EXEC"] = "/some/path/swiftc"
1559+
env["SWIFT_DRIVER_CLANG_EXEC"] = "/some/path/clang"
1560+
guard let clangPathInXcode = try? clangPathInActiveXcode() else {
1561+
throw XCTSkip()
1562+
}
1563+
let clangRelativeArcLite = clangPathInXcode.parentDirectory.parentDirectory
1564+
.appending(components: "lib", "arc", "libarclite_macosx.a")
1565+
1566+
var driver = try Driver(args: commonArgs + ["-target", "x86_64-apple-macosx10.9"], env: env)
1567+
let plannedJobs = try driver.planBuild()
1568+
1569+
XCTAssertEqual(3, plannedJobs.count)
1570+
let linkJob = plannedJobs[2]
1571+
XCTAssertEqual(linkJob.kind, .link)
1572+
let cmd = linkJob.commandLine
1573+
XCTAssertTrue(cmd.contains(subsequence: [.flag("-force_load"), .path(.absolute(clangRelativeArcLite))]))
1574+
}
1575+
#endif
15401576
}
15411577
}
15421578

0 commit comments

Comments
 (0)