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
|
//===--- NinjaBuildFile.swift ---------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//
struct NinjaBuildFile {
var bindings: Bindings
var rules: [String: Rule]
var buildEdges: [BuildEdge] = []
init(
bindings: [String: String],
rules: [String: Rule],
buildEdges: [BuildEdge]
) {
self.bindings = Bindings(storage: bindings)
self.buildEdges = buildEdges
self.rules = rules
}
}
extension NinjaBuildFile {
var buildConfiguration: BuildConfiguration? {
bindings[.configuration]
.flatMap { BuildConfiguration(rawValue: $0) }
}
}
extension NinjaBuildFile {
struct Bindings: Hashable {
let values: [String: String]
init(storage: [String : String]) {
self.values = storage
}
subscript(key: String) -> String? {
values[key]
}
}
struct Rule: Equatable {
let name: String
var bindings: Bindings
init(name: String, bindings: [String: String]) {
self.name = name
self.bindings = Bindings(storage: bindings)
}
}
struct BuildEdge: Hashable {
let ruleName: String
let inputs: [String]
let outputs: [String]
let dependencies: [String]
var bindings: Bindings
var isPhony: Bool {
ruleName == "phony"
}
init(
ruleName: String,
inputs: [String], outputs: [String], dependencies: [String],
bindings: [String: String]
) {
self.ruleName = ruleName
self.inputs = inputs
self.outputs = outputs
self.dependencies = dependencies
self.bindings = Bindings(storage: bindings)
}
static func phony(for outputs: [String], inputs: [String]) -> Self {
return Self(
ruleName: "phony", inputs: inputs, outputs: outputs, dependencies: [], bindings: [:]
)
}
}
}
fileprivate enum NinjaCommandLineError: Error {
case unknownRule(String)
case missingCommandBinding
}
extension NinjaBuildFile {
func commandLine(for edge: BuildEdge) throws -> String {
guard let rule = self.rules[edge.ruleName] else {
throw NinjaCommandLineError.unknownRule(edge.ruleName)
}
// Helper to get a substitution value for ${key}.
// Note that we don't do built-in substitutions (e.g. $in, $out) for now.
func value(for key: String) -> String? {
edge.bindings[key] ?? rule.bindings[key] ?? self.bindings[key]
}
func eval(string: String) -> String {
var result = ""
string.scanningUTF8 { scanner in
while scanner.hasInput {
if let prefix = scanner.eat(while: { $0 != "$" }) {
result += String(utf8: prefix)
}
guard scanner.tryEat("$") else {
// Reached the end.
break
}
let substituted: String? = scanner.tryEating { scanner in
// Parse the variable name.
let key: String
if scanner.tryEat("{"), let keyName = scanner.eat(while: { $0 != "}" }), scanner.tryEat("}") {
key = String(utf8: keyName)
} else if let keyName = scanner.eat(while: { $0.isNinjaVarName }) {
key = String(utf8: keyName)
} else {
return nil
}
return value(for: key)
}
if let substituted {
// Recursive substitutions.
result += eval(string: substituted)
} else {
// Was not a variable, restore '$' and move on.
result += "$"
}
}
}
return result
}
guard let commandLine = rule.bindings["command"] else {
throw NinjaCommandLineError.missingCommandBinding
}
return eval(string: commandLine)
}
}
extension Byte {
fileprivate var isNinjaVarName: Bool {
switch self {
case "0"..."9", "a"..."z", "A"..."Z", "_", "-":
return true
default:
return false
}
}
}
extension NinjaBuildFile: CustomDebugStringConvertible {
var debugDescription: String {
buildEdges.map(\.debugDescription).joined(separator: "\n")
}
}
extension NinjaBuildFile.BuildEdge: CustomDebugStringConvertible {
var debugDescription: String {
"""
{
inputs: \(inputs)
outputs: \(outputs)
dependencies: \(dependencies)
bindings: \(bindings)
isPhony: \(isPhony)
}
"""
}
}
extension NinjaBuildFile.Bindings {
enum Key: String {
case configuration = "CONFIGURATION"
case defines = "DEFINES"
case flags = "FLAGS"
case includes = "INCLUDES"
case swiftModule = "SWIFT_MODULE"
case swiftModuleName = "SWIFT_MODULE_NAME"
case swiftLibraryName = "SWIFT_LIBRARY_NAME"
case swiftSources = "SWIFT_SOURCES"
}
subscript(key: Key) -> String? {
return self[key.rawValue]
}
}
|