|
| 1 | +# Reproducible build of the kotlin-richards benchmark. |
| 2 | +# |
| 3 | +# Stage 1 compiles the Kotlin sources to an optimized WasmGC core module via the |
| 4 | +# Kotlin/Wasm `wasmWasi` target. Stage 2 turns that core module into a WASI |
| 5 | +# *component* with wasm-tools and the WASI preview1 command adapter. |
| 6 | + |
| 7 | +# --------------------------------------------------------------------------- |
| 8 | +# Stage 1: Kotlin -> optimized WasmGC core module. |
| 9 | +# --------------------------------------------------------------------------- |
| 10 | +FROM gradle:8.13-jdk21 AS kotlin-build |
| 11 | +WORKDIR /src |
| 12 | + |
| 13 | +# Warm the Kotlin/Native + Binaryen toolchain and dependency caches in a layer |
| 14 | +# that does not depend on our sources, so editing the Kotlin only re-runs the |
| 15 | +# (fast) compile below. |
| 16 | +COPY settings.gradle.kts gradle.properties build.gradle.kts ./ |
| 17 | +RUN mkdir -p src/wasmWasiMain/kotlin \ |
| 18 | + && printf 'fun main() {}\n' > src/wasmWasiMain/kotlin/Stub.kt \ |
| 19 | + && gradle --no-daemon compileProductionExecutableKotlinWasmWasiOptimize \ |
| 20 | + && rm -rf src |
| 21 | + |
| 22 | +# Compile the real sources. Produces, with Binaryen already applied: |
| 23 | +# build/compileSync/wasmWasi/main/productionExecutable/optimized/kotlin-richards.wasm |
| 24 | +COPY src ./src |
| 25 | +RUN gradle --no-daemon compileProductionExecutableKotlinWasmWasiOptimize |
| 26 | + |
| 27 | +# --------------------------------------------------------------------------- |
| 28 | +# Stage 2: core module -> WASI component. |
| 29 | +# --------------------------------------------------------------------------- |
| 30 | +FROM debian:bookworm-slim AS component-build |
| 31 | + |
| 32 | +# Pinned tool versions. The adapter targets WASI 0.2.6 (matches sightglass). |
| 33 | +ARG WASM_TOOLS_VERSION=1.247.0 |
| 34 | +ARG ADAPTER_WASMTIME_VERSION=37.0.0 |
| 35 | +WORKDIR /work |
| 36 | + |
| 37 | +RUN set -eux; \ |
| 38 | + apt-get update; \ |
| 39 | + apt-get install -y --no-install-recommends curl xz-utils ca-certificates; \ |
| 40 | + rm -rf /var/lib/apt/lists/*; \ |
| 41 | + case "$(uname -m)" in \ |
| 42 | + x86_64) WT_ARCH=x86_64-linux ;; \ |
| 43 | + aarch64|arm64) WT_ARCH=aarch64-linux ;; \ |
| 44 | + *) echo "unsupported arch: $(uname -m)" >&2; exit 1 ;; \ |
| 45 | + esac; \ |
| 46 | + curl -sSL -o wasm-tools.tar.gz \ |
| 47 | + "https://github.com/bytecodealliance/wasm-tools/releases/download/v${WASM_TOOLS_VERSION}/wasm-tools-${WASM_TOOLS_VERSION}-${WT_ARCH}.tar.gz"; \ |
| 48 | + tar -xzf wasm-tools.tar.gz; \ |
| 49 | + cp "wasm-tools-${WASM_TOOLS_VERSION}-${WT_ARCH}/wasm-tools" /usr/local/bin/; \ |
| 50 | + wasm-tools --version; \ |
| 51 | + curl -sSL -o wasi_snapshot_preview1.command.wasm \ |
| 52 | + "https://github.com/bytecodealliance/wasmtime/releases/download/v${ADAPTER_WASMTIME_VERSION}/wasi_snapshot_preview1.command.wasm" |
| 53 | + |
| 54 | +COPY --from=kotlin-build \ |
| 55 | + /src/build/compileSync/wasmWasi/main/productionExecutable/optimized/kotlin-richards.wasm \ |
| 56 | + core.wasm |
| 57 | +COPY wit ./wit |
| 58 | + |
| 59 | +# The Kotlin WASI executable exports `_initialize` (a WASI "reactor" entry that |
| 60 | +# runs `fun main()`). Rename that export to `_start` so the preview1 *command* |
| 61 | +# adapter binds it to `wasi:cli/run`'s `run`, which is how sightglass invokes a |
| 62 | +# component benchmark. Then embed the world describing the `bench` import and |
| 63 | +# create the component (the adapter supplies the WASI imports + `run` export). |
| 64 | +RUN set -eux; \ |
| 65 | + wasm-tools print core.wasm \ |
| 66 | + | sed -E 's/\(export "_initialize" \(func /(export "_start" (func /' \ |
| 67 | + | wasm-tools parse -o command.wasm; \ |
| 68 | + wasm-tools component embed wit/bench.wit --world richards command.wasm -o embedded.wasm; \ |
| 69 | + wasm-tools component new embedded.wasm \ |
| 70 | + --adapt wasi_snapshot_preview1=wasi_snapshot_preview1.command.wasm \ |
| 71 | + -o kotlin-richards.wasm; \ |
| 72 | + wasm-tools validate --features all kotlin-richards.wasm; \ |
| 73 | + mkdir -p /benchmark; \ |
| 74 | + cp kotlin-richards.wasm /benchmark/ |
| 75 | +# We output the Wasm file to the `/benchmark` directory, where the client |
| 76 | +# expects it. |
0 commit comments