generated from Kotlin/kotlin-wasm-wasi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
376 lines (347 loc) · 11.6 KB
/
build.gradle.kts
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
@file:OptIn(ExperimentalWasmDsl::class)
import org.gradle.internal.os.OperatingSystem
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import java.util.*
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.undercouchDownload) apply false
}
repositories {
mavenCentral()
mavenLocal()
}
kotlin {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions{
freeCompilerArgs.addAll(listOf("-opt-in","-keep-deprecated-functions","-Xwasm-generate-wat","-Xno-exceptions","-Xno-inline","-Xno-optimize"))
}
wasmWasi {
nodejs()
binaries.executable()
}
sourceSets {
commonMain{
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.2-SNAPSHOT")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")
}
}
wasmWasiMain {
}
val wasmWasiTest by getting {
dependencies {
implementation(libs.kotlin.test)
}
}
}
}
// Uncomment following block to turn off using the Exception Handling proposal.
// Note, with this option, the compiler will generate `unreachable` instruction instead of throw,
// and a Wasm module will stop execution in this case.
//
// tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile>().configureEach {
// compilerOptions.freeCompilerArgs.addAll(listOf("-Xwasm-use-traps-instead-of-exceptions"))
// }
// Uncomment following block to force using the final version of the Exception Handling proposal.
// Note, the new opcodes are not supported yet in WAMR and Node.js
//
// tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile>().configureEach {
// compilerOptions.freeCompilerArgs.addAll(listOf("-Xwasm-use-new-exception-proposal"))
// }
enum class OsName { WINDOWS, MAC, LINUX, UNKNOWN }
enum class OsArch { X86_32, X86_64, ARM64, UNKNOWN }
data class OsType(val name: OsName, val arch: OsArch)
val currentOsType = run {
val gradleOs = OperatingSystem.current()
val osName = when {
gradleOs.isMacOsX -> OsName.MAC
gradleOs.isWindows -> OsName.WINDOWS
gradleOs.isLinux -> OsName.LINUX
else -> OsName.UNKNOWN
}
val osArch = when (providers.systemProperty("sun.arch.data.model").get()) {
"32" -> OsArch.X86_32
"64" -> when (providers.systemProperty("os.arch").get().lowercase(Locale.getDefault())) {
"aarch64" -> OsArch.ARM64
else -> OsArch.X86_64
}
else -> OsArch.UNKNOWN
}
OsType(osName, osArch)
}
//// Deno tasks
//val unzipDeno = run {
//// val denoVersion = "1.38.3"
//// val denoDirectory = "https://github.com/denoland/deno/releases/download/v$denoVersion"
//// val denoSuffix = when (currentOsType) {
//// OsType(OsName.LINUX, OsArch.X86_64) -> "x86_64-unknown-linux-gnu"
//// OsType(OsName.MAC, OsArch.X86_64) -> "x86_64-apple-darwin"
//// OsType(OsName.MAC, OsArch.ARM64) -> "aarch64-apple-darwin"
//// else -> return@run null
//// }
//// val denoLocation = "$denoDirectory/deno-$denoSuffix.zip"
////
//// val downloadedTools = File(layout.buildDirectory.asFile.get(), "tools")
////
//// val downloadDeno = tasks.register("denoDownload", Download::class) {
//// src(denoLocation)
//// dest(File(downloadedTools, "deno-$denoVersion-$denoSuffix.zip"))
//// overwrite(false)
//// }
////
//// tasks.register("denoUnzip", Copy::class) {
//// dependsOn(downloadDeno)
//// from(zipTree(downloadDeno.get().dest))
//// val unpackedDir = File(downloadedTools, "deno-$denoVersion-$denoSuffix")
//// into(unpackedDir)
//// }
//}
//fun getDenoExecutableText(wasmFileName: String): String = """
//import Context from "https://deno.land/[email protected]/wasi/snapshot_preview1.ts";
//
//const context = new Context({
// args: Deno.args,
// env: Deno.env.toObject(),
//});
//
//const binary = await Deno.readFile("./$wasmFileName");
//const module = await WebAssembly.compile(binary);
//const wasmInstance = await WebAssembly.instantiate(module, {
// "wasi_snapshot_preview1": context.exports,
//});
//
//context.initialize(wasmInstance);
//wasmInstance.exports.startUnitTests?.();
//"""
//
//fun Project.createDenoExecutableFile(
// taskName: String,
// wasmFileName: Provider<String>,
// outputDirectory: Provider<File>,
// resultFileName: String,
//): TaskProvider<Task> = tasks.register(taskName, Task::class) {
// val denoMjs = outputDirectory.map { it.resolve(resultFileName) }
// inputs.property("wasmFileName", wasmFileName)
// outputs.file(denoMjs)
//
// doFirst {
// denoMjs.get().writeText(getDenoExecutableText(wasmFileName.get()))
// }
//}
//
//fun Project.createDenoExec(
// nodeMjsFile: RegularFileProperty,
// taskName: String,
// taskGroup: String?
//): TaskProvider<Exec> {
// val denoFileName = "runUnitTestsDeno.mjs"
//
// val outputDirectory = nodeMjsFile.map { it.asFile.parentFile }
// val wasmFileName = nodeMjsFile.map { "${it.asFile.nameWithoutExtension}.wasm" }
//
// val denoFileTask = createDenoExecutableFile(
// taskName = "${taskName}CreateDenoFile",
// wasmFileName = wasmFileName,
// outputDirectory = outputDirectory,
// resultFileName = denoFileName
// )
//
// return tasks.register(taskName, Exec::class) {
// if (unzipDeno != null) {
// dependsOn(unzipDeno)
// }
// dependsOn(denoFileTask)
//
// taskGroup?.let {
// group = it
// }
//
// description = "Executes tests with Deno"
//
// val newArgs = mutableListOf<String>()
//
// executable = when (currentOsType.name) {
// OsName.WINDOWS -> "deno.exe"
// else -> "deno"
//// else -> unzipDeno?.let { File(unzipDeno.get().destinationDir, "deno").absolutePath } ?: "deno"
// }
//
// newArgs.add("run")
// newArgs.add("--allow-read")
// newArgs.add("--allow-env")
//
// newArgs.add(denoFileName)
//
// args(newArgs)
// doFirst {
// workingDir(outputDirectory)
// }
// }
//}
//
//tasks.withType<KotlinJsTest>().all {
// val denoExecTask = createDenoExec(
// inputFileProperty,
// name.replace("Node", "Deno"),
// group
// )
//
// denoExecTask.configure {
// dependsOn (
// project.provider { [email protected] }
// )
// }
//
// tasks.withType<KotlinTestReport> {
// dependsOn(denoExecTask)
// }
//}
//tasks.withType<NodeJsExec>().all {
// val denoExecTask = createDenoExec(
// inputFileProperty,
// name.replace("Node", "Deno"),
// group
// )
//
// denoExecTask.configure {
// dependsOn (
// project.provider { [email protected] }
// )
// }
//}
// WasmEdge tasks
val wasmEdgeVersion = "0.14.0"
val wasmEdgeInnerSuffix = when (currentOsType.name) {
OsName.LINUX -> "Linux"
OsName.MAC -> "Darwin"
OsName.WINDOWS -> "Windows"
else -> error("unsupported os type $currentOsType")
}
//val unzipWasmEdge = run {
// val wasmEdgeDirectory = "https://github.com/WasmEdge/WasmEdge/releases/download/$wasmEdgeVersion"
// val wasmEdgeSuffix = when (currentOsType) {
// OsType(OsName.LINUX, OsArch.X86_64) -> "manylinux_2_28_x86_64.tar.gz"
// OsType(OsName.MAC, OsArch.X86_64) -> "darwin_x86_64.tar.gz"
// OsType(OsName.MAC, OsArch.ARM64) -> "darwin_arm64.tar.gz"
// OsType(OsName.WINDOWS, OsArch.X86_32),
// OsType(OsName.WINDOWS, OsArch.X86_64) -> "windows.zip"
// else -> error("unsupported os type $currentOsType")
// }
//
// val artifactName = "WasmEdge-$wasmEdgeVersion-$wasmEdgeSuffix"
// val wasmEdgeLocation = "$wasmEdgeDirectory/$artifactName"
//
// val downloadedTools = File(layout.buildDirectory.asFile.get(), "tools")
//
// val downloadWasmEdge = tasks.register("wasmEdgeDownload", Download::class) {
// src(wasmEdgeLocation)
// dest(File(downloadedTools, artifactName))
// overwrite(false)
// }
//
// tasks.register("wasmEdgeUnzip", Copy::class) {
// dependsOn(downloadWasmEdge)
//
// val archive = downloadWasmEdge.get().dest
//
// val subfolder = "WasmEdge-$wasmEdgeVersion-$wasmEdgeInnerSuffix"
//
// from(if (archive.extension == "zip") zipTree(archive) else tarTree(archive))
//
// val currentOsTypeForConfigurationCache = currentOsType.name
//
// val unzipDirectory = downloadedTools.resolve(subfolder)
//
// into(unzipDirectory)
//
// doLast {
// if (currentOsTypeForConfigurationCache !in setOf(OsName.MAC, OsName.LINUX)) return@doLast
//
// val libDirectory = unzipDirectory.resolve(subfolder).toPath()
// .resolve(if (currentOsTypeForConfigurationCache == OsName.MAC) "lib" else "lib64")
//
// val targets = if (currentOsTypeForConfigurationCache == OsName.MAC)
// listOf("libwasmedge.0.1.0.dylib", "libwasmedge.0.1.0.tbd")
// else listOf("libwasmedge.so.0.1.0")
//
// targets.forEach {
// val target = libDirectory.resolve(it)
// val firstLink = libDirectory.resolve(it.replace("0.1.0", "0")).also(Files::deleteIfExists)
// val secondLink = libDirectory.resolve(it.replace(".0.1.0", "")).also(Files::deleteIfExists)
//
// Files.createSymbolicLink(firstLink, target)
// Files.createSymbolicLink(secondLink, target)
// }
// }
// }
//}
//fun Project.createWasmEdgeExec(
// nodeMjsFile: RegularFileProperty,
// taskName: String,
// taskGroup: String?,
// startFunction: String
//): TaskProvider<Exec> {
// val outputDirectory = nodeMjsFile.map { it.asFile.parentFile }
// val wasmFileName = nodeMjsFile.map { "${it.asFile.nameWithoutExtension}.wasm" }
//
//// return tasks.register(taskName, Exec::class) {
//// dependsOn(unzipWasmEdge)
//// inputs.property("wasmFileName", wasmFileName)
////
//// taskGroup?.let { group = it }
////
//// description = "Executes tests with WasmEdge"
////
//// val wasmEdgeDirectory = unzipWasmEdge.get().destinationDir.resolve("WasmEdge-$wasmEdgeVersion-$wasmEdgeInnerSuffix")
////
//// executable = wasmEdgeDirectory.resolve("bin/wasmedge").absolutePath
////
//// doFirst {
//// val newArgs = mutableListOf<String>()
////
//// newArgs.add("--enable-gc")
//// newArgs.add("--enable-exception-handling")
////
//// newArgs.add(wasmFileName.get())
//// newArgs.add(startFunction)
////
//// args(newArgs)
//// workingDir(outputDirectory)
//// }
//// }
//}
tasks.withType<KotlinJsTest>().all {
// val wasmEdgeRunTask = createWasmEdgeExec(
// inputFileProperty,
// name.replace("Node", "WasmEdge"),
// group,
// "startUnitTests"
// )
//
// wasmEdgeRunTask.configure {
// dependsOn (
// project.provider { [email protected] }
// )
// }
//
// tasks.withType<KotlinTestReport> {
// dependsOn(wasmEdgeRunTask)
// }
}
tasks.withType<NodeJsExec>().all {
// val wasmEdgeRunTask = createWasmEdgeExec(
// inputFileProperty,
// name.replace("Node", "WasmEdge"),
// group,
// "dummy"
// )
//
// wasmEdgeRunTask.configure {
// dependsOn (
// project.provider { [email protected] }
// )
// }
}