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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023-2024 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 Foundation
import TSCBasic
// Ideally wouldn't expose these (it defeats the purpose of this module), but we should replace this entire API with
// a BSP server, so this is good enough for now (and LSP is using all these types internally anyway).
import Basics
import Build
import PackageGraph
internal import PackageLoading
import PackageModel
import SPMBuildCore
public enum BuildDestination {
case host
case target
}
public enum BuildTargetCompiler {
case swift
case clang
}
/// Information about a source file that belongs to a target.
public struct SourceItem {
/// The URL of the source file itself.
public let sourceFile: URL
/// If the file has a unique output path (eg. for clang files), the output paths. `nil` for eg. Swift targets,
/// which don't have unique output paths for each file.
public let outputFile: URL?
}
public protocol BuildTarget {
/// Source files in the target
var sources: [SourceItem] { get }
/// Header files in the target
var headers: [URL] { get }
/// The resource files in the target.
var resources: [URL] { get }
/// Files in the target that were marked as ignored.
var ignored: [URL] { get }
/// Other kinds of files in the target.
var others: [URL] { get }
/// The name of the target. It should be possible to build a target by passing this name to `swift build --target`
var name: String { get }
/// The compiler that is responsible for building this target.
var compiler: BuildTargetCompiler { get }
var destination: BuildDestination { get }
/// Whether the target is part of the root package that the user opened or if it's part of a package dependency.
var isPartOfRootPackage: Bool { get }
var isTestTarget: Bool { get }
func compileArguments(for fileURL: URL) throws -> [String]
}
private struct WrappedClangTargetBuildDescription: BuildTarget {
private let description: ClangModuleBuildDescription
let isPartOfRootPackage: Bool
let isTestTarget: Bool
init(description: ClangModuleBuildDescription, isPartOfRootPackage: Bool) {
self.description = description
self.isPartOfRootPackage = isPartOfRootPackage
self.isTestTarget = description.isTestTarget
}
public var sources: [SourceItem] {
guard let compilePaths = try? description.compilePaths() else {
return []
}
return compilePaths.map {
SourceItem(sourceFile: $0.source.asURL, outputFile: $0.object.asURL)
}
}
public var headers: [URL] {
return description.clangTarget.headers.map(\.asURL)
}
var resources: [URL] {
return description.resources.map(\.path.asURL)
}
var ignored: [URL] {
return description.ignored.map(\.asURL)
}
var others: [URL] {
var others = Set(description.others)
for pluginResult in description.buildToolPluginInvocationResults {
for buildCommand in pluginResult.buildCommands {
others.formUnion(buildCommand.inputFiles)
}
}
return others.map(\.asURL)
}
public var name: String {
return description.clangTarget.name
}
var compiler: BuildTargetCompiler { .clang }
public var destination: BuildDestination {
return description.destination == .host ? .host : .target
}
public func compileArguments(for fileURL: URL) throws -> [String] {
let filePath = try resolveSymlinks(try Basics.AbsolutePath(validating: fileURL.path))
let commandLine = try description.emitCommandLine(for: filePath)
// First element on the command line is the compiler itself, not an argument.
return Array(commandLine.dropFirst())
}
}
private struct WrappedSwiftTargetBuildDescription: BuildTarget {
private let description: SwiftModuleBuildDescription
let isPartOfRootPackage: Bool
let isTestTarget: Bool
init(description: SwiftModuleBuildDescription, isPartOfRootPackage: Bool) {
self.description = description
self.isPartOfRootPackage = isPartOfRootPackage
self.isTestTarget = description.isTestTarget
}
public var name: String {
return description.target.name
}
var compiler: BuildTargetCompiler { .swift }
public var destination: BuildDestination {
return description.destination == .host ? .host : .target
}
var sources: [SourceItem] {
return description.sources.map {
return SourceItem(sourceFile: $0.asURL, outputFile: nil)
}
}
var headers: [URL] { [] }
var resources: [URL] {
return description.resources.map(\.path.asURL)
}
var ignored: [URL] {
return description.ignored.map(\.asURL)
}
var others: [URL] {
var others = Set(description.others)
for pluginResult in description.buildToolPluginInvocationResults {
for buildCommand in pluginResult.buildCommands {
others.formUnion(buildCommand.inputFiles)
}
}
return others.map(\.asURL)
}
var outputPaths: [URL] {
get throws {
struct NotSupportedError: Error, CustomStringConvertible {
var description: String { "Getting output paths for a Swift target is not supported" }
}
throw NotSupportedError()
}
}
func compileArguments(for fileURL: URL) throws -> [String] {
// Note: we ignore the `fileURL` here as the expectation is that we get a command line for the entire target
// in case of Swift.
let commandLine = try description.emitCommandLine(scanInvocation: false, writeOutputFileMap: false)
// First element on the command line is the compiler itself, not an argument.
return Array(commandLine.dropFirst())
}
}
public struct BuildDescription {
private let buildPlan: Build.BuildPlan
/// The inputs of the build plan so we don't need to re-compute them on every call to
/// `fileAffectsSwiftOrClangBuildSettings`.
private let inputs: [Build.BuildPlan.Input]
/// Wrap an already constructed build plan.
public init(buildPlan: Build.BuildPlan) {
self.buildPlan = buildPlan
self.inputs = buildPlan.inputs
}
/// Construct a build description, compiling build tool plugins and generating their output when necessary.
public static func load(
destinationBuildParameters: BuildParameters,
toolsBuildParameters: BuildParameters,
packageGraph: ModulesGraph,
pluginConfiguration: PluginConfiguration,
traitConfiguration: TraitConfiguration,
disableSandbox: Bool,
scratchDirectory: URL,
fileSystem: any FileSystem,
observabilityScope: ObservabilityScope
) async throws -> (description: BuildDescription, errors: String) {
let bufferedOutput = BufferedOutputByteStream()
let threadSafeOutput = ThreadSafeOutputByteStream(bufferedOutput)
// This is quite an abuse of `BuildOperation`, building plugins should really be refactored out of it. Though
// even better would be to have a BSP server that handles both preparing and getting settings.
// https://github.com/swiftlang/swift-package-manager/issues/8287
let operation = BuildOperation(
productsBuildParameters: destinationBuildParameters,
toolsBuildParameters: toolsBuildParameters,
cacheBuildManifest: true,
packageGraphLoader: { packageGraph },
pluginConfiguration: pluginConfiguration,
scratchDirectory: try Basics.AbsolutePath(validating: scratchDirectory.path),
traitConfiguration: traitConfiguration,
additionalFileRules: FileRuleDescription.swiftpmFileTypes,
pkgConfigDirectories: [],
outputStream: threadSafeOutput,
logLevel: .error,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
let plan = try await operation.generatePlan()
return (BuildDescription(buildPlan: plan), bufferedOutput.bytes.description)
}
func getBuildTarget(
for module: ResolvedModule,
destination: BuildParameters.Destination
) -> BuildTarget? {
if let description = self.buildPlan.description(for: module, context: destination) {
let modulesGraph = self.buildPlan.graph
switch description {
case .clang(let description):
return WrappedClangTargetBuildDescription(
description: description,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(description.package.id)
)
case .swift(let description):
return WrappedSwiftTargetBuildDescription(
description: description,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(description.package.id)
)
}
} else {
if module.type == .plugin, let package = self.buildPlan.graph.package(for: module) {
let modulesGraph = self.buildPlan.graph
return PluginTargetBuildDescription(
target: module,
toolsVersion: package.manifest.toolsVersion,
toolchain: buildPlan.toolsBuildParameters.toolchain,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(package.id)
)
}
return nil
}
}
public func traverseModules(
callback: (any BuildTarget, _ parent: (any BuildTarget)?) -> Void
) {
self.buildPlan.traverseModules { module, parent in
let parentDescription: (any BuildTarget)? = if let parent {
getBuildTarget(for: parent.0, destination: parent.1)
} else {
nil
}
if let description = getBuildTarget(for: module.0, destination: module.1) {
callback(description, parentDescription)
}
}
}
/// Returns `true` if the file at the given path might influence build settings for a `swiftc` or `clang` invocation
/// generated by SwiftPM.
public func fileAffectsSwiftOrClangBuildSettings(_ url: URL) -> Bool {
guard let filePath = try? Basics.AbsolutePath(validating: url.path) else {
return false
}
for input in self.inputs {
switch input {
case .directoryStructure(let path):
if path.isAncestor(of: filePath) {
return true
}
case .file(let path):
if filePath == path {
return true
}
}
}
return false
}
}
|