Skip to content

Commit 1c6ef57

Browse files
authored
Compute path to swift-plugin-server on demand (#6907)
This takes a significant amount of time when the selected Xcode doesn't have a `swift-plugin-server` executable: ``` /tmp/exec ❯ time /usr/bin/xcrun --find swift-plugin-server xcrun: error: sh -c '/Users/neonacho/Downloads/Xcode.app/Contents/Developer/usr/bin/xcodebuild -sdk /Users/neonacho/Downloads/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -find swift-plugin-server 2> /dev/null' failed with exit code 17664: (null) (errno=No such file or directory) xcrun: error: unable to find utility "swift-plugin-server", not a developer tool or in PATH /usr/bin/xcrun --find swift-plugin-server 0.34s user 0.17s system 32% cpu 1.579 total ``` Doing this on demand is at least gated on `self.buildParameters.toolchain.isSwiftDevelopmentToolchain` so it shouldn't happen for the primary usage scenario on macOS. (cherry picked from commit de8e6e8)
1 parent 71633ac commit 1c6ef57

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public final class SwiftTargetBuildDescription {
394394
#endif
395395

396396
// If we're using an OSS toolchain, add the required arguments bringing in the plugin server from the default toolchain if available.
397-
if self.buildParameters.toolchain.isSwiftDevelopmentToolchain, driverSupport.checkSupportedFrontendFlags(flags: ["-external-plugin-path"], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem), let pluginServer = self.buildParameters.toolchain.swiftPluginServerPath {
397+
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 {
398398
let toolchainUsrPath = pluginServer.parentDirectory.parentDirectory
399399
let pluginPathComponents = ["lib", "swift", "host", "plugins"]
400400

Sources/PackageModel/Toolchain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public protocol Toolchain {
2323
var isSwiftDevelopmentToolchain: Bool { get }
2424

2525
/// Path to the Swift plugin server utility.
26-
var swiftPluginServerPath: AbsolutePath? { get }
26+
var swiftPluginServerPath: AbsolutePath? { get throws }
2727

2828
/// Path containing the macOS Swift stdlib.
2929
var macosSwiftStdlib: AbsolutePath { get throws }

Sources/PackageModel/ToolchainConfiguration.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public struct ToolchainConfiguration {
4343
/// This is optional for example on macOS w/o Xcode.
4444
public var xctestPath: AbsolutePath?
4545

46-
/// Path to the Swift plugin server utility.
47-
public var swiftPluginServerPath: AbsolutePath?
48-
4946
/// Creates the set of manifest resources associated with a `swiftc` executable.
5047
///
5148
/// - Parameters:
@@ -56,16 +53,14 @@ public struct ToolchainConfiguration {
5653
/// - swiftPMLibrariesRootPath: Custom path for SwiftPM libraries. Computed based on the compiler path by default.
5754
/// - sdkRootPath: Optional path to SDK root.
5855
/// - xctestPath: Optional path to XCTest.
59-
/// - swiftPluginServerPath: Optional path to the Swift plugin server executable.
6056
public init(
6157
librarianPath: AbsolutePath,
6258
swiftCompilerPath: AbsolutePath,
6359
swiftCompilerFlags: [String] = [],
6460
swiftCompilerEnvironment: EnvironmentVariables = .process(),
6561
swiftPMLibrariesLocation: SwiftPMLibrariesLocation? = nil,
6662
sdkRootPath: AbsolutePath? = nil,
67-
xctestPath: AbsolutePath? = nil,
68-
swiftPluginServerPath: AbsolutePath? = nil
63+
xctestPath: AbsolutePath? = nil
6964
) {
7065
let swiftPMLibrariesLocation = swiftPMLibrariesLocation ?? {
7166
return .init(swiftCompilerPath: swiftCompilerPath)
@@ -78,7 +73,6 @@ public struct ToolchainConfiguration {
7873
self.swiftPMLibrariesLocation = swiftPMLibrariesLocation
7974
self.sdkRootPath = sdkRootPath
8075
self.xctestPath = xctestPath
81-
self.swiftPluginServerPath = swiftPluginServerPath
8276
}
8377
}
8478

Sources/PackageModel/UserToolchain.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,10 @@ public final class UserToolchain: Toolchain {
559559
environment: environment
560560
)
561561

562-
let swiftPluginServerPath: AbsolutePath?
563562
let xctestPath: AbsolutePath?
564563
if case .custom(_, let useXcrun) = searchStrategy, !useXcrun {
565-
swiftPluginServerPath = nil
566564
xctestPath = nil
567565
} else {
568-
swiftPluginServerPath = try Self.derivePluginServerPath(triple: triple)
569566
xctestPath = try Self.deriveXCTestPath(
570567
destination: self.destination,
571568
triple: triple,
@@ -580,8 +577,7 @@ public final class UserToolchain: Toolchain {
580577
swiftCompilerEnvironment: environment,
581578
swiftPMLibrariesLocation: swiftPMLibrariesLocation,
582579
sdkRootPath: self.destination.pathsConfiguration.sdkRootPath,
583-
xctestPath: xctestPath,
584-
swiftPluginServerPath: swiftPluginServerPath
580+
xctestPath: xctestPath
585581
)
586582
}
587583

@@ -655,8 +651,8 @@ public final class UserToolchain: Toolchain {
655651

656652
private static func derivePluginServerPath(triple: Triple) throws -> AbsolutePath? {
657653
if triple.isDarwin() {
658-
let xctestFindArgs = ["/usr/bin/xcrun", "--find", "swift-plugin-server"]
659-
if let path = try? TSCBasic.Process.checkNonZeroExit(arguments: xctestFindArgs, environment: [:])
654+
let pluginServerPathFindArgs = ["/usr/bin/xcrun", "--find", "swift-plugin-server"]
655+
if let path = try? TSCBasic.Process.checkNonZeroExit(arguments: pluginServerPathFindArgs, environment: [:])
660656
.spm_chomp() {
661657
return try AbsolutePath(validating: path)
662658
}
@@ -787,7 +783,13 @@ public final class UserToolchain: Toolchain {
787783
configuration.xctestPath
788784
}
789785

786+
private let _swiftPluginServerPath = ThreadSafeBox<AbsolutePath?>()
787+
790788
public var swiftPluginServerPath: AbsolutePath? {
791-
configuration.swiftPluginServerPath
789+
get throws {
790+
try _swiftPluginServerPath.memoize {
791+
return try Self.derivePluginServerPath(triple: self.triple)
792+
}
793+
}
792794
}
793795
}

0 commit comments

Comments
 (0)