Skip to content

Maintain relative SDK paths with no -working-directory flag #1711

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

Merged
merged 1 commit into from
Oct 11, 2024
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
20 changes: 10 additions & 10 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2875,19 +2875,19 @@ extension Driver {

// Validate the SDK if we found one.
if let sdkPath = sdkPath {
let path: AbsolutePath
let path: VirtualPath

// FIXME: TSC should provide a better utility for this.
if let absPath = try? AbsolutePath(validating: sdkPath) {
path = absPath
} else if let cwd = fileSystem.currentWorkingDirectory, let absPath = try? AbsolutePath(validating: sdkPath, relativeTo: cwd) {
path = absPath
path = .absolute(absPath)
} else if let relPath = try? RelativePath(validating: sdkPath) {
path = .relative(relPath)
} else {
diagnosticsEngine.emit(.warning_no_such_sdk(sdkPath))
return nil
}

if !fileSystem.exists(path) {
if (try? fileSystem.exists(path)) != true {
diagnosticsEngine.emit(.warning_no_such_sdk(sdkPath))
} else if (targetTriple?.isDarwin ?? (defaultToolchainType == DarwinToolchain.self)) {
if isSDKTooOld(sdkPath: path, fileSystem: fileSystem,
Expand All @@ -2897,7 +2897,7 @@ extension Driver {
}
}

return .absolute(path)
return path
}

return nil
Expand All @@ -2906,15 +2906,15 @@ extension Driver {

// SDK checking: attempt to diagnose if the SDK we are pointed at is too old.
extension Driver {
static func isSDKTooOld(sdkPath: AbsolutePath, fileSystem: FileSystem,
static func isSDKTooOld(sdkPath: VirtualPath, fileSystem: FileSystem,
diagnosticsEngine: DiagnosticsEngine) -> Bool {
let sdkInfoReadAttempt = DarwinToolchain.readSDKInfo(fileSystem, VirtualPath.absolute(sdkPath).intern())
let sdkInfoReadAttempt = DarwinToolchain.readSDKInfo(fileSystem, sdkPath.intern())
guard let sdkInfo = sdkInfoReadAttempt else {
diagnosticsEngine.emit(.warning_no_sdksettings_json(sdkPath.pathString))
diagnosticsEngine.emit(.warning_no_sdksettings_json(sdkPath.name))
return false
}
guard let sdkVersion = try? Version(string: sdkInfo.versionString, lenient: true) else {
diagnosticsEngine.emit(.warning_fail_parse_sdk_ver(sdkInfo.versionString, sdkPath.pathString))
diagnosticsEngine.emit(.warning_fail_parse_sdk_ver(sdkInfo.versionString, sdkPath.name))
return false
}
if sdkInfo.canonicalName.hasPrefix("macos") {
Expand Down
9 changes: 8 additions & 1 deletion Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7262,23 +7262,31 @@ final class SwiftDriverTests: XCTestCase {
// Inputs with relative paths with no -working-directory flag should remain relative
var driver = try Driver(args: ["swiftc",
"-target", "arm64-apple-ios13.1",
"-resource-dir", "relresourcepath",
"-sdk", "relsdkpath",
"foo.swift"])
let plannedJobs = try driver.planBuild()
let compileJob = plannedJobs[0]
XCTAssertEqual(compileJob.kind, .compile)
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-primary-file", try toPathOption("foo.swift", isRelative: true)]))
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-resource-dir", try toPathOption("relresourcepath", isRelative: true)]))
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-sdk", try toPathOption("relsdkpath", isRelative: true)]))
}

do {
// Inputs with relative paths with -working-directory flag should prefix all inputs
var driver = try Driver(args: ["swiftc",
"-target", "arm64-apple-ios13.1",
"-resource-dir", "relresourcepath",
"-sdk", "relsdkpath",
"foo.swift",
"-working-directory", "/foo/bar"])
let plannedJobs = try driver.planBuild()
let compileJob = plannedJobs[0]
XCTAssertEqual(compileJob.kind, .compile)
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-primary-file", try toPathOption("/foo/bar/foo.swift", isRelative: false)]))
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-resource-dir", try toPathOption("/foo/bar/relresourcepath", isRelative: false)]))
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-sdk", try toPathOption("/foo/bar/relsdkpath", isRelative: false)]))
}

try withTemporaryFile { fileMapFile in
Expand Down Expand Up @@ -7331,7 +7339,6 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertEqual(compileJob.kind, .compile)
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-o", try toPathOption("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.o", isRelative: false)]))
}

}

func testRelativeResourceDir() throws {
Expand Down