Skip to content

Commit c846d28

Browse files
Remove unused toolchain components from WebAssembly SDK (#80)
Currently, Wasm SDK contains whole host toolchain package contents, but some components are unnecessary and duplicated with the installed host toolchain. This PR removes unused components as Linux SDK generator does, and in addition, removes lldb-related components. The total reduction in `.artifactbundle` for Linux host is: 2.7GB -> 1.3GB ( -1.1GB by platform libraries and host tools, -300MB by lldb components)
1 parent b94744f commit c846d28

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import GeneratorEngine
1414
import struct SystemPackage.FilePath
1515

16-
private let unusedDarwinPlatforms = [
16+
let unusedDarwinPlatforms = [
1717
"watchsimulator",
1818
"iphonesimulator",
1919
"appletvsimulator",
@@ -22,7 +22,7 @@ private let unusedDarwinPlatforms = [
2222
"appletvos",
2323
]
2424

25-
private let unusedHostBinaries = [
25+
let unusedHostBinaries = [
2626
"clangd",
2727
"docc",
2828
"dsymutil",
@@ -31,28 +31,42 @@ private let unusedHostBinaries = [
3131
"swift-package-collection",
3232
]
3333

34+
let unusedHostLibraries = [
35+
"sourcekitd.framework",
36+
"libsourcekitdInProc.so",
37+
]
38+
3439
extension SwiftSDKGenerator {
3540
func unpackHostSwift(hostSwiftPackagePath: FilePath) async throws {
3641
logGenerationStep("Unpacking and copying Swift binaries for the host triple...")
3742
let pathsConfiguration = self.pathsConfiguration
3843

3944
try await inTemporaryDirectory { fileSystem, tmpDir in
4045
try await fileSystem.unpack(file: hostSwiftPackagePath, into: tmpDir)
41-
// Remove libraries for platforms we don't intend cross-compiling to
42-
for platform in unusedDarwinPlatforms {
43-
try await fileSystem.removeRecursively(at: tmpDir.appending("usr/lib/swift/\(platform)"))
44-
try await fileSystem.removeRecursively(at: tmpDir.appending("usr/lib/swift_static/\(platform)"))
45-
}
46-
try await fileSystem.removeRecursively(at: tmpDir.appending("usr/lib/sourcekitd.framework"))
47-
48-
for binary in unusedHostBinaries {
49-
try await fileSystem.removeRecursively(at: tmpDir.appending("usr/bin/\(binary)"))
50-
}
51-
46+
try await self.removeToolchainComponents(tmpDir)
5247
try await fileSystem.rsync(from: tmpDir.appending("usr"), to: pathsConfiguration.toolchainDirPath)
5348
}
5449
}
5550

51+
func removeToolchainComponents(
52+
_ packagePath: FilePath,
53+
platforms: [String] = unusedDarwinPlatforms,
54+
libraries: [String] = unusedHostLibraries,
55+
binaries: [String] = unusedHostBinaries
56+
) async throws {
57+
// Remove libraries for platforms we don't intend cross-compiling to
58+
for platform in platforms {
59+
try self.removeRecursively(at: packagePath.appending("usr/lib/swift/\(platform)"))
60+
try self.removeRecursively(at: packagePath.appending("usr/lib/swift_static/\(platform)"))
61+
}
62+
for binary in binaries {
63+
try self.removeRecursively(at: packagePath.appending("usr/bin/\(binary)"))
64+
}
65+
for library in libraries {
66+
try self.removeRecursively(at: packagePath.appending("usr/lib/\(library)"))
67+
}
68+
}
69+
5670
func unpackTargetSwiftPackage(targetSwiftPackagePath: FilePath, relativePathToRoot: [FilePath.Component], sdkDirPath: FilePath) async throws {
5771
logGenerationStep("Unpacking Swift distribution for the target triple...")
5872

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ public actor SwiftSDKGenerator {
199199
return result
200200
}
201201

202+
func contentsOfDirectory(at path: FilePath) throws -> [String] {
203+
try self.fileManager.contentsOfDirectory(atPath: path.string)
204+
}
205+
202206
func copy(from source: FilePath, to destination: FilePath) throws {
203207
try self.removeRecursively(at: destination)
204208
try self.fileManager.copyItem(atPath: source.string, toPath: destination.string)

Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe {
4949
let pathsConfiguration = generator.pathsConfiguration
5050
logGenerationStep("Copying Swift binaries for the host triple...")
5151
try await generator.rsync(from: self.hostSwiftPackagePath.appending("usr"), to: pathsConfiguration.toolchainDirPath)
52+
53+
logGenerationStep("Removing unused toolchain components...")
54+
let liblldbNames: [String] = try await {
55+
let libDirPath = pathsConfiguration.toolchainDirPath.appending("usr/lib")
56+
return try await generator.contentsOfDirectory(at: libDirPath).filter { dirEntry in
57+
// liblldb is version suffixed: liblldb.so.17.0.0
58+
dirEntry.hasPrefix("liblldb")
59+
}
60+
}()
61+
try await generator.removeToolchainComponents(
62+
pathsConfiguration.toolchainDirPath,
63+
platforms: unusedDarwinPlatforms + ["linux", "embedded"],
64+
libraries: unusedHostLibraries + liblldbNames,
65+
binaries: unusedHostBinaries + ["lldb", "lldb-argdumper", "lldb-server"]
66+
)
67+
5268
try await self.copyTargetSwift(from: self.targetSwiftPackagePath.appending("usr/lib"), generator: generator)
5369

5470
let autolinkExtractPath = generator.pathsConfiguration.toolchainBinDirPath.appending("swift-autolink-extract")

0 commit comments

Comments
 (0)