Skip to content

Commit

Permalink
[6.0] Properly handle swift-testing installations in toolchain/SDK (#…
Browse files Browse the repository at this point in the history
…7856)

- Explanation:

On macOS SwiftPM should prefer swift-testing installed into a custom
toolchain when used. On Windows we need special logic to discover
swift-testing location.

- Add special swift compiler "extra" flags to favor swift-testing
installed in a toolchain.
- Inject `-I`, `-L` on Windows that point to where swift-testing is
installed in SDKROOT.
- Inject a path to testing on `PATH` environment variable on Windows to
make sure that the library is always discoverable.

- Main Branch PR:
#7840

- Resolves: rdar://132828246

- Risk: Medium (Although changes are only viable with toolchains have
certain directories in them and test we could do for testing was manual
validation).

- Reviewed By: @MaxDesiatov @rintaro 

- Testing: Existing tests and manual validation using new toolchain
(which is currently in development) on Windows and a custom toolchain
plus CommandLine tools on macOS.
  • Loading branch information
xedin authored Aug 13, 2024
1 parent 5b27e1a commit 708f50b
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 45 deletions.
7 changes: 5 additions & 2 deletions Sources/Commands/Utilities/TestingSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,11 @@ enum TestingSupport {
}
#if !os(macOS)
#if os(Windows)
if let location = toolchain.xctestPath {
env.prependPath(key: .path, value: location.pathString)
if let xctestLocation = toolchain.xctestPath {
env.prependPath(key: .path, value: xctestLocation.pathString)
}
if let swiftTestingLocation = toolchain.swiftTestingPathOnWindows {
env.prependPath(key: .path, value: swiftTestingLocation.pathString)
}
#endif
return env
Expand Down
14 changes: 11 additions & 3 deletions Sources/PackageModel/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,24 @@ extension Toolchain {

public var hostLibDir: AbsolutePath {
get throws {
return try toolchainLibDir.appending(components: ["swift", "host"])
try Self.toolchainLibDir(swiftCompilerPath: self.swiftCompilerPath).appending(
components: ["swift", "host"]
)
}
}

public var macosSwiftStdlib: AbsolutePath {
get throws {
return try AbsolutePath(validating: "../../lib/swift/macosx", relativeTo: resolveSymlinks(swiftCompilerPath))
try Self.toolchainLibDir(swiftCompilerPath: self.swiftCompilerPath).appending(
components: ["swift", "macosx"]
)
}
}

public var toolchainLibDir: AbsolutePath {
get throws {
// FIXME: Not sure if it's better to base this off of Swift compiler or our own binary.
return try AbsolutePath(validating: "../../lib", relativeTo: resolveSymlinks(swiftCompilerPath))
try Self.toolchainLibDir(swiftCompilerPath: self.swiftCompilerPath)
}
}

Expand All @@ -107,4 +111,8 @@ extension Toolchain {
public var extraSwiftCFlags: [String] {
extraFlags.swiftCompilerFlags
}

package static func toolchainLibDir(swiftCompilerPath: AbsolutePath) throws -> AbsolutePath {
try AbsolutePath(validating: "../../lib", relativeTo: resolveSymlinks(swiftCompilerPath))
}
}
8 changes: 7 additions & 1 deletion Sources/PackageModel/ToolchainConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public struct ToolchainConfiguration {
/// This is optional for example on macOS w/o Xcode.
public var xctestPath: AbsolutePath?

/// Path to the swift-testing utility.
/// Currently computed only for Windows.
public var swiftTestingPath: AbsolutePath?

/// Creates the set of manifest resources associated with a `swiftc` executable.
///
/// - Parameters:
Expand All @@ -59,7 +63,8 @@ public struct ToolchainConfiguration {
swiftCompilerEnvironment: Environment = .current,
swiftPMLibrariesLocation: SwiftPMLibrariesLocation? = nil,
sdkRootPath: AbsolutePath? = nil,
xctestPath: AbsolutePath? = nil
xctestPath: AbsolutePath? = nil,
swiftTestingPath: AbsolutePath? = nil
) {
let swiftPMLibrariesLocation = swiftPMLibrariesLocation ?? {
return .init(swiftCompilerPath: swiftCompilerPath)
Expand All @@ -72,6 +77,7 @@ public struct ToolchainConfiguration {
self.swiftPMLibrariesLocation = swiftPMLibrariesLocation
self.sdkRootPath = sdkRootPath
self.xctestPath = xctestPath
self.swiftTestingPath = swiftTestingPath
}
}

Expand Down
Loading

0 comments on commit 708f50b

Please sign in to comment.