Skip to content

Commit a4977d5

Browse files
Add compiler and linker options for wasm32-wasip1-threads
1 parent 90287f1 commit a4977d5

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension SwiftSDKGenerator {
3737
}
3838

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

4343
return toolsetJSONPath

Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
104104
self.versionsConfiguration = versionsConfiguration
105105
}
106106

107-
public func applyPlatformOptions(toolset: inout Toolset) {
107+
public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) {
108108
toolset.swiftCompiler = Toolset.ToolProperties(extraCLIOptions: ["-use-ld=lld", "-Xlinker", "-R/usr/lib/swift/linux/"])
109109
toolset.cxxCompiler = Toolset.ToolProperties(extraCLIOptions: ["-lstdc++"])
110110
toolset.linker = Toolset.ToolProperties(path: "ld.lld")

Sources/SwiftSDKGenerator/SwiftSDKRecipes/SwiftSDKRecipe.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public struct SwiftSDKProduct {
2222
/// A protocol describing a set of platform specific instructions to make a Swift SDK
2323
public protocol SwiftSDKRecipe: Sendable {
2424
/// Update the given toolset with platform specific options
25-
func applyPlatformOptions(toolset: inout Toolset)
25+
func applyPlatformOptions(
26+
toolset: inout Toolset, targetTriple: Triple
27+
)
2628
func applyPlatformOptions(
2729
metadata: inout SwiftSDKMetadataV4.TripleProperties,
2830
paths: PathsConfiguration,
@@ -37,7 +39,7 @@ public protocol SwiftSDKRecipe: Sendable {
3739
}
3840

3941
extension SwiftSDKRecipe {
40-
public func applyPlatformOptions(toolset: inout Toolset) {}
42+
public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) {}
4143
public func applyPlatformOptions(
4244
metadata: inout SwiftSDKMetadataV4.TripleProperties,
4345
paths: PathsConfiguration,

Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,34 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe {
4545
"\(self.swiftVersion)_wasm"
4646
}
4747

48-
public func applyPlatformOptions(toolset: inout Toolset) {
48+
public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) {
4949
// We only support static linking for WebAssembly for now, so make it the default.
5050
toolset.swiftCompiler = Toolset.ToolProperties(extraCLIOptions: ["-static-stdlib"])
51+
if targetTriple.environmentName == "threads" {
52+
// Enable features required for threading support
53+
let ccOptions = [
54+
"-matomics", "-mbulk-memory", "-mthread-model", "posix",
55+
"-pthread", "-ftls-model=local-exec"
56+
]
57+
// Tell LLVM codegen in swiftc to enable those features via clang options
58+
toolset.swiftCompiler?.extraCLIOptions?.append(contentsOf: ccOptions.flatMap {
59+
["-Xcc", $0]
60+
})
61+
// Tell the C and C++ compilers to enable those features
62+
toolset.cCompiler = Toolset.ToolProperties(extraCLIOptions: ccOptions)
63+
toolset.cxxCompiler = Toolset.ToolProperties(extraCLIOptions: ccOptions)
64+
65+
let linkerOptions = [
66+
// Shared memory is required for WASI threads ABI
67+
// See https://github.com/WebAssembly/wasi-threads for more information.
68+
"--import-memory", "--export-memory", "--shared-memory",
69+
// Set the maximum memory size to 1GB because shared memory must specify
70+
// a maximum size. 1GB is chosen as a conservative default, but it can be
71+
// overridden by the user-provided --max-memory linker option.
72+
"--max-memory=1073741824",
73+
]
74+
toolset.linker = Toolset.ToolProperties(extraCLIOptions: linkerOptions)
75+
}
5176
}
5277

5378
public func applyPlatformOptions(

0 commit comments

Comments
 (0)