Skip to content

TestingMacros plugin location is inconsistent between platforms #1039

Open
@ADKaster

Description

@ADKaster

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:

  1. 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"
]
  1. find_library for Testing, using those paths
  2. find_library for TestingMacros, 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.
  3. Create an imported target for SwiftTesting::SwiftTesting (or whatever one's preferred naming is).
  4. 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.

if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(plugin_destination_dir "lib/swift/host/plugins/testing")
set_property(TARGET TestingMacros PROPERTY INSTALL_RPATH)
else()
set(plugin_destination_dir "lib/swift/host/plugins")

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

Metadata

Metadata

Assignees

Labels

build🧱 Affects the project's build configuration or processenhancementNew feature or requestmacros🔭 Related to Swift macros such as @Test or #expecttools integration🛠️ Integration of swift-testing into tools/IDEs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions