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
|
//===--- SwiftTargets.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 SwiftTargets {
private var targets: [SwiftTarget] = []
private var outputAliases: [String: [String]] = [:]
private var dependenciesByTargetName: [String: Set<String>] = [:]
private var targetsByName: [String: SwiftTarget] = [:]
private var targetsByOutput: [String: SwiftTarget] = [:]
private var addedFiles: Set<RelativePath> = []
// Track some state for debugging
private var debugLogUnknownFlags: Set<String> = []
init(for buildDir: RepoBuildDir) throws {
log.debug("[*] Reading Swift targets from build.ninja")
for rule in try buildDir.ninjaFile.buildEdges {
try tryAddTarget(for: rule, buildDir: buildDir)
}
targets.sort(by: { $0.name < $1.name })
log.debug("-------- SWIFT TARGET DEPS --------")
for target in targets {
var deps: Set<SwiftTarget> = []
for dep in dependenciesByTargetName[target.name] ?? [] {
for output in allOutputs(for: dep) {
guard let depTarget = targetsByOutput[output] else { continue }
deps.insert(depTarget)
}
}
target.dependencies = deps.sorted(by: \.name)
log.debug("| '\(target.name)' has deps: \(target.dependencies)")
}
log.debug("-----------------------------------")
if !debugLogUnknownFlags.isEmpty {
log.debug("---------- UNKNOWN FLAGS ----------")
for flag in debugLogUnknownFlags.sorted() {
log.debug("| \(flag)")
}
log.debug("-----------------------------------")
}
}
private func allOutputs(for output: String) -> Set<String> {
// TODO: Should we attempt to do canonicalization instead?
var stack: [String] = [output]
var results: Set<String> = []
while let last = stack.popLast() {
guard results.insert(last).inserted else { continue }
for alias in outputAliases[last] ?? [] {
stack.append(alias)
}
}
return results
}
private mutating func computeBuildArgs(
for edge: NinjaBuildFile.BuildEdge,
in ninja: NinjaBuildFile
) throws -> BuildArgs? {
let commandLine = try ninja.commandLine(for: edge)
let command = try CommandParser.parseKnownCommandOnly(commandLine)
guard let command, command.executable.knownCommand == .swiftc else {
return nil
}
var buildArgs = BuildArgs(for: .swiftc, args: command.args)
// Only include known flags for now.
buildArgs = buildArgs.filter { arg in
if arg.flag != nil {
return true
}
if log.logLevel <= .debug {
// Note the unknown flags.
guard let value = arg.value, value.hasPrefix("-") else { return false }
debugLogUnknownFlags.insert(value)
}
return false
}
return buildArgs
}
/// Check to see if this is a forced-XXX-dep.swift file, which is only used
/// to hack around CMake dependencies, and can be dropped.
private func isForcedDepSwiftFile(_ path: AbsolutePath) -> Bool {
path.fileName.scanningUTF8 { scanner in
guard scanner.tryEat(utf8: "forced-") else {
return false
}
while scanner.tryEat() {
if scanner.tryEat(utf8: "-dep.swift"), !scanner.hasInput {
return true
}
}
return false
}
}
func getSources(
from edge: NinjaBuildFile.BuildEdge, buildDir: RepoBuildDir
) throws -> SwiftTarget.Sources {
let files: [AnyPath] = edge.inputs.map(AnyPath.init)
// Split the files into repo sources and external sources. Repo sources
// are those under the repo path, external sources are outside that path,
// and are either for dependencies such as swift-syntax, or are generated
// from e.g de-gyb'ing.
var sources = SwiftTarget.Sources()
for input in files where input.language == .swift {
switch input {
case .relative(let r):
// A relative path is for a file in the build directory, it's external.
let abs = buildDir.path.appending(r)
guard abs.exists else { continue }
sources.externalSources.append(abs)
case .absolute(let a):
guard a.exists, let rel = a.removingPrefix(buildDir.repoPath) else {
sources.externalSources.append(a)
continue
}
sources.repoSources.append(rel)
}
}
// Avoid adding forced dependency files.
sources.externalSources = sources.externalSources
.filter { !isForcedDepSwiftFile($0) }
return sources
}
private mutating func tryAddTarget(
for edge: NinjaBuildFile.BuildEdge,
buildDir: RepoBuildDir
) throws {
// Phonies are only used to track aliases.
if edge.isPhony {
for output in edge.outputs {
outputAliases[output, default: []] += edge.inputs
}
return
}
// Ignore build rules that don't have object file or swiftmodule outputs.
let forBuild = edge.outputs.contains(
where: { $0.hasExtension(.o) }
)
let forModule = edge.outputs.contains(
where: { $0.hasExtension(.swiftmodule) }
)
guard forBuild || forModule else {
return
}
let primaryOutput = edge.outputs.first!
let sources = try getSources(from: edge, buildDir: buildDir)
let repoSources = sources.repoSources
let externalSources = sources.externalSources
// Is this for a build (producing a '.o'), we need to have at least one
// repo source. Module dependencies can use external sources.
guard !repoSources.isEmpty || (forModule && !externalSources.isEmpty) else {
return
}
guard let buildArgs = try computeBuildArgs(for: edge, in: buildDir.ninjaFile) else { return }
// Pick up the module name from the arguments, or use an explicitly
// specified module name if we have one. The latter might be invalid so
// may not be part of the build args (e.g 'swift-plugin-server'), but is
// fine for generation.
let moduleName = buildArgs.lastValue(for: .moduleName) ?? edge.bindings[.swiftModuleName]
guard let moduleName else {
log.debug("! Skipping Swift target with output \(primaryOutput); no module name")
return
}
let moduleLinkName = buildArgs.lastValue(for: .moduleLinkName) ?? edge.bindings[.swiftLibraryName]
let name = moduleLinkName ?? moduleName
// Add the dependencies. We track dependencies for any input files, along
// with any recorded swiftmodule dependencies.
dependenciesByTargetName.withValue(for: name, default: []) { deps in
deps.formUnion(
edge.inputs.filter {
$0.hasExtension(.swiftmodule) || $0.hasExtension(.o)
}
)
deps.formUnion(
edge.dependencies.filter { $0.hasExtension(.swiftmodule) }
)
}
var buildRule: SwiftTarget.BuildRule?
var emitModuleRule: SwiftTarget.EmitModuleRule?
if forBuild && !repoSources.isEmpty {
// Bail if we've already recorded a target with one of these inputs.
// TODO: Attempt to merge?
// TODO: Should we be doing this later?
for input in repoSources {
guard addedFiles.insert(input).inserted else {
log.debug("""
! Skipping '\(name)' with output '\(primaryOutput)'; \
contains input '\(input)' already added
""")
return
}
}
// We've already ensured that `repoSources` is non-empty.
let parent = repoSources.commonAncestor!
buildRule = .init(
parentPath: parent, sources: sources, buildArgs: buildArgs
)
}
if forModule {
emitModuleRule = .init(sources: sources, buildArgs: buildArgs)
}
let target = targetsByName[name] ?? {
log.debug("+ Discovered Swift target '\(name)' with output '\(primaryOutput)'")
let target = SwiftTarget(name: name, moduleName: moduleName)
targetsByName[name] = target
targets.append(target)
return target
}()
for output in edge.outputs {
targetsByOutput[output] = target
}
if buildRule == nil || target.buildRule == nil {
if let buildRule {
target.buildRule = buildRule
}
} else {
log.debug("""
! Skipping '\(name)' build rule for \
'\(primaryOutput)'; already added
""")
}
if emitModuleRule == nil || target.emitModuleRule == nil {
if let emitModuleRule {
target.emitModuleRule = emitModuleRule
}
} else {
log.debug("""
! Skipping '\(name)' emit module rule for \
'\(primaryOutput)'; already added
""")
}
}
func getTargets(below path: RelativePath) -> [SwiftTarget] {
targets.filter { target in
guard let parent = target.buildRule?.parentPath, parent.starts(with: path)
else {
return false
}
return true
}
}
}
|