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 327 328 329 330 331 332 333 334 335
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2022 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 Dispatch
import Foundation
import SPMBuildCore
import Basics
import CoreCommands
import PackageGraph
import PackageModel
import SourceControl
import Workspace
import protocol TSCBasic.DiagnosticLocation
import class Basics.AsyncProcess
import struct Basics.AsyncProcessResult
import func TSCBasic.withTemporaryFile
import enum TSCUtility.Diagnostics
import struct TSCUtility.SerializedDiagnostics
import var TSCUtility.verbosity
/// Helper for emitting a JSON API baseline for a module.
struct APIDigesterBaselineDumper {
/// The revision to emit a baseline for.
let baselineRevision: Revision
/// The root package path.
let packageRoot: AbsolutePath
/// Parameters used when building end products.
let productsBuildParameters: BuildParameters
/// Parameters used when building tools (plugins and macros).
let toolsBuildParameters: BuildParameters
/// The API digester tool.
let apiDigesterTool: SwiftAPIDigester
/// The observabilityScope for emitting errors/warnings.
let observabilityScope: ObservabilityScope
init(
baselineRevision: Revision,
packageRoot: AbsolutePath,
productsBuildParameters: BuildParameters,
toolsBuildParameters: BuildParameters,
apiDigesterTool: SwiftAPIDigester,
observabilityScope: ObservabilityScope
) {
self.baselineRevision = baselineRevision
self.packageRoot = packageRoot
self.productsBuildParameters = productsBuildParameters
self.toolsBuildParameters = toolsBuildParameters
self.apiDigesterTool = apiDigesterTool
self.observabilityScope = observabilityScope
}
/// Emit the API baseline files and return the path to their directory.
func emitAPIBaseline(
for modulesToDiff: Set<String>,
at baselineDir: AbsolutePath?,
force: Bool,
logLevel: Basics.Diagnostic.Severity,
swiftCommandState: SwiftCommandState
) throws -> AbsolutePath {
var modulesToDiff = modulesToDiff
let apiDiffDir = productsBuildParameters.apiDiff
let baselineDir = (baselineDir ?? apiDiffDir).appending(component: baselineRevision.identifier)
let baselinePath: (String)->AbsolutePath = { module in
baselineDir.appending(component: module + ".json")
}
if !force {
// Baselines which already exist don't need to be regenerated.
modulesToDiff = modulesToDiff.filter {
!swiftCommandState.fileSystem.exists(baselinePath($0))
}
}
guard !modulesToDiff.isEmpty else {
// If none of the baselines need to be regenerated, return.
return baselineDir
}
// Setup a temporary directory where we can checkout and build the baseline treeish.
let baselinePackageRoot = apiDiffDir.appending("\(baselineRevision.identifier)-checkout")
if swiftCommandState.fileSystem.exists(baselinePackageRoot) {
try swiftCommandState.fileSystem.removeFileTree(baselinePackageRoot)
}
// Clone the current package in a sandbox and checkout the baseline revision.
let repositoryProvider = GitRepositoryProvider()
let specifier = RepositorySpecifier(path: baselinePackageRoot)
let workingCopy = try repositoryProvider.createWorkingCopy(
repository: specifier,
sourcePath: packageRoot,
at: baselinePackageRoot,
editable: false
)
try workingCopy.checkout(revision: baselineRevision)
// Create the workspace for this package.
let workspace = try Workspace(
forRootPackage: baselinePackageRoot,
cancellator: swiftCommandState.cancellator
)
let graph = try workspace.loadPackageGraph(
rootPath: baselinePackageRoot,
observabilityScope: self.observabilityScope
)
// Don't emit a baseline for a module that didn't exist yet in this revision.
modulesToDiff.formIntersection(graph.apiDigesterModules)
// Abort if we weren't able to load the package graph.
if observabilityScope.errorsReported {
throw Diagnostics.fatalError
}
// Update the data path input build parameters so it's built in the sandbox.
var productsBuildParameters = productsBuildParameters
productsBuildParameters.dataPath = workspace.location.scratchDirectory
// Build the baseline module.
// FIXME: We need to implement the build tool invocation closure here so that build tool plugins work with the APIDigester. rdar://86112934
let buildSystem = try swiftCommandState.createBuildSystem(
explicitBuildSystem: .native,
cacheBuildManifest: false,
productsBuildParameters: productsBuildParameters,
toolsBuildParameters: toolsBuildParameters,
packageGraphLoader: { graph }
)
try buildSystem.build()
// Dump the SDK JSON.
try swiftCommandState.fileSystem.createDirectory(baselineDir, recursive: true)
let group = DispatchGroup()
let semaphore = DispatchSemaphore(value: Int(productsBuildParameters.workers))
let errors = ThreadSafeArrayStore<Swift.Error>()
for module in modulesToDiff {
semaphore.wait()
DispatchQueue.sharedConcurrent.async(group: group) {
do {
try apiDigesterTool.emitAPIBaseline(
to: baselinePath(module),
for: module,
buildPlan: buildSystem.buildPlan
)
} catch {
errors.append(error)
}
semaphore.signal()
}
}
group.wait()
for error in errors.get() {
observabilityScope.emit(error)
}
if observabilityScope.errorsReported {
throw Diagnostics.fatalError
}
return baselineDir
}
}
/// A wrapper for the swift-api-digester tool.
public struct SwiftAPIDigester {
/// The file system to use
let fileSystem: FileSystem
/// The absolute path to `swift-api-digester` in the toolchain.
let tool: AbsolutePath
init(fileSystem: FileSystem, tool: AbsolutePath) {
self.fileSystem = fileSystem
self.tool = tool
}
/// Emit an API baseline file for the specified module at the specified location.
public func emitAPIBaseline(
to outputPath: AbsolutePath,
for module: String,
buildPlan: SPMBuildCore.BuildPlan
) throws {
var args = ["-dump-sdk", "-compiler-style-diags"]
args += try buildPlan.createAPIToolCommonArgs(includeLibrarySearchPaths: false)
args += ["-module", module, "-o", outputPath.pathString]
let result = try runTool(args)
if !self.fileSystem.exists(outputPath) {
throw Error.failedToGenerateBaseline(module: module)
}
try self.fileSystem.readFileContents(outputPath).withData { data in
if let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any] {
guard let abiRoot = jsonObject["ABIRoot"] as? [String:Any] else {
throw Error.failedToValidateBaseline(module: module)
}
guard let symbols = abiRoot["children"] as? NSArray, symbols.count > 0 else {
throw Error.noSymbolsInBaseline(module: module, toolOutput: try result.utf8Output())
}
}
}
}
/// Compare the current package API to a provided baseline file.
public func compareAPIToBaseline(
at baselinePath: AbsolutePath,
for module: String,
buildPlan: SPMBuildCore.BuildPlan,
except breakageAllowlistPath: AbsolutePath?
) throws -> ComparisonResult? {
var args = [
"-diagnose-sdk",
"-baseline-path", baselinePath.pathString,
"-module", module
]
args.append(contentsOf: try buildPlan.createAPIToolCommonArgs(includeLibrarySearchPaths: false))
if let breakageAllowlistPath {
args.append(contentsOf: ["-breakage-allowlist-path", breakageAllowlistPath.pathString])
}
return try? withTemporaryFile(deleteOnClose: false) { file in
args.append(contentsOf: ["-serialize-diagnostics-path", file.path.pathString])
try runTool(args)
let contents = try self.fileSystem.readFileContents(file.path)
guard contents.count > 0 else {
return nil
}
let serializedDiagnostics = try SerializedDiagnostics(bytes: contents)
let apiDigesterCategory = "api-digester-breaking-change"
let apiBreakingChanges = serializedDiagnostics.diagnostics.filter { $0.category == apiDigesterCategory }
let otherDiagnostics = serializedDiagnostics.diagnostics.filter { $0.category != apiDigesterCategory }
return ComparisonResult(moduleName: module,
apiBreakingChanges: apiBreakingChanges,
otherDiagnostics: otherDiagnostics)
}
}
@discardableResult private func runTool(_ args: [String]) throws -> AsyncProcessResult {
let arguments = [tool.pathString] + args
let process = AsyncProcess(
arguments: arguments,
outputRedirection: .collect(redirectStderr: true)
)
try process.launch()
return try process.waitUntilExit()
}
}
extension SwiftAPIDigester {
public enum Error: Swift.Error, CustomStringConvertible {
case failedToGenerateBaseline(module: String)
case failedToValidateBaseline(module: String)
case noSymbolsInBaseline(module: String, toolOutput: String)
public var description: String {
switch self {
case .failedToGenerateBaseline(let module):
return "failed to generate baseline for \(module)"
case .failedToValidateBaseline(let module):
return "failed to validate baseline for \(module)"
case .noSymbolsInBaseline(let module, let toolOutput):
return "baseline for \(module) contains no symbols, swift-api-digester output: \(toolOutput)"
}
}
}
}
extension SwiftAPIDigester {
/// The result of comparing a module's API to a provided baseline.
public struct ComparisonResult {
/// The name of the module being diffed.
var moduleName: String
/// Breaking changes made to the API since the baseline was generated.
var apiBreakingChanges: [SerializedDiagnostics.Diagnostic]
/// Other diagnostics emitted while comparing the current API to the baseline.
var otherDiagnostics: [SerializedDiagnostics.Diagnostic]
/// `true` if the comparison succeeded and no breaking changes were found, otherwise `false`.
var hasNoAPIBreakingChanges: Bool {
apiBreakingChanges.isEmpty && otherDiagnostics.filter { [.fatal, .error].contains($0.level) }.isEmpty
}
}
}
extension BuildParameters {
/// The directory containing artifacts for API diffing operations.
var apiDiff: AbsolutePath {
dataPath.appending("apidiff")
}
}
extension ModulesGraph {
/// The list of modules that should be used as an input to the API digester.
var apiDigesterModules: [String] {
self.rootPackages
.flatMap(\.products)
.filter { $0.type.isLibrary }
.flatMap(\.modules)
.filter { $0.underlying is SwiftModule }
.map { $0.c99name }
}
}
extension SerializedDiagnostics.SourceLocation {
public var description: String {
return "\(filename):\(line):\(column)"
}
}
#if compiler(<6.0)
extension SerializedDiagnostics.SourceLocation: DiagnosticLocation {}
#else
extension SerializedDiagnostics.SourceLocation: @retroactive DiagnosticLocation {}
#endif
|