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] For immediate mode prefer using the default SDK over the SDKROOT variable #1390

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/SwiftDriver/Driver/Driver.swift
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
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