Skip to content

Compute path to swift-plugin-server on demand #6899

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 1 commit into from
Sep 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ public final class SwiftTargetBuildDescription {
#endif

// If we're using an OSS toolchain, add the required arguments bringing in the plugin server from the default toolchain if available.
if self.buildParameters.toolchain.isSwiftDevelopmentToolchain, driverSupport.checkSupportedFrontendFlags(flags: ["-external-plugin-path"], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem), let pluginServer = self.buildParameters.toolchain.swiftPluginServerPath {
if self.buildParameters.toolchain.isSwiftDevelopmentToolchain, driverSupport.checkSupportedFrontendFlags(flags: ["-external-plugin-path"], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem), let pluginServer = try self.buildParameters.toolchain.swiftPluginServerPath {
let toolchainUsrPath = pluginServer.parentDirectory.parentDirectory
let pluginPathComponents = ["lib", "swift", "host", "plugins"]

Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageModel/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public protocol Toolchain {
var isSwiftDevelopmentToolchain: Bool { get }

/// Path to the Swift plugin server utility.
var swiftPluginServerPath: AbsolutePath? { get }
var swiftPluginServerPath: AbsolutePath? { get throws }

/// Path containing the macOS Swift stdlib.
var macosSwiftStdlib: AbsolutePath { get throws }
Expand Down
8 changes: 1 addition & 7 deletions Sources/PackageModel/ToolchainConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public struct ToolchainConfiguration {
/// This is optional for example on macOS w/o Xcode.
public var xctestPath: AbsolutePath?

/// Path to the Swift plugin server utility.
public var swiftPluginServerPath: AbsolutePath?

/// Creates the set of manifest resources associated with a `swiftc` executable.
///
/// - Parameters:
Expand All @@ -55,16 +52,14 @@ public struct ToolchainConfiguration {
/// - swiftPMLibrariesRootPath: Custom path for SwiftPM libraries. Computed based on the compiler path by default.
/// - sdkRootPath: Optional path to SDK root.
/// - xctestPath: Optional path to XCTest.
/// - swiftPluginServerPath: Optional path to the Swift plugin server executable.
public init(
librarianPath: AbsolutePath,
swiftCompilerPath: AbsolutePath,
swiftCompilerFlags: [String] = [],
swiftCompilerEnvironment: EnvironmentVariables = .process(),
swiftPMLibrariesLocation: SwiftPMLibrariesLocation? = nil,
sdkRootPath: AbsolutePath? = nil,
xctestPath: AbsolutePath? = nil,
swiftPluginServerPath: AbsolutePath? = nil
xctestPath: AbsolutePath? = nil
) {
let swiftPMLibrariesLocation = swiftPMLibrariesLocation ?? {
return .init(swiftCompilerPath: swiftCompilerPath)
Expand All @@ -77,7 +72,6 @@ public struct ToolchainConfiguration {
self.swiftPMLibrariesLocation = swiftPMLibrariesLocation
self.sdkRootPath = sdkRootPath
self.xctestPath = xctestPath
self.swiftPluginServerPath = swiftPluginServerPath
}
}

Expand Down
18 changes: 10 additions & 8 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,10 @@ public final class UserToolchain: Toolchain {
environment: environment
)

let swiftPluginServerPath: AbsolutePath?
let xctestPath: AbsolutePath?
if case .custom(_, let useXcrun) = searchStrategy, !useXcrun {
swiftPluginServerPath = nil
xctestPath = nil
} else {
swiftPluginServerPath = try Self.derivePluginServerPath(triple: triple)
xctestPath = try Self.deriveXCTestPath(
swiftSDK: self.swiftSDK,
triple: triple,
Expand All @@ -622,8 +619,7 @@ public final class UserToolchain: Toolchain {
swiftCompilerEnvironment: environment,
swiftPMLibrariesLocation: swiftPMLibrariesLocation,
sdkRootPath: self.swiftSDK.pathsConfiguration.sdkRootPath,
xctestPath: xctestPath,
swiftPluginServerPath: swiftPluginServerPath
xctestPath: xctestPath
)
}

Expand Down Expand Up @@ -697,8 +693,8 @@ public final class UserToolchain: Toolchain {

private static func derivePluginServerPath(triple: Triple) throws -> AbsolutePath? {
if triple.isDarwin() {
let xctestFindArgs = ["/usr/bin/xcrun", "--find", "swift-plugin-server"]
if let path = try? TSCBasic.Process.checkNonZeroExit(arguments: xctestFindArgs, environment: [:])
let pluginServerPathFindArgs = ["/usr/bin/xcrun", "--find", "swift-plugin-server"]
if let path = try? TSCBasic.Process.checkNonZeroExit(arguments: pluginServerPathFindArgs, environment: [:])
.spm_chomp() {
return try AbsolutePath(validating: path)
}
Expand Down Expand Up @@ -829,7 +825,13 @@ public final class UserToolchain: Toolchain {
configuration.xctestPath
}

private let _swiftPluginServerPath = ThreadSafeBox<AbsolutePath?>()

public var swiftPluginServerPath: AbsolutePath? {
configuration.swiftPluginServerPath
get throws {
try _swiftPluginServerPath.memoize {
return try Self.derivePluginServerPath(triple: self.targetTriple)
}
}
}
}