|
| 1 | +# Detect platform for WASI SDK |
| 2 | +UNAME_S := $(shell uname -s) |
| 3 | +ifeq ($(UNAME_S),Linux) |
| 4 | + WASI_SDK_PLATFORM = x86_64-linux |
| 5 | +else ifeq ($(UNAME_S),Darwin) |
| 6 | + WASI_SDK_PLATFORM = x86_64-macos |
| 7 | +else |
| 8 | + $(error Unsupported platform: $(UNAME_S)) |
| 9 | +endif |
| 10 | + |
| 11 | +# WASI SDK paths and URLs |
| 12 | +WASI_SDK_VERSION = 25.0 |
| 13 | +WASI_SDK_DIR = wasi-sdk-$(WASI_SDK_VERSION)-$(WASI_SDK_PLATFORM) |
| 14 | +WASI_SDK_PATH = $(shell pwd)/$(WASI_SDK_DIR) |
| 15 | +WASI_SDK_URL = $(or $(WASI_SDK_DOWNLOAD_URL),https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/$(WASI_SDK_DIR).tar.gz) |
| 16 | + |
| 17 | +# WASI build flags for C++ compilation with wasmtime |
| 18 | +WASIFLAGS = -std=c++14 \ |
| 19 | + --target=wasm32-wasi \ |
| 20 | + --sysroot=$(WASI_SDK_PATH)/share/wasi-sysroot \ |
| 21 | + -D_WASI_EMULATED_SIGNAL \ |
| 22 | + -lwasi-emulated-signal \ |
| 23 | + -fno-exceptions |
| 24 | + |
| 25 | +# Use the WASI SDK C++ compiler |
| 26 | +CXX = $(WASI_SDK_PATH)/bin/clang++ |
| 27 | + |
| 28 | +# Detect wasmtime location (system PATH or home directory) |
| 29 | +WASMTIME := $(shell which wasmtime 2>/dev/null || echo "$$HOME/.wasmtime/bin/wasmtime") |
| 30 | + |
| 31 | +# Default target |
| 32 | +all: run a.out |
| 33 | + |
| 34 | +# Run the example with wasmtime (installs all deps automatically) |
| 35 | +run: main.wasm |
| 36 | + $(WASMTIME) run --allow-precompiled main.wasm |
| 37 | + |
| 38 | +# CI expects a.out; we create it as a bash script that simply execs wasmtime |
| 39 | +a.out: main.wasm |
| 40 | + @printf '%s\n' '#!/usr/bin/env bash' 'exec $(WASMTIME) run --allow-precompiled "$$(dirname "$$0")/main.wasm" "$$@"' > $@ |
| 41 | + @chmod +x $@ |
| 42 | + |
| 43 | +main.wasm: wasi-sdk-installed main.cpp generated.h generated.cpp \ |
| 44 | + target/wasm32-wasip1/release/libexample_tutorial_wasm32.a |
| 45 | + $(CXX) $(WASIFLAGS) \ |
| 46 | + main.cpp generated.cpp \ |
| 47 | + target/wasm32-wasip1/release/libexample_tutorial_wasm32.a \ |
| 48 | + -o $@ |
| 49 | + |
| 50 | +target/wasm32-wasip1/release/libexample_tutorial_wasm32.a: wasm32-wasip1-target generated.h ./src/generated.rs ./src/lib.rs |
| 51 | + cargo build --target=wasm32-wasip1 --release |
| 52 | + |
| 53 | +generated.h ./src/generated.rs generated.cpp: main32.zng |
| 54 | + cargo run --release --manifest-path ../../zngur-cli/Cargo.toml g main32.zng |
| 55 | + |
| 56 | +# Install wasmtime and WASI SDK automatically |
| 57 | +wasi-sdk-installed: |
| 58 | + @echo "Checking dependencies..." |
| 59 | + @if ! command -v wasmtime >/dev/null 2>&1 && [ ! -f "$$HOME/.wasmtime/bin/wasmtime" ]; then \ |
| 60 | + echo "Installing wasmtime..."; \ |
| 61 | + curl https://wasmtime.dev/install.sh -sSf | bash; \ |
| 62 | + fi |
| 63 | + @if [ ! -d "$(WASI_SDK_DIR)" ]; then \ |
| 64 | + echo "Installing WASI SDK for $(WASI_SDK_PLATFORM)..."; \ |
| 65 | + curl -L -o wasi-sdk-latest.tar.gz "$(WASI_SDK_URL)" && \ |
| 66 | + tar -xzf wasi-sdk-latest.tar.gz; \ |
| 67 | + fi |
| 68 | + @touch wasi-sdk-installed |
| 69 | + |
| 70 | +# Ensure wasm32-wasip1 target is installed |
| 71 | +wasm32-wasip1-target: |
| 72 | + @rustup target list --installed | grep -q wasm32-wasip1 || { \ |
| 73 | + echo "Installing wasm32-wasip1 Rust target..."; \ |
| 74 | + rustup target add wasm32-wasip1; \ |
| 75 | + } |
| 76 | + @touch wasm32-wasip1-target |
| 77 | + |
| 78 | +clean: |
| 79 | + cargo clean |
| 80 | + rm -f generated.h ./src/generated.rs generated.cpp generated.o |
| 81 | + rm -f main.wasm example_tutorial_wasm32.wasm a.out wasi-sdk-latest.tar.gz |
| 82 | + rm -f wasi-sdk-installed wasm32-wasip1-target |
| 83 | + rm -rf wasi-sdk-* |
| 84 | + |
| 85 | +# Legacy target for manual dependency installation (now automatic) |
| 86 | +install-deps: wasi-sdk-installed wasm32-wasip1-target |
| 87 | + |
| 88 | +FORCE: ; |
0 commit comments