Skip to content

Commit 7319148

Browse files
authored
Simplify cross-platform hashing implementations (#388)
Eliminate the package dependency on swift-crypto to simplify the bootstrapped build. Instead, vendor the SHA256 implementation from swift-tools-support-core and use it as a fallback if neither WinSDK nor CryptoKit is available.
1 parent 253fbdf commit 7319148

29 files changed

+248
-83
lines changed

Package.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ let package = Package(
195195
"SWBCSupport",
196196
"SWBLibc",
197197
.product(name: "ArgumentParser", package: "swift-argument-parser"),
198-
.product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: [.linux, .openbsd, .android])),
199198
.product(name: "SystemPackage", package: "swift-system", condition: .when(platforms: [.linux, .android, .windows])),
200199
],
201200
exclude: ["CMakeLists.txt"],
@@ -427,7 +426,6 @@ for target in package.targets {
427426
// `SWIFTCI_USE_LOCAL_DEPS` configures if dependencies are locally available to build
428427
if useLocalDependencies {
429428
package.dependencies += [
430-
.package(path: "../swift-crypto"),
431429
.package(path: "../swift-driver"),
432430
.package(path: "../swift-system"),
433431
.package(path: "../swift-argument-parser"),
@@ -437,7 +435,6 @@ if useLocalDependencies {
437435
}
438436
} else {
439437
package.dependencies += [
440-
.package(url: "https://github.com/apple/swift-crypto.git", "2.0.0"..<"4.0.0"),
441438
.package(url: "https://github.com/swiftlang/swift-driver.git", branch: "main"),
442439
.package(url: "https://github.com/apple/swift-system.git", .upToNextMajor(from: "1.4.1")),
443440
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.3"),

Plugins/cmake-smoke-test/cmake-smoke-test.swift

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ struct CMakeSmokeTest: CommandPlugin {
4141
let swiftSystemURL = try findSiblingRepository("swift-system", swiftBuildURL: swiftBuildURL)
4242
let swiftSystemBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-system")
4343

44-
let swiftAsn1URL = try findSiblingRepository("swift-asn1", swiftBuildURL: swiftBuildURL)
45-
let swiftAsn1BuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-asn1")
46-
47-
let swiftCryptoURL = try findSiblingRepository("swift-crypto", swiftBuildURL: swiftBuildURL)
48-
let swiftCryptoBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-crypto")
49-
5044
let llbuildURL = try findSiblingRepository("llbuild", swiftBuildURL: swiftBuildURL)
5145
let llbuildBuildURL = context.pluginWorkDirectoryURL.appending(component: "llbuild")
5246

@@ -56,7 +50,7 @@ struct CMakeSmokeTest: CommandPlugin {
5650
let swiftDriverURL = try findSiblingRepository("swift-driver", swiftBuildURL: swiftBuildURL)
5751
let swiftDriverBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-driver")
5852

59-
for url in [swiftToolsSupportCoreBuildURL, swiftAsn1BuildURL, swiftCryptoBuildURL, swiftSystemBuildURL, llbuildBuildURL, swiftArgumentParserBuildURL, swiftDriverBuildURL, swiftBuildBuildURL] {
53+
for url in [swiftToolsSupportCoreBuildURL, swiftSystemBuildURL, llbuildBuildURL, swiftArgumentParserBuildURL, swiftDriverBuildURL, swiftBuildBuildURL] {
6054
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
6155
}
6256

@@ -69,8 +63,6 @@ struct CMakeSmokeTest: CommandPlugin {
6963
"-DArgumentParser_DIR=\(swiftArgumentParserBuildURL.appending(components: "cmake", "modules").path())",
7064
"-DLLBuild_DIR=\(llbuildBuildURL.appending(components: "cmake", "modules").path())",
7165
"-DTSC_DIR=\(swiftToolsSupportCoreBuildURL.appending(components: "cmake", "modules").path())",
72-
"-DSwiftASN1_DIR=\(swiftAsn1BuildURL.appending(components: "cmake", "modules").path())",
73-
"-DSwiftCrypto_DIR=\(swiftCryptoBuildURL.appending(components: "cmake", "modules").path())",
7466
"-DSwiftDriver_DIR=\(swiftDriverBuildURL.appending(components: "cmake", "modules").path())",
7567
"-DSwiftSystem_DIR=\(swiftSystemBuildURL.appending(components: "cmake", "modules").path())"
7668
]
@@ -87,18 +79,6 @@ struct CMakeSmokeTest: CommandPlugin {
8779
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftToolsSupportCoreBuildURL)
8880
print("Built swift-tools-support-core")
8981

90-
if hostOS != .macOS && hostOS != .windows {
91-
print("Building swift-asn1")
92-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftAsn1URL.path()], workingDirectory: swiftAsn1BuildURL)
93-
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftAsn1BuildURL)
94-
print("Built swift-asn1")
95-
96-
print("Building swift-crypto")
97-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftCryptoURL.path()], workingDirectory: swiftCryptoBuildURL)
98-
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftCryptoBuildURL)
99-
print("Built swift-crypto")
100-
}
101-
10282
if hostOS != .macOS {
10383
print("Building swift-system")
10484
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftSystemURL.path()], workingDirectory: swiftSystemBuildURL)

Sources/SWBBuildSystem/BuildOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
15301530
return
15311531
}
15321532

