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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
private let namesToExclude = [".git", ".build"]
public struct LLBuildManifestWriter {
private let manifest: LLBuildManifest
// FIXME: since JSON is a superset of YAML and we don't need to parse these manifests,
// we should just use `JSONEncoder` instead.
private var buffer = """
client:
name: basic
file-system: device-agnostic
tools: {}
"""
private init(manifest: LLBuildManifest) {
self.manifest = manifest
self.render(targets: manifest.targets)
self.buffer += "default: \(manifest.defaultTarget.asJSON)\n"
self.render(nodes: manifest.commands.values.flatMap { $0.tool.inputs + $0.tool.outputs })
self.render(commands: manifest.commands)
}
public static func write(_ manifest: LLBuildManifest, at path: AbsolutePath, fileSystem: FileSystem) throws {
let writer = LLBuildManifestWriter(manifest: manifest)
try fileSystem.writeFileContents(path, string: writer.buffer)
}
private mutating func render(targets: [LLBuildManifest.TargetName: Target]) {
self.buffer += "targets:\n"
for (_, target) in targets.sorted(by: { $0.key < $1.key }) {
self.buffer += " \(target.name.asJSON): \(target.nodes.map(\.name).sorted().asJSON)\n"
}
}
private mutating func render(nodes: [Node]) {
// We need to explicitly configure certain kinds of nodes.
let directoryStructureNodes = Set(nodes.filter { $0.kind == .directoryStructure })
.sorted(by: { $0.name < $1.name })
let commandTimestampNodes = Set(nodes.filter { $0.attributes?.isCommandTimestamp == true })
.sorted(by: { $0.name < $1.name })
let mutatedNodes = Set(nodes.filter { $0.attributes?.isMutated == true })
.sorted(by: { $0.name < $1.name })
if !directoryStructureNodes.isEmpty || !mutatedNodes.isEmpty || !commandTimestampNodes.isEmpty {
self.buffer += "nodes:\n"
}
for node in directoryStructureNodes {
self.render(directoryStructure: node)
}
for node in commandTimestampNodes {
self.render(isCommandTimestamp: node)
}
for node in mutatedNodes {
self.render(isMutated: node)
}
}
private mutating func render(directoryStructure node: Node) {
self.buffer += """
\(node.asJSON):
is-directory-structure: true
content-exclusion-patterns: \(namesToExclude.asJSON)
"""
}
private mutating func render(isCommandTimestamp node: Node) {
self.buffer += """
\(node.asJSON):
is-command-timestamp: true
"""
}
private mutating func render(isMutated node: Node) {
self.buffer += """
\(node.asJSON):
is-mutated: true
"""
}
private mutating func render(commands: [LLBuildManifest.CmdName: Command]) {
self.buffer += "commands:\n"
for (_, command) in commands.sorted(by: { $0.key < $1.key }) {
self.buffer += " \(command.name.asJSON):\n"
let tool = command.tool
var manifestToolWriter = ManifestToolStream()
manifestToolWriter["tool"] = tool
manifestToolWriter["inputs"] = tool.inputs
manifestToolWriter["outputs"] = tool.outputs
if tool.alwaysOutOfDate {
manifestToolWriter["always-out-of-date"] = "true"
}
tool.write(to: &manifestToolWriter)
self.buffer += "\(manifestToolWriter.buffer)\n"
}
}
}
public struct ManifestToolStream {
fileprivate var buffer = ""
public subscript(key: String) -> Int {
get { fatalError() }
set {
self.buffer += " \(key): \(newValue.description.asJSON)\n"
}
}
public subscript(key: String) -> String {
get { fatalError() }
set {
self.buffer += " \(key): \(newValue.asJSON)\n"
}
}
public subscript(key: String) -> ToolProtocol {
get { fatalError() }
set {
self.buffer += " \(key): \(type(of: newValue).name)\n"
}
}
public subscript(key: String) -> AbsolutePath {
get { fatalError() }
set {
self.buffer += " \(key): \(newValue.pathString.asJSON)\n"
}
}
public subscript(key: String) -> [AbsolutePath] {
get { fatalError() }
set {
self.buffer += " \(key): \(newValue.map(\.pathString).asJSON)\n"
}
}
public subscript(key: String) -> [Node] {
get { fatalError() }
set {
self.buffer += " \(key): \(newValue.map(\.encodingName).asJSON)\n"
}
}
public subscript(key: String) -> Bool {
get { fatalError() }
set {
self.buffer += " \(key): \(newValue.description)\n"
}
}
public subscript(key: String) -> [String] {
get { fatalError() }
set {
self.buffer += " \(key): \(newValue.asJSON)\n"
}
}
public subscript(key: String) -> [String: String] {
get { fatalError() }
set {
self.buffer += " \(key):\n"
for (key, value) in newValue.sorted(by: { $0.key < $1.key }) {
self.buffer += " \(key.asJSON): \(value.asJSON)\n"
}
}
}
package subscript(key: String) -> Environment {
get { fatalError() }
set {
self.buffer += " \(key):\n"
for (key, value) in newValue.sorted(by: { $0.key < $1.key }) {
self.buffer += " \(key.rawValue.asJSON): \(value.asJSON)\n"
}
}
}
}
extension [String] {
fileprivate var asJSON: String {
"""
[\(self.map(\.asJSON).joined(separator: ","))]
"""
}
}
extension Node {
fileprivate var asJSON: String {
self.encodingName.asJSON
}
}
extension Node {
fileprivate var encodingName: String {
switch kind {
case .virtual, .file:
return name
case .directory, .directoryStructure:
return name + "/"
}
}
}
extension String {
fileprivate var asJSON: String {
"\"\(self.jsonEscaped)\""
}
private var jsonEscaped: String {
// See RFC7159 for reference: https://tools.ietf.org/html/rfc7159
String(decoding: self.utf8.flatMap { character -> [UInt8] in
// Handle string escapes; we use constants here to directly match the RFC.
switch character {
// Literal characters.
case 0x20 ... 0x21, 0x23 ... 0x5B, 0x5D ... 0xFF:
return [character]
// Single-character escaped characters.
case 0x22: // '"'
return [
0x5C, // '\'
0x22, // '"'
]
case 0x5C: // '\\'
return [
0x5C, // '\'
0x5C, // '\'
]
case 0x08: // '\b'
return [
0x5C, // '\'
0x62, // 'b'
]
case 0x0C: // '\f'
return [
0x5C, // '\'
0x66, // 'b'
]
case 0x0A: // '\n'
return [
0x5C, // '\'
0x6E, // 'n'
]
case 0x0D: // '\r'
return [
0x5C, // '\'
0x72, // 'r'
]
case 0x09: // '\t'
return [
0x5C, // '\'
0x74, // 't'
]
// Multi-character escaped characters.
default:
return [
0x5C, // '\'
0x75, // 'u'
hexdigit(0),
hexdigit(0),
hexdigit(character >> 4),
hexdigit(character & 0xF),
]
}
}, as: UTF8.self)
}
}
/// Convert an integer in 0..<16 to its hexadecimal ASCII character.
private func hexdigit(_ value: UInt8) -> UInt8 {
value < 10 ? (0x30 + value) : (0x41 + value - 10)
}
|