swiftlang / swift-testing

A modern, expressive testing package for Swift
Apache License 2.0
1.81k stars 77 forks source link

Documentation: Update CMake.md to use the ABI entry point #828

Open ADKaster opened 1 week ago

ADKaster commented 1 week ago

The SwiftPM entry point is unstable and the new ABIv0 entry point has already been added to the library.

Motivation:

Using the SwiftPM entry point when building tests from a CMake project as recommended in the documentation is outdated and unwise.

Modifications:

Replace the example with one using the new ABIv0 entry point.

Result:

CMake projects should stop relying on the SwiftPM entry point.

Checklist:

ADKaster commented 1 week ago

cc @compnerd

ADKaster commented 1 week ago

Hm. Chatting with Jonathan on Slack some more, it seems like the fact that this works with the Swift OSS toolchain on linux is a toolchain bug: we shouldn't be able to access the private module from user code.

Another alternative (with -enable-experimental-feature Extern) looks like this:

import Foundation

typealias EntryPoint = @convention(thin) @Sendable (_ configurationJSON: UnsafeRawBufferPointer?, _ recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void) async throws -> Bool

@_extern(c, "swt_abiv0_getEntryPoint")
func swt_abiv0_getEntryPoint() -> UnsafeRawPointer

@main struct Runner {
    static func main() async throws {
        let configurationJSON: UnsafeRawBufferPointer? = nil
        let recordHandler: @Sendable (UnsafeRawBufferPointer) -> Void = { _ in }

        let entryPoint = unsafeBitCast(swt_abiv0_getEntryPoint(), to: EntryPoint.self)

        if try await entryPoint(configurationJSON, recordHandler) {
            exit(EXIT_SUCCESS)
        } else {
            exit(EXIT_FAILURE)
        }
    }
}