Skip to content

Commit a9e3527

Browse files
authored
refactor ManifestResources into simpler ToolchainConfiguration (#3656)
* refactor ManifestResources into simpler ToolchainConfiguration motivation: improve code readability, prep work for further api refactoring for configuration changes: * remove ManifestResources abstraction and impl into plain struct named ToolchainConfiguration * udpate test module to define defaults for ToolchainConfiguration and UserToolchain to simplify and clean code * update call-sites, tests
1 parent dffd4b3 commit a9e3527

30 files changed

+237
-240
lines changed

Sources/Commands/SwiftTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ public class SwiftTool {
644644
// The `cache` directory is in the plugins directory and is where the plugin script runner caches
645645
// compiled plugin binaries and any other derived information.
646646
let cacheDir = pluginsDir.appending(component: "cache")
647-
let pluginScriptRunner = try DefaultPluginScriptRunner(cacheDir: cacheDir, manifestResources: self._hostToolchain.get().manifestResources)
647+
let pluginScriptRunner = try DefaultPluginScriptRunner(cacheDir: cacheDir, toolchain: self._hostToolchain.get().configuration)
648648

649649
// The `outputs` directory contains subdirectories for each combination of package, target, and plugin.
650650
// Each usage of a plugin has an output directory that is writable by the plugin, where it can write
@@ -873,7 +873,7 @@ public class SwiftTool {
873873

874874
return try ManifestLoader(
875875
// Always use the host toolchain's resources for parsing manifest.
876-
manifestResources: self._hostToolchain.get().manifestResources,
876+
toolchain: self._hostToolchain.get().configuration,
877877
isManifestSandboxEnabled: !self.options.shouldDisableSandbox,
878878
cacheDir: cachePath,
879879
extraManifestFlags: extraManifestFlags

Sources/PackageLoading/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ add_library(PackageLoading
1717
PlatformRegistry.swift
1818
Target+PkgConfig.swift
1919
TargetSourcesBuilder.swift
20-
ToolsVersionLoader.swift
21-
UserManifestResources.swift)
20+
ToolsVersionLoader.swift)
2221
target_link_libraries(PackageLoading PUBLIC
2322
TSCBasic
2423
Basics

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,6 @@ public enum ManifestParseError: Swift.Error, Equatable {
2323
case runtimeManifestErrors([String])
2424
}
2525

26-
/// Resources required for manifest loading.
27-
///
28-
/// These requirements are abstracted out to make it easier to add support for
29-
/// using the package manager with alternate toolchains in the future.
30-
public protocol ManifestResourceProvider {
31-
/// The path of the swift compiler.
32-
var swiftCompiler: AbsolutePath { get }
33-
34-
/// The path of the library resources.
35-
var libDir: AbsolutePath { get }
36-
37-
/// The path to SDK root.
38-
///
39-
/// If provided, it will be passed to the swift interpreter.
40-
var sdkRoot: AbsolutePath? { get }
41-
42-
/// The bin directory.
43-
var binDir: AbsolutePath? { get }
44-
45-
/// Extra flags to pass the Swift compiler.
46-
var swiftCompilerFlags: [String] { get }
47-
48-
/// XCTest Location
49-
var xctestLocation: AbsolutePath? { get }
50-
}
51-
52-
/// Default implemention for the resource provider.
53-
public extension ManifestResourceProvider {
54-
55-
var sdkRoot: AbsolutePath? {
56-
return nil
57-
}
58-
59-
var binDir: AbsolutePath? {
60-
return nil
61-
}
62-
63-
var swiftCompilerFlags: [String] {
64-
return []
65-
}
66-
67-
var xctestLocation: AbsolutePath? {
68-
return nil
69-
}
70-
}
71-
7226
/// Protocol for the manifest loader interface.
7327
public protocol ManifestLoaderProtocol {
7428
/// Load the manifest for the package at `path`.
@@ -124,7 +78,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
12478
private static var _hostTriple = ThreadSafeBox<Triple>()
12579
private static var _packageDescriptionMinimumDeploymentTarget = ThreadSafeBox<String>()
12680

127-
private let resources: ManifestResourceProvider
81+
private let toolchain: ToolchainConfiguration
12882
private let serializedDiagnostics: Bool
12983
private let isManifestSandboxEnabled: Bool
13084
private let delegate: ManifestLoaderDelegate?
@@ -137,14 +91,14 @@ public final class ManifestLoader: ManifestLoaderProtocol {
13791
private let operationQueue: OperationQueue
13892

13993
public init(
140-
manifestResources: ManifestResourceProvider,
94+
toolchain: ToolchainConfiguration,
14195
serializedDiagnostics: Bool = false,
14296
isManifestSandboxEnabled: Bool = true,
14397
cacheDir: AbsolutePath? = nil,
14498
delegate: ManifestLoaderDelegate? = nil,
14599
extraManifestFlags: [String] = []
146100
) {
147-
self.resources = manifestResources
101+
self.toolchain = toolchain
148102
self.serializedDiagnostics = serializedDiagnostics
149103
self.isManifestSandboxEnabled = isManifestSandboxEnabled
150104
self.delegate = delegate
@@ -157,6 +111,26 @@ public final class ManifestLoader: ManifestLoaderProtocol {
157111
self.operationQueue.maxConcurrentOperationCount = Concurrency.maxOperations
158112
}
159113

114+
// deprecated 8/2021
115+
@available(*, deprecated, message: "use non-deprecated constructor instead")
116+
public convenience init(
117+
manifestResources: ToolchainConfiguration,
118+
serializedDiagnostics: Bool = false,
119+
isManifestSandboxEnabled: Bool = true,
120+
cacheDir: AbsolutePath? = nil,
121+
delegate: ManifestLoaderDelegate? = nil,
122+
extraManifestFlags: [String] = []
123+
) {
124+
self.init(
125+
toolchain: manifestResources,
126+
serializedDiagnostics: serializedDiagnostics,
127+
isManifestSandboxEnabled: isManifestSandboxEnabled,
128+
cacheDir: cacheDir,
129+
delegate: delegate,
130+
extraManifestFlags: extraManifestFlags
131+
)
132+
}
133+
160134
/// Loads a root manifest from a path using the resources associated with a particular `swiftc` executable.
161135
///
162136
/// - Parameters:
@@ -177,8 +151,8 @@ public final class ManifestLoader: ManifestLoaderProtocol {
177151
completion: @escaping (Result<Manifest, Error>) -> Void
178152
) {
179153
do {
180-
let resources = try UserManifestResources(swiftCompiler: swiftCompiler, swiftCompilerFlags: swiftCompilerFlags)
181-
let loader = ManifestLoader(manifestResources: resources)
154+
let toolchain = try ToolchainConfiguration(swiftCompiler: swiftCompiler, swiftCompilerFlags: swiftCompilerFlags)
155+
let loader = ManifestLoader(toolchain: toolchain)
182156
let toolsVersion = try ToolsVersionLoader().load(at: path, fileSystem: fileSystem)
183157
let packageLocation = fileSystem.isFile(path) ? path.parentDirectory : path
184158
let packageIdentity = identityResolver.resolveIdentity(for: packageLocation)
@@ -713,14 +687,14 @@ public final class ManifestLoader: ManifestLoaderProtocol {
713687
let moduleCachePath = (ProcessEnv.vars["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"]).flatMap{ AbsolutePath.init($0) }
714688

715689
var cmd: [String] = []
716-
cmd += [resources.swiftCompiler.pathString]
690+
cmd += [self.toolchain.swiftCompiler.pathString]
717691
cmd += verbosity.ccArgs
718692

719693
let macOSPackageDescriptionPath: AbsolutePath
720694
// If we got the binDir that means we could be developing SwiftPM in Xcode
721695
// which produces a framework for dynamic package products.
722696
let packageFrameworkPath = runtimePath.appending(component: "PackageFrameworks")
723-
if resources.binDir != nil, localFileSystem.exists(packageFrameworkPath) {
697+
if self.toolchain.binDir != nil, localFileSystem.exists(packageFrameworkPath) {
724698
cmd += [
725699
"-F", packageFrameworkPath.pathString,
726700
"-framework", "PackageDescription",
@@ -746,7 +720,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
746720
// Use the same minimum deployment target as the PackageDescription library (with a fallback of 10.15).
747721
#if os(macOS)
748722
let triple = Self._hostTriple.memoize {
749-
Triple.getHostTriple(usingSwiftCompiler: resources.swiftCompiler)
723+
Triple.getHostTriple(usingSwiftCompiler: self.toolchain.swiftCompiler)
750724
}
751725

752726
let version = try Self._packageDescriptionMinimumDeploymentTarget.memoize {
@@ -756,7 +730,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
756730
#endif
757731

758732
// Add any extra flags required as indicated by the ManifestLoader.
759-
cmd += resources.swiftCompilerFlags
733+
cmd += self.toolchain.swiftCompilerFlags
760734

761735
cmd += self.interpreterFlags(for: toolsVersion)
762736
if let moduleCachePath = moduleCachePath {
@@ -879,7 +853,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
879853
cmd += ["-swift-version", toolsVersion.swiftLanguageVersion.rawValue]
880854
cmd += ["-I", runtimePath.pathString]
881855
#if os(macOS)
882-
if let sdkRoot = resources.sdkRoot ?? self.sdkRoot() {
856+
if let sdkRoot = self.toolchain.sdkRoot ?? self.sdkRoot() {
883857
cmd += ["-sdk", sdkRoot.pathString]
884858
}
885859
#endif
@@ -890,18 +864,18 @@ public final class ManifestLoader: ManifestLoaderProtocol {
890864
/// Returns the runtime path given the manifest version and path to libDir.
891865
private func runtimePath(for version: ToolsVersion) -> AbsolutePath {
892866
// Bin dir will be set when developing swiftpm without building all of the runtimes.
893-
if let binDir = resources.binDir {
867+
if let binDir = self.toolchain.binDir {
894868
return binDir
895869
}
896870

897871
// Otherwise we use the standard location of the manifest API in the toolchain, if it exists.
898-
let manifestAPIDir = resources.libDir.appending(component: "ManifestAPI")
872+
let manifestAPIDir = self.toolchain.libDir.appending(component: "ManifestAPI")
899873
if localFileSystem.exists(manifestAPIDir) {
900874
return manifestAPIDir
901875
}
902876

903877
// Otherwise, fall back on the old location (this would indicate that we're using an old toolchain).
904-
return resources.libDir.appending(version.runtimeSubpath)
878+
return self.toolchain.libDir.appending(version.runtimeSubpath)
905879
}
906880

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

Sources/PackageModel/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ add_library(PackageModel
3131
SupportedLanguageExtension.swift
3232
SwiftLanguageVersion.swift
3333
Target.swift
34+
ToolchainConfiguration.swift
3435
ToolsVersion.swift
3536
ToolsVersionSpecificationGeneration.swift)
3637
target_link_libraries(PackageModel PUBLIC
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -10,33 +10,45 @@
1010

1111
import TSCBasic
1212

13-
/// Concrete object for manifest resource provider.
14-
public struct UserManifestResources: ManifestResourceProvider {
13+
/// Toolchain configuration required for evaluation os swift code such as the manifests or plugins
14+
///
15+
/// These requirements are abstracted out to make it easier to add support for
16+
/// using the package manager with alternate toolchains in the future.
17+
public struct ToolchainConfiguration {
18+
/// The path of the swift compiler.
1519
public let swiftCompiler: AbsolutePath
20+
21+
/// Extra flags to pass the Swift compiler.
1622
public let swiftCompilerFlags: [String]
23+
24+
/// The path of the library resources.
1725
public let libDir: AbsolutePath
26+
27+
/// The bin directory.
28+
public let binDir: AbsolutePath?
29+
30+
/// The path to SDK root.
31+
///
32+
/// If provided, it will be passed to the swift interpreter.
1833
public let sdkRoot: AbsolutePath?
34+
35+
/// XCTest Location
1936
public let xctestLocation: AbsolutePath?
20-
public let binDir: AbsolutePath?
2137

2238
public init(
2339
swiftCompiler: AbsolutePath,
2440
swiftCompilerFlags: [String],
2541
libDir: AbsolutePath,
42+
binDir: AbsolutePath? = nil,
2643
sdkRoot: AbsolutePath? = nil,
27-
xctestLocation: AbsolutePath? = nil,
28-
binDir: AbsolutePath? = nil
44+
xctestLocation: AbsolutePath? = nil
2945
) {
3046
self.swiftCompiler = swiftCompiler
3147
self.swiftCompilerFlags = swiftCompilerFlags
3248
self.libDir = libDir
49+
self.binDir = binDir
3350
self.sdkRoot = sdkRoot
3451
self.xctestLocation = xctestLocation
35-
self.binDir = binDir
36-
}
37-
38-
public static func libDir(forBinDir binDir: AbsolutePath) -> AbsolutePath {
39-
return binDir.parentDirectory.appending(components: "lib", "swift", "pm")
4052
}
4153

4254
/// Creates the set of manifest resources associated with a `swiftc` executable.
@@ -48,7 +60,11 @@ public struct UserManifestResources: ManifestResourceProvider {
4860
self.init(
4961
swiftCompiler: swiftCompiler,
5062
swiftCompilerFlags: swiftCompilerFlags,
51-
libDir: UserManifestResources.libDir(forBinDir: binDir)
63+
libDir: Self.libDir(forBinDir: binDir)
5264
)
5365
}
66+
67+
public static func libDir(forBinDir binDir: AbsolutePath) -> AbsolutePath {
68+
return binDir.parentDirectory.appending(components: "lib", "swift", "pm")
69+
}
5470
}

Sources/SPMTestSupport/Resources.swift

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)