Description
Motivation
Including swift-testing in the 6.x toolchains has made it easier than ever to include the testing framework in out-of-toolchain user projects.
Unfortunately, for CMake projects, some manual elbow grease is required. I'll link a swiftlang/swift issue here later that goes into more details on the hoops needed to set up a CMake project from 0.
In order to link a test main.swift against swift-testing on Linux, both the libTesting.so dylib and the libTestingMacros.so plugin dylib must be found. In order to actually find these dylibs from a Tarball or swiftly install of swift 6.0.3, there's a few steps:
- query
swiftc -print-target-info
to find the runtimeLibraryImportPaths (likely using CMake's builtin json parser/walker methods instead of jq)
$ swiftc -print-target-info | jq '.paths.runtimeLibraryImportPaths'
[
"/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2025-03-17/usr/lib/swift/linux",
"/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2025-03-17/usr/lib/swift/linux/x86_64"
]
find_library
forTesting
, using those pathsfind_library
forTestingMacros
, using a relative path of../host/plugins/testing
from the discovered paths. There is no information from print-target-info that gives the host path, or the host plugin path.- Create an imported target for
SwiftTesting::SwiftTesting
(or whatever one's preferred naming is). - Attach some
INTERFACE_COMPILE_OPTIONS
to the target to either-load-plugin-library
or-plugin-path
to where libTestingMacros.so is.
This logic is made slightly more complicated because the path to the plugin library is different depending on platform. For a project that wants to be cross-platform, some extra legwork is required to make it worth with both Darwin and non-Darwin host toolchains.
In TestingMacros/CMakeLists.txt, there is an explicit choice: On Darwin, use lib/swift/host/plugins/testing
. on non-Darwin, use lib/swift/host/plugins
.
swift-testing/Sources/TestingMacros/CMakeLists.txt
Lines 67 to 71 in 7ccbd68
Proposed solution
It would be less work for projects writing their own FindSwiftTesting.cmake
modules if the path to libTestingMacros.{dll,dylib,so}
was consistent across platforms.
Standardizing on lib/swift/host/plugins/testing
would be the obvious choice, as I've been told by @stmontgomery that the symlink from lib/swift/host/plugins/libTestingMacros.dylib to lib/swift/host/plugins/testing/libTestingMacros.dylib in Xcode.app 16.2 (and probably earlier as well?) should not be relied upon.
Alternatives considered
Standardizing on host/plugins
rather than host/plugins/testing
could work, but presumably there's a reason to use the testing subdirectory.
Additional information
No response