Skip to content

Commit 3615d06

Browse files
committed
[Driver] For immediate mode prefer using the default SDK over the SDKROOT 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.
1 parent 07277b7 commit 3615d06

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Sources/SwiftDriver/Driver/Driver.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -2609,13 +2609,17 @@ extension Driver {
26092609

26102610
if let arg = parsedOptions.getLastArgument(.sdk) {
26112611
sdkPath = arg.asSingle
2612-
} else if let SDKROOT = env["SDKROOT"] {
2613-
sdkPath = SDKROOT
26142612
} else if compilerMode == .immediate || compilerMode == .repl {
26152613
// In immediate modes, query the toolchain for a default SDK.
2614+
// We avoid using `SDKROOT` because that may be set to a compilation platform (like iOS)
2615+
// that is different than the host.
26162616
sdkPath = try? toolchain.defaultSDKPath(targetTriple)?.pathString
26172617
}
26182618

2619+
if sdkPath == nil, let SDKROOT = env["SDKROOT"] {
2620+
sdkPath = SDKROOT
2621+
}
2622+
26192623
// An empty string explicitly clears the SDK.
26202624
if sdkPath == "" {
26212625
sdkPath = nil

Tests/SwiftDriverTests/SwiftDriverTests.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -3495,7 +3495,17 @@ final class SwiftDriverTests: XCTestCase {
34953495

34963496
func testImmediateMode() throws {
34973497
do {
3498-
var driver = try Driver(args: ["swift", "foo.swift"])
3498+
var env: [String: String] = ProcessEnv.vars
3499+
#if os(macOS)
3500+
let executor = try SwiftDriverExecutor(diagnosticsEngine: DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler]),
3501+
processSet: ProcessSet(),
3502+
fileSystem: localFileSystem,
3503+
env: ProcessEnv.vars)
3504+
let iosSDKPath = try executor.checkNonZeroExit(
3505+
args: "xcrun", "-sdk", "iphoneos", "--show-sdk-path").spm_chomp()
3506+
env["SDKROOT"] = iosSDKPath
3507+
#endif
3508+
var driver = try Driver(args: ["swift", "foo.swift"], env: env)
34993509
let plannedJobs = try driver.planBuild()
35003510
XCTAssertEqual(plannedJobs.count, 1)
35013511
let job = plannedJobs[0]
@@ -3509,7 +3519,13 @@ final class SwiftDriverTests: XCTestCase {
35093519
XCTAssertTrue(job.commandLine.contains(.flag("foo")))
35103520

35113521
if driver.targetTriple.isMacOSX {
3512-
XCTAssertTrue(job.commandLine.contains(.flag("-sdk")))
3522+
let sdkIdx = try XCTUnwrap(job.commandLine.firstIndex(of: .flag("-sdk")))
3523+
let sdkPathOpt: VirtualPath? = switch job.commandLine[sdkIdx + 1] {
3524+
case .path(let path): path
3525+
default: nil
3526+
}
3527+
let sdkPath = try XCTUnwrap(sdkPathOpt)
3528+
XCTAssertTrue(sdkPath.name.contains("MacOSX.platform"))
35133529
}
35143530

35153531
XCTAssertFalse(job.commandLine.contains(.flag("--")))

0 commit comments

Comments
 (0)