Skip to content

Commit

Permalink
[Driver] For immediate mode prefer using the default SDK over the `SD…
Browse files Browse the repository at this point in the history
…KROOT` variable

Avoid using `SDKROOT` because that may be set to a compilation platform (like iOS) that is different than the host.
For immediate mode the host SDK is what needs to be used.
  • Loading branch information
akyrtzi committed Jul 4, 2023
1 parent 07277b7 commit 3615d06
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
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

0 comments on commit 3615d06

Please sign in to comment.