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
8 changes: 6 additions & 2 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2609,13 +2609,17 @@ extension Driver {

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.
// We avoid using `SDKROOT` because that may be set to a compilation platform (like iOS)
// that is different than the host.
sdkPath = try? toolchain.defaultSDKPath(targetTriple)?.pathString
}

if sdkPath == nil, let SDKROOT = env["SDKROOT"] {
sdkPath = SDKROOT
}

// An empty string explicitly clears the SDK.
if sdkPath == "" {
sdkPath = nil
Expand Down
20 changes: 18 additions & 2 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3495,7 +3495,17 @@ final class SwiftDriverTests: XCTestCase {

func testImmediateMode() throws {
do {
var driver = try Driver(args: ["swift", "foo.swift"])
var env: [String: String] = ProcessEnv.vars
#if os(macOS)
let executor = try SwiftDriverExecutor(diagnosticsEngine: DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler]),
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: ProcessEnv.vars)
let iosSDKPath = try executor.checkNonZeroExit(
args: "xcrun", "-sdk", "iphoneos", "--show-sdk-path").spm_chomp()
env["SDKROOT"] = iosSDKPath
#endif
var driver = try Driver(args: ["swift", "foo.swift"], env: env)
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
let job = plannedJobs[0]
Expand All @@ -3509,7 +3519,13 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertTrue(job.commandLine.contains(.flag("foo")))

if driver.targetTriple.isMacOSX {
XCTAssertTrue(job.commandLine.contains(.flag("-sdk")))
let sdkIdx = try XCTUnwrap(job.commandLine.firstIndex(of: .flag("-sdk")))
let sdkPathOpt: VirtualPath? = switch job.commandLine[sdkIdx + 1] {
case .path(let path): path
default: nil
}
let sdkPath = try XCTUnwrap(sdkPathOpt)
XCTAssertTrue(sdkPath.name.contains("MacOSX.platform"))
}

XCTAssertFalse(job.commandLine.contains(.flag("--")))
Expand Down