Skip to content

Commit 731449c

Browse files
committed
deprecate static APIs with inefficient defaults
motivation: configuration work has pointed out that some of the static APIs (which are questionable to begin with) use inefficient defaults changes: * deprecate Workspace.create, Workspace.loadRootGraph, PackageBuilder.loadRootPackage, ManifestLoader.loadRootManifest and replace them with instance methods on workspace * add a simple constructor to workspace, that uses the host toolchain by default * adjust call-sites, test and examples
1 parent 15bbfe4 commit 731449c

File tree

9 files changed

+231
-102
lines changed

9 files changed

+231
-102
lines changed

Examples/package-info/Package.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
// swift-tools-version:4.2
1+
// swift-tools-version:5.1
2+
23
import PackageDescription
34

45
let package = Package(
56
name: "package-info",
7+
platforms: [
8+
.macOS(.v10_15),
9+
.iOS(.v13)
10+
],
611
dependencies: [
712
// This just points to the SwiftPM at the root of this repository.
813
.package(path: "../../"),

Examples/package-info/Sources/package-info/main.swift

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,29 @@ import Workspace
66
// PREREQUISITES
77
// ============
88

9-
// We will need to know where the Swift compiler is.
10-
let swiftCompiler: AbsolutePath = {
11-
let string: String
12-
#if os(macOS)
13-
string = try! Process.checkNonZeroExit(args: "xcrun", "--sdk", "macosx", "-f", "swiftc").spm_chomp()
14-
#else
15-
string = try! Process.checkNonZeroExit(args: "which", "swiftc").spm_chomp()
16-
#endif
17-
return AbsolutePath(string)
18-
}()
19-
209
// We need a package to work with.
21-
// This assumes there is one in the current working directory:
22-
let packagePath = localFileSystem.currentWorkingDirectory!
10+
// This computes the path of this package root based on the file location
11+
let packagePath = AbsolutePath(#file).parentDirectory.parentDirectory.parentDirectory
2312

2413
// LOADING
2514
// =======
2615

27-
// Note:
28-
// This simplified API has been added since 0.4.0 was released.
29-
// See older revisions for examples that work with 0.4.0.
30-
3116
// There are several levels of information available.
3217
// Each takes longer to load than the level above it, but provides more detail.
3318
let diagnostics = DiagnosticsEngine()
34-
let identityResolver = DefaultIdentityResolver()
35-
let manifest = try tsc_await { ManifestLoader.loadRootManifest(at: packagePath, swiftCompiler: swiftCompiler, swiftCompilerFlags: [], identityResolver: identityResolver, on: .global(), completion: $0) }
36-
let loadedPackage = try tsc_await { PackageBuilder.loadRootPackage(at: packagePath, swiftCompiler: swiftCompiler, swiftCompilerFlags: [], identityResolver: identityResolver, diagnostics: diagnostics, on: .global(), completion: $0) }
37-
let graph = try Workspace.loadRootGraph(at: packagePath, swiftCompiler: swiftCompiler, swiftCompilerFlags: [], identityResolver: identityResolver, diagnostics: diagnostics)
19+
let workspace = try Workspace(forRootPackage: packagePath)
20+
let manifest = try tsc_await { workspace.loadRootManifest(at: packagePath, diagnostics: diagnostics, completion: $0) }
21+
22+
let package = try tsc_await { workspace.loadRootPackage(at: packagePath, diagnostics: diagnostics, completion: $0) }
23+
guard !diagnostics.hasErrors else {
24+
fatalError("error package manifest: \(diagnostics)")
25+
}
26+
27+
let graph = try workspace.loadPackageGraph(rootPath: packagePath, diagnostics: diagnostics)
28+
guard !diagnostics.hasErrors else {
29+
fatalError("error loading package dependencies: \(diagnostics)")
30+
}
31+
3832

3933
// EXAMPLES
4034
// ========
@@ -46,7 +40,7 @@ let targets = manifest.targets.map({ $0.name }).joined(separator: ", ")
4640
print("Targets:", targets)
4741

4842
// Package
49-
let executables = loadedPackage.targets.filter({ $0.type == .executable }).map({ $0.name })
43+
let executables = package.targets.filter({ $0.type == .executable }).map({ $0.name })
5044
print("Executable targets:", executables)
5145

5246
// PackageGraph

Sources/Commands/APIDigester.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct APIDigesterBaselineDumper {
104104
try workingCopy.checkout(revision: baselineRevision)
105105

106106
// Create the workspace for this package.
107-
let workspace = Workspace.create(
107+
let workspace = try Workspace(
108108
forRootPackage: baselinePackageRoot,
109109
manifestLoader: manifestLoader,
110110
repositoryManager: repositoryManager

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
120120
/// - diagnostics: Optional. The diagnostics engine.
121121
/// - on: The dispatch queue to perform asynchronous operations on.
122122
/// - completion: The completion handler .
123+
@available(*, deprecated, message: "use workspace API instead")
123124
public static func loadRootManifest(
124125
at path: AbsolutePath,
125126
swiftCompiler: AbsolutePath,
@@ -131,7 +132,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
131132
completion: @escaping (Result<Manifest, Error>) -> Void
132133
) {
133134
do {
134-
let toolchain = try ToolchainConfiguration(swiftCompiler: swiftCompiler, swiftCompilerFlags: swiftCompilerFlags)
135+
let toolchain = ToolchainConfiguration(swiftCompiler: swiftCompiler, swiftCompilerFlags: swiftCompilerFlags)
135136
let loader = ManifestLoader(toolchain: toolchain)
136137
let toolsVersion = try ToolsVersionLoader().load(at: path, fileSystem: fileSystem)
137138
let packageLocation = fileSystem.isFile(path) ? path.parentDirectory : path

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public final class PackageBuilder {
242242
/// Create the special REPL product for this package.
243243
private let createREPLProduct: Bool
244244

245-
/// The additionla file detection rules.
245+
/// The additional file detection rules.
246246
private let additionalFileRules: [FileRuleDescription]
247247

248248
/// Minimum deployment target of XCTest per platform.
@@ -296,6 +296,7 @@ public final class PackageBuilder {
296296
/// - diagnostics: Optional. The diagnostics engine.
297297
/// - on: The dispatch queue to perform asynchronous operations on.
298298
/// - completion: The completion handler .
299+
@available(*, deprecated, message: "use workspace API instead")
299300
public static func loadRootPackage(
300301
at path: AbsolutePath,
301302
swiftCompiler: AbsolutePath,

Sources/PackageModel/ToolchainConfiguration.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,31 @@ public struct ToolchainConfiguration {
3535
/// XCTest Location
3636
public let xctestLocation: AbsolutePath?
3737

38+
/// Creates the set of manifest resources associated with a `swiftc` executable.
39+
///
40+
/// - Parameters:
41+
/// - swiftCompiler: The absolute path of the associated `swiftc` executable.
42+
/// - 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
3847
public init(
3948
swiftCompiler: AbsolutePath,
40-
swiftCompilerFlags: [String],
41-
libDir: AbsolutePath,
49+
swiftCompilerFlags: [String] = [],
50+
libDir: AbsolutePath? = nil,
4251
binDir: AbsolutePath? = nil,
4352
sdkRoot: AbsolutePath? = nil,
4453
xctestLocation: AbsolutePath? = nil
4554
) {
4655
self.swiftCompiler = swiftCompiler
4756
self.swiftCompilerFlags = swiftCompilerFlags
48-
self.libDir = libDir
57+
self.libDir = libDir ?? Self.libDir(forBinDir: swiftCompiler.parentDirectory)
4958
self.binDir = binDir
5059
self.sdkRoot = sdkRoot
5160
self.xctestLocation = xctestLocation
5261
}
5362

54-
/// Creates the set of manifest resources associated with a `swiftc` executable.
55-
///
56-
/// - Parameters:
57-
/// - swiftCompiler: The absolute path of the associated `swiftc` executable.
58-
public init(swiftCompiler: AbsolutePath, swiftCompilerFlags: [String]) throws {
59-
let binDir = swiftCompiler.parentDirectory
60-
self.init(
61-
swiftCompiler: swiftCompiler,
62-
swiftCompilerFlags: swiftCompilerFlags,
63-
libDir: Self.libDir(forBinDir: binDir)
64-
)
65-
}
66-
6763
public static func libDir(forBinDir binDir: AbsolutePath) -> AbsolutePath {
6864
return binDir.parentDirectory.appending(components: "lib", "swift", "pm")
6965
}

Sources/SPMTestSupport/Toolchain.swift

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,74 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2017 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
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
9+
*/
1010

11+
//
12+
//import SPMBuildCore
1113
import Foundation
14+
//import PackageLoading
1215
import PackageModel
1316
import Workspace
1417
import TSCBasic
1518

1619
#if os(macOS)
17-
private func macOSBundleRoot() -> AbsolutePath {
20+
private func bundleRoot() -> AbsolutePath {
1821
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
1922
return AbsolutePath(bundle.bundlePath).parentDirectory
2023
}
2124
fatalError()
2225
}
2326
#endif
2427

25-
private func resolveBinDir() -> AbsolutePath {
28+
/*
29+
public class Resources: ManifestResourceProvider {
30+
31+
public var swiftCompiler: AbsolutePath {
32+
return toolchain.manifestResources.swiftCompiler
33+
}
34+
35+
public var libDir: AbsolutePath {
36+
return toolchain.manifestResources.libDir
37+
}
38+
39+
public var binDir: AbsolutePath? {
40+
return toolchain.manifestResources.binDir
41+
}
42+
43+
public var swiftCompilerFlags: [String] {
44+
return []
45+
}
46+
47+
#if os(macOS)
48+
public var sdkPlatformFrameworksPath: AbsolutePath {
49+
return Destination.sdkPlatformFrameworkPaths()!.fwk
50+
}
51+
#endif
52+
53+
public let toolchain: UserToolchain
54+
55+
public static let `default` = Resources()
56+
57+
private init() {
58+
let binDir: AbsolutePath
59+
#if os(macOS)
60+
binDir = bundleRoot()
61+
#else
62+
binDir = AbsolutePath(CommandLine.arguments[0], relativeTo: localFileSystem.currentWorkingDirectory!).parentDirectory
63+
#endif
64+
toolchain = try! UserToolchain(destination: Destination.hostDestination(binDir))
65+
}
66+
}
67+
*/
68+
69+
private func binDir() -> AbsolutePath {
2670
#if os(macOS)
27-
return macOSBundleRoot()
71+
return bundleRoot()
2872
#else
2973
return AbsolutePath(CommandLine.arguments[0], relativeTo: localFileSystem.currentWorkingDirectory!).parentDirectory
3074
#endif
@@ -54,7 +98,7 @@ extension ToolchainConfiguration {
5498
extension UserToolchain {
5599
public static var `default`: Self {
56100
get {
57-
let binDir = resolveBinDir()
101+
let binDir = binDir()
58102
return try! .init(destination: Destination.hostDestination(binDir))
59103
}
60104
}

0 commit comments

Comments
 (0)