Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Driver: simplify `Driver.computeSDKPath(_:compilerMode:toolchain:targ… #1771

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 17 additions & 39 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2854,53 +2854,31 @@ extension Driver {
diagnosticsEngine: DiagnosticsEngine,
env: [String: String]
) -> VirtualPath? {
var sdkPath: String?

if let arg = parsedOptions.getLastArgument(.sdk) {
sdkPath = arg.asSingle
} else if let SDKROOT = env["SDKROOT"] {
sdkPath = SDKROOT
} else if compilerMode == .immediate || compilerMode == .repl {
// In immediate modes, query the toolchain for a default SDK.
sdkPath = try? toolchain.defaultSDKPath(targetTriple)?.pathString
guard let sdkPath = parsedOptions.getLastArgument(.sdk)?.asSingle ??
env["SDKROOT"] ??
// In immediate modes, query the toolchain for a default SDK.
([.immediate, .repl].contains(compilerMode) ? try? toolchain.defaultSDKPath(targetTriple)?.pathString : nil),
!sdkPath.isEmpty else {
return nil
}

// An empty string explicitly clears the SDK.
if sdkPath == "" {
sdkPath = nil
}
// Validate the SDK if we found one.
let path: VirtualPath = .absolute(.init(URL(fileURLWithPath: sdkPath).absoluteURL.path))

// Delete trailing /.
sdkPath = sdkPath.map { $0.count > 1 && $0.last == "/" ? String($0.dropLast()) : $0 }
guard FileManager.default.fileExists(atPath: path.description) else {
diagnosticsEngine.emit(.warning_no_such_sdk(path.description))
return path
}

// Validate the SDK if we found one.
if let sdkPath = sdkPath {
let path: VirtualPath

// FIXME: TSC should provide a better utility for this.
if let absPath = try? AbsolutePath(validating: sdkPath) {
path = .absolute(absPath)
} else if let relPath = try? RelativePath(validating: sdkPath) {
path = .relative(relPath)
} else {
diagnosticsEngine.emit(.warning_no_such_sdk(sdkPath))
if targetTriple?.isDarwin ?? (defaultToolchainType == DarwinToolchain.self) {
if isSDKTooOld(sdkPath: path, fileSystem: fileSystem,
diagnosticsEngine: diagnosticsEngine) {
diagnosticsEngine.emit(.error_sdk_too_old(path.description))
return nil
}

if (try? fileSystem.exists(path)) != true {
diagnosticsEngine.emit(.warning_no_such_sdk(sdkPath))
} else if (targetTriple?.isDarwin ?? (defaultToolchainType == DarwinToolchain.self)) {
if isSDKTooOld(sdkPath: path, fileSystem: fileSystem,
diagnosticsEngine: diagnosticsEngine) {
diagnosticsEngine.emit(.error_sdk_too_old(sdkPath))
return nil
}
}

return path
}

return nil
return path
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7271,7 +7271,7 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertEqual(compileJob.kind, .compile)
try XCTAssertJobInvocationMatches(compileJob, .flag("-primary-file"), toPathOption("foo.swift", isRelative: true))
try XCTAssertJobInvocationMatches(compileJob, .flag("-resource-dir"), toPathOption("relresourcepath", isRelative: true))
try XCTAssertJobInvocationMatches(compileJob, .flag("-sdk"), toPathOption("relsdkpath", isRelative: true))
try XCTAssertJobInvocationMatches(compileJob, .flag("-sdk"), .path(.absolute(.init(URL(fileURLWithPath: FileManager.default.currentDirectoryPath).appendingPathComponent("relsdkpath").path))))
}

do {
Expand Down