Skip to content

[PackageModel] Toolchain: Split SwiftTesting flags between swift comp… #7903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,23 +410,23 @@ public final class UserToolchain: Toolchain {
static func deriveMacOSSpecificSwiftTestingFlags(
derivedSwiftCompiler: AbsolutePath,
fileSystem: any FileSystem
) -> [String] {
) -> (swiftCFlags: [String], linkerFlags: [String]) {
// If this is CommandLineTools all we need to add is a frameworks path.
if let frameworksPath = try? AbsolutePath(
validating: "../../Library/Developer/Frameworks",
relativeTo: resolveSymlinks(derivedSwiftCompiler).parentDirectory
), fileSystem.exists(frameworksPath.appending("Testing.framework")) {
return [
"-F", frameworksPath.pathString,
"-Xlinker", "-rpath",
"-Xlinker", frameworksPath.pathString
]
return (swiftCFlags: [
"-F", frameworksPath.pathString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this framework search path not also needed for the linker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be sufficient, I tested with commandline tools only and -F + -Xlinker -rpath builds and runs the swift-testing tests without issues.

], linkerFlags: [
"-rpath", frameworksPath.pathString
])
}

guard let toolchainLibDir = try? toolchainLibDir(
swiftCompilerPath: derivedSwiftCompiler
) else {
return []
return (swiftCFlags: [], linkerFlags: [])
}

let testingLibDir = toolchainLibDir.appending(
Expand All @@ -438,15 +438,16 @@ public final class UserToolchain: Toolchain {
)

guard fileSystem.exists(testingLibDir), fileSystem.exists(testingPluginsDir) else {
return []
return (swiftCFlags: [], linkerFlags: [])
}

return [
return (swiftCFlags: [
"-I", testingLibDir.pathString,
"-L", testingLibDir.pathString,
"-plugin-path", testingPluginsDir.pathString,
"-Xlinker", "-rpath", "-Xlinker", testingLibDir.pathString,
]
"-plugin-path", testingPluginsDir.pathString
], linkerFlags: [
"-rpath", testingLibDir.pathString
])
}

internal static func deriveSwiftCFlags(
Expand Down Expand Up @@ -669,11 +670,15 @@ public final class UserToolchain: Toolchain {
self.targetTriple = triple

var swiftCompilerFlags: [String] = []
var extraLinkerFlags: [String] = []

#if os(macOS)
swiftCompilerFlags += Self.deriveMacOSSpecificSwiftTestingFlags(
let (swiftCFlags, linkerFlags) = Self.deriveMacOSSpecificSwiftTestingFlags(
derivedSwiftCompiler: swiftCompilers.compile,
fileSystem: fileSystem
)
swiftCompilerFlags += swiftCFlags
extraLinkerFlags += linkerFlags
#endif

swiftCompilerFlags += try Self.deriveSwiftCFlags(
Expand All @@ -683,11 +688,13 @@ public final class UserToolchain: Toolchain {
fileSystem: fileSystem
)

extraLinkerFlags += swiftSDK.toolset.knownTools[.linker]?.extraCLIOptions ?? []

self.extraFlags = BuildFlags(
cCompilerFlags: swiftSDK.toolset.knownTools[.cCompiler]?.extraCLIOptions ?? [],
cxxCompilerFlags: swiftSDK.toolset.knownTools[.cxxCompiler]?.extraCLIOptions ?? [],
swiftCompilerFlags: swiftCompilerFlags,
linkerFlags: swiftSDK.toolset.knownTools[.linker]?.extraCLIOptions ?? [],
linkerFlags: extraLinkerFlags,
xcbuildFlags: swiftSDK.toolset.knownTools[.xcbuild]?.extraCLIOptions ?? [])

self.includeSearchPaths = swiftSDK.pathsConfiguration.includeSearchPaths ?? []
Expand Down
113 changes: 113 additions & 0 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4649,6 +4649,119 @@ final class BuildPlanTests: XCTestCase {
])
}

func testSwiftTestingFlagsOnMacOSWithCustomToolchain() async throws {
#if !os(macOS)
// This is testing swift-testing in a toolchain which is macOS only feature.
try XCTSkipIf(true, "test is only supported on macOS")
#endif

let fs = InMemoryFileSystem(
emptyFiles:
"/fake/path/lib/swift/macosx/testing/Testing.swiftmodule",
"/fake/path/lib/swift/host/plugins/testing/libTesting.dylib",
"/Pkg/Sources/Lib/main.swift",
"/Pkg/Tests/LibTest/test.swift"
)
try fs.createMockToolchain()

let userSwiftSDK = SwiftSDK(
hostTriple: .x86_64MacOS,
targetTriple: .x86_64MacOS,
toolset: .init(
knownTools: [
.cCompiler: .init(extraCLIOptions: []),
.swiftCompiler: .init(extraCLIOptions: []),
],
rootPaths: ["/fake/path/to"]
),
pathsConfiguration: .init(
sdkRootPath: "/fake/sdk",
swiftResourcesPath: "/fake/lib/swift",
swiftStaticResourcesPath: "/fake/lib/swift_static"
)
)
let mockToolchain = try UserToolchain(
swiftSDK: userSwiftSDK,
environment: .mockEnvironment,
fileSystem: fs
)

XCTAssertEqual(
mockToolchain.extraFlags.swiftCompilerFlags,
[
"-I", "/fake/path/lib/swift/macosx/testing",
"-L", "/fake/path/lib/swift/macosx/testing",
"-plugin-path", "/fake/path/lib/swift/host/plugins/testing",
"-sdk", "/fake/sdk",
]
)
XCTAssertEqual(
mockToolchain.extraFlags.linkerFlags,
["-rpath", "/fake/path/lib/swift/macosx/testing"]
)

let observability = ObservabilitySystem.makeForTesting()
let graph = try loadModulesGraph(
fileSystem: fs,
manifests: [
Manifest.createRootManifest(
displayName: "Pkg",
path: "/Pkg",
targets: [
TargetDescription(name: "Lib", dependencies: []),
TargetDescription(
name: "LibTest",
dependencies: ["Lib"],
type: .test
),
]
),
],
observabilityScope: observability.topScope
)
XCTAssertNoDiagnostics(observability.diagnostics)

let result = try await BuildPlanResult(plan: mockBuildPlan(
toolchain: mockToolchain,
graph: graph,
commonFlags: .init(),
fileSystem: fs,
observabilityScope: observability.topScope
))
result.checkProductsCount(2)
result.checkTargetsCount(3)

let testProductLinkArgs = try result.buildProduct(for: "Lib").linkArguments()
XCTAssertMatch(testProductLinkArgs, [
.anySequence,
"-I", "/fake/path/lib/swift/macosx/testing",
"-L", "/fake/path/lib/swift/macosx/testing",
.anySequence,
"-Xlinker", "-rpath",
"-Xlinker", "/fake/path/lib/swift/macosx/testing",
])

let libModuleArgs = try result.moduleBuildDescription(for: "Lib").swift().compileArguments()
XCTAssertMatch(libModuleArgs, [
.anySequence,
"-I", "/fake/path/lib/swift/macosx/testing",
"-L", "/fake/path/lib/swift/macosx/testing",
"-plugin-path", "/fake/path/lib/swift/host/plugins/testing",
.anySequence,
])
XCTAssertNoMatch(libModuleArgs, ["-Xlinker"])

let testModuleArgs = try result.moduleBuildDescription(for: "LibTest").swift().compileArguments()
XCTAssertMatch(testModuleArgs, [
.anySequence,
"-I", "/fake/path/lib/swift/macosx/testing",
"-L", "/fake/path/lib/swift/macosx/testing",
"-plugin-path", "/fake/path/lib/swift/host/plugins/testing",
.anySequence,
])
XCTAssertNoMatch(testModuleArgs, ["-Xlinker"])
}

func testUserToolchainWithToolsetCompileFlags() async throws {
let fileSystem = InMemoryFileSystem(
emptyFiles:
Expand Down