Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions test/wasm-components-test-project/2_build_motoko_component.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
source "$(dirname "$0")/lib/set_env.sh"

ROOT_DIR=$(dirname "$(realpath $0)")
cd $ROOT_DIR || exit

echo --- Running embed... &&
wasm-tools component embed target/motoko.wit target/motoko.wasm -o target/motoko-embed.wasm &&
echo --- Creating Motoko component... &&
wasm-tools component new target/motoko-embed.wasm -v -o target/motoko-component.wasm --adapt wasi_snapshot_preview1=lib/wasi-adapter.wasm &&
echo --- Composing components... &&
wac compose target/motoko.wac -d motoko:component=target/motoko-component.wasm --deps-dir $MOPS_DIR -o target/motoko-composed.wasm &&
echo --- Composing done, output written to target/motoko-composed.wasm
MO_SRC_DIR="src/motoko"
COMPONENT_DEPS=""
for folder in $ROOT_DIR/$MOPS_DIR/component/*@*; do
package_with_version=$(basename -- "$folder")
package="${package_with_version%@*}"
component_name=$(echo "$package" | tr '_' '-')
COMPONENT_DEPS="$COMPONENT_DEPS -d component:$component_name=$ROOT_DIR/$MOPS_DIR/component/$package_with_version/$package.wasm "
done

cd $ROOT_DIR || exit
echo --- Running mo2wc... &&
MO2WC=./src/rust/mo2wc/target/release/mo2wc
RUST_LOG=info $MO2WC compose --wac-file target/motoko.wac --wasm-module-file target/motoko.wasm --wit-file target/motoko.wit \
Copy link
Contributor

@rvanasa rvanasa Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it could make sense to include a more concise shorthand:

mo2wc compose target/motoko

...equivalent to the flags in the current implementation...

mo2wc compose --wac-file target/motoko.wac --wasm-module-file target/motoko.wasm --wit-file target/motoko.wit

$COMPONENT_DEPS \
-o target/out.wasm
wasmtime run target/out.wasm
27 changes: 6 additions & 21 deletions test/wasm-components-test-project/lib/build_rust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,22 @@ source "$(dirname "$0")/set_env.sh"

ROOT_DIR=$(dirname "$(realpath $0)")/..
export RUST_DIR=$ROOT_DIR/src/rust
export COMPONENT_DIR=$ROOT_DIR/$MOPS_DIR/component
export MOPS_COMPONENTS_DIR=$ROOT_DIR/$MOPS_DIR/component
echo "NOTE: This script may fail when run in Motoko's development nix-shell, which uses an old version of cargo/rustc."
echo " If this happens, run it in a separate shell with your regular Rust development setup."

build_rust_component() {
component_name=$1
# Rust-conventions:
# * WASMs generated by cargo build use underscores in their names
# WIT/WAC-conventions:
# * the component name must use hyphens as separators (not underscores)
# * imported components must use naming `package_name:module-name`
# * importing `component:foo-bar` expects binary `foo-bar.wasm` in folder `component`
# * if folder `component` contains both `foo-bar.wasm` and a subfolder `foo-bar`, WAC gets confused...
# Motoko-conventions:
# * importing `foo-bar` expects a file lib.mo in folder `foo-bar` (i.e. foo-bar.mo would not work)
#
# Hence, the layout in mops:
# - all components are in subfolder mops/component/
# - component `foo-bar` contributes the following:
# * a subfolder mops/component/foo_bar (with underscore), containing file lib.mo
# * a WASM file: mops/component/foo-bar.wasm
wac_compatible_component_name=$(echo "$component_name" | tr '_' '-')

echo --- Building Rust component $component_name to target/${component_name}.wasm
cd $RUST_DIR/$component_name || exit
cargo build --$BUILD_MODE --target $TARGET &&
wasm-tools component new target/$TARGET/$BUILD_MODE/${component_name}.wasm -o target/${component_name}.wasm &&
echo ... Copying to mops-dir $COMPONENT_DIR/${component_name}@0.0.1
mkdir -pv "$COMPONENT_DIR/[email protected]"
cp target/${component_name}.wasm $COMPONENT_DIR/${wac_compatible_component_name}.wasm
cp ${component_name}.mo $COMPONENT_DIR/${component_name}@0.0.1/lib.mo
COMPONENT_DIR="$MOPS_COMPONENTS_DIR/[email protected]"
echo ... Copying to mops-dir $COMPONENT_DIR
mkdir -pv $COMPONENT_DIR
cp target/${component_name}.wasm $COMPONENT_DIR/
cp ${component_name}.mo $COMPONENT_DIR/lib.mo
echo ... DONE building $component_name
}

Expand Down
Loading
Loading