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