Path: blob/main/examples/min-platform/build.sh
3050 views
#!/bin/sh12# An example script to build and run the `min-platform` example by building both3# the embedding itself as well as the example host which will run it.4#5# This script takes a single argument which is a path to a Rust target json6# file. Example targets are `x86_64-unknown-none` or `aarch64-unknown-none`.7#8# This script must be executed with the current-working-directory as9# `examples/min-platform`.1011target=$112if [ "$target" = "" ]; then13echo "Usage: $0 <target>"14exit 115fi1617REPO_DIR=$(dirname $0)/../..18HOST_DIR=$REPO_DIR/examples/min-platform19EMBEDDING_DIR=$HOST_DIR/embedding2021set -ex2223if [ "$WASMTIME_SIGNALS_BASED_TRAPS" = "1" ]; then24cflags="$cflags -DWASMTIME_VIRTUAL_MEMORY -DWASMTIME_NATIVE_SIGNALS"25features="$features,custom"26fi2728if [ "$WASMTIME_CUSTOM_SYNC" = "1" ]; then29cflags="$cflags -DWASMTIME_CUSTOM_SYNC"30features="$features,custom-sync-primitives"31fi3233if [ "$MIN_PLATFORM_EXAMPLE_DISABLE_WASI" != "1" ]; then34features="$features,wasi"35cargo build \36--manifest-path=$REPO_DIR/examples/wasm/Cargo.toml \37--target wasm32-wasip2 \38--release39WASI_EXAMPLE_PATH=$REPO_DIR/target/wasm32-wasip2/release/wasi.wasm40fi4142# First compile the C implementation of the platform symbols that will be43# required by our embedding. This is the `embedding/wasmtime-platform.c` file.44# The header file used is generated from Rust source code with the `cbindgen`45# utility which can be installed with:46#47# cargo install cbindgen48#49# which ensures that Rust & C agree on types and such.50cbindgen "$REPO_DIR/crates/wasmtime/src/runtime/vm/sys/custom/capi.rs" \51--config "$EMBEDDING_DIR/cbindgen.toml" > "$EMBEDDING_DIR/wasmtime-platform.h"52clang -shared -O2 -o "$HOST_DIR/libwasmtime-platform.so" "$EMBEDDING_DIR/wasmtime-platform.c" \53-D_GNU_SOURCE $cflags5455# Next the embedding itself is built.56#57# Note that this builds the embedding as a static library, here58# `libembedding.a`. This embedding is then turned into a dynamic library for the59# host platform using `cc` afterwards. The `*-unknown-none` targets themselves60# don't support dynamic libraries so this is a bit of a dance to get around the61# fact that we're pretending this examples in't being compiled for linux.62cargo build \63--manifest-path $EMBEDDING_DIR/Cargo.toml \64--target $target \65--no-default-features \66--features "$features" \67--release68cc \69-Wl,--gc-sections \70-Wl,--whole-archive \71"$REPO_DIR/target/$target/release/libembedding.a" \72-Wl,--no-whole-archive \73-shared \74-o "$HOST_DIR/libembedding.so"7576# The final step here is running the host, in the current directory, which will77# load the embedding and execute it.78cargo run --manifest-path "$HOST_DIR/Cargo.toml" --release --no-default-features --features "$features" -- \79"$target" \80"$HOST_DIR/libembedding.so" \81"$HOST_DIR/libwasmtime-platform.so" \82$WASI_EXAMPLE_PATH838485