Skip to content

Commit 397c7d6

Browse files
authored
[6.0] Fix checksum computation for Swift SDK bundles (#7749)
Cherry-pick of #7748. **Explanation**: This feature [was specified in the corresponding proposal for Swift SDKs](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md#swift-sdk-installation-and-configuration): > For Swift SDKs installed from remote URLs an additional `--checksum` option is required, through which users of a Swift SDK can specify a checksum provided by a publisher of the SDK. The latter can produce a checksum by running `swift package compute-checksum` command (introduced in [SE-0272](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0272-swiftpm-binary-dependencies.md)) with the Swift SDK bundle archive as an argument. Currently, `swift package compute-checksum` is unable to handle `.tar.gz` bundles, which is a commonly used format for Swift SDKs. We're fixing that here by adding correct and unified handling for archive extensions. This change is isolated to the `swift package compute-checksum` subcommand. The `--checksum` option is added in a subsequent PR: #7722. **Scope**: Isolated to `swift package compute-checksum` subcommand. **Risk**: Low, no existing behavior is **Testing**: Automated test cases modified/added and passing in a subsequent PR: #7723 **Issue**: rdar://130590711 **Reviewer**: @bnbarham
1 parent 8f3b288 commit 397c7d6

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

Sources/Basics/Archiver/Archiver.swift

+4
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,8 @@ extension Archiver {
9595
self.validate(path: path, completion: { continuation.resume(with: $0) })
9696
}
9797
}
98+
99+
package func isFileSupported(_ lastPathComponent: String) -> Bool {
100+
self.supportedExtensions.contains(where: { lastPathComponent.hasSuffix($0) })
101+
}
98102
}

Sources/Commands/PackageCommands/ComputeChecksum.swift

+3-10
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,10 @@ struct ComputeChecksum: SwiftCommand {
2828
var path: AbsolutePath
2929

3030
func run(_ swiftCommandState: SwiftCommandState) throws {
31-
let binaryArtifactsManager = try Workspace.BinaryArtifactsManager(
32-
fileSystem: swiftCommandState.fileSystem,
33-
authorizationProvider: swiftCommandState.getAuthorizationProvider(),
34-
hostToolchain: swiftCommandState.getHostToolchain(),
35-
checksumAlgorithm: SHA256(),
36-
cachePath: .none,
37-
customHTTPClient: .none,
38-
customArchiver: .none,
39-
delegate: .none
31+
let checksum = try Workspace.BinaryArtifactsManager.checksum(
32+
forBinaryArtifactAt: self.path,
33+
fileSystem: swiftCommandState.fileSystem
4034
)
41-
let checksum = try binaryArtifactsManager.checksum(forBinaryArtifactAt: path)
4235
print(checksum)
4336
}
4437
}

Sources/Workspace/Workspace+BinaryArtifacts.swift

+22-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import SPMBuildCore
1818

1919
import struct TSCBasic.ByteString
2020
import protocol TSCBasic.HashAlgorithm
21-
21+
import struct TSCBasic.SHA256
2222
import enum TSCUtility.Diagnostics
2323

2424
extension Workspace {
@@ -537,20 +537,35 @@ extension Workspace {
537537
return result.get()
538538
}
539539

540-
public func checksum(forBinaryArtifactAt path: AbsolutePath) throws -> String {
540+
package static func checksum(
541+
forBinaryArtifactAt path: AbsolutePath,
542+
hashAlgorithm: HashAlgorithm = SHA256(),
543+
archiver: (any Archiver)? = nil,
544+
fileSystem: any FileSystem
545+
) throws -> String {
546+
let archiver = archiver ?? UniversalArchiver(fileSystem)
541547
// Validate the path has a supported extension.
542-
guard let pathExtension = path.extension, self.archiver.supportedExtensions.contains(pathExtension) else {
543-
let supportedExtensionList = self.archiver.supportedExtensions.joined(separator: ", ")
548+
guard let lastPathComponent = path.components.last, archiver.isFileSupported(lastPathComponent) else {
549+
let supportedExtensionList = archiver.supportedExtensions.joined(separator: ", ")
544550
throw StringError("unexpected file type; supported extensions are: \(supportedExtensionList)")
545551
}
546552

547553
// Ensure that the path with the accepted extension is a file.
548-
guard self.fileSystem.isFile(path) else {
554+
guard fileSystem.isFile(path) else {
549555
throw StringError("file not found at path: \(path.pathString)")
550556
}
551557

552-
let contents = try self.fileSystem.readFileContents(path)
553-
return self.checksumAlgorithm.hash(contents).hexadecimalRepresentation
558+
let contents = try fileSystem.readFileContents(path)
559+
return hashAlgorithm.hash(contents).hexadecimalRepresentation
560+
}
561+
562+
public func checksum(forBinaryArtifactAt path: AbsolutePath) throws -> String {
563+
try Self.checksum(
564+
forBinaryArtifactAt: path,
565+
hashAlgorithm: self.checksumAlgorithm,
566+
archiver: self.archiver,
567+
fileSystem: self.fileSystem
568+
)
554569
}
555570

556571
public func cancel(deadline: DispatchTime) throws {

0 commit comments

Comments
 (0)