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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2018 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
//
//===----------------------------------------------------------------------===//
// We either export the llbuildSwift shared library or the llbuild framework.
#if canImport(llbuildSwift)
@_exported import llbuildSwift
@_exported import llbuild
#else
@_exported import llbuild
#endif
import Basics
import Foundation
/// An llbuild value.
public protocol LLBuildValue: Codable {
}
/// An llbuild key.
public protocol LLBuildKey: Codable {
/// The value that this key computes.
associatedtype BuildValue: LLBuildValue
/// The rule that this key operates on.
associatedtype BuildRule: LLBuildRule
}
public protocol LLBuildEngineDelegate {
func lookupRule(rule: String, key: Key) -> Rule
}
public final class LLBuildEngine {
enum Error: Swift.Error, CustomStringConvertible {
case failed(errors: [String])
var description: String {
switch self {
case .failed(let errors):
return errors.joined(separator: "\n")
}
}
}
private final class Delegate: BuildEngineDelegate {
let delegate: LLBuildEngineDelegate
var errors: [String] = []
init(_ delegate: LLBuildEngineDelegate) {
self.delegate = delegate
}
func lookupRule(_ key: Key) -> Rule {
let ruleKey = try! RuleKey(key)
return delegate.lookupRule(
rule: ruleKey.rule, key: Key(ruleKey.data))
}
func error(_ message: String) {
errors.append(message)
}
}
private let engine: BuildEngine
private let delegate: Delegate
public init(delegate: LLBuildEngineDelegate) {
self.delegate = Delegate(delegate)
engine = BuildEngine(delegate: self.delegate)
}
deinit {
engine.close()
}
public func build<T: LLBuildKey>(key: T) throws -> T.BuildValue {
// Clear out any errors from the previous build.
delegate.errors.removeAll()
let encodedKey = try RuleKey(
rule: T.BuildRule.ruleName, data: try key.toKey().data).toKey()
let value = engine.build(key: encodedKey)
// Throw if the engine encountered any fatal error during the build.
if !delegate.errors.isEmpty || value.data.isEmpty {
throw Error.failed(errors: delegate.errors)
}
return try T.BuildValue(value)
}
public func attachDB(path: String, schemaVersion: Int = 2) throws {
try engine.attachDB(path: path, schemaVersion: schemaVersion)
}
public func close() {
engine.close()
}
}
// FIXME: Rename to something else.
public class LLTaskBuildEngine {
let engine: TaskBuildEngine
init(_ engine: TaskBuildEngine) {
self.engine = engine
}
public func taskNeedsInput<T: LLBuildKey>(_ key: T, inputID: Int) throws {
let encodedKey = try RuleKey(
rule: T.BuildRule.ruleName, data: try key.toKey().data).toKey()
engine.taskNeedsInput(encodedKey, inputID: inputID)
}
public func taskIsComplete<T: LLBuildValue>(_ result: T) throws {
engine.taskIsComplete(try result.toValue(), forceChange: false)
}
}
/// An individual build rule.
open class LLBuildRule: Rule, Task {
/// The name of the rule.
///
/// This name will be available in the delegate's lookupRule(rule:key:).
open class var ruleName: String {
fatalError("subclass responsibility")
}
public init() {
}
public func createTask() -> Task {
return self
}
public func start(_ engine: TaskBuildEngine) {
self.start(LLTaskBuildEngine(engine))
}
public func provideValue(_ engine: TaskBuildEngine, inputID: Int, value: Value) {
self.provideValue(LLTaskBuildEngine(engine), inputID: inputID, value: value)
}
public func inputsAvailable(_ engine: TaskBuildEngine) {
self.inputsAvailable(LLTaskBuildEngine(engine))
}
// MARK:-
open func isResultValid(_ priorValue: Value) -> Bool {
return true
}
open func start(_ engine: LLTaskBuildEngine) {
}
open func provideValue(_ engine: LLTaskBuildEngine, inputID: Int, value: Value) {
}
open func inputsAvailable(_ engine: LLTaskBuildEngine) {
}
}
// MARK:- Helpers
private struct RuleKey: Codable {
let rule: String
let data: [UInt8]
init(rule: String, data: [UInt8]) {
self.rule = rule
self.data = data
}
init(_ key: Key) throws {
try self.init(key.data)
}
init(_ data: [UInt8]) throws {
self = try fromBytes(data)
}
func toKey() throws -> Key {
return try Key(toBytes(self))
}
}
public extension LLBuildKey {
init(_ key: Key) throws {
try self.init(key.data)
}
init(_ data: [UInt8]) throws {
do {
self = try fromBytes(data)
} catch {
let stringValue: String
if let str = String(bytes: data, encoding: .utf8) {
stringValue = str
} else {
stringValue = String(describing: data)
}
throw InternalError("LLBuildKey: ###\(error.interpolationDescription)### ----- ###\(stringValue)###")
}
}
func toKey() throws -> Key {
return try Key(toBytes(self))
}
}
public extension LLBuildValue {
init(_ value: Value) throws {
self = try fromBytes(value.data)
}
func toValue() throws -> Value {
return try Value(toBytes(self))
}
}
private func fromBytes<T: Decodable>(_ bytes: [UInt8]) throws -> T {
var bytes = bytes
let data = Data(bytes: &bytes, count: bytes.count)
let decoder = JSONDecoder.makeWithDefaults()
return try decoder.decode(T.self, from: data)
}
private func toBytes<T: Encodable>(_ value: T) throws -> [UInt8] {
let encoder = JSONEncoder.makeWithDefaults()
let encoded = try encoder.encode(value)
return [UInt8](encoded)
}
|