Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/examples/min-platform/build.sh
3050 views
1
#!/bin/sh
2
3
# An example script to build and run the `min-platform` example by building both
4
# the embedding itself as well as the example host which will run it.
5
#
6
# This script takes a single argument which is a path to a Rust target json
7
# file. Example targets are `x86_64-unknown-none` or `aarch64-unknown-none`.
8
#
9
# This script must be executed with the current-working-directory as
10
# `examples/min-platform`.
11
12
target=$1
13
if [ "$target" = "" ]; then
14
echo "Usage: $0 <target>"
15
exit 1
16
fi
17
18
REPO_DIR=$(dirname $0)/../..
19
HOST_DIR=$REPO_DIR/examples/min-platform
20
EMBEDDING_DIR=$HOST_DIR/embedding
21
22
set -ex
23
24
if [ "$WASMTIME_SIGNALS_BASED_TRAPS" = "1" ]; then
25
cflags="$cflags -DWASMTIME_VIRTUAL_MEMORY -DWASMTIME_NATIVE_SIGNALS"
26
features="$features,custom"
27
fi
28
29
if [ "$WASMTIME_CUSTOM_SYNC" = "1" ]; then
30
cflags="$cflags -DWASMTIME_CUSTOM_SYNC"
31
features="$features,custom-sync-primitives"
32
fi
33
34
if [ "$MIN_PLATFORM_EXAMPLE_DISABLE_WASI" != "1" ]; then
35
features="$features,wasi"
36
cargo build \
37
--manifest-path=$REPO_DIR/examples/wasm/Cargo.toml \
38
--target wasm32-wasip2 \
39
--release
40
WASI_EXAMPLE_PATH=$REPO_DIR/target/wasm32-wasip2/release/wasi.wasm
41
fi
42
43
# First compile the C implementation of the platform symbols that will be
44
# required by our embedding. This is the `embedding/wasmtime-platform.c` file.
45
# The header file used is generated from Rust source code with the `cbindgen`
46
# utility which can be installed with:
47
#
48
# cargo install cbindgen
49
#
50
# which ensures that Rust & C agree on types and such.
51
cbindgen "$REPO_DIR/crates/wasmtime/src/runtime/vm/sys/custom/capi.rs" \
52
--config "$EMBEDDING_DIR/cbindgen.toml" > "$EMBEDDING_DIR/wasmtime-platform.h"
53
clang -shared -O2 -o "$HOST_DIR/libwasmtime-platform.so" "$EMBEDDING_DIR/wasmtime-platform.c" \
54
-D_GNU_SOURCE $cflags
55
56
# Next the embedding itself is built.
57
#
58
# Note that this builds the embedding as a static library, here
59
# `libembedding.a`. This embedding is then turned into a dynamic library for the
60
# host platform using `cc` afterwards. The `*-unknown-none` targets themselves
61
# don't support dynamic libraries so this is a bit of a dance to get around the
62
# fact that we're pretending this examples in't being compiled for linux.
63
cargo build \
64
--manifest-path $EMBEDDING_DIR/Cargo.toml \
65
--target $target \
66
--no-default-features \
67
--features "$features" \
68
--release
69
cc \
70
-Wl,--gc-sections \
71
-Wl,--whole-archive \
72
"$REPO_DIR/target/$target/release/libembedding.a" \
73
-Wl,--no-whole-archive \
74
-shared \
75
-o "$HOST_DIR/libembedding.so"
76
77
# The final step here is running the host, in the current directory, which will
78
# load the embedding and execute it.
79
cargo run --manifest-path "$HOST_DIR/Cargo.toml" --release --no-default-features --features "$features" -- \
80
"$target" \
81
"$HOST_DIR/libembedding.so" \
82
"$HOST_DIR/libwasmtime-platform.so" \
83
$WASI_EXAMPLE_PATH
84
85