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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ A collection of utility components that remix wasi:logging types and interfaces.
## Components

- [`levels`](./components/levels/)
- [`to-stderr`](./components/to-stderr/)
- [`to-stdout`](./components/to-stdout/)

## Build
Expand All @@ -31,7 +32,7 @@ make components

The build creates each component in [`components`](./components) into `target/components`, e.g. the levels component at `target/components/levels/levels.wasm`, along with `target/components/interface.wasm`, the `componentized:logging` WIT package. Each component is also built with debug info, e.g. `target/components/levels/levels.debug.wasm`.

The cli tools the build uses, [`wasm-tools`](https://github.com/bytecodealliance/wasm-tools) and [`wkg`](https://github.com/bytecodealliance/wasm-pkg-tools), are pinned in [`tools/Cargo.toml`](./tools/Cargo.toml) and installed into `target/tools/<platform>`, e.g. `target/tools/aarch64-apple-darwin`, as needed, or ahead of time with `make tools`. Dependabot bumps the pinned versions.
The cli tools the build uses, [`wac`](https://github.com/bytecodealliance/wac), [`wasm-tools`](https://github.com/bytecodealliance/wasm-tools) and [`wkg`](https://github.com/bytecodealliance/wasm-pkg-tools), are pinned in [`tools/Cargo.toml`](./tools/Cargo.toml) and installed into `target/tools/<platform>`, e.g. `target/tools/aarch64-apple-darwin`, as needed, or ahead of time with `make tools`. Dependabot bumps the pinned versions.

## Community

Expand Down
9 changes: 9 additions & 0 deletions components/to-stderr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `to-stderr`

Implements wasi:logging/logging@0.1.0-draft writing log statements to stderr via wasi:cli/stderr@0.3.0. A composition of [`to-stdout`](../to-stdout/) with its wasi:cli/stdout@0.3.0 import satisfied by wasi:cli/stderr@0.3.0.

Log output takes the form:

```
2025-02-10 18:04:35.925Z LEVEL [component]: message
```
11 changes: 11 additions & 0 deletions components/to-stderr/to-stderr.wac
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package componentized:logging;

import stderr: wasi:cli/stderr@0.3.0;

let to-stdout = new local:to-stdout {
// write log statements to stderr instead of stdout
"wasi:cli/stdout@0.3.0": stderr,
...
};

export to-stdout["wasi:logging/logging@0.1.0-draft"];
17 changes: 12 additions & 5 deletions make/components.mk
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
COMPONENTS_DIR := target/components

# the WIT packages of components/wit/deps, encoded where `wac compose` looks for a package a
# composition names without instantiating it, e.g. `import stderr: wasi:cli/stderr@0.3.0;`
WAC_DEPS_DIR := target/wac-deps

COMPONENTS = $(sort $(foreach file,$(wildcard $(addprefix components/*/,wit/*.constants.wit *.properties *.wac *.wkg Cargo.toml)),$(word 2,$(subst /, ,$(file)))))

.PHONY: clean-components
clean-components: clean-wit
rm -rf ${COMPONENTS_DIR}
rm -rf ${COMPONENTS_DIR} ${WAC_DEPS_DIR}

.PHONY: components
components: ${COMPONENTS_DIR}/interface.wasm $(foreach component,$(COMPONENTS),${COMPONENTS_DIR}/$(component)/$(component).wasm ${COMPONENTS_DIR}/$(component)/$(component).debug.wasm)
Expand Down Expand Up @@ -35,11 +39,11 @@ else ifneq ($(wildcard components/$1/$1.wac),)
# the local packages the composition instantiates, e.g. `new local:latch-n2 { ... }`
WAC_DEPS_$1 := $$(shell grep -v '^\s*//' components/$1/$1.wac | grep -oE 'local:[a-z0-9-]+' | sed 's/^local://' | sort -u)

${COMPONENTS_DIR}/$1/$1.wasm: components/$1/$1.wac $$(foreach component,$$(WAC_DEPS_$1),$${COMPONENTS_DIR}/$$(component)/$$(component).wasm) ${COMPONENTS_DIR}/$1/README.md | $(call tool,wac-cli)
$(WAC) compose $$(foreach component,$$(WAC_DEPS_$1),-d local:$$(component)=$${COMPONENTS_DIR}/$$(component)/$$(component).wasm) -o ${COMPONENTS_DIR}/$1/$1.wasm components/$1/$1.wac
${COMPONENTS_DIR}/$1/$1.wasm: components/$1/$1.wac $$(foreach component,$$(WAC_DEPS_$1),$${COMPONENTS_DIR}/$$(component)/$$(component).wasm) ${COMPONENTS_DIR}/$1/README.md ${WAC_DEPS_DIR} | $(call tool,wac-cli)
$(WAC) compose --deps-dir ${WAC_DEPS_DIR} $$(foreach component,$$(WAC_DEPS_$1),-d local:$$(component)=$${COMPONENTS_DIR}/$$(component)/$$(component).wasm) -o ${COMPONENTS_DIR}/$1/$1.wasm components/$1/$1.wac

${COMPONENTS_DIR}/$1/$1.debug.wasm: components/$1/$1.wac $$(foreach component,$$(WAC_DEPS_$1),$${COMPONENTS_DIR}/$$(component)/$$(component).debug.wasm) ${COMPONENTS_DIR}/$1/README.md | $(call tool,wac-cli)
$(WAC) compose $$(foreach component,$$(WAC_DEPS_$1),-d local:$$(component)=$${COMPONENTS_DIR}/$$(component)/$$(component).debug.wasm) -o ${COMPONENTS_DIR}/$1/$1.debug.wasm components/$1/$1.wac
${COMPONENTS_DIR}/$1/$1.debug.wasm: components/$1/$1.wac $$(foreach component,$$(WAC_DEPS_$1),$${COMPONENTS_DIR}/$$(component)/$$(component).debug.wasm) ${COMPONENTS_DIR}/$1/README.md ${WAC_DEPS_DIR} | $(call tool,wac-cli)
$(WAC) compose --deps-dir ${WAC_DEPS_DIR} $$(foreach component,$$(WAC_DEPS_$1),-d local:$$(component)=$${COMPONENTS_DIR}/$$(component)/$$(component).debug.wasm) -o ${COMPONENTS_DIR}/$1/$1.debug.wasm components/$1/$1.wac

else ifneq ($(wildcard components/$1/$1.wkg),)

Expand Down Expand Up @@ -70,6 +74,9 @@ endef

$(foreach component,$(COMPONENTS),$(eval $(call BUILD_COMPONENT,$(component))))

${WAC_DEPS_DIR}: components/wit/deps scripts/wac-deps.sh | $(call tool,wasm-tools)
WASM_TOOLS=$(WASM_TOOLS) scripts/wac-deps.sh components/wit/deps ${WAC_DEPS_DIR}

${COMPONENTS_DIR}/interface.wasm: wit/deps README.md | $(call tool,wkg)
@mkdir -p ${COMPONENTS_DIR}
$(WKG) build -o ${COMPONENTS_DIR}/interface.wasm
Expand Down
46 changes: 46 additions & 0 deletions scripts/wac-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# Encode each WIT package of a wit/deps directory where `wac compose --deps-dir` looks for it, so a
# composition can name the interfaces of a package it does not instantiate, e.g.
# `import stderr: wasi:cli/stderr@0.3.0;`.
#
# WASM_TOOLS=<wasm-tools> scripts/wac-deps.sh <wit/deps> <deps-dir>
#
# wac looks for a versioned package at `<namespace>/<name>/<version>.wasm`, e.g.
#
# wit/deps/wasi-cli-0.3.0 -> <deps-dir>/wasi/cli/0.3.0.wasm
#
# Each package is encoded with the other packages as its dependencies.

set -euo pipefail

deps=$1
out=$2
wasm_tools=${WASM_TOOLS:-wasm-tools}

work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT

rm -rf "$out"
mkdir -p "$out"

for package in "$deps"/*/; do
package=${package%/}
rm -rf "$work/package"
mkdir -p "$work/package/deps"
cp "$package"/*.wit "$work/package/"
for dep in "$deps"/*/; do
dep=${dep%/}
[[ "$dep" == "$package" ]] || cp -R "$dep" "$work/package/deps/"
done
"$wasm_tools" component wit "$work/package" --wasm -o "$work/package.wasm"

# e.g. `package wasi:cli@0.3.0;`
id=$("$wasm_tools" component wit "$work/package.wasm" | sed -n 's/^package \(.*\);$/\1/p' | head -n 1)
namespace=${id%%:*}
name=${id#*:}
name=${name%%@*}
version=${id#*@}
mkdir -p "$out/$namespace/$name"
cp "$work/package.wasm" "$out/$namespace/$name/$version.wasm"
done
2 changes: 2 additions & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ path = "lib.rs"
# the binaries each tool installs, the Makefile runs each by path from a variable named after it,
# e.g. `$(WASM_TOOLS)` for `wasm-tools`
[package.metadata.bins]
wac-cli = ["wac"]
wasm-tools = ["wasm-tools"]
wkg = ["wkg"]

[dependencies]
wac-cli = "=0.12.0"
wasm-tools = "=1.261.0"
wkg = "=0.16.1"

Expand Down
Loading