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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
//===----------------------------------------------------------------------===//
//
// 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 SPMLLBuild
import class TSCBasic.LocalFileOutputByteStream
import class TSCUtility.IndexStore
class CustomLLBuildCommand: SPMLLBuild.ExternalCommand {
let context: BuildExecutionContext
required init(_ context: BuildExecutionContext) {
self.context = context
}
func getSignature(_: SPMLLBuild.Command) -> [UInt8] {
[]
}
func execute(
_: SPMLLBuild.Command,
_: SPMLLBuild.BuildSystemCommandInterface
) -> Bool {
fatalError("subclass responsibility")
}
}
private protocol TestBuildCommand {}
extension IndexStore.TestCaseClass.TestMethod {
fileprivate var allTestsEntry: String {
let baseName = name.hasSuffix("()") ? String(name.dropLast(2)) : name
return "(\"\(baseName)\", \(isAsync ? "asyncTest(\(baseName))" : baseName))"
}
}
extension TestEntryPointTool {
public static var mainFileName: String {
"runner.swift"
}
}
final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
private func write(
tests: [IndexStore.TestCaseClass],
forModule module: String,
fileSystem: Basics.FileSystem,
path: AbsolutePath
) throws {
let testsByClassNames = Dictionary(grouping: tests, by: { $0.name }).sorted(by: { $0.key < $1.key })
var content = "import XCTest\n"
content += "@testable import \(module)\n"
for iterator in testsByClassNames {
// 'className' provides uniqueness for derived class.
let className = iterator.key
let testMethods = iterator.value.flatMap(\.testMethods)
content +=
#"""
fileprivate extension \#(className) {
@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
static nonisolated(unsafe) let __allTests__\#(className) = [
\#(testMethods.map(\.allTestsEntry).joined(separator: ",\n "))
]
}
"""#
}
content +=
#"""
@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
func __\#(module)__allTests() -> [XCTestCaseEntry] {
return [
\#(
testsByClassNames.map { "testCase(\($0.key).__allTests__\($0.key))" }
.joined(separator: ",\n ")
)
]
}
"""#
try fileSystem.writeFileContents(path, string: content)
}
private func execute(fileSystem: Basics.FileSystem, tool: TestDiscoveryTool) throws {
let outputs = tool.outputs.compactMap { try? AbsolutePath(validating: $0.name) }
if case .loadableBundle = context.productsBuildParameters.testingParameters.testProductStyle {
// When building an XCTest bundle, test discovery is handled by the
// test harness process (i.e. this is the Darwin path.)
for file in outputs {
try fileSystem.writeIfChanged(path: file, string: "")
}
return
}
let index = self.context.productsBuildParameters.indexStore
let api = try self.context.indexStoreAPI.get()
let store = try IndexStore.open(store: TSCAbsolutePath(index), api: api)
// FIXME: We can speed this up by having one llbuild command per object file.
let tests = try store
.listTests(in: tool.inputs.map { try TSCAbsolutePath(AbsolutePath(validating: $0.name)) })
let testsByModule = Dictionary(grouping: tests, by: { $0.module.spm_mangledToC99ExtendedIdentifier() })
// Find the main file path.
guard let mainFile = outputs.first(where: { path in
path.basename == TestDiscoveryTool.mainFileName
}) else {
throw InternalError("main output (\(TestDiscoveryTool.mainFileName)) not found")
}
// Write one file for each test module.
//
// We could write everything in one file but that can easily run into type conflicts due
// in complex packages with large number of test modules.
for file in outputs where file != mainFile {
// FIXME: This is relying on implementation detail of the output but passing the
// the context all the way through is not worth it right now.
let module = file.basenameWithoutExt.spm_mangledToC99ExtendedIdentifier()
guard let tests = testsByModule[module] else {
// This module has no tests so just write an empty file for it.
try fileSystem.writeFileContents(file, bytes: "")
continue
}
try write(
tests: tests,
forModule: module,
fileSystem: fileSystem,
path: file
)
}
let testsKeyword = tests.isEmpty ? "let" : "var"
// Write the main file.
let stream = try LocalFileOutputByteStream(mainFile)
stream.send(
#"""
import XCTest
@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
public func __allDiscoveredTests() -> [XCTestCaseEntry] {
\#(testsKeyword) tests = [XCTestCaseEntry]()
\#(testsByModule.keys.map { "tests += __\($0)__allTests()" }.joined(separator: "\n "))
return tests
}
"""#
)
stream.flush()
}
override func execute(
_ command: SPMLLBuild.Command,
_: SPMLLBuild.BuildSystemCommandInterface
) -> Bool {
do {
// This tool will never run without the build description.
guard let buildDescription = self.context.buildDescription else {
throw InternalError("unknown build description")
}
guard let tool = buildDescription.testDiscoveryCommands[command.name] else {
throw InternalError("command \(command.name) not registered")
}
try self.execute(fileSystem: self.context.fileSystem, tool: tool)
return true
} catch {
self.context.observabilityScope.emit(error)
return false
}
}
}
final class TestEntryPointCommand: CustomLLBuildCommand, TestBuildCommand {
private func execute(fileSystem: Basics.FileSystem, tool: TestEntryPointTool) throws {
let outputs = tool.outputs.compactMap { try? AbsolutePath(validating: $0.name) }
// Find the main output file
let mainFileName = TestEntryPointTool.mainFileName
guard let mainFile = outputs.first(where: { path in
path.basename == mainFileName
}) else {
throw InternalError("main file output (\(mainFileName)) not found")
}
// Write the main file.
let stream = try LocalFileOutputByteStream(mainFile)
// Find the inputs, which are the names of the test discovery module(s)
let inputs = tool.inputs.compactMap { try? AbsolutePath(validating: $0.name) }
let discoveryModuleNames = inputs.map(\.basenameWithoutExt)
let testObservabilitySetup: String
let buildParameters = self.context.productsBuildParameters
if buildParameters.testingParameters.experimentalTestOutput && buildParameters.triple.supportsTestSummary {
testObservabilitySetup = "_ = SwiftPMXCTestObserver()\n"
} else {
testObservabilitySetup = ""
}
let isXCTMainAvailable: String = switch buildParameters.testingParameters.testProductStyle {
case .entryPointExecutable:
"canImport(XCTest)"
case .loadableBundle:
"false"
}
/// On WASI, we can't block the main thread, so XCTestMain is defined as async.
let awaitXCTMainKeyword = if buildParameters.triple.isWASI() {
"await"
} else {
""
}
var needsAsyncMainWorkaround = false
if buildParameters.triple.isLinux() {
// FIXME: work around crash on Amazon Linux 2 when main function is async (rdar://128303921)
needsAsyncMainWorkaround = true
} else if buildParameters.triple.isDarwin() {
#if compiler(<5.10)
// FIXME: work around duplicate async_Main symbols (SEE https://github.com/swiftlang/swift/pull/69113)
needsAsyncMainWorkaround = true
#endif
}
stream.send(
#"""
#if canImport(Testing)
import Testing
#endif
#if \#(isXCTMainAvailable)
\#(generateTestObservationCode(buildParameters: buildParameters))
import XCTest
\#(discoveryModuleNames.map { "import \($0)" }.joined(separator: "\n"))
#endif
@main
@available(macOS 10.15, iOS 11, watchOS 4, tvOS 11, *)
@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
struct Runner {
private static func testingLibrary() -> String {
var iterator = CommandLine.arguments.makeIterator()
while let argument = iterator.next() {
if argument == "--testing-library", let libraryName = iterator.next() {
return libraryName.lowercased()
}
}
// Fallback if not specified: run XCTest (legacy behavior)
return "xctest"
}
#if \#(needsAsyncMainWorkaround)
@_silgen_name("$ss13_runAsyncMainyyyyYaKcF")
private static func _runAsyncMain(_ asyncFun: @Sendable @escaping () async throws -> ())
#endif
static func main() \#(needsAsyncMainWorkaround ? "" : "async") {
let testingLibrary = Self.testingLibrary()
#if canImport(Testing)
if testingLibrary == "swift-testing" {
#if \#(needsAsyncMainWorkaround)
_runAsyncMain {
await Testing.__swiftPMEntryPoint() as Never
}
#else
await Testing.__swiftPMEntryPoint() as Never
#endif
}
#endif
#if \#(isXCTMainAvailable)
if testingLibrary == "xctest" {
\#(testObservabilitySetup)
\#(awaitXCTMainKeyword) XCTMain(__allDiscoveredTests()) as Never
}
#endif
}
}
"""#
)
stream.flush()
}
override func execute(
_ command: SPMLLBuild.Command,
_: SPMLLBuild.BuildSystemCommandInterface
) -> Bool {
do {
// This tool will never run without the build description.
guard let buildDescription = self.context.buildDescription else {
throw InternalError("unknown build description")
}
guard let tool = buildDescription.testEntryPointCommands[command.name] else {
throw InternalError("command \(command.name) not registered")
}
try self.execute(fileSystem: self.context.fileSystem, tool: tool)
return true
} catch {
self.context.observabilityScope.emit(error)
return false
}
}
}
final class WriteAuxiliaryFileCommand: CustomLLBuildCommand {
override func getSignature(_ command: SPMLLBuild.Command) -> [UInt8] {
guard let buildDescription = self.context.buildDescription else {
self.context.observabilityScope.emit(error: "unknown build description")
return []
}
guard let tool = buildDescription.writeCommands[command.name] else {
self.context.observabilityScope.emit(error: "command \(command.name) not registered")
return []
}
do {
let encoder = JSONEncoder.makeWithDefaults()
var hash = Data()
hash += try encoder.encode(tool.inputs)
hash += try encoder.encode(tool.outputs)
return [UInt8](hash)
} catch {
self.context.observabilityScope.emit(error: "getSignature() failed: \(error.interpolationDescription)")
return []
}
}
override func execute(
_ command: SPMLLBuild.Command,
_: SPMLLBuild.BuildSystemCommandInterface
) -> Bool {
let outputFilePath: AbsolutePath
let tool: WriteAuxiliaryFile!
do {
guard let buildDescription = self.context.buildDescription else {
throw InternalError("unknown build description")
}
guard let _tool = buildDescription.writeCommands[command.name] else {
throw StringError("command \(command.name) not registered")
}
tool = _tool
guard let output = tool.outputs.first, output.kind == .file else {
throw StringError("invalid output path")
}
outputFilePath = try AbsolutePath(validating: output.name)
} catch {
self.context.observabilityScope
.emit(error: "failed to write auxiliary file: \(error.interpolationDescription)")
return false
}
do {
try self.context.fileSystem.writeIfChanged(path: outputFilePath, string: self.getFileContents(tool: tool))
return true
} catch {
self.context.observabilityScope
.emit(
error: "failed to write auxiliary file '\(outputFilePath.pathString)': \(error.interpolationDescription)"
)
return false
}
}
func getFileContents(tool: WriteAuxiliaryFile) throws -> String {
guard tool.inputs.first?.kind == .virtual,
let generatedFileType = tool.inputs.first?.name.dropFirst().dropLast()
else {
throw StringError("invalid inputs")
}
for fileType in WriteAuxiliary.fileTypes {
if generatedFileType == fileType.name {
return try fileType.getFileContents(inputs: Array(tool.inputs.dropFirst()))
}
}
throw InternalError("unhandled generated file type '\(generatedFileType)'")
}
}
final class PackageStructureCommand: CustomLLBuildCommand {
override func getSignature(_: SPMLLBuild.Command) -> [UInt8] {
let encoder = JSONEncoder.makeWithDefaults()
// Include build parameters and process env in the signature.
var hash = Data()
hash += try! encoder.encode(self.context.productsBuildParameters)
hash += try! encoder.encode(self.context.toolsBuildParameters)
hash += try! encoder.encode(Environment.current)
return [UInt8](hash)
}
override func execute(
_: SPMLLBuild.Command,
_: SPMLLBuild.BuildSystemCommandInterface
) -> Bool {
self.context.packageStructureDelegate.packageStructureChanged()
}
}
final class CopyCommand: CustomLLBuildCommand {
override func execute(
_ command: SPMLLBuild.Command,
_: SPMLLBuild.BuildSystemCommandInterface
) -> Bool {
do {
// This tool will never run without the build description.
guard let buildDescription = self.context.buildDescription else {
throw InternalError("unknown build description")
}
guard let tool = buildDescription.copyCommands[command.name] else {
throw StringError("command \(command.name) not registered")
}
let input = try AbsolutePath(validating: tool.inputs[0].name)
let output = try AbsolutePath(validating: tool.outputs[0].name)
try self.context.fileSystem.createDirectory(output.parentDirectory, recursive: true)
try self.context.fileSystem.removeFileTree(output)
try self.context.fileSystem.copy(from: input, to: output)
} catch {
self.context.observabilityScope.emit(error)
return false
}
return true
}
}
|