REPO_ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
BUILTINS_DIR := builtins
ASSETS_DIR := assets/builtins
ADAPTER := $(BUILTINS_DIR)/wasi_snapshot_preview1.reactor.wasm
BUILTIN_NAMES := $(patsubst $(BUILTINS_DIR)/%/Cargo.toml,%,$(wildcard $(BUILTINS_DIR)/*/Cargo.toml))
BUILTIN_WASMS := $(addprefix $(ASSETS_DIR)/,$(addsuffix .wasm,$(BUILTIN_NAMES)))
CANONICAL_WIT := $(wildcard wit/tier1/*.wit wit/common/*.wit)
.PHONY: build-builtins check-env clean-builtins
build-builtins: check-env $(BUILTIN_WASMS)
check-env:
@missing=0; out=""; \
for tool in cargo wkg wasm-tools; do \
if ! command -v $$tool >/dev/null 2>&1; then \
out="$$out\n - $$tool"; \
case $$tool in \
cargo) out="$$out (install via rustup: https://rustup.rs)" ;; \
wkg) out="$$out (cargo install wkg)" ;; \
wasm-tools) out="$$out (cargo install wasm-tools)" ;; \
esac; \
missing=1; \
fi; \
done; \
if ! rustup target list --installed 2>/dev/null | grep -q '^wasm32-wasip1$$'; then \
out="$$out\n - rustup target wasm32-wasip1 (rustup target add wasm32-wasip1)"; \
missing=1; \
fi; \
if [ $$missing -eq 1 ]; then \
printf 'check-env: missing required tools:%b\n' "$$out" >&2; \
exit 1; \
fi
.SECONDEXPANSION:
$(ASSETS_DIR)/%.wasm: \
$$(shell find $(BUILTINS_DIR)/$$* -type f \
\( -name '*.rs' -o -name '*.wit' -o -name '*.toml' \) \
-not -path '*/target/*' -not -path '*/wit/deps/*' 2>/dev/null) \
$(CANONICAL_WIT) \
$(ADAPTER)
@mkdir -p $(@D)
@echo "── Building builtin: $* ──"
cd $(BUILTINS_DIR)/$* && wkg wit fetch
cd $(BUILTINS_DIR)/$* && cargo build --release --target wasm32-wasip1
wasm-tools component new \
$(BUILTINS_DIR)/$*/target/wasm32-wasip1/release/$(subst -,_,$*).wasm \
--adapt $(ADAPTER) \
--skip-validation \
-o $@
@echo " → $@"
clean-builtins:
rm -rf $(ASSETS_DIR)
@for d in $(BUILTINS_DIR)/*/; do \
[ -f "$$d/Cargo.toml" ] || continue; \
rm -rf "$$d/target" "$$d/wit/deps" "$$d/wkg.lock"; \
done