1533-
let signatureCtx = MD5Context()
1533+
let signatureCtx = InsecureHashContext()
15341534
signatureCtx.add(string: "CleanupCompileCache")
15351535
signatureCtx.add(string: cachePath.str)
15361536
let signature = signatureCtx.signature

Sources/SWBCore/EnvironmentBindings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public struct EnvironmentBindings: Sendable {
4141
}
4242

4343
/// Add a signature of the bindings into the given context.
44-
public func computeSignature(into ctx: MD5Context) {
44+
public func computeSignature(into ctx: InsecureHashContext) {
4545
for (variable, val) in bindings {
4646
// The signature computation should record the variable name and value data, and the positions which divide them.
4747
ctx.add(string: variable)

Sources/SWBCore/LibSwiftDriver/PlannedBuild.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public struct SwiftDriverJob: Serializable, CustomDebugStringConvertible {
103103
self.cacheKeys = job.outputCacheKeys.reduce(into: [String]()) { result, key in
104104
result.append(key.value)
105105
}.sorted()
106-
let md5 = MD5Context()
106+
let md5 = InsecureHashContext()
107107
for arg in commandLine {
108108
md5.add(bytes: arg)
109109
}

Sources/SWBCore/PlannedTask.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public struct TaskIdentifier: Comparable, Hashable, Serializable, CustomStringCo
2828
}
2929

3030
public var sandboxProfileSentinel: String {
31-
let taskIdentifierChecksumContext = MD5Context()
31+
let taskIdentifierChecksumContext = InsecureHashContext()
3232
taskIdentifierChecksumContext.add(string: self.rawValue)
3333
return taskIdentifierChecksumContext.signature.asString
3434
}
@@ -45,7 +45,7 @@ extension TaskIdentifier {
4545
}
4646

4747
public init(forTarget: ConfiguredTarget?, dynamicTaskPayload: ByteString, priority: TaskPriority) {
48-
let ctx = MD5Context()
48+
let ctx = InsecureHashContext()
4949
ctx.add(bytes: dynamicTaskPayload)
5050
self.rawValue = "P\(priority.rawValue):\(forTarget?.guid.stringValue ?? ""):\(forTarget?.parameters.configuration ?? ""):\(ctx.signature.asString)"
5151
}

Sources/SWBCore/ProjectModel/BuildPhase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public class BuildPhaseWithBuildFiles: BuildPhase, @unchecked Sendable
124124

125125
/// Returns a string that can be used as a suffix for the base name of a derived file in order to make its filename unique. This is based on the file's path but contains only characters that have no special meaning in filenames. For example, the string will never contain a path separator character.
126126
public static func filenameUniquefierSuffixFor(path: Path) -> String {
127-
let digester = MD5Context.init()
127+
let digester = InsecureHashContext.init()
128128
digester.add(string: path.normalize().str)
129129
return digester.signature.asString
130130
}

Sources/SWBCore/SpecImplementations/Tools/CCompiler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ public class ClangCompilerSpec : CompilerSpec, SpecIdentifierType, GCCCompatible
690690
previousArg = argAsByteString
691691
}
692692

693-
let ctx = MD5Context()
693+
let ctx = InsecureHashContext()
694694
ctx.add(string: inputFileType.identifier)
695695
ctx.add(string: self.identifier)
696696

@@ -1412,7 +1412,7 @@ public class ClangCompilerSpec : CompilerSpec, SpecIdentifierType, GCCCompatible
14121412
return nil
14131413
}
14141414

1415-
let md5 = MD5Context()
1415+
let md5 = InsecureHashContext()
14161416
md5.add(string: prefixHeader.str)
14171417
let sharingIdentHashValue = md5.signature
14181418
let baseCachePath = scope.evaluate(BuiltinMacros.SHARED_PRECOMPS_DIR)

Sources/SWBCore/SpecImplementations/Tools/SwiftCompiler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ public final class SwiftCommandOutputParser: TaskOutputParser {
951951
ruleInfo = "\(ruleInfo) \(path.str.quotedDescription)"
952952
}
953953
let signature: ByteString = {
954-
let md5 = MD5Context()
954+
let md5 = InsecureHashContext()
955955
md5.add(string: ruleInfo)
956956
return md5.signature
957957
}()
@@ -3584,7 +3584,7 @@ extension SwiftCompilerSpec {
35843584
static public func computeRuleInfoAndSignatureForPerFileVirtualBatchSubtask(variant: String, arch: String, path: Path) -> ([String], ByteString) {
35853585
let ruleInfo = ["SwiftCompile", variant, arch, path.str.quotedDescription]
35863586
let signature: ByteString = {
3587-
let md5 = MD5Context()
3587+
let md5 = InsecureHashContext()
35883588
md5.add(string: ruleInfo.joined(separator: " "))
35893589
return md5.signature
35903590
}()

Sources/SWBTaskConstruction/TaskProducers/OtherTaskProducers/CustomTaskProducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class CustomTaskProducer: PhasedTaskProducer, TaskProducer {
3737

3838
if outputs.isEmpty {
3939
// If there are no outputs, create a virtual output that can be wired up to gates
40-
let md5Context = MD5Context()
40+
let md5Context = InsecureHashContext()
4141
for arg in commandLine {
4242
md5Context.add(string: arg)
4343
}

0 commit comments

Comments
 (0)