@@ -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 }
0 commit comments