build: download prebuilt wasm-tools

This commit is contained in:
Cayden Tisdale 2026-09-05 20:53:09 -04:00
parent a4dba0be3d
commit eddfc0c5f9
No known key found for this signature in database

View File

@ -4,6 +4,7 @@ import org.gradle.process.CommandLineArgumentProvider
import org.gradle.api.tasks.Sync import org.gradle.api.tasks.Sync
import org.gradle.jvm.tasks.Jar import org.gradle.jvm.tasks.Jar
import org.jetbrains.kotlin.gradle.targets.wasm.binaryen.BinaryenExec import org.jetbrains.kotlin.gradle.targets.wasm.binaryen.BinaryenExec
import java.net.URI
plugins { plugins {
alias(libs.plugins.kotlinMultiplatform) alias(libs.plugins.kotlinMultiplatform)
@ -28,6 +29,20 @@ val executableSuffix = if (System.getProperty("os.name").startsWith("Windows", i
val wasmToolsVersion = "1.258.0" val wasmToolsVersion = "1.258.0"
val wasmToolsDirectory = layout.projectDirectory.dir("tools/wasm-tools/$wasmToolsVersion") val wasmToolsDirectory = layout.projectDirectory.dir("tools/wasm-tools/$wasmToolsVersion")
val wasmTools = wasmToolsDirectory.file("bin/wasm-tools$executableSuffix") val wasmTools = wasmToolsDirectory.file("bin/wasm-tools$executableSuffix")
val wasmToolsTarget = when {
System.getProperty("os.name").startsWith("Mac", ignoreCase = true) &&
System.getProperty("os.arch") in setOf("aarch64", "arm64") -> "aarch64-macos"
System.getProperty("os.name").startsWith("Mac", ignoreCase = true) -> "x86_64-macos"
System.getProperty("os.name").startsWith("Windows", ignoreCase = true) &&
System.getProperty("os.arch") in setOf("aarch64", "arm64") -> "aarch64-windows"
System.getProperty("os.name").startsWith("Windows", ignoreCase = true) -> "x86_64-windows"
System.getProperty("os.arch") in setOf("aarch64", "arm64") -> "aarch64-linux"
System.getProperty("os.arch") in setOf("x86_64", "amd64") -> "x86_64-linux"
else -> error("Unsupported wasm-tools host architecture: ${System.getProperty("os.arch")}")
}
val wasmToolsArchiveExtension = if (wasmToolsTarget.endsWith("windows")) "zip" else "tar.gz"
val wasmToolsUrl = "https://github.com/bytecodealliance/wasm-tools/releases/download/v$wasmToolsVersion/" +
"wasm-tools-$wasmToolsVersion-$wasmToolsTarget.$wasmToolsArchiveExtension"
val projectWasmName = rootProject.name val projectWasmName = rootProject.name
val embeddedReleaseComponent = layout.buildDirectory.file( val embeddedReleaseComponent = layout.buildDirectory.file(
@ -35,24 +50,52 @@ val embeddedReleaseComponent = layout.buildDirectory.file(
) )
val releaseComponent = layout.buildDirectory.file("$projectWasmName.wasm") val releaseComponent = layout.buildDirectory.file("$projectWasmName.wasm")
val installWasmTools by tasks.registering(Exec::class) { val installWasmTools by tasks.registering {
group = "build setup" group = "build setup"
description = "Installs the pinned wasm-tools executable." description = "Downloads the pinned wasm-tools release executable."
inputs.property("version", wasmToolsVersion) inputs.property("version", wasmToolsVersion)
outputs.dir(wasmToolsDirectory) inputs.property("target", wasmToolsTarget)
inputs.property("url", wasmToolsUrl)
outputs.file(wasmTools)
commandLine( doLast {
providers.environmentVariable("CARGO_HOME") val installationDirectory = wasmToolsDirectory.asFile
.orElse(providers.systemProperty("user.home").map { "$it/.cargo" }) val archive = temporaryDir.resolve("wasm-tools.$wasmToolsArchiveExtension")
.map { "$it/bin/cargo$executableSuffix" } val installedExecutable = wasmTools.asFile
.get(), installationDirectory.deleteRecursively()
"install", installationDirectory.mkdirs()
"wasm-tools", installedExecutable.parentFile.mkdirs()
"--version", wasmToolsVersion,
"--locked", URI(wasmToolsUrl).toURL().openStream().use { input ->
"--root", wasmToolsDirectory.asFile.absolutePath, archive.outputStream().use { output -> input.copyTo(output) }
}
fun runCommand(vararg command: String) {
check(ProcessBuilder(*command).inheritIO().start().waitFor() == 0) {
"wasm-tools archive extraction failed"
}
}
if (wasmToolsTarget.endsWith("windows")) {
runCommand(
"powershell", "-NoProfile", "-Command",
"Expand-Archive -Force '$archive' '$installationDirectory'; " +
"Get-ChildItem -Path '$installationDirectory' -Recurse -Filter wasm-tools.exe | " +
"Select-Object -First 1 | Copy-Item -Destination '${wasmTools.asFile}'",
) )
} else {
runCommand("tar", "-xzf", archive.absolutePath, "-C", installationDirectory.absolutePath, "--strip-components=1")
}
if (!installedExecutable.isFile) {
val downloadedExecutable = installationDirectory.walkTopDown().firstOrNull {
it.isFile && it.name == "wasm-tools$executableSuffix"
}
checkNotNull(downloadedExecutable) { "wasm-tools archive did not contain wasm-tools$executableSuffix" }
downloadedExecutable.copyTo(installedExecutable, overwrite = true)
}
installedExecutable.setExecutable(true)
}
} }
val apiSourcesJar = project(":api").tasks.named<Jar>("wasmWasiSourcesJar") val apiSourcesJar = project(":api").tasks.named<Jar>("wasmWasiSourcesJar")