-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathPackageToJS.swift
418 lines (375 loc) · 16.6 KB
/
PackageToJS.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import Foundation
struct PackageToJS {
struct PackageOptions {
/// Path to the output directory
var outputPath: String?
/// Name of the package (default: lowercased Package.swift name)
var packageName: String?
/// Whether to explain the build plan
var explain: Bool = false
/// Whether to use CDN for dependency packages
var useCDN: Bool
}
struct BuildOptions {
/// Product to build (default: executable target if there's only one)
var product: String?
/// Whether to split debug information into a separate file (default: false)
var splitDebug: Bool
/// Whether to apply wasm-opt optimizations in release mode (default: true)
var noOptimize: Bool
/// The options for packaging
var packageOptions: PackageOptions
}
struct TestOptions {
/// Whether to only build tests, don't run them
var buildOnly: Bool
/// Lists all tests
var listTests: Bool
/// The filter to apply to the tests
var filter: [String]
/// The prelude script to use for the tests
var prelude: String?
/// The environment to use for the tests
var environment: String?
/// Whether to run tests in the browser with inspector enabled
var inspect: Bool
/// The options for packaging
var packageOptions: PackageOptions
}
}
struct PackageToJSError: Swift.Error, CustomStringConvertible {
let description: String
init(_ message: String) {
self.description = "Error: " + message
}
}
/// Plans the build for packaging.
struct PackagingPlanner {
/// The options for packaging
let options: PackageToJS.PackageOptions
/// The package ID of the package that this plugin is running on
let packageId: String
/// The directory of the package that contains this plugin
let selfPackageDir: URL
/// The path of this file itself, used to capture changes of planner code
let selfPath: String
/// The directory for the final output
let outputDir: URL
/// The directory for intermediate files
let intermediatesDir: URL
/// The filename of the .wasm file
let wasmFilename = "main.wasm"
/// The path to the .wasm product artifact
let wasmProductArtifact: URL
init(
options: PackageToJS.PackageOptions,
packageId: String,
pluginWorkDirectoryURL: URL,
selfPackageDir: URL,
outputDir: URL,
wasmProductArtifact: URL
) {
self.options = options
self.packageId = packageId
self.selfPackageDir = selfPackageDir
self.outputDir = outputDir
self.intermediatesDir = pluginWorkDirectoryURL.appending(path: outputDir.lastPathComponent + ".tmp")
self.selfPath = String(#filePath)
self.wasmProductArtifact = wasmProductArtifact
}
// MARK: - Primitive build operations
private static func syncFile(from: String, to: String) throws {
if FileManager.default.fileExists(atPath: to) {
try FileManager.default.removeItem(atPath: to)
}
try FileManager.default.copyItem(atPath: from, toPath: to)
try FileManager.default.setAttributes(
[.modificationDate: Date()], ofItemAtPath: to
)
}
private static func createDirectory(atPath: String) throws {
guard !FileManager.default.fileExists(atPath: atPath) else { return }
try FileManager.default.createDirectory(
atPath: atPath, withIntermediateDirectories: true, attributes: nil
)
}
private static func runCommand(_ command: URL, _ arguments: [String]) throws {
let task = Process()
task.executableURL = command
task.arguments = arguments
task.currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
try task.run()
task.waitUntilExit()
guard task.terminationStatus == 0 else {
throw PackageToJSError("Command failed with status \(task.terminationStatus)")
}
}
// MARK: - Build plans
/// Construct the build plan and return the root task key
func planBuild(
make: inout MiniMake,
buildOptions: PackageToJS.BuildOptions
) throws -> MiniMake.TaskKey {
let (allTasks, _, _) = try planBuildInternal(
make: &make, splitDebug: buildOptions.splitDebug, noOptimize: buildOptions.noOptimize
)
return make.addTask(
inputTasks: allTasks, output: "all", attributes: [.phony, .silent]
) { _ in }
}
func deriveBuildConfiguration() -> (configuration: String, triple: String) {
// e.g. path/to/.build/wasm32-unknown-wasi/debug/Basic.wasm -> ("debug", "wasm32-unknown-wasi")
// First, resolve symlink to get the actual path as SwiftPM 6.0 and earlier returns unresolved
// symlink path for product artifact.
let wasmProductArtifact = self.wasmProductArtifact.resolvingSymlinksInPath()
let buildConfiguration = wasmProductArtifact.deletingLastPathComponent().lastPathComponent
let triple = wasmProductArtifact.deletingLastPathComponent().deletingLastPathComponent().lastPathComponent
return (buildConfiguration, triple)
}
private func planBuildInternal(
make: inout MiniMake,
splitDebug: Bool, noOptimize: Bool
) throws -> (
allTasks: [MiniMake.TaskKey],
outputDirTask: MiniMake.TaskKey,
packageJsonTask: MiniMake.TaskKey
) {
// Prepare output directory
let outputDirTask = make.addTask(
inputFiles: [selfPath], output: outputDir.path, attributes: [.silent]
) {
try Self.createDirectory(atPath: $0.output)
}
var packageInputs: [MiniMake.TaskKey] = []
// Guess the build configuration from the parent directory name of .wasm file
let (buildConfiguration, _) = deriveBuildConfiguration()
let wasm: MiniMake.TaskKey
let shouldOptimize: Bool
let wasmOptPath = try? which("wasm-opt")
if buildConfiguration == "debug" {
shouldOptimize = false
} else {
if wasmOptPath != nil {
shouldOptimize = !noOptimize
} else {
print("Warning: wasm-opt not found in PATH, skipping optimizations")
shouldOptimize = false
}
}
let intermediatesDirTask = make.addTask(
inputFiles: [selfPath], output: intermediatesDir.path, attributes: [.silent]
) {
try Self.createDirectory(atPath: $0.output)
}
let finalWasmPath = outputDir.appending(path: wasmFilename).path
if let wasmOptPath = wasmOptPath, shouldOptimize {
// Optimize the wasm in release mode
// If splitDebug is true, we need to place the DWARF-stripped wasm file (but "name" section remains)
// in the output directory.
let stripWasmPath = (splitDebug ? outputDir : intermediatesDir).appending(path: wasmFilename + ".debug").path
// First, strip DWARF sections as their existence enables DWARF preserving mode in wasm-opt
let stripWasm = make.addTask(
inputFiles: [selfPath, wasmProductArtifact.path], inputTasks: [outputDirTask, intermediatesDirTask],
output: stripWasmPath
) {
print("Stripping DWARF debug info...")
try Self.runCommand(wasmOptPath, [wasmProductArtifact.path, "--strip-dwarf", "--debuginfo", "-o", $0.output])
}
// Then, run wasm-opt with all optimizations
wasm = make.addTask(
inputFiles: [selfPath, stripWasmPath], inputTasks: [outputDirTask, stripWasm],
output: finalWasmPath
) {
print("Optimizing the wasm file...")
try Self.runCommand(wasmOptPath, [stripWasmPath, "-Os", "-o", $0.output])
}
} else {
// Copy the wasm product artifact
wasm = make.addTask(
inputFiles: [selfPath, wasmProductArtifact.path], inputTasks: [outputDirTask],
output: finalWasmPath
) {
try Self.syncFile(from: wasmProductArtifact.path, to: $0.output)
}
}
packageInputs.append(wasm)
let wasmImportsPath = intermediatesDir.appending(path: "wasm-imports.json")
let wasmImportsTask = make.addTask(
inputFiles: [selfPath, finalWasmPath], inputTasks: [outputDirTask, intermediatesDirTask, wasm],
output: wasmImportsPath.path
) {
let metadata = try parseImports(moduleBytes: Array(try Data(contentsOf: URL(fileURLWithPath: finalWasmPath))))
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let jsonData = try jsonEncoder.encode(metadata)
try jsonData.write(to: URL(fileURLWithPath: $0.output))
}
packageInputs.append(wasmImportsTask)
let platformsDir = outputDir.appending(path: "platforms")
let platformsDirTask = make.addTask(
inputFiles: [selfPath], output: platformsDir.path, attributes: [.silent]
) {
try Self.createDirectory(atPath: $0.output)
}
let packageJsonTask = planCopyTemplateFile(
make: &make, file: "Plugins/PackageToJS/Templates/package.json", output: "package.json", outputDirTask: outputDirTask,
inputFiles: [], inputTasks: []
)
packageInputs.append(packageJsonTask)
// Copy the template files
for (file, output) in [
("Plugins/PackageToJS/Templates/index.js", "index.js"),
("Plugins/PackageToJS/Templates/index.d.ts", "index.d.ts"),
("Plugins/PackageToJS/Templates/instantiate.js", "instantiate.js"),
("Plugins/PackageToJS/Templates/instantiate.d.ts", "instantiate.d.ts"),
("Plugins/PackageToJS/Templates/platforms/browser.js", "platforms/browser.js"),
("Plugins/PackageToJS/Templates/platforms/browser.d.ts", "platforms/browser.d.ts"),
("Plugins/PackageToJS/Templates/platforms/browser.worker.js", "platforms/browser.worker.js"),
("Plugins/PackageToJS/Templates/platforms/node.js", "platforms/node.js"),
("Plugins/PackageToJS/Templates/platforms/node.d.ts", "platforms/node.d.ts"),
("Sources/JavaScriptKit/Runtime/index.mjs", "runtime.js"),
] {
packageInputs.append(planCopyTemplateFile(
make: &make, file: file, output: output, outputDirTask: outputDirTask,
inputFiles: [wasmImportsPath.path], inputTasks: [platformsDirTask, wasmImportsTask],
wasmImportsPath: wasmImportsPath.path
))
}
return (packageInputs, outputDirTask, packageJsonTask)
}
/// Construct the test build plan and return the root task key
func planTestBuild(
make: inout MiniMake
) throws -> (rootTask: MiniMake.TaskKey, binDir: URL) {
var (allTasks, outputDirTask, packageJsonTask) = try planBuildInternal(
make: &make, splitDebug: false, noOptimize: false
)
// Install npm dependencies used in the test harness
let npm = try which("npm")
allTasks.append(make.addTask(
inputFiles: [
selfPath,
outputDir.appending(path: "package.json").path,
], inputTasks: [outputDirTask, packageJsonTask],
output: intermediatesDir.appending(path: "npm-install.stamp").path
) {
try Self.runCommand(npm, ["-C", outputDir.path, "install"])
_ = FileManager.default.createFile(atPath: $0.output, contents: Data(), attributes: nil)
})
let binDir = outputDir.appending(path: "bin")
let binDirTask = make.addTask(
inputFiles: [selfPath], inputTasks: [outputDirTask],
output: binDir.path
) {
try Self.createDirectory(atPath: $0.output)
}
allTasks.append(binDirTask)
// Copy the template files
for (file, output) in [
("Plugins/PackageToJS/Templates/test.js", "test.js"),
("Plugins/PackageToJS/Templates/test.d.ts", "test.d.ts"),
("Plugins/PackageToJS/Templates/test.browser.html", "test.browser.html"),
("Plugins/PackageToJS/Templates/bin/test.js", "bin/test.js"),
] {
allTasks.append(planCopyTemplateFile(
make: &make, file: file, output: output, outputDirTask: outputDirTask,
inputFiles: [], inputTasks: [binDirTask]
))
}
let rootTask = make.addTask(
inputTasks: allTasks, output: "all", attributes: [.phony, .silent]
) { _ in }
return (rootTask, binDir)
}
private func planCopyTemplateFile(
make: inout MiniMake,
file: String,
output: String,
outputDirTask: MiniMake.TaskKey,
inputFiles: [String],
inputTasks: [MiniMake.TaskKey],
wasmImportsPath: String? = nil
) -> MiniMake.TaskKey {
struct Salt: Encodable {
let conditions: [String: Bool]
let substitutions: [String: String]
}
let inputPath = selfPackageDir.appending(path: file)
let (_, triple) = deriveBuildConfiguration()
let conditions = [
"USE_SHARED_MEMORY": triple == "wasm32-unknown-wasip1-threads",
"IS_WASI": triple.hasPrefix("wasm32-unknown-wasi"),
"USE_WASI_CDN": options.useCDN,
]
let constantSubstitutions = [
"PACKAGE_TO_JS_MODULE_PATH": wasmFilename,
"PACKAGE_TO_JS_PACKAGE_NAME": options.packageName ?? packageId.lowercased(),
]
let salt = Salt(conditions: conditions, substitutions: constantSubstitutions)
return make.addTask(
inputFiles: [selfPath, inputPath.path] + inputFiles, inputTasks: [outputDirTask] + inputTasks,
output: outputDir.appending(path: output).path, salt: salt
) {
var substitutions = constantSubstitutions
if let wasmImportsPath = wasmImportsPath {
let importEntries = try JSONDecoder().decode([ImportEntry].self, from: Data(contentsOf: URL(fileURLWithPath: wasmImportsPath)))
let memoryImport = importEntries.first { $0.module == "env" && $0.name == "memory" }
if case .memory(let type) = memoryImport?.kind {
substitutions["PACKAGE_TO_JS_MEMORY_INITIAL"] = "\(type.minimum)"
substitutions["PACKAGE_TO_JS_MEMORY_MAXIMUM"] = "\(type.maximum ?? type.minimum)"
substitutions["PACKAGE_TO_JS_MEMORY_SHARED"] = "\(type.shared)"
}
}
var content = try String(contentsOf: inputPath, encoding: .utf8)
let options = PreprocessOptions(conditions: conditions, substitutions: substitutions)
content = try preprocess(source: content, file: file, options: options)
try content.write(toFile: $0.output, atomically: true, encoding: .utf8)
}
}
}
// MARK: - Utilities
func which(_ executable: String) throws -> URL {
let pathSeparator: Character
#if os(Windows)
pathSeparator = ";"
#else
pathSeparator = ":"
#endif
let paths = ProcessInfo.processInfo.environment["PATH"]!.split(separator: pathSeparator)
for path in paths {
let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable)
if FileManager.default.isExecutableFile(atPath: url.path) {
return url
}
}
throw PackageToJSError("Executable \(executable) not found in PATH")
}
func logCommandExecution(_ command: String, _ arguments: [String]) {
var fullArguments = [command]
fullArguments.append(contentsOf: arguments)
print("$ \(fullArguments.map { "\"\($0)\"" }.joined(separator: " "))")
}
extension Foundation.Process {
// Monitor termination/interrruption signals to forward them to child process
func setSignalForwarding(_ signalNo: Int32) -> DispatchSourceSignal {
let signalSource = DispatchSource.makeSignalSource(signal: signalNo)
signalSource.setEventHandler { [self] in
signalSource.cancel()
kill(processIdentifier, signalNo)
}
signalSource.resume()
return signalSource
}
func forwardTerminationSignals(_ body: () throws -> Void) rethrows {
let sources = [
setSignalForwarding(SIGINT),
setSignalForwarding(SIGTERM),
]
defer {
for source in sources {
source.cancel()
}
}
try body()
}
}