Is it possible to compile my Kotlin to Wasm (wasi)...
# webassembly
d
Is it possible to compile my Kotlin to Wasm (wasi) using the command line instead of gradle? Id like to use a different build system but having trouble figuring out what gradle is doing. I assume ultimately there’s just a call to kotlin-native?
j
I believe it still uses the kotlin-js compiler binary with a few flags to get Wasm out, but I haven't checked in a few months.
d
Interesting, thanks for that. How did you check that out? Would the best way be to go through the Kotlin wasm gradle plugin source code?
j
I've built a few tools for debugging compiler plugins and evaluating the output of the various backends
All of that runs the compiler from the CLI
d
Oh no way, that sounds really useful. Anything open source I could check out?
j
No, it's all hacked stuff. Hoping to upstream it into Romain's Kotlin explorer tool some day
👍 1
s
I use this script sometimes. If you want to replicate gradle build, you can run gradle with
-d
flag, and fish for kotlinc-js arguments in a big debug log.
❤️ 1
And then I can do
Copy code
wasmtime -W gc=y -W function-references=y build/wasm/foo.wasm
And you may also want to optimize the .wasm file with
wasm-opt
. Gradle does this by default.
d
@Svyatoslav Kuzmich [JB] thank you so much for that, it was exactly what I was looking for. In case anyone can't find the compiler command in gradle, you have to set this gradle.property:
Copy code
kotlin.compiler.execution.strategy=in-process
Otherwise the compilation happens in a daemon and doesn't print in the logs.
s
Glad it helped! Note that these flags are an internal implementation detail of the gradle plugin and are likely to change. I’ve moved on to using a small Gradle project just to compile *.kt to .wasm, and then I run it from a master shell build script https://github.com/skuzmich/kotlin-wasm-cm-experiments/blob/d86bcf6e509a2ab8d174946484de83f818be2a4c/run.sh#L25