Skip to content
Merged
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
4 changes: 3 additions & 1 deletion Docker/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ ENV LIND_WASM_ROOT=/home/${USERNAME}/lind-wasm
USER root
RUN install -D -m 0755 /home/${USERNAME}/lind-wasm/scripts/lind_compile /usr/local/bin/lind_compile \
&& install -D -m 0755 /home/${USERNAME}/lind-wasm/scripts/lind_run /usr/local/bin/lind_run \
&& install -D -m 0755 /home/${USERNAME}/lind-wasm/scripts/cargo-lind_compile /usr/local/bin/cargo-lind_compile \
&& ln -sf /usr/local/bin/lind_compile /usr/local/bin/lind-clang \
&& ln -sf /usr/local/bin/lind_run /usr/local/bin/lind-wasm
&& ln -sf /usr/local/bin/lind_run /usr/local/bin/lind-wasm \
&& ln -sf /usr/local/bin/cargo-lind_compile /usr/local/bin/lind-cargo-build
USER ${USERNAME}

CMD ["/bin/bash"]
5 changes: 3 additions & 2 deletions Docker/Dockerfile.e2e
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ COPY --parents scripts tests tools skip_test_cases.txt .
ENV LIND_WASM_ROOT=/
RUN install -D -m 0755 /scripts/lind_compile /usr/local/bin/lind_compile \
&& install -D -m 0755 /scripts/lind_run /usr/local/bin/lind_run \
&& install -D -m 0755 /scripts/cargo-lind_compile /usr/local/bin/cargo-lind_compile \
&& ln -sf /usr/local/bin/lind_compile /usr/local/bin/lind-clang \
&& ln -sf /usr/local/bin/lind_run /usr/local/bin/lind-wasm

&& ln -sf /usr/local/bin/lind_run /usr/local/bin/lind-wasm \
&& ln -sf /usr/local/bin/cargo-lind_compile /usr/local/bin/lind-cargo-build

# Run all tests, print results, and exit with 1, if any test fails; 0 otherwise
FROM base AS test
Expand Down
16 changes: 11 additions & 5 deletions docs/contribute/compile-with-rust.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Compiling Rust Code with `lind-glibc` (Shared Memory Enabled)

To compile Rust programs against **`lind-glibc`**, you must configure Rust to:
To compile Rust programs against **`lind-glibc`**:

0. Use the nightly Rust toolchain (because `-Z build-std` is a nightly-only feature)
1. Use the custom `config.toml` configuration file
2. Use the custom `wasip1-clang` linker wrapper
3. Rebuild the Rust standard library (`std`) with these features enabled
- Ensure that the `scripts/cargo-lind_compile` script exists in `PATH`
- Run `cargo lind_compile` at the root of your Rust crate.

`cargo-lind-compile` is a drop-in replace for `cargo build`. It supports the same flags (such as --release) and is intended to be used in same contexts.

Internally, `cargo lind_compile` runs `cargo build` with the configurations detailed below.

It then runs `lind-compile --opt-only` on this output `.wasm` binary to optimize it in place.

Alternatively, you can use `lind-cargo-build` at the root of your Rust crate to run this script.

---
## 1. Cargo Configuration (`.cargo/config.toml`)
Expand Down
6 changes: 5 additions & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ lind-wasm hello.cwasm
*Here is what happens under the hood:*

1. `lind_compile` compiles `hello.c` into a WebAssembly (WASM)
binary using headers etc. from *lind-glibc*.
binary that is linked against *lind-glibc*.
1. `lind_run` runs the compiled wasm using *lind-wasm* runtime
and the *lind-posix* microvisor.

---

To compile a Rust crate into a *lind-glibc* linked WASM binary, follow this guide: [Compiling Rust Code with `lind-glibc`](./contribute/compile-with-rust.md)

## What's next!

The Lind documentation is currently under heavy construction. Please [submit an
Expand Down
49 changes: 49 additions & 0 deletions scripts/cargo-lind_compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
set -e

shift

# Determine profile from arguments
PROFILE="debug"
CARGO_ARGS=("--target" "wasm32-wasip1")

for arg in "$@"; do
if [ "$arg" = "--release" ] || [ "$arg" = "-r" ]; then
PROFILE="release"
fi
CARGO_ARGS+=("$arg")
done

# Set linker (allow override via environment variable)
LINKER="${LIND_WASM_LINKER:-/home/lind/lind-wasm/scripts/wasip1-clang.sh}"
export CARGO_TARGET_WASM32_WASIP1_LINKER="$LINKER"

# Set rustflags for wasm32-wasip1 target
export CARGO_TARGET_WASM32_WASIP1_RUSTFLAGS="\
-C link-self-contained=no \
-C target-feature=+crt-static,+atomics,+bulk-memory \
-C link-arg=-Wl,--import-memory \
-C link-arg=-Wl,--export-memory \
-C link-arg=-Wl,--shared-memory \
-C link-arg=-Wl,--max-memory=67108864 \
-C link-arg=-Wl,--export=__stack_pointer \
-C link-arg=-Wl,--export=__stack_low \
-C link-arg=-Wl,--export=pass_fptr_to_wt"

# Set wasm-opt and wasmtime paths (allow override via environment variables)
WASM_OPT_BIN="${WASM_OPT_BIN:-wasm-opt}"
WASMTIME_BIN="${WASMTIME_BIN:-wasmtime}"

echo "Building with profile: $PROFILE"
cargo +nightly build -Z build-std=std,panic_abort "${CARGO_ARGS[@]}"

# Find the wasm file in the appropriate profile directory
OUT_WASM=$(find "target/wasm32-wasip1/$PROFILE" -maxdepth 1 -name "*.wasm" ! -name "*deps*" 2>/dev/null | head -n1)

if [ -z "$OUT_WASM" ]; then
echo "Error: No wasm file found in target/wasm32-wasip1/$PROFILE"
exit 1
fi

echo "----------------"
exec lind_compile --opt-only "$OUT_WASM"
24 changes: 23 additions & 1 deletion scripts/wasip1-clang.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#!/usr/bin/env bash
set -euo pipefail

SYSROOT=/home/lind-wasm/src/glibc/sysroot
# --- repo root discovery (env var -> script dir -> git) ---
if [[ -n "${LIND_WASM_ROOT:-}" && -d "${LIND_WASM_ROOT}" ]]; then
REPO_ROOT="${LIND_WASM_ROOT}"
else
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
if [[ -f "${SCRIPT_DIR}/../Makefile" ]]; then
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
else
if command -v git >/dev/null 2>&1; then
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
else
REPO_ROOT=""
fi
fi
fi

if [[ -z "${REPO_ROOT}" || ! -d "${REPO_ROOT}" ]]; then
echo "ERROR: Could not locate lind-wasm repo root." >&2
echo "Hint: export LIND_WASM_ROOT=/path/to/lind-wasm" >&2
exit 2
fi

SYSROOT="$REPO_ROOT/src/glibc/sysroot"
LIBDIR="$SYSROOT/lib/wasm32-wasi"
CRT1="$LIBDIR/crt1.o"

Expand Down
Loading