Skip to content

SwiftDriver: change the response file emission #1000

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
Jan 28, 2022
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
9 changes: 8 additions & 1 deletion Sources/SwiftDriver/Execution/ArgsResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ public final class ArgsResolver {
}

private func createResponseFileIfNeeded(for job: Job, resolvedArguments: inout [String], forceResponseFiles: Bool) throws -> Bool {
func quote(_ string: String) -> String {
return "\"\(String(string.flatMap { ["\\", "\""].contains($0) ? "\\\($0)" : "\($0)" }))\""
}

if forceResponseFiles ||
(job.supportsResponseFiles && !commandLineFitsWithinSystemLimits(path: resolvedArguments[0], args: resolvedArguments)) {
assert(!forceResponseFiles || job.supportsResponseFiles,
Expand All @@ -176,8 +180,11 @@ public final class ArgsResolver {

// FIXME: Need a way to support this for distributed build systems...
if let absPath = responseFilePath.absolutePath {
// Adopt the same technique as clang -
// Wrap all arguments in double quotes to ensure that both Unix and
// Windows tools understand the response file.
try fileSystem.writeFileContents(absPath) {
$0 <<< resolvedArguments[1...].map{ $0.spm_shellEscaped() }.joined(separator: "\n")
$0 <<< resolvedArguments[1...].map { quote($0) }.joined(separator: "\n")
}
resolvedArguments = [resolvedArguments[0], "@\(absPath.pathString)"]
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1205,9 +1205,9 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertEqual(resolvedArgs[1].first, "@")
let responseFilePath = try AbsolutePath(validating: String(resolvedArgs[1].dropFirst()))
let contents = try localFileSystem.readFileContents(responseFilePath).description
XCTAssertTrue(contents.hasPrefix("-frontend\n-interpret\nfoo.swift"))
XCTAssertTrue(contents.contains("-D\nTEST_20000"))
XCTAssertTrue(contents.contains("-D\nTEST_1"))
XCTAssertTrue(contents.hasPrefix("\"-frontend\"\n\"-interpret\"\n\"foo.swift\""))
XCTAssertTrue(contents.contains("\"-D\"\n\"TEST_20000\""))
XCTAssertTrue(contents.contains("\"-D\"\n\"TEST_1\""))
}
// Forced response file
do {
Expand All @@ -1221,7 +1221,7 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertEqual(resolvedArgs[1].first, "@")
let responseFilePath = try AbsolutePath(validating: String(resolvedArgs[1].dropFirst()))
let contents = try localFileSystem.readFileContents(responseFilePath).description
XCTAssertTrue(contents.hasPrefix("-frontend\n-interpret\nfoo.swift"))
XCTAssertTrue(contents.hasPrefix("\"-frontend\"\n\"-interpret\"\n\"foo.swift\""))
}

// No response file
Expand Down