Skip to content

Commit 7aefb96

Browse files
neonichuMaxDesiatov
authored andcommitted
Compute path to swift-plugin-server on demand (#6899)
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.
1 parent 37ccd26 commit 7aefb96

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ public final class SwiftTargetBuildDescription {
887887
#endif
888888

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

Sources/PackageModel/Toolchain.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public protocol Toolchain {
2929
var isSwiftDevelopmentToolchain: Bool { get }
3030

3131
/// Path to the Swift plugin server utility.
32-
var swiftPluginServerPath: AbsolutePath? { get }
32+
var swiftPluginServerPath: AbsolutePath? { get throws }
3333

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

Sources/PackageModel/ToolchainConfiguration.swift

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

45-
/// Path to the Swift plugin server utility.
46-
public var swiftPluginServerPath: AbsolutePath?
47-
4845
/// Creates the set of manifest resources associated with a `swiftc` executable.
4946
///
5047
/// - Parameters:
@@ -55,16 +52,14 @@ public struct ToolchainConfiguration {
5552
/// - swiftPMLibrariesRootPath: Custom path for SwiftPM libraries. Computed based on the compiler path by default.
5653
/// - sdkRootPath: Optional path to SDK root.
5754
/// - xctestPath: Optional path to XCTest.
58-
/// - swiftPluginServerPath: Optional path to the Swift plugin server executable.
5955
public init(
6056
librarianPath: AbsolutePath,
6157
swiftCompilerPath: AbsolutePath,
6258
swiftCompilerFlags: [String] = [],
6359
swiftCompilerEnvironment: EnvironmentVariables = .process(),
6460
swiftPMLibrariesLocation: SwiftPMLibrariesLocation? = nil,
6561
sdkRootPath: AbsolutePath? = nil,
66-
xctestPath: AbsolutePath? = nil,
67-
swiftPluginServerPath: AbsolutePath? = nil
62+
xctestPath: AbsolutePath? = nil
6863
) {
6964
let swiftPMLibrariesLocation = swiftPMLibrariesLocation ?? {
7065
return .init(swiftCompilerPath: swiftCompilerPath)
@@ -77,7 +72,6 @@ public struct ToolchainConfiguration {
7772
self.swiftPMLibrariesLocation = swiftPMLibrariesLocation
7873
self.sdkRootPath = sdkRootPath
7974
self.xctestPath = xctestPath
80-
self.swiftPluginServerPath = swiftPluginServerPath
8175
}
8276
}
8377

Sources/PackageModel/UserToolchain.swift

+10-8
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,10 @@ public final class UserToolchain: Toolchain {
601601
environment: environment
602602
)
603603

604-
let swiftPluginServerPath: AbsolutePath?
605604
let xctestPath: AbsolutePath?
606605
if case .custom(_, let useXcrun) = searchStrategy, !useXcrun {
607-
swiftPluginServerPath = nil
608606
xctestPath = nil
609607
} else {
610-
swiftPluginServerPath = try Self.derivePluginServerPath(triple: triple)
611608
xctestPath = try Self.deriveXCTestPath(
612609
swiftSDK: self.swiftSDK,
613610
triple: triple,
@@ -622,8 +619,7 @@ public final class UserToolchain: Toolchain {
622619
swiftCompilerEnvironment: environment,
623620
swiftPMLibrariesLocation: swiftPMLibrariesLocation,
624621
sdkRootPath: self.swiftSDK.pathsConfiguration.sdkRootPath,
625-
xctestPath: xctestPath,
626-
swiftPluginServerPath: swiftPluginServerPath
622+
xctestPath: xctestPath
627623
)
628624
}
629625

@@ -697,8 +693,8 @@ public final class UserToolchain: Toolchain {
697693

698694
private static func derivePluginServerPath(triple: Triple) throws -> AbsolutePath? {
699695
if triple.isDarwin() {
700-
let xctestFindArgs = ["/usr/bin/xcrun", "--find", "swift-plugin-server"]
701-
if let path = try? TSCBasic.Process.checkNonZeroExit(arguments: xctestFindArgs, environment: [:])
696+
let pluginServerPathFindArgs = ["/usr/bin/xcrun", "--find", "swift-plugin-server"]
697+
if let path = try? TSCBasic.Process.checkNonZeroExit(arguments: pluginServerPathFindArgs, environment: [:])
702698
.spm_chomp() {
703699
return try AbsolutePath(validating: path)
704700
}
@@ -829,7 +825,13 @@ public final class UserToolchain: Toolchain {
829825
configuration.xctestPath
830826
}
831827

828+
private let _swiftPluginServerPath = ThreadSafeBox<AbsolutePath?>()
829+
832830
public var swiftPluginServerPath: AbsolutePath? {
833-
configuration.swiftPluginServerPath
831+
get throws {
832+
try _swiftPluginServerPath.memoize {
833+
return try Self.derivePluginServerPath(triple: self.targetTriple)
834+
}
835+
}
834836
}
835837
}

0 commit comments

Comments
 (0)