Skip to content

Commit 16ea580

Browse files
Add USE_SHARED_MEMORY condition
1 parent 3cb6a5d commit 16ea580

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

Plugins/PackageToJS/Sources/PackageToJS.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,24 @@ struct PackagingPlanner {
5555
let intermediatesDir: URL
5656
/// The filename of the .wasm file
5757
let wasmFilename = "main.wasm"
58+
/// The path to the .wasm product artifact
59+
let wasmProductArtifact: URL
5860

5961
init(
6062
options: PackageToJS.PackageOptions,
6163
packageId: String,
6264
pluginWorkDirectoryURL: URL,
6365
selfPackageDir: URL,
64-
outputDir: URL
66+
outputDir: URL,
67+
wasmProductArtifact: URL
6568
) {
6669
self.options = options
6770
self.packageId = packageId
6871
self.selfPackageDir = selfPackageDir
6972
self.outputDir = outputDir
7073
self.intermediatesDir = pluginWorkDirectoryURL.appending(path: outputDir.lastPathComponent + ".tmp")
7174
self.selfPath = String(#filePath)
75+
self.wasmProductArtifact = wasmProductArtifact
7276
}
7377

7478
// MARK: - Primitive build operations
@@ -107,21 +111,26 @@ struct PackagingPlanner {
107111
/// Construct the build plan and return the root task key
108112
func planBuild(
109113
make: inout MiniMake,
110-
splitDebug: Bool,
111-
wasmProductArtifact: URL
114+
splitDebug: Bool
112115
) throws -> MiniMake.TaskKey {
113116
let (allTasks, _) = try planBuildInternal(
114-
make: &make, splitDebug: splitDebug, wasmProductArtifact: wasmProductArtifact
117+
make: &make, splitDebug: splitDebug
115118
)
116119
return make.addTask(
117120
inputTasks: allTasks, output: "all", attributes: [.phony, .silent]
118121
) { _ in }
119122
}
120123

124+
func deriveBuildConfiguration() -> (configuration: String, triple: String) {
125+
// e.g. path/to/.build/wasm32-unknown-wasi/debug/Basic.wasm -> ("debug", "wasm32-unknown-wasi")
126+
let buildConfiguration = wasmProductArtifact.deletingLastPathComponent().lastPathComponent
127+
let triple = wasmProductArtifact.deletingLastPathComponent().deletingLastPathComponent().lastPathComponent
128+
return (buildConfiguration, triple)
129+
}
130+
121131
private func planBuildInternal(
122132
make: inout MiniMake,
123-
splitDebug: Bool,
124-
wasmProductArtifact: URL
133+
splitDebug: Bool
125134
) throws -> (allTasks: [MiniMake.TaskKey], outputDirTask: MiniMake.TaskKey) {
126135
// Prepare output directory
127136
let outputDirTask = make.addTask(
@@ -133,7 +142,7 @@ struct PackagingPlanner {
133142
var packageInputs: [MiniMake.TaskKey] = []
134143

135144
// Guess the build configuration from the parent directory name of .wasm file
136-
let buildConfiguration = wasmProductArtifact.deletingLastPathComponent().lastPathComponent
145+
let (buildConfiguration, triple) = deriveBuildConfiguration()
137146
let wasm: MiniMake.TaskKey
138147

139148
let shouldOptimize: Bool
@@ -229,10 +238,9 @@ struct PackagingPlanner {
229238
/// Construct the test build plan and return the root task key
230239
func planTestBuild(
231240
make: inout MiniMake,
232-
wasmProductArtifact: URL
233241
) throws -> (rootTask: MiniMake.TaskKey, binDir: URL) {
234242
var (allTasks, outputDirTask) = try planBuildInternal(
235-
make: &make, splitDebug: false, wasmProductArtifact: wasmProductArtifact
243+
make: &make, splitDebug: false
236244
)
237245

238246
let binDir = outputDir.appending(path: "bin")
@@ -272,12 +280,16 @@ struct PackagingPlanner {
272280
let substitutions = [
273281
"@PACKAGE_TO_JS_MODULE_PATH@": wasmFilename
274282
]
283+
let (buildConfiguration, triple) = deriveBuildConfiguration()
284+
let conditions = [
285+
"USE_SHARED_MEMORY": triple == "wasm32-unknown-wasip1-threads"
286+
]
275287
return make.addTask(
276288
inputFiles: [selfPath, inputPath.path], inputTasks: [outputDirTask] + inputs,
277289
output: outputDir.appending(path: output).path
278290
) {
279291
var content = try String(contentsOf: inputPath, encoding: .utf8)
280-
let options = PreprocessOptions(substitutions: substitutions)
292+
let options = PreprocessOptions(conditions: conditions, substitutions: substitutions)
281293
content = try preprocess(source: content, file: file, options: options)
282294
try content.write(toFile: $0.output, atomically: true, encoding: .utf8)
283295
}

Plugins/PackageToJS/Sources/PackageToJSPlugin.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ struct PackageToJSPlugin: CommandPlugin {
116116
)
117117
let planner = PackagingPlanner(
118118
options: buildOptions.packageOptions, context: context, selfPackage: selfPackage,
119-
outputDir: outputDir)
119+
outputDir: outputDir, wasmProductArtifact: productArtifact)
120120
let rootTask = try planner.planBuild(
121-
make: &make, splitDebug: buildOptions.splitDebug, wasmProductArtifact: productArtifact)
121+
make: &make, splitDebug: buildOptions.splitDebug)
122122
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
123123
print("Packaging...")
124124
try make.build(output: rootTask)
@@ -186,9 +186,9 @@ struct PackageToJSPlugin: CommandPlugin {
186186
)
187187
let planner = PackagingPlanner(
188188
options: testOptions.packageOptions, context: context, selfPackage: selfPackage,
189-
outputDir: outputDir)
189+
outputDir: outputDir, wasmProductArtifact: productArtifact)
190190
let (rootTask, binDir) = try planner.planTestBuild(
191-
make: &make, wasmProductArtifact: productArtifact)
191+
make: &make)
192192
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
193193
print("Packaging tests...")
194194
try make.build(output: rootTask)
@@ -424,14 +424,16 @@ extension PackagingPlanner {
424424
options: PackageToJS.PackageOptions,
425425
context: PluginContext,
426426
selfPackage: Package,
427-
outputDir: URL
427+
outputDir: URL,
428+
wasmProductArtifact: URL
428429
) {
429430
self.init(
430431
options: options,
431432
packageId: context.package.id,
432433
pluginWorkDirectoryURL: context.pluginWorkDirectoryURL,
433434
selfPackageDir: selfPackage.directoryURL,
434-
outputDir: outputDir
435+
outputDir: outputDir,
436+
wasmProductArtifact: wasmProductArtifact
435437
)
436438
}
437439
}

Plugins/PackageToJS/Sources/Preprocess.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func preprocess(source: String, file: String?, options: PreprocessOptions) throw
2424
}
2525

2626
struct PreprocessOptions {
27-
/// The variables to replace in the source code
28-
var variables: [String: Bool] = [:]
27+
/// The conditions to evaluate in the source code
28+
var conditions: [String: Bool] = [:]
2929
/// The variables to substitute in the source code
3030
var substitutions: [String: String] = [:]
3131
}
@@ -341,7 +341,7 @@ private struct Preprocessor {
341341
case .block(let content):
342342
appendBlock(content: content)
343343
case .if(let condition, let then, let `else`, let position):
344-
guard let condition = options.variables[condition] else {
344+
guard let condition = options.conditions[condition] else {
345345
throw undefinedVariableError(name: condition, at: position)
346346
}
347347
let blocks = condition ? then : `else`

0 commit comments

Comments
 (0)