Skip to content

Commit 15bbfe4

Browse files
committed
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 2226d7e commit 15bbfe4

30 files changed

+211
-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: 13 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
@@ -177,8 +131,8 @@ public final class ManifestLoader: ManifestLoaderProtocol {
177131
completion: @escaping (Result<Manifest, Error>) -> Void
178132
) {
179133
do {
180-
let resources = try UserManifestResources(swiftCompiler: swiftCompiler, swiftCompilerFlags: swiftCompilerFlags)
181-
let loader = ManifestLoader(manifestResources: resources)
134+
let toolchain = try ToolchainConfiguration(swiftCompiler: swiftCompiler, swiftCompilerFlags: swiftCompilerFlags)
135+
let loader = ManifestLoader(toolchain: toolchain)
182136
let toolsVersion = try ToolsVersionLoader().load(at: path, fileSystem: fileSystem)
183137
let packageLocation = fileSystem.isFile(path) ? path.parentDirectory : path
184138
let packageIdentity = identityResolver.resolveIdentity(for: packageLocation)
@@ -713,14 +667,14 @@ public final class ManifestLoader: ManifestLoaderProtocol {
713667
let moduleCachePath = (ProcessEnv.vars["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"]).flatMap{ AbsolutePath.init($0) }
714668

715669
var cmd: [String] = []
716-
cmd += [resources.swiftCompiler.pathString]
670+
cmd += [self.toolchain.swiftCompiler.pathString]
717671
cmd += verbosity.ccArgs
718672

719673
let macOSPackageDescriptionPath: AbsolutePath
720674
// If we got the binDir that means we could be developing SwiftPM in Xcode
721675
// which produces a framework for dynamic package products.
722676
let packageFrameworkPath = runtimePath.appending(component: "PackageFrameworks")
723-
if resources.binDir != nil, localFileSystem.exists(packageFrameworkPath) {
677+
if self.toolchain.binDir != nil, localFileSystem.exists(packageFrameworkPath) {
724678
cmd += [
725679
"-F", packageFrameworkPath.pathString,
726680
"-framework", "PackageDescription",
@@ -746,7 +700,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
746700
// Use the same minimum deployment target as the PackageDescription library (with a fallback of 10.15).
747701
#if os(macOS)
748702
let triple = Self._hostTriple.memoize {
749-
Triple.getHostTriple(usingSwiftCompiler: resources.swiftCompiler)
703+
Triple.getHostTriple(usingSwiftCompiler: self.toolchain.swiftCompiler)
750704
}
751705

752706
let version = try Self._packageDescriptionMinimumDeploymentTarget.memoize {
@@ -756,7 +710,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
756710
#endif
757711

758712
// Add any extra flags required as indicated by the ManifestLoader.
759-
cmd += resources.swiftCompilerFlags
713+
cmd += self.toolchain.swiftCompilerFlags
760714

761715
cmd += self.interpreterFlags(for: toolsVersion)
762716
if let moduleCachePath = moduleCachePath {
@@ -879,7 +833,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
879833
cmd += ["-swift-version", toolsVersion.swiftLanguageVersion.rawValue]
880834
cmd += ["-I", runtimePath.pathString]
881835
#if os(macOS)
882-
if let sdkRoot = resources.sdkRoot ?? self.sdkRoot() {
836+
if let sdkRoot = self.toolchain.sdkRoot ?? self.sdkRoot() {
883837
cmd += ["-sdk", sdkRoot.pathString]
884838
}
885839
#endif
@@ -890,18 +844,18 @@ public final class ManifestLoader: ManifestLoaderProtocol {
890844
/// Returns the runtime path given the manifest version and path to libDir.
891845
private func runtimePath(for version: ToolsVersion) -> AbsolutePath {
892846
// Bin dir will be set when developing swiftpm without building all of the runtimes.
893-
if let binDir = resources.binDir {
847+
if let binDir = self.toolchain.binDir {
894848
return binDir
895849
}
896850

897851
// Otherwise we use the standard location of the manifest API in the toolchain, if it exists.
898-
let manifestAPIDir = resources.libDir.appending(component: "ManifestAPI")
852+
let manifestAPIDir = self.toolchain.libDir.appending(component: "ManifestAPI")
899853
if localFileSystem.exists(manifestAPIDir) {
900854
return manifestAPIDir
901855
}
902856

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

907861
/// 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.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import Foundation
12+
import PackageModel
13+
import Workspace
14+
import TSCBasic
15+
16+
#if os(macOS)
17+
private func macOSBundleRoot() -> AbsolutePath {
18+
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
19+
return AbsolutePath(bundle.bundlePath).parentDirectory
20+
}
21+
fatalError()
22+
}
23+
#endif
24+
25+
private func resolveBinDir() -> AbsolutePath {
26+
#if os(macOS)
27+
return macOSBundleRoot()
28+
#else
29+
return AbsolutePath(CommandLine.arguments[0], relativeTo: localFileSystem.currentWorkingDirectory!).parentDirectory
30+
#endif
31+
}
32+
33+
extension ToolchainConfiguration {
34+
public static var `default`: Self {
35+
get {
36+
let toolchain = UserToolchain.default
37+
return .init(
38+
swiftCompiler: toolchain.configuration.swiftCompiler,
39+
swiftCompilerFlags: [],
40+
libDir: toolchain.configuration.libDir,
41+
binDir: toolchain.configuration.binDir
42+
)
43+
}
44+
}
45+
46+
#if os(macOS)
47+
public var sdkPlatformFrameworksPath: AbsolutePath {
48+
return Destination.sdkPlatformFrameworkPaths()!.fwk
49+
}
50+
#endif
51+
52+
}
53+
54+
extension UserToolchain {
55+
public static var `default`: Self {
56+
get {
57+
let binDir = resolveBinDir()
58+
return try! .init(destination: Destination.hostDestination(binDir))
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)