Skip to content

Commit 71dd0fa

Browse files
WebAssembly: Specify resource directory explicitly (#85)
* WebAssembly: Specify resource directory explicitly When compiler is vendored in the SDK, the resource directory is inferred from the location of the compiler executable. This is not the case when the compiler is not vendored, so we need to specify the resource directory explicitly to work with both cases. * Apply suggestions from code review Co-authored-by: Max Desiatov <[email protected]> --------- Co-authored-by: Max Desiatov <[email protected]>
1 parent 37e0dad commit 71dd0fa

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extension SwiftSDKGenerator {
6060

6161
let toolsetJSONPath = try await generateToolsetJSON(recipe: recipe)
6262

63-
try await generateDestinationJSON(toolsetPath: toolsetJSONPath, sdkDirPath: swiftSDKProduct.sdkDirPath)
63+
try await generateDestinationJSON(toolsetPath: toolsetJSONPath, sdkDirPath: swiftSDKProduct.sdkDirPath, recipe: recipe)
6464

6565
try await generateArtifactBundleManifest()
6666

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension SwiftSDKGenerator {
4343
return toolsetJSONPath
4444
}
4545

46-
func generateDestinationJSON(toolsetPath: FilePath, sdkDirPath: FilePath) throws {
46+
func generateDestinationJSON(toolsetPath: FilePath, sdkDirPath: FilePath, recipe: SwiftSDKRecipe) throws {
4747
logGenerationStep("Generating destination JSON file...")
4848

4949
let destinationJSONPath = pathsConfiguration.swiftSDKRootPath.appending("swift-sdk.json")
@@ -63,15 +63,23 @@ extension SwiftSDKGenerator {
6363
""")
6464
}
6565

66+
var metadata = SwiftSDKMetadataV4.TripleProperties(
67+
sdkRootPath: relativeSDKDir.string,
68+
toolsetPaths: [relativeToolsetPath.string]
69+
)
70+
71+
recipe.applyPlatformOptions(
72+
metadata: &metadata,
73+
paths: pathsConfiguration,
74+
targetTriple: self.targetTriple
75+
)
76+
6677
try writeFile(
6778
at: destinationJSONPath,
6879
encoder.encode(
6980
SwiftSDKMetadataV4(
7081
targetTriples: [
71-
self.targetTriple.linuxConventionDescription: .init(
72-
sdkRootPath: relativeSDKDir.string,
73-
toolsetPaths: [relativeToolsetPath.string]
74-
),
82+
self.targetTriple.linuxConventionDescription: metadata,
7583
]
7684
)
7785
)

Sources/SwiftSDKGenerator/Serialization/SwiftSDKMetadata.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ struct DestinationV3: Encodable {
7373
}
7474

7575
/// Represents v4 schema of `swift-sdk.json` (previously `destination.json`) files used for cross-compilation.
76-
struct SwiftSDKMetadataV4: Encodable {
77-
struct TripleProperties: Encodable {
76+
public struct SwiftSDKMetadataV4: Encodable {
77+
public struct TripleProperties: Encodable {
7878
/// Path relative to `swift-sdk.json` containing SDK root.
7979
var sdkRootPath: String
8080

Sources/SwiftSDKGenerator/SwiftSDKRecipes/SwiftSDKRecipe.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public struct SwiftSDKProduct {
2222
public protocol SwiftSDKRecipe: Sendable {
2323
/// Update the given toolset with platform specific options
2424
func applyPlatformOptions(toolset: inout Toolset)
25+
func applyPlatformOptions(
26+
metadata: inout SwiftSDKMetadataV4.TripleProperties,
27+
paths: PathsConfiguration,
28+
targetTriple: Triple
29+
)
2530

2631
/// The default identifier of the Swift SDK
2732
var defaultArtifactID: String { get }
@@ -32,4 +37,9 @@ public protocol SwiftSDKRecipe: Sendable {
3237

3338
extension SwiftSDKRecipe {
3439
public func applyPlatformOptions(toolset: inout Toolset) {}
40+
public func applyPlatformOptions(
41+
metadata: inout SwiftSDKMetadataV4.TripleProperties,
42+
paths: PathsConfiguration,
43+
targetTriple: Triple
44+
) {}
3545
}

Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe {
4141
toolset.swiftCompiler = Toolset.ToolProperties(extraCLIOptions: ["-static-stdlib"])
4242
}
4343

44+
public func applyPlatformOptions(
45+
metadata: inout SwiftSDKMetadataV4.TripleProperties,
46+
paths: PathsConfiguration,
47+
targetTriple: Triple
48+
) {
49+
var relativeToolchainDir = paths.toolchainDirPath
50+
guard relativeToolchainDir.removePrefix(paths.swiftSDKRootPath) else {
51+
fatalError("The toolchain bin directory path must be a subdirectory of the Swift SDK root path.")
52+
}
53+
metadata.swiftStaticResourcesPath = relativeToolchainDir.appending("usr/lib/swift_static").string
54+
metadata.swiftResourcesPath = metadata.swiftStaticResourcesPath
55+
}
56+
4457
public func makeSwiftSDK(
4558
generator: SwiftSDKGenerator,
4659
engine: Engine,

0 commit comments

Comments
 (0)