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

Remove a dependency on Yams by encoding the file list in JSON #1701

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 12 additions & 26 deletions Sources/SwiftDriver/Execution/ArgsResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
//
//===----------------------------------------------------------------------===//

import class Foundation.JSONEncoder
import class Foundation.NSLock

import func TSCBasic.withTemporaryDirectory
import protocol TSCBasic.FileSystem
import struct TSCBasic.AbsolutePath
import struct TSCBasic.SHA256

@_implementationOnly import Yams

/// How the resolver is to handle usage of response files
public enum ResponseFileHandling {
case forced
Expand Down Expand Up @@ -166,35 +165,22 @@ public final class ArgsResolver {
throws {
// FIXME: Need a way to support this for distributed build systems...
if let absPath = path.absolutePath {
// This uses Yams to escape and quote strings, but not to output the whole yaml file because
// it sometimes outputs mappings in explicit block format (https://yaml.org/spec/1.2/spec.html#id2798057)
// and the frontend (llvm) only seems to support implicit block format.
try fileSystem.writeFileContents(absPath) { out in
for (input, map) in outputFileMap.entries {
out.send("\(quoteAndEscape(path: VirtualPath.lookup(input))):")
if map.isEmpty {
out.send(" {}\n")
} else {
out.send("\n")
for (type, output) in map {
out.send(" \(type.name): \(quoteAndEscape(path: VirtualPath.lookup(output)))\n")
}
}
}
let codableOutputFileMap: [String: [String: String]]

codableOutputFileMap = try! Dictionary(uniqueKeysWithValues: outputFileMap.entries.map { (input, map) in
try (unsafeResolve(path: VirtualPath.lookup(input)), Dictionary(uniqueKeysWithValues: map.map { (type, output) in
try (type.name, unsafeResolve(path: VirtualPath.lookup(output)))
}))
})

let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
try! out.send(encoder.encode(codableOutputFileMap))
}
}
}

private func quoteAndEscape(path: VirtualPath) -> String {
let inputNode = Node.scalar(Node.Scalar(try! unsafeResolve(path: path),
Tag(.str), .doubleQuoted))
// Width parameter of -1 sets preferred line-width to unlimited so that no extraneous
// line-breaks will be inserted during serialization.
let string = try! Yams.serialize(node: inputNode, width: -1)
// Remove the newline from the end
return string.trimmingCharacters(in: .whitespacesAndNewlines)
}

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