Skip to content

Commit 2501def

Browse files
committed
Add preliminary support for Embedded Swift in recipes
1 parent ed6a85b commit 2501def

File tree

7 files changed

+141
-74
lines changed

7 files changed

+141
-74
lines changed

Package.resolved

Lines changed: 59 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.8
1+
// swift-tools-version: 5.9
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Triple.Arch {
3434
}
3535

3636
extension SwiftSDKGenerator {
37-
public func run(recipe: SwiftSDKRecipe) async throws {
37+
package func run(recipe: SwiftSDKRecipe) async throws {
3838
try await withQueryEngine(OSFileSystem(), self.logger, cacheLocation: self.engineCachePath) {
3939
engine in
4040
let httpClientType: HTTPClientProtocol.Type
@@ -58,13 +58,29 @@ extension SwiftSDKGenerator {
5858

5959
let toolsetJSONPath = try await self.generateToolsetJSON(recipe: recipe)
6060

61-
try await generateDestinationJSON(
62-
toolsetPath: toolsetJSONPath,
63-
sdkDirPath: swiftSDKProduct.sdkDirPath,
64-
recipe: recipe
65-
)
61+
var artifacts = try await [
62+
self.artifactID: generateSwiftSDKMetadata(
63+
toolsetPath: toolsetJSONPath,
64+
sdkDirPath: swiftSDKProduct.sdkDirPath,
65+
recipe: recipe
66+
)
67+
]
68+
69+
if recipe.shouldSupportEmbeddedSwift {
70+
let toolsetJSONPath = try await self.generateToolsetJSON(recipe: recipe, isForEmbeddedSwift: true)
6671

67-
try await generateArtifactBundleManifest(hostTriples: swiftSDKProduct.hostTriples)
72+
artifacts["\(self.artifactID)-embedded"] = try await generateSwiftSDKMetadata(
73+
toolsetPath: toolsetJSONPath,
74+
sdkDirPath: swiftSDKProduct.sdkDirPath,
75+
recipe: recipe,
76+
isForEmbeddedSwift: true
77+
)
78+
}
79+
80+
try await generateArtifactBundleManifest(
81+
hostTriples: swiftSDKProduct.hostTriples,
82+
artifacts: artifacts
83+
)
6884

6985
// Extra spaces added for readability for the user
7086
print(

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ private let encoder: JSONEncoder = {
2121
}()
2222

2323
extension SwiftSDKGenerator {
24-
func generateToolsetJSON(recipe: SwiftSDKRecipe) throws -> FilePath {
24+
func generateToolsetJSON(recipe: SwiftSDKRecipe, isForEmbeddedSwift: Bool = false) throws -> FilePath {
2525
logger.info("Generating toolset JSON file...")
2626

27-
let toolsetJSONPath = pathsConfiguration.swiftSDKRootPath.appending("toolset.json")
27+
let toolsetJSONPath = pathsConfiguration.swiftSDKRootPath.appending("\(isForEmbeddedSwift ? "embedded-" : "")toolset.json")
2828

2929
var relativeToolchainBinDir = pathsConfiguration.toolchainBinDirPath
3030

@@ -37,18 +37,21 @@ extension SwiftSDKGenerator {
3737
}
3838

3939
var toolset = Toolset(rootPath: relativeToolchainBinDir.string)
40-
recipe.applyPlatformOptions(toolset: &toolset, targetTriple: self.targetTriple)
40+
recipe.applyPlatformOptions(toolset: &toolset, targetTriple: self.targetTriple, isForEmbeddedSwift: isForEmbeddedSwift)
4141
try writeFile(at: toolsetJSONPath, encoder.encode(toolset))
4242

4343
return toolsetJSONPath
4444
}
4545

46-
func generateDestinationJSON(toolsetPath: FilePath, sdkDirPath: FilePath, recipe: SwiftSDKRecipe)
47-
throws
48-
{
49-
logger.info("Generating destination JSON file...")
46+
func generateSwiftSDKMetadata(
47+
toolsetPath: FilePath,
48+
sdkDirPath: FilePath,
49+
recipe: SwiftSDKRecipe,
50+
isForEmbeddedSwift: Bool = false
51+
) throws -> FilePath {
52+
logger.info("Generating Swift SDK metadata JSON file...")
5053

51-
let destinationJSONPath = pathsConfiguration.swiftSDKRootPath.appending("swift-sdk.json")
54+
let destinationJSONPath = pathsConfiguration.swiftSDKRootPath.appending("\(isForEmbeddedSwift ? "embedded-" : "")swift-sdk.json")
5255

5356
var relativeToolchainBinDir = pathsConfiguration.toolchainBinDirPath
5457
var relativeSDKDir = sdkDirPath
@@ -75,7 +78,8 @@ extension SwiftSDKGenerator {
7578
recipe.applyPlatformOptions(
7679
metadata: &metadata,
7780
paths: pathsConfiguration,
78-
targetTriple: self.targetTriple
81+
targetTriple: self.targetTriple,
82+
isForEmbeddedSwift: isForEmbeddedSwift
7983
)
8084

8185
try writeFile(
@@ -88,9 +92,11 @@ extension SwiftSDKGenerator {
8892
)
8993
)
9094
)
95+
96+
return destinationJSONPath
9197
}
9298

93-
func generateArtifactBundleManifest(hostTriples: [Triple]?) throws {
99+
func generateArtifactBundleManifest(hostTriples: [Triple]?, artifacts: [String: FilePath]) throws {
94100
logger.info("Generating .artifactbundle info JSON file...")
95101

96102
let artifactBundleManifestPath = pathsConfiguration.artifactBundlePath.appending("info.json")
@@ -100,18 +106,18 @@ extension SwiftSDKGenerator {
100106
encoder.encode(
101107
ArtifactsArchiveMetadata(
102108
schemaVersion: "1.0",
103-
artifacts: [
104-
artifactID: .init(
109+
artifacts: artifacts.mapValues {
110+
.init(
105111
type: .swiftSDK,
106112
version: self.bundleVersion,
107113
variants: [
108114
.init(
109-
path: FilePath(artifactID).appending(self.targetTriple.triple).string,
115+
path: $0.string,
110116
supportedTriples: hostTriples.map { $0.map(\.triple) }
111117
)
112118
]
113119
)
114-
]
120+
}
115121
)
116122
)
117123
)

Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import Logging
1616

1717
import struct SystemPackage.FilePath
1818

19-
public struct LinuxRecipe: SwiftSDKRecipe {
20-
public enum TargetSwiftSource: Sendable {
19+
package struct LinuxRecipe: SwiftSDKRecipe {
20+
package enum TargetSwiftSource: Sendable {
2121
case docker(baseSwiftDockerImage: String)
2222
case localPackage(FilePath)
2323
case remoteTarball
2424
}
2525

26-
public enum HostSwiftSource: Sendable, Equatable {
26+
package enum HostSwiftSource: Sendable, Equatable {
2727
case localPackage(FilePath)
2828
case remoteTarball
2929
case preinstalled
@@ -35,7 +35,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
3535
let targetSwiftSource: TargetSwiftSource
3636
let hostSwiftSource: HostSwiftSource
3737
let versionsConfiguration: VersionsConfiguration
38-
public let logger: Logger
38+
package let logger: Logger
3939

4040
var shouldUseDocker: Bool {
4141
if case .docker = self.targetSwiftSource {
@@ -44,7 +44,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
4444
return false
4545
}
4646

47-
public init(
47+
package init(
4848
targetTriple: Triple,
4949
hostTriple: Triple,
5050
linuxDistribution: LinuxDistribution,
@@ -98,7 +98,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
9898
)
9999
}
100100

101-
public init(
101+
package init(
102102
mainTargetTriple: Triple,
103103
mainHostTriple: Triple,
104104
linuxDistribution: LinuxDistribution,
@@ -116,7 +116,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
116116
self.logger = logger
117117
}
118118

119-
public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) {
119+
package func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple, isForEmbeddedSwift: Bool) {
120120
if self.hostSwiftSource == .preinstalled {
121121
toolset.rootPath = nil
122122
}
@@ -146,10 +146,11 @@ public struct LinuxRecipe: SwiftSDKRecipe {
146146
toolset.librarian = Toolset.ToolProperties(path: "llvm-ar")
147147
}
148148

149-
public func applyPlatformOptions(
149+
package func applyPlatformOptions(
150150
metadata: inout SwiftSDKMetadataV4.TripleProperties,
151151
paths: PathsConfiguration,
152-
targetTriple: Triple
152+
targetTriple: Triple,
153+
isForEmbeddedSwift: Bool
153154
) {
154155
var relativeSDKDir = self.sdkDirPath(paths: paths)
155156
guard relativeSDKDir.removePrefix(paths.swiftSDKRootPath) else {
@@ -159,7 +160,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
159160
metadata.swiftStaticResourcesPath = relativeSDKDir.appending("usr/lib/swift_static").string
160161
}
161162

162-
public var defaultArtifactID: String {
163+
package var defaultArtifactID: String {
163164
"""
164165
\(self.versionsConfiguration.swiftVersion)_\(self.linuxDistribution.name.rawValue)_\(
165166
self.linuxDistribution
@@ -219,7 +220,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
219220
return [self.mainHostTriple]
220221
}
221222

222-
public func makeSwiftSDK(
223+
package func makeSwiftSDK(
223224
generator: SwiftSDKGenerator,
224225
engine: QueryEngine,
225226
httpClient client: some HTTPClientProtocol

0 commit comments

Comments
 (0)