-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathMiniMake.swift
326 lines (287 loc) · 11.3 KB
/
MiniMake.swift
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import Foundation
/// A minimal build system
///
/// This build system is a traditional mtime-based incremental build system.
struct MiniMake {
/// Attributes of a task
enum TaskAttribute: String, Codable {
/// Task is phony, meaning it must be built even if its inputs are up to date
case phony
/// Don't print anything when building this task
case silent
}
/// Information about a task enough to capture build
/// graph changes
struct TaskInfo: Encodable {
/// Input tasks not yet built
let wants: [TaskKey]
/// Set of file paths that must be built before this task
let inputs: [BuildPath]
/// Output file path
let output: BuildPath
/// Attributes of the task
let attributes: [TaskAttribute]
/// Salt for the task, used to differentiate between otherwise identical tasks
var salt: Data?
}
/// A task to build
struct Task {
let info: TaskInfo
/// Input tasks (files and phony tasks) not yet built
let wants: Set<TaskKey>
/// Attributes of the task
let attributes: Set<TaskAttribute>
/// Key of the task
let key: TaskKey
/// Build operation
let build: (_ task: Task, _ scope: VariableScope) throws -> Void
/// Whether the task is done
var isDone: Bool
var inputs: [BuildPath] { self.info.inputs }
var output: BuildPath { self.info.output }
}
/// A task key
struct TaskKey: Encodable, Hashable, Comparable, CustomStringConvertible {
let id: String
var description: String { self.id }
fileprivate init(id: String) {
self.id = id
}
func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.id)
}
static func < (lhs: TaskKey, rhs: TaskKey) -> Bool { lhs.id < rhs.id }
}
struct VariableScope {
let variables: [String: String]
func resolve(path: BuildPath) -> URL {
var components = [String]()
for component in path.components {
switch component {
case .prefix(let variable):
guard let value = variables[variable] else {
fatalError("Build path variable \"\(variable)\" not defined!")
}
components.append(value)
case .constant(let path):
components.append(path)
}
}
guard let first = components.first else {
fatalError("Build path is empty")
}
var url = URL(fileURLWithPath: first)
for component in components.dropFirst() {
url = url.appending(path: component)
}
return url
}
}
/// All tasks in the build system
private var tasks: [TaskKey: Task]
/// Whether to explain why tasks are built
private var shouldExplain: Bool
/// Prints progress of the build
private var printProgress: ProgressPrinter.PrintProgress
init(
explain: Bool = false,
printProgress: @escaping ProgressPrinter.PrintProgress
) {
self.tasks = [:]
self.shouldExplain = explain
self.printProgress = printProgress
}
/// Adds a task to the build system
mutating func addTask(
inputFiles: [BuildPath] = [], inputTasks: [TaskKey] = [], output: BuildPath,
attributes: [TaskAttribute] = [], salt: (any Encodable)? = nil,
build: @escaping (_ task: Task, _ scope: VariableScope) throws -> Void
) -> TaskKey {
let taskKey = TaskKey(id: output.description)
let saltData = try! salt.map {
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
return try encoder.encode($0)
}
let info = TaskInfo(
wants: inputTasks, inputs: inputFiles, output: output, attributes: attributes,
salt: saltData
)
self.tasks[taskKey] = Task(
info: info, wants: Set(inputTasks), attributes: Set(attributes),
key: taskKey, build: build, isDone: false)
return taskKey
}
/// Computes a stable fingerprint of the build graph
///
/// This fingerprint must be stable across builds and must change
/// if the build graph changes in any way.
func computeFingerprint(root: TaskKey, prettyPrint: Bool = false) throws -> Data {
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
if prettyPrint {
encoder.outputFormatting.insert(.prettyPrinted)
}
let tasks = self.tasks.sorted { $0.key < $1.key }.map { $0.value.info }
return try encoder.encode(tasks)
}
private func explain(_ message: @autoclosure () -> String) {
if self.shouldExplain {
print(message())
}
}
private func violated(_ message: @autoclosure () -> String) {
print(message())
}
/// Prints progress of the build
struct ProgressPrinter {
struct Context {
let subject: Task
let total: Int
let built: Int
let scope: VariableScope
}
typealias PrintProgress = (_ context: Context, _ message: String) -> Void
/// Total number of tasks to build
let total: Int
/// Number of tasks built so far
var built: Int
/// Prints progress of the build
var printProgress: PrintProgress
init(total: Int, printProgress: @escaping PrintProgress) {
self.total = total
self.built = 0
self.printProgress = printProgress
}
private static var green: String { "\u{001B}[32m" }
private static var yellow: String { "\u{001B}[33m" }
private static var reset: String { "\u{001B}[0m" }
mutating func started(_ task: Task, scope: VariableScope) {
self.print(task, scope, "\(Self.green)building\(Self.reset)")
}
mutating func skipped(_ task: Task, scope: VariableScope) {
self.print(task, scope, "\(Self.yellow)skipped\(Self.reset)")
}
private mutating func print(_ task: Task, _ scope: VariableScope, _ message: @autoclosure () -> String) {
guard !task.attributes.contains(.silent) else { return }
self.printProgress(Context(subject: task, total: self.total, built: self.built, scope: scope), message())
self.built += 1
}
}
/// Computes the total number of tasks to build used for progress display
private func computeTotalTasksForDisplay(task: Task) -> Int {
var visited = Set<TaskKey>()
func visit(task: Task) -> Int {
guard !visited.contains(task.key) else { return 0 }
visited.insert(task.key)
var total = task.attributes.contains(.silent) ? 0 : 1
for want in task.wants {
total += visit(task: self.tasks[want]!)
}
return total
}
return visit(task: task)
}
/// Cleans all outputs of all tasks
func cleanEverything(scope: VariableScope) {
for task in self.tasks.values {
try? FileManager.default.removeItem(at: scope.resolve(path: task.output))
}
}
/// Starts building
func build(output: TaskKey, scope: VariableScope) throws {
/// Returns true if any of the task's inputs have a modification date later than the task's output
func shouldBuild(task: Task) -> Bool {
if task.attributes.contains(.phony) {
return true
}
let outputURL = scope.resolve(path: task.output)
if !FileManager.default.fileExists(atPath: outputURL.path) {
explain("Task \(task.output) should be built because it doesn't exist")
return true
}
let outputMtime = try? outputURL.resourceValues(forKeys: [.contentModificationDateKey])
.contentModificationDate
return task.inputs.contains { input in
let inputURL = scope.resolve(path: input)
// Ignore directory modification times
var isDirectory: ObjCBool = false
let fileExists = FileManager.default.fileExists(
atPath: inputURL.path, isDirectory: &isDirectory)
if fileExists && isDirectory.boolValue {
return false
}
let inputMtime = try? inputURL.resourceValues(forKeys: [.contentModificationDateKey]
).contentModificationDate
let shouldBuild =
outputMtime == nil || inputMtime == nil || outputMtime! < inputMtime!
if shouldBuild {
explain(
"Task \(task.output) should be re-built because \(input) is newer: \(outputMtime?.timeIntervalSince1970 ?? 0) < \(inputMtime?.timeIntervalSince1970 ?? 0)"
)
}
return shouldBuild
}
}
var progressPrinter = ProgressPrinter(
total: self.computeTotalTasksForDisplay(task: self.tasks[output]!),
printProgress: self.printProgress
)
// Make a copy of the tasks so we can mutate the state
var tasks = self.tasks
func runTask(taskKey: TaskKey) throws {
guard var task = tasks[taskKey] else {
violated("Task \(taskKey) not found")
return
}
guard !task.isDone else { return }
// Build dependencies first
for want in task.wants.sorted() {
try runTask(taskKey: want)
}
if shouldBuild(task: task) {
progressPrinter.started(task, scope: scope)
try task.build(task, scope)
} else {
progressPrinter.skipped(task, scope: scope)
}
task.isDone = true
tasks[taskKey] = task
}
try runTask(taskKey: output)
}
}
struct BuildPath: Encodable, Hashable, CustomStringConvertible {
enum Component: Hashable, CustomStringConvertible {
case prefix(variable: String)
case constant(String)
var description: String {
switch self {
case .prefix(let variable): return "$\(variable)"
case .constant(let path): return path
}
}
}
fileprivate let components: [Component]
var description: String { self.components.map(\.description).joined(separator: "/") }
init(phony: String) {
self.components = [.constant(phony)]
}
init(prefix: String, _ tail: String...) {
self.components = [.prefix(variable: prefix)] + tail.map(Component.constant)
}
init(absolute: String) {
self.components = [.constant(absolute)]
}
private init(components: [Component]) {
self.components = components
}
func appending(path: String) -> BuildPath {
return BuildPath(components: self.components + [.constant(path)])
}
func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.description)
}
}