Skip to content

Commit 3e838b2

Browse files
committed
refactor toolchain SwiftPM libraries path
motivation: improve logic in how Manifest and Plugin APIs locations are derived changes: * rename fields on ToolchainConfiguration to better articulate their meaning * change logic in UserToolchain to derive the SwiftPM libraries path more clearly and reliably * adjust call-sites and test
1 parent 7cb50e4 commit 3e838b2

File tree

10 files changed

+201
-125
lines changed

10 files changed

+201
-125
lines changed

Sources/Commands/SwiftRunTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public struct SwiftRunTool: SwiftCommand {
136136
let arguments = buildOp.buildPlan!.createREPLArguments()
137137
print("Launching Swift REPL with arguments: \(arguments.joined(separator: " "))")
138138
try run(
139-
swiftTool.getToolchain().swiftInterpreter,
139+
swiftTool.getToolchain().swiftInterpreterPath,
140140
originalWorkingDirectory: swiftTool.originalWorkingDirectory,
141141
arguments: arguments)
142142

@@ -171,7 +171,7 @@ public struct SwiftRunTool: SwiftCommand {
171171
if let executable = options.executable, isValidSwiftFilePath(executable) {
172172
swiftTool.diagnostics.emit(.runFileDeprecation)
173173
// Redirect execution to the toolchain's swift executable.
174-
let swiftInterpreterPath = try swiftTool.getToolchain().swiftInterpreter
174+
let swiftInterpreterPath = try swiftTool.getToolchain().swiftInterpreterPath
175175
// Prepend the script to interpret to the arguments.
176176
let arguments = [executable] + options.arguments
177177
try run(

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -689,14 +689,14 @@ public final class ManifestLoader: ManifestLoaderProtocol {
689689
let moduleCachePath = (ProcessEnv.vars["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"]).flatMap{ AbsolutePath.init($0) }
690690

691691
var cmd: [String] = []
692-
cmd += [self.toolchain.swiftCompiler.pathString]
692+
cmd += [self.toolchain.swiftCompilerPath.pathString]
693693
cmd += verbosity.ccArgs
694694

695695
let macOSPackageDescriptionPath: AbsolutePath
696-
// If we got the binDir that means we could be developing SwiftPM in Xcode
696+
// If we got the swiftPMBinaryPath that means we could be developing SwiftPM in Xcode
697697
// which produces a framework for dynamic package products.
698698
let packageFrameworkPath = runtimePath.appending(component: "PackageFrameworks")
699-
if self.toolchain.binDir != nil, localFileSystem.exists(packageFrameworkPath) {
699+
if localFileSystem.exists(packageFrameworkPath) {
700700
cmd += [
701701
"-F", packageFrameworkPath.pathString,
702702
"-framework", "PackageDescription",
@@ -722,7 +722,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
722722
// Use the same minimum deployment target as the PackageDescription library (with a fallback of 10.15).
723723
#if os(macOS)
724724
let triple = Self._hostTriple.memoize {
725-
Triple.getHostTriple(usingSwiftCompiler: self.toolchain.swiftCompiler)
725+
Triple.getHostTriple(usingSwiftCompiler: self.toolchain.swiftCompilerPath)
726726
}
727727

728728
let version = try Self._packageDescriptionMinimumDeploymentTarget.memoize {
@@ -855,7 +855,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
855855
cmd += ["-swift-version", toolsVersion.swiftLanguageVersion.rawValue]
856856
cmd += ["-I", runtimePath.pathString]
857857
#if os(macOS)
858-
if let sdkRoot = self.toolchain.sdkRoot ?? self.sdkRoot() {
858+
if let sdkRoot = self.toolchain.sdkRootPath ?? self.sdkRoot() {
859859
cmd += ["-sdk", sdkRoot.pathString]
860860
}
861861
#endif
@@ -865,19 +865,13 @@ public final class ManifestLoader: ManifestLoaderProtocol {
865865

866866
/// Returns the runtime path given the manifest version and path to libDir.
867867
private func runtimePath(for version: ToolsVersion) -> AbsolutePath {
868-
// Bin dir will be set when developing swiftpm without building all of the runtimes.
869-
if let binDir = self.toolchain.binDir {
870-
return binDir
871-
}
872-
873-
// Otherwise we use the standard location of the manifest API in the toolchain, if it exists.
874-
let manifestAPIDir = self.toolchain.libDir.appending(component: "ManifestAPI")
868+
let manifestAPIDir = self.toolchain.swiftPMLibrariesLocation.manifestAPI
875869
if localFileSystem.exists(manifestAPIDir) {
876870
return manifestAPIDir
877871
}
878872

879-
// Otherwise, fall back on the old location (this would indicate that we're using an old toolchain).
880-
return self.toolchain.libDir.appending(version.runtimeSubpath)
873+
// Fall back on the old location (this would indicate that we're using an old toolchain).
874+
return self.toolchain.swiftPMLibrariesLocation.root.appending(version.runtimeSubpath)
881875
}
882876

883877
/// Returns path to the manifest database inside the given cache directory.

Sources/PackageModel/ToolchainConfiguration.swift

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,54 @@ import TSCBasic
1616
/// using the package manager with alternate toolchains in the future.
1717
public struct ToolchainConfiguration {
1818
/// The path of the swift compiler.
19-
public let swiftCompiler: AbsolutePath
19+
public let swiftCompilerPath: AbsolutePath
2020

2121
/// Extra flags to pass the Swift compiler.
2222
public let swiftCompilerFlags: [String]
2323

24-
/// The path of the library resources.
25-
public let libDir: AbsolutePath
24+
/// SwiftPM library paths.
25+
public let swiftPMLibrariesLocation: SwiftPMLibrariesLocation
2626

27-
/// The bin directory.
28-
public let binDir: AbsolutePath?
27+
/// The swiftpm binary directory.
28+
//public let swiftPMBinaryPath: AbsolutePath?
2929

3030
/// The path to SDK root.
3131
///
3232
/// If provided, it will be passed to the swift interpreter.
33-
public let sdkRoot: AbsolutePath?
33+
public let sdkRootPath: AbsolutePath?
3434

3535
/// XCTest Location
36-
public let xctestLocation: AbsolutePath?
36+
public let xctestPath: AbsolutePath?
3737

3838
/// Creates the set of manifest resources associated with a `swiftc` executable.
3939
///
4040
/// - Parameters:
41-
/// - swiftCompiler: The absolute path of the associated `swiftc` executable.
41+
/// - swiftCompilerPath: The absolute path of the associated `swiftc` executable.
4242
/// - swiftCompilerFlags: Extra flags to pass the Swift compiler.: Extra flags to pass the Swift compiler.
43-
/// - libDir: The path of the library resources.
44-
/// - binDir: The bin directory.
45-
/// - sdkRoot: The path to SDK root.
46-
/// - xctestLocation: XCTest Location
43+
/// - swiftPMLibrariesRootPath: Custom path for SwiftPM libraries.
44+
/// - sdkRootPath: The path to SDK root.
45+
/// - xctestPath: XCTest Location
46+
public init(
47+
swiftCompilerPath: AbsolutePath,
48+
swiftCompilerFlags: [String] = [],
49+
swiftPMLibrariesLocation: SwiftPMLibrariesLocation? = nil,
50+
sdkRootPath: AbsolutePath? = nil,
51+
xctestPath: AbsolutePath? = nil
52+
) {
53+
let swiftPMLibrariesLocation = swiftPMLibrariesLocation ?? {
54+
let rootPath = Self.swiftPMLibrariesPath(from: swiftCompilerPath.parentDirectory.parentDirectory)
55+
return .init(root: rootPath)
56+
}()
57+
58+
self.swiftCompilerPath = swiftCompilerPath
59+
self.swiftCompilerFlags = swiftCompilerFlags
60+
self.swiftPMLibrariesLocation = swiftPMLibrariesLocation
61+
self.sdkRootPath = sdkRootPath
62+
self.xctestPath = xctestPath
63+
}
64+
65+
// deprecated 8/2021
66+
@available(*, deprecated, message: "use non-deprecated initializer instead")
4767
public init(
4868
swiftCompiler: AbsolutePath,
4969
swiftCompilerFlags: [String] = [],
@@ -52,15 +72,36 @@ public struct ToolchainConfiguration {
5272
sdkRoot: AbsolutePath? = nil,
5373
xctestLocation: AbsolutePath? = nil
5474
) {
55-
self.swiftCompiler = swiftCompiler
56-
self.swiftCompilerFlags = swiftCompilerFlags
57-
self.libDir = libDir ?? Self.libDir(forBinDir: swiftCompiler.parentDirectory)
58-
self.binDir = binDir
59-
self.sdkRoot = sdkRoot
60-
self.xctestLocation = xctestLocation
75+
self.init(
76+
swiftCompilerPath: swiftCompiler,
77+
swiftCompilerFlags: swiftCompilerFlags,
78+
swiftPMLibrariesLocation: libDir.map { .init(root: $0) },
79+
sdkRootPath: sdkRoot,
80+
xctestPath: xctestLocation
81+
)
82+
}
83+
84+
public static func swiftPMLibrariesPath(from rootPath: AbsolutePath) -> AbsolutePath {
85+
return rootPath.appending(components: "lib", "swift", "pm")
6186
}
6287

63-
public static func libDir(forBinDir binDir: AbsolutePath) -> AbsolutePath {
64-
return binDir.parentDirectory.appending(components: "lib", "swift", "pm")
88+
public struct SwiftPMLibrariesLocation {
89+
public var root: AbsolutePath
90+
public var manifestAPI: AbsolutePath
91+
public var pluginsAPI: AbsolutePath
92+
93+
public init(root: AbsolutePath, manifestAPI: AbsolutePath, pluginsAPI: AbsolutePath) {
94+
self.root = root
95+
self.manifestAPI = manifestAPI
96+
self.pluginsAPI = pluginsAPI
97+
}
98+
99+
public init(root: AbsolutePath) {
100+
self.init(
101+
root: root,
102+
manifestAPI: root.appending(component: "ManifestAPI"),
103+
pluginsAPI: root.appending(component: "PluginsAPI")
104+
)
105+
}
65106
}
66107
}

Sources/SPMTestSupport/Toolchain.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ extension ToolchainConfiguration {
3535
get {
3636
let toolchain = UserToolchain.default
3737
return .init(
38-
swiftCompiler: toolchain.configuration.swiftCompiler,
38+
swiftCompilerPath: toolchain.configuration.swiftCompilerPath,
3939
swiftCompilerFlags: [],
40-
libDir: toolchain.configuration.libDir,
41-
binDir: toolchain.configuration.binDir
40+
swiftPMLibrariesLocation: toolchain.configuration.swiftPMLibrariesLocation
4241
)
4342
}
4443
}

Sources/Workspace/DefaultPluginScriptRunner.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,29 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner {
4040

4141
public var hostTriple: Triple {
4242
return Self._hostTriple.memoize {
43-
Triple.getHostTriple(usingSwiftCompiler: self.toolchain.swiftCompiler)
43+
Triple.getHostTriple(usingSwiftCompiler: self.toolchain.swiftCompilerPath)
4444
}
4545
}
4646

4747
/// Helper function that compiles a plugin script as an executable and returns the path to it.
4848
fileprivate func compile(sources: Sources, toolsVersion: ToolsVersion, cacheDir: AbsolutePath) throws -> AbsolutePath {
4949
// FIXME: Much of this is copied from the ManifestLoader and should be consolidated.
5050

51-
// Bin dir will be set when developing swiftpm without building all of the runtimes.
52-
let runtimePath = self.toolchain.binDir ?? self.toolchain.libDir.appending(component: "PluginAPI")
51+
let runtimePath = self.toolchain.swiftPMLibrariesLocation.pluginsAPI
5352

5453
// Compile the package plugin script.
55-
var command = [self.toolchain.swiftCompiler.pathString]
54+
var command = [self.toolchain.swiftCompilerPath.pathString]
5655

5756
// FIXME: Workaround for the module cache bug that's been haunting Swift CI
5857
// <rdar://problem/48443680>
5958
let moduleCachePath = ProcessEnv.vars["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"]
6059

61-
// If we got the binDir that means we could be developing SwiftPM in Xcode
60+
// If we got the swiftPMBinaryPath that means we could be developing SwiftPM in Xcode
6261
// which produces a framework for dynamic package products.
6362
let packageFrameworkPath = runtimePath.appending(component: "PackageFrameworks")
6463

6564
let macOSPackageDescriptionPath: AbsolutePath
66-
if self.toolchain.binDir != nil, localFileSystem.exists(packageFrameworkPath) {
65+
if localFileSystem.exists(packageFrameworkPath) {
6766
command += [
6867
"-F", packageFrameworkPath.pathString,
6968
"-framework", "PackagePlugin",
@@ -101,7 +100,7 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner {
101100
command += ["-swift-version", toolsVersion.swiftLanguageVersion.rawValue]
102101
command += ["-I", runtimePath.pathString]
103102
#if os(macOS)
104-
if let sdkRoot = self.toolchain.sdkRoot ?? self.sdkRoot() {
103+
if let sdkRoot = self.toolchain.sdkRootPath ?? self.sdkRoot() {
105104
command += ["-sdk", sdkRoot.pathString]
106105
}
107106
#endif

0 commit comments

Comments
 (0)