1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
//===--------------- ArgsResolver.swift - Argument Resolution -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
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
case disabled
case heuristic
}
/// Resolver for a job's argument template.
public final class ArgsResolver {
/// The map of virtual path to the actual path.
public var pathMapping: [VirtualPath: String]
/// The file system used by the resolver.
private let fileSystem: FileSystem
/// Path to the directory that will contain the temporary files.
// FIXME: We probably need a dedicated type for this...
private let temporaryDirectory: VirtualPath
private let lock = NSLock()
public init(fileSystem: FileSystem, temporaryDirectory: VirtualPath? = nil) throws {
self.pathMapping = [:]
self.fileSystem = fileSystem
if let temporaryDirectory = temporaryDirectory {
self.temporaryDirectory = temporaryDirectory
} else {
// FIXME: withTemporaryDirectory uses FileManager.default, need to create a FileSystem.temporaryDirectory api.
let tmpDir: AbsolutePath = try withTemporaryDirectory(removeTreeOnDeinit: false) { path in
// FIXME: TSC removes empty directories even when removeTreeOnDeinit is false. This seems like a bug.
try fileSystem.writeFileContents(path.appending(component: ".keep-directory")) { $0.send("") }
return path
}
self.temporaryDirectory = .absolute(tmpDir)
}
}
public func resolveArgumentList(for job: Job, useResponseFiles: ResponseFileHandling = .heuristic)
throws -> [String] {
let (arguments, _) = try resolveArgumentList(for: job, useResponseFiles: useResponseFiles)
return arguments
}
public func resolveArgumentList(for job: Job, useResponseFiles: ResponseFileHandling = .heuristic)
throws -> ([String], usingResponseFile: Bool) {
let tool = try resolve(.path(job.tool))
var arguments = [tool] + (try resolveArgumentList(for: job.commandLine))
let usingResponseFile = try createResponseFileIfNeeded(for: job, resolvedArguments: &arguments,
useResponseFiles: useResponseFiles)
return (arguments, usingResponseFile)
}
public func resolveArgumentList(for commandLine: [Job.ArgTemplate]) throws -> [String] {
return try commandLine.map { try resolve($0) }
}
@available(*, deprecated, message: "use resolveArgumentList(for:,useResponseFiles:,quotePaths:)")
public func resolveArgumentList(for job: Job, forceResponseFiles: Bool,
quotePaths: Bool = false) throws -> [String] {
let useResponseFiles: ResponseFileHandling = forceResponseFiles ? .forced : .heuristic
return try resolveArgumentList(for: job, useResponseFiles: useResponseFiles)
}
@available(*, deprecated, message: "use resolveArgumentList(for:,useResponseFiles:,quotePaths:)")
public func resolveArgumentList(for job: Job, forceResponseFiles: Bool,
quotePaths: Bool = false) throws -> ([String], usingResponseFile: Bool) {
let useResponseFiles: ResponseFileHandling = forceResponseFiles ? .forced : .heuristic
return try resolveArgumentList(for: job, useResponseFiles: useResponseFiles)
}
/// Resolve the given argument.
public func resolve(_ arg: Job.ArgTemplate) throws -> String {
switch arg {
case .flag(let flag):
return flag
case .path(let path):
return try lock.withLock {
return try unsafeResolve(path: path)
}
case .responseFilePath(let path):
return "@\(try resolve(.path(path)))"
case let .joinedOptionAndPath(option, path):
return option + (try resolve(.path(path)))
case let .squashedArgumentList(option: option, args: args):
return try option + args.map {
try resolve($0)
}.joined(separator: " ")
}
}
/// Needs to be done inside of `lock`. Marked unsafe to make that more obvious.
private func unsafeResolve(path: VirtualPath) throws -> String {
// If there was a path mapping, use it.
if let actualPath = pathMapping[path] {
return actualPath
}
// Return the path from the temporary directory if this is a temporary file.
if path.isTemporary {
let actualPath = temporaryDirectory.appending(component: path.name)
switch path {
case .temporary:
break // No special behavior required.
case let .temporaryWithKnownContents(_, contents):
// FIXME: Need a way to support this for distributed build systems...
if let absolutePath = actualPath.absolutePath {
try fileSystem.writeFileContents(absolutePath, bytes: .init(contents))
}
case let .fileList(_, .list(items)):
try createFileList(path: actualPath, contents: items)
case let .fileList(_, .outputFileMap(map)):
try createFileList(path: actualPath, outputFileMap: map)
case .relative, .absolute, .standardInput, .standardOutput:
fatalError("Not a temporary path.")
}
let result = actualPath.name
pathMapping[path] = result
return result
}
// Otherwise, return the path.
let result = path.name
pathMapping[path] = result
return result
}
private func createFileList(path: VirtualPath, contents: [VirtualPath]) throws {
// FIXME: Need a way to support this for distributed build systems...
if let absPath = path.absolutePath {
try fileSystem.writeFileContents(absPath) { out in
for path in contents {
try! out.send("\(unsafeResolve(path: path))\n")
}
}
}
}
private func createFileList(path: VirtualPath, outputFileMap: OutputFileMap)
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")
}
}
}
}
}
}
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)" }))\""
}
guard useResponseFiles != .disabled else {
return false
}
let forceResponseFiles = useResponseFiles == .forced
if forceResponseFiles ||
(job.supportsResponseFiles && !commandLineFitsWithinSystemLimits(path: resolvedArguments[0], args: resolvedArguments)) {
assert(!forceResponseFiles || job.supportsResponseFiles,
"Platform does not support response files for job: \(job)")
// Match the integrated driver's behavior, which uses response file names of the form "arguments-[0-9a-zA-Z].resp".
let hash = SHA256().hash(resolvedArguments.joined(separator: " ")).hexadecimalRepresentation
let responseFilePath = temporaryDirectory.appending(component: "arguments-\(hash).resp")
// 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.send(resolvedArguments[2...].map { quote($0) }.joined(separator: "\n"))
}
resolvedArguments = [resolvedArguments[0], resolvedArguments[1], "@\(absPath.pathString)"]
}
return true
}
return false
}
/// Remove the temporary directory from disk.
public func removeTemporaryDirectory() throws {
// Only try to remove if we have an absolute path.
if let absPath = temporaryDirectory.absolutePath {
try fileSystem.removeFileTree(absPath)
}
}
}
fileprivate extension NSLock {
/// NOTE: Keep in sync with SwiftPM's 'Sources/Basics/NSLock+Extensions.swift'
/// Execute the given block while holding the lock.
func withLock<T> (_ body: () throws -> T) rethrows -> T {
lock()
defer { unlock() }
return try body()
}
}
|