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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2018-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 Basics
import Foundation
import LLBuildManifest
import SPMBuildCore
import struct TSCBasic.ByteString
/// Contains the description of the build that is needed during the execution.
public struct BuildDescription: Codable {
public typealias CommandName = String
public typealias TargetName = String
public typealias CommandLineFlag = String
/// The Swift compiler invocation targets.
let swiftCommands: [LLBuildManifest.CmdName: SwiftCompilerTool]
/// The Swift compiler frontend invocation targets.
let swiftFrontendCommands: [LLBuildManifest.CmdName: SwiftFrontendTool]
/// The map of test discovery commands.
let testDiscoveryCommands: [LLBuildManifest.CmdName: TestDiscoveryTool]
/// The map of test entry point commands.
let testEntryPointCommands: [LLBuildManifest.CmdName: TestEntryPointTool]
/// The map of copy commands.
let copyCommands: [LLBuildManifest.CmdName: CopyTool]
/// The map of write commands.
let writeCommands: [LLBuildManifest.CmdName: WriteAuxiliaryFile]
/// A flag that indicates this build should perform a check for whether targets only import
/// their explicitly-declared dependencies
let explicitTargetDependencyImportCheckingMode: BuildParameters.TargetDependencyImportCheckingMode
/// Every target's set of dependencies.
let targetDependencyMap: [TargetName: [TargetName]]
/// A full swift driver command-line invocation used to dependency-scan a given Swift target
let swiftTargetScanArgs: [TargetName: [CommandLineFlag]]
/// A set of all targets with generated source
let generatedSourceTargetSet: Set<TargetName>
/// The built test products.
public let builtTestProducts: [BuiltTestProduct]
/// Distilled information about any plugins defined in the package.
let pluginDescriptions: [PluginBuildDescription]
public init(
plan: BuildPlan,
swiftCommands: [LLBuildManifest.CmdName: SwiftCompilerTool],
swiftFrontendCommands: [LLBuildManifest.CmdName: SwiftFrontendTool],
testDiscoveryCommands: [LLBuildManifest.CmdName: TestDiscoveryTool],
testEntryPointCommands: [LLBuildManifest.CmdName: TestEntryPointTool],
copyCommands: [LLBuildManifest.CmdName: CopyTool],
writeCommands: [LLBuildManifest.CmdName: WriteAuxiliaryFile],
pluginDescriptions: [PluginBuildDescription]
) throws {
self.swiftCommands = swiftCommands
self.swiftFrontendCommands = swiftFrontendCommands
self.testDiscoveryCommands = testDiscoveryCommands
self.testEntryPointCommands = testEntryPointCommands
self.copyCommands = copyCommands
self.writeCommands = writeCommands
self.explicitTargetDependencyImportCheckingMode = plan.destinationBuildParameters.driverParameters
.explicitTargetDependencyImportCheckingMode
self.targetDependencyMap = try plan.targets
.reduce(into: [TargetName: [TargetName]]()) { partial, targetBuildDescription in
let deps = try targetBuildDescription.target.recursiveDependencies(
satisfying: targetBuildDescription.buildParameters.buildEnvironment
)
.compactMap(\.module).map(\.c99name)
partial[targetBuildDescription.target.c99name] = deps
}
var targetCommandLines: [TargetName: [CommandLineFlag]] = [:]
var generatedSourceTargets: [TargetName] = []
for description in plan.targets {
guard case .swift(let desc) = description else {
continue
}
let buildParameters = description.buildParameters
targetCommandLines[desc.target.c99name] =
try desc.emitCommandLine(scanInvocation: true) + [
"-driver-use-frontend-path", buildParameters.toolchain.swiftCompilerPath.pathString,
]
if case .discovery = desc.testTargetRole {
generatedSourceTargets.append(desc.target.c99name)
}
}
generatedSourceTargets.append(
contentsOf: plan.pluginDescriptions
.map(\.moduleC99Name)
)
self.swiftTargetScanArgs = targetCommandLines
self.generatedSourceTargetSet = Set(generatedSourceTargets)
self.builtTestProducts = try plan.buildProducts.filter { $0.product.type == .test }.map { desc in
try BuiltTestProduct(
productName: desc.product.name,
binaryPath: desc.binaryPath,
packagePath: desc.package.path,
testEntryPointPath: desc.product.underlying.testEntryPointPath
)
}
self.pluginDescriptions = pluginDescriptions
}
public func write(fileSystem: Basics.FileSystem, path: AbsolutePath) throws {
let encoder = JSONEncoder.makeWithDefaults()
let data = try encoder.encode(self)
try fileSystem.writeFileContents(path, bytes: ByteString(data))
}
public static func load(fileSystem: Basics.FileSystem, path: AbsolutePath) throws -> BuildDescription {
let contents: Data = try fileSystem.readFileContents(path)
let decoder = JSONDecoder.makeWithDefaults()
return try decoder.decode(BuildDescription.self, from: contents)
}
}
|