Skip to content

Commit 58313e4

Browse files
committed
[PackageLoading] Manifest caching using llbuild
<rdar://problem/35596212> Add support for an evaluated manifest cache This adds support for caching the package manifest files which have physical path on the disk. This basically means that manifest caching will work in all operations except during dependency resolution.
1 parent 3e71e57 commit 58313e4

File tree

12 files changed

+620
-54
lines changed

12 files changed

+620
-54
lines changed

Sources/Basic/Result.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ public struct AnyError: Swift.Error, CustomStringConvertible {
101101
}
102102
}
103103

104+
/// Represents a string error.
105+
public struct StringError: Equatable, Codable, CustomStringConvertible, Error {
106+
107+
/// The description of the error.
108+
public let description: String
109+
110+
/// Create an instance of StringError.
111+
public init(_ description: String) {
112+
self.description = description
113+
}
114+
}
115+
104116
// AnyError specific helpers.
105117
extension Result where ErrorType == AnyError {
106118
/// Initialise with something that throws AnyError.
@@ -133,3 +145,55 @@ extension Result where ErrorType == AnyError {
133145
}
134146
}
135147
}
148+
149+
extension Result where ErrorType == StringError {
150+
/// Create an instance of Result<Value, StringError>.
151+
///
152+
/// Errors will be encoded as StringError using their description.
153+
public init(string body: () throws -> Value) {
154+
do {
155+
self = .success(try body())
156+
} catch let error as StringError {
157+
self = .failure(error)
158+
} catch {
159+
self = .failure(StringError(String(describing: error)))
160+
}
161+
}
162+
}
163+
164+
extension Result: Equatable where Value: Equatable, ErrorType: Equatable {}
165+
166+
extension Result: Codable where Value: Codable, ErrorType: Codable {
167+
private enum CodingKeys: String, CodingKey {
168+
case success, failure
169+
}
170+
171+
public func encode(to encoder: Encoder) throws {
172+
var container = encoder.container(keyedBy: CodingKeys.self)
173+
switch self {
174+
case .success(let value):
175+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .success)
176+
try unkeyedContainer.encode(value)
177+
case .failure(let error):
178+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .failure)
179+
try unkeyedContainer.encode(error)
180+
}
181+
}
182+
183+
public init(from decoder: Decoder) throws {
184+
let values = try decoder.container(keyedBy: CodingKeys.self)
185+
guard let key = values.allKeys.first(where: values.contains) else {
186+
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
187+
}
188+
switch key {
189+
case .success:
190+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
191+
let value = try unkeyedValues.decode(Value.self)
192+
self = .success(value)
193+
case .failure:
194+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
195+
let error = try unkeyedValues.decode(ErrorType.self)
196+
self = .failure(error)
197+
}
198+
}
199+
}

Sources/Commands/Options.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class ToolOptions {
4040
/// Disables sandboxing when executing subprocesses.
4141
public var shouldDisableSandbox = false
4242

43+
/// Disables manifest caching.
44+
public var shouldDisableManifestCaching = false
45+
4346
/// Path to the compilation destination describing JSON file.
4447
public var customCompileDestination: AbsolutePath?
4548

Sources/Commands/SwiftTool.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ public class SwiftTool<Options: ToolOptions> {
331331
usage: "Disable using the sandbox when executing subprocesses"),
332332
to: { $0.shouldDisableSandbox = $1 })
333333

334+
binder.bind(
335+
option: parser.add(option: "--disable-package-manifest-caching", kind: Bool.self,
336+
usage: "Disable caching Package.swift manifests"),
337+
to: { $0.shouldDisableManifestCaching = $1 })
338+
334339
binder.bind(
335340
option: parser.add(option: "--version", kind: Bool.self),
336341
to: { $0.shouldPrintVersion = $1 })
@@ -723,7 +728,9 @@ public class SwiftTool<Options: ToolOptions> {
723728
try ManifestLoader(
724729
// Always use the host toolchain's resources for parsing manifest.
725730
resources: self._hostToolchain.dematerialize().manifestResources,
726-
isManifestSandboxEnabled: !self.options.shouldDisableSandbox
731+
isManifestSandboxEnabled: !self.options.shouldDisableSandbox,
732+
isManifestCachingEnabled: !self.options.shouldDisableManifestCaching,
733+
cacheDir: self.buildPath
727734
)
728735
})
729736
}()

0 commit comments

Comments
 (0)