Skip to content

Commit bb3fdfe

Browse files
authored
Merge pull request #1390 from akyrtzi/akyrtzi/pr/ignore-SDKROOT-for-immediate
[Driver] For immediate mode prefer using the default SDK over the `SDKROOT` variable
2 parents b6d89c5 + 3615d06 commit bb3fdfe

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,13 +2612,17 @@ extension Driver {
26122612

26132613
if let arg = parsedOptions.getLastArgument(.sdk) {
26142614
sdkPath = arg.asSingle
2615-
} else if let SDKROOT = env["SDKROOT"] {
2616-
sdkPath = SDKROOT
26172615
} else if compilerMode == .immediate || compilerMode == .repl {
26182616
// In immediate modes, query the toolchain for a default SDK.
2617+
// We avoid using `SDKROOT` because that may be set to a compilation platform (like iOS)
2618+
// that is different than the host.
26192619
sdkPath = try? toolchain.defaultSDKPath(targetTriple)?.pathString
26202620
}
26212621

2622+
if sdkPath == nil, let SDKROOT = env["SDKROOT"] {
2623+
sdkPath = SDKROOT
2624+
}
2625+
26222626
// An empty string explicitly clears the SDK.
26232627
if sdkPath == "" {
26242628
sdkPath = nil

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,7 +3527,17 @@ final class SwiftDriverTests: XCTestCase {
35273527

35283528
func testImmediateMode() throws {
35293529
do {
3530-
var driver = try Driver(args: ["swift", "foo.swift"])
3530+
var env: [String: String] = ProcessEnv.vars
3531+
#if os(macOS)
3532+
let executor = try SwiftDriverExecutor(diagnosticsEngine: DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler]),
3533+
processSet: ProcessSet(),
3534+
fileSystem: localFileSystem,
3535+
env: ProcessEnv.vars)
3536+
let iosSDKPath = try executor.checkNonZeroExit(
3537+
args: "xcrun", "-sdk", "iphoneos", "--show-sdk-path").spm_chomp()
3538+
env["SDKROOT"] = iosSDKPath
3539+
#endif
3540+
var driver = try Driver(args: ["swift", "foo.swift"], env: env)
35313541
let plannedJobs = try driver.planBuild()
35323542
XCTAssertEqual(plannedJobs.count, 1)
35333543
let job = plannedJobs[0]
@@ -3541,7 +3551,13 @@ final class SwiftDriverTests: XCTestCase {
35413551
XCTAssertTrue(job.commandLine.contains(.flag("foo")))
35423552

35433553
if driver.targetTriple.isMacOSX {
3544-
XCTAssertTrue(job.commandLine.contains(.flag("-sdk")))
3554+
let sdkIdx = try XCTUnwrap(job.commandLine.firstIndex(of: .flag("-sdk")))
3555+
let sdkPathOpt: VirtualPath? = switch job.commandLine[sdkIdx + 1] {
3556+
case .path(let path): path
3557+
default: nil
3558+
}
3559+
let sdkPath = try XCTUnwrap(sdkPathOpt)
3560+
XCTAssertTrue(sdkPath.name.contains("MacOSX.platform"))
35453561
}
35463562

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

0 commit comments

Comments
 (0)