Skip to content
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
25 changes: 16 additions & 9 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,11 @@ public final class UserToolchain: Toolchain {
static func deriveMacOSSpecificSwiftTestingFlags(
derivedSwiftCompiler: AbsolutePath,
fileSystem: any FileSystem
) -> [String] {
) -> (swiftCFlags: [String], linkerFlags: [String]) {
guard let toolchainLibDir = try? toolchainLibDir(
swiftCompilerPath: derivedSwiftCompiler
) else {
return []
return (swiftCFlags: [], linkerFlags: [])
}

let testingLibDir = toolchainLibDir.appending(
Expand All @@ -426,15 +426,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 @@ -657,11 +658,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 @@ -671,11 +676,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 @@ -4633,6 +4633,119 @@ final class BuildPlanTests: XCTestCase {
])
}

func testSwiftTestingFlagsOnMacOSWithCustomToolchain() 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 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() throws {
let fileSystem = InMemoryFileSystem(
emptyFiles:
Expand Down