@@ -55,20 +55,24 @@ struct PackagingPlanner {
55
55
let intermediatesDir : URL
56
56
/// The filename of the .wasm file
57
57
let wasmFilename = " main.wasm "
58
+ /// The path to the .wasm product artifact
59
+ let wasmProductArtifact : URL
58
60
59
61
init (
60
62
options: PackageToJS . PackageOptions ,
61
63
packageId: String ,
62
64
pluginWorkDirectoryURL: URL ,
63
65
selfPackageDir: URL ,
64
- outputDir: URL
66
+ outputDir: URL ,
67
+ wasmProductArtifact: URL
65
68
) {
66
69
self . options = options
67
70
self . packageId = packageId
68
71
self . selfPackageDir = selfPackageDir
69
72
self . outputDir = outputDir
70
73
self . intermediatesDir = pluginWorkDirectoryURL. appending ( path: outputDir. lastPathComponent + " .tmp " )
71
74
self . selfPath = String ( #filePath)
75
+ self . wasmProductArtifact = wasmProductArtifact
72
76
}
73
77
74
78
// MARK: - Primitive build operations
@@ -107,21 +111,26 @@ struct PackagingPlanner {
107
111
/// Construct the build plan and return the root task key
108
112
func planBuild(
109
113
make: inout MiniMake ,
110
- splitDebug: Bool ,
111
- wasmProductArtifact: URL
114
+ splitDebug: Bool
112
115
) throws -> MiniMake . TaskKey {
113
116
let ( allTasks, _) = try planBuildInternal (
114
- make: & make, splitDebug: splitDebug, wasmProductArtifact : wasmProductArtifact
117
+ make: & make, splitDebug: splitDebug
115
118
)
116
119
return make. addTask (
117
120
inputTasks: allTasks, output: " all " , attributes: [ . phony, . silent]
118
121
) { _ in }
119
122
}
120
123
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
+
121
131
private func planBuildInternal(
122
132
make: inout MiniMake ,
123
- splitDebug: Bool ,
124
- wasmProductArtifact: URL
133
+ splitDebug: Bool
125
134
) throws -> ( allTasks: [ MiniMake . TaskKey ] , outputDirTask: MiniMake . TaskKey ) {
126
135
// Prepare output directory
127
136
let outputDirTask = make. addTask (
@@ -133,7 +142,7 @@ struct PackagingPlanner {
133
142
var packageInputs : [ MiniMake . TaskKey ] = [ ]
134
143
135
144
// Guess the build configuration from the parent directory name of .wasm file
136
- let buildConfiguration = wasmProductArtifact . deletingLastPathComponent ( ) . lastPathComponent
145
+ let ( buildConfiguration, triple ) = deriveBuildConfiguration ( )
137
146
let wasm : MiniMake . TaskKey
138
147
139
148
let shouldOptimize : Bool
@@ -229,10 +238,9 @@ struct PackagingPlanner {
229
238
/// Construct the test build plan and return the root task key
230
239
func planTestBuild(
231
240
make: inout MiniMake ,
232
- wasmProductArtifact: URL
233
241
) throws -> ( rootTask: MiniMake . TaskKey , binDir: URL ) {
234
242
var ( allTasks, outputDirTask) = try planBuildInternal (
235
- make: & make, splitDebug: false , wasmProductArtifact : wasmProductArtifact
243
+ make: & make, splitDebug: false
236
244
)
237
245
238
246
let binDir = outputDir. appending ( path: " bin " )
@@ -272,12 +280,16 @@ struct PackagingPlanner {
272
280
let substitutions = [
273
281
" @PACKAGE_TO_JS_MODULE_PATH@ " : wasmFilename
274
282
]
283
+ let ( buildConfiguration, triple) = deriveBuildConfiguration ( )
284
+ let conditions = [
285
+ " USE_SHARED_MEMORY " : triple == " wasm32-unknown-wasip1-threads "
286
+ ]
275
287
return make. addTask (
276
288
inputFiles: [ selfPath, inputPath. path] , inputTasks: [ outputDirTask] + inputs,
277
289
output: outputDir. appending ( path: output) . path
278
290
) {
279
291
var content = try String ( contentsOf: inputPath, encoding: . utf8)
280
- let options = PreprocessOptions ( substitutions: substitutions)
292
+ let options = PreprocessOptions ( conditions : conditions , substitutions: substitutions)
281
293
content = try preprocess ( source: content, file: file, options: options)
282
294
try content. write ( toFile: $0. output, atomically: true , encoding: . utf8)
283
295
}
0 commit comments