Path: blob/main/examples/min-platform/build.sh
1690 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 [ "$MIN_PLATFORM_EXAMPLE_DISABLE_WASI" != "1" ]; then29features="$features,wasi"30cargo build \31--manifest-path=$REPO_DIR/examples/wasm/Cargo.toml \32--target wasm32-wasip2 \33--release34WASI_EXAMPLE_PATH=$REPO_DIR/target/wasm32-wasip2/release/wasi.wasm35fi3637# First compile the C implementation of the platform symbols that will be38# required by our embedding. This is the `embedding/wasmtime-platform.c` file.39# The header file used is generated from Rust source code with the `cbindgen`40# utility which can be installed with:41#42# cargo install cbindgen43#44# which ensures that Rust & C agree on types and such.45cbindgen "$REPO_DIR/crates/wasmtime/src/runtime/vm/sys/custom/capi.rs" \46--config "$EMBEDDING_DIR/cbindgen.toml" > "$EMBEDDING_DIR/wasmtime-platform.h"47clang -shared -O2 -o "$HOST_DIR/libwasmtime-platform.so" "$EMBEDDING_DIR/wasmtime-platform.c" \48-D_GNU_SOURCE $cflags4950# Next the embedding itself is built.51#52# Note that this builds the embedding as a static library, here53# `libembedding.a`. This embedding is then turned into a dynamic library for the54# host platform using `cc` afterwards. The `*-unknown-none` targets themselves55# don't support dynamic libraries so this is a bit of a dance to get around the56# fact that we're pretending this examples in't being compiled for linux.57cargo build \58--manifest-path $EMBEDDING_DIR/Cargo.toml \59--target $target \60--no-default-features \61--features "$features" \62--release63cc \64-Wl,--gc-sections \65-Wl,--whole-archive \66"$REPO_DIR/target/$target/release/libembedding.a" \67-Wl,--no-whole-archive \68-shared \69-o "$HOST_DIR/libembedding.so"7071# The final step here is running the host, in the current directory, which will72# load the embedding and execute it.73cargo run --manifest-path "$HOST_DIR/Cargo.toml" --release --no-default-features --features "$features" -- \74"$target" \75"$HOST_DIR/libembedding.so" \76"$HOST_DIR/libwasmtime-platform.so" \77$WASI_EXAMPLE_PATH787980