Skip to content

Add a Rust based Wasm language module that supports the component model #1122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 21, 2024
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
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 80
9 changes: 7 additions & 2 deletions auto/help
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ cat << END
java OPTIONS configure Java module
run "./configure java --help" to see available options

wasm OPTIONS configure WebAssembly module
run "./configure wasm --help" to see available options
wasm OPTIONS configure WebAssembly module
Copy link
Collaborator

Choose a reason for hiding this comment

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

99% of our users will want wasm-wasi-component... I wonder if we can signpost this better?

Suggested change
wasm OPTIONS configure WebAssembly module
wasm OPTIONS configure raw (not Component Model) WebAssembly module

Hm. I don't like that either. Food for thought if you have something better that immediately comes to mind.

Not blocking on this feedback.

Copy link
Member Author

Choose a reason for hiding this comment

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

Users should be consuming it via packages... If they're building from source they hopefully have some idea of what they're doing and then there's documentation...

run "./configure wasm --help" to see available options

wasm-wasi-component OPTIONS
configure WebAssembly Component Model module
run "./configure wasm-wasi-component --help" to see
available options

END
4 changes: 4 additions & 0 deletions auto/modules/conf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ case "$nxt_module" in
. auto/modules/wasm
;;

wasm-wasi-component)
. auto/modules/wasm-wasi-component
;;

*)
echo
echo $0: error: invalid module \"$nxt_module\".
Expand Down
119 changes: 119 additions & 0 deletions auto/modules/wasm-wasi-component
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Copyright (C) Andrew Clayton
# Copyright (C) F5, Inc.


NXT_WCM_MODULE=wasm-wasi-component
NXT_WCM_MOD_NAME=`echo $NXT_WCM_MODULE | tr '-' '_'`.unit.so


shift

for nxt_option; do

case "$nxt_option" in
-*=*) value=`echo "$nxt_option" | sed -e 's/[-_a-zA-Z0-9]*=//'` ;;
*) value="" ;;
esac

case "$nxt_option" in

--help)
cat << END

END
exit 0
;;

*)
echo
echo $0: error: invalid $NXT_WCM_MODULE option \"$nxt_option\"
echo
exit 1
;;
esac

done


if [ ! -f $NXT_AUTOCONF_DATA ]; then
echo
echo Please run common $0 before configuring module \"$nxt_module\".
echo
exit 1
fi

. $NXT_AUTOCONF_DATA

NXT_WCM_WASM_TOOLS_BIN=${NXT_WCM_WASM_TOOLS_BIN=}


$echo "configuring $NXT_WCM_MODULE module"
$echo "configuring $NXT_WCM_MODULE module ..." >> $NXT_AUTOCONF_ERR

$echo -n "looking for rust compiler ... "

if [ -z `which rustc 2>/dev/null` ]; then
$echo "not found."
exit 1;
fi

$echo "found."

$echo -n "looking for cargo ... "

if [ -z `which cargo 2>/dev/null` ]; then
$echo "not found."
exit 1;
fi

$echo "found."


if grep ^$NXT_WCM_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
$echo
$echo $0: error: duplicate \"$NXT_WCM_MODULE\" module configured.
$echo
exit 1;
fi


$echo " + $NXT_WCM_MODULE module: $NXT_WCM_MOD_NAME"


NXT_OS=$(uname -o)

if [ $NXT_OS = "Darwin" ]; then
NXT_CARGO_CMD="cargo rustc --release --manifest-path src/wasm-wasi-component/Cargo.toml -- --emit link=target/release/libwasm_wasi_component.so -C link-args='-undefined dynamic_lookup'"
else
NXT_CARGO_CMD="cargo build --release --manifest-path src/wasm-wasi-component/Cargo.toml"
fi


cat << END >> $NXT_MAKEFILE

.PHONY: ${NXT_WCM_MODULE}
.PHONY: ${NXT_WCM_MODULE}-install
.PHONY: ${NXT_WCM_MODULE}-uninstall

all: ${NXT_WCM_MODULE}

${NXT_WCM_MODULE}: $NXT_BUILD_DIR/lib/unit/modules/$NXT_WCM_MOD_NAME

$NXT_BUILD_DIR/lib/unit/modules/$NXT_WCM_MOD_NAME:
make build/src/nxt_unit.o
$NXT_CARGO_CMD

install: ${NXT_WCM_MODULE}-install

${NXT_WCM_MODULE}-install: ${NXT_WCM_MODULE} install-check
install -d \$(DESTDIR)$NXT_MODULESDIR
install -p src/wasm-wasi-component/target/release/libwasm_wasi_component.so \\
\$(DESTDIR)$NXT_MODULESDIR/$NXT_WCM_MOD_NAME

uninstall: ${NXT_WCM_MODULE}-uninstall

${NXT_WCM_MODULE}-uninstall:
rm -f \$(DESTDIR)$NXT_MODULESDIR/$NXT_WCM_MOD_NAME
@rmdir -p \$(DESTDIR)$NXT_MODULESDIR 2>/dev/null || true

END
15 changes: 8 additions & 7 deletions pkg/docker/Dockerfile.wasm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LABEL org.opencontainers.image.url="https://unit.nginx.org"
LABEL org.opencontainers.image.source="https://github.com/nginx/unit"
LABEL org.opencontainers.image.documentation="https://unit.nginx.org/installation/#docker-images"
LABEL org.opencontainers.image.vendor="NGINX Docker Maintainers <[email protected]>"
LABEL org.opencontainers.image.version="1.31.1"
LABEL org.opencontainers.image.version="1.32.0"

RUN set -ex \
&& savedAptMark="$(apt-mark showmanual)" \
Expand All @@ -15,7 +15,7 @@ RUN set -ex \
&& mkdir -p /usr/lib/unit/modules /usr/lib/unit/debug-modules \
&& mkdir -p /usr/src/unit \
&& cd /usr/src/unit \
&& git clone --depth 1 -b 1.31.1-1 https://github.com/nginx/unit \
&& git clone --depth 1 -b 1.32.0-1 https://github.com/nginx/unit \
&& cd unit \
&& NCPU="$(getconf _NPROCESSORS_ONLN)" \
&& DEB_HOST_MULTIARCH="$(dpkg-architecture -q DEB_HOST_MULTIARCH)" \
Expand Down Expand Up @@ -45,7 +45,8 @@ RUN set -ex \
&& make -j $NCPU unitd \
&& install -pm755 build/sbin/unitd /usr/sbin/unitd \
&& make clean \
&& export RUST_VERSION=1.71.0 \
&& apt-get install --no-install-recommends --no-install-suggests -y libclang-dev \
&& export RUST_VERSION=1.76.0 \
&& export RUSTUP_HOME=/usr/src/unit/rustup \
&& export CARGO_HOME=/usr/src/unit/cargo \
&& export PATH=/usr/src/unit/cargo/bin:$PATH \
Expand All @@ -67,12 +68,12 @@ RUN set -ex \
&& make -C pkg/contrib .wasmtime \
&& install -pm 755 pkg/contrib/wasmtime/target/release/libwasmtime.so /usr/lib/$(dpkg-architecture -q DEB_HOST_MULTIARCH)/ \
&& ./configure $CONFIGURE_ARGS_MODULES --cc-opt="$CC_OPT" --modulesdir=/usr/lib/unit/debug-modules --debug \
&& ./configure wasm --include-path=`pwd`/pkg/contrib/wasmtime/crates/c-api/include --lib-path=/usr/lib/$(dpkg-architecture -q DEB_HOST_MULTIARCH)/ \
&& make -j $NCPU wasm-install \
&& ./configure wasm --include-path=`pwd`/pkg/contrib/wasmtime/crates/c-api/include --lib-path=/usr/lib/$(dpkg-architecture -q DEB_HOST_MULTIARCH)/ && ./configure wasm-wasi-component \
&& make -j $NCPU wasm-install wasm-wasi-component-install \
&& make clean \
&& ./configure $CONFIGURE_ARGS_MODULES --cc-opt="$CC_OPT" --modulesdir=/usr/lib/unit/modules \
&& ./configure wasm --include-path=`pwd`/pkg/contrib/wasmtime/crates/c-api/include --lib-path=/usr/lib/$(dpkg-architecture -q DEB_HOST_MULTIARCH)/ \
&& make -j $NCPU wasm-install \
&& ./configure wasm --include-path=`pwd`/pkg/contrib/wasmtime/crates/c-api/include --lib-path=/usr/lib/$(dpkg-architecture -q DEB_HOST_MULTIARCH)/ && ./configure wasm-wasi-component \
&& make -j $NCPU wasm-install wasm-wasi-component-install \
&& cd \
&& rm -rf /usr/src/unit \
&& for f in /usr/sbin/unitd /usr/lib/unit/modules/*.unit.so; do \
Expand Down
10 changes: 6 additions & 4 deletions pkg/docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ MODULE_PREBUILD_ruby ?= /bin/true

VERSIONS_wasm ?=
CONTAINER_wasm ?= debian:$(VARIANT)-slim
CONFIGURE_wasm ?= wasm --include-path=\`pwd\`/pkg/contrib/wasmtime/crates/c-api/include --lib-path=/usr/lib/\$$(dpkg-architecture -q DEB_HOST_MULTIARCH)/
INSTALL_wasm ?= wasm-install
RUN_wasm ?= /bin/true
CONFIGURE_wasm ?= wasm --include-path=\`pwd\`/pkg/contrib/wasmtime/crates/c-api/include --lib-path=/usr/lib/\$$(dpkg-architecture -q DEB_HOST_MULTIARCH)/ \&\& ./configure wasm-wasi-component
INSTALL_wasm ?= wasm-install wasm-wasi-component-install
RUN_wasm ?= /bin/true

define MODULE_PREBUILD_wasm
export RUST_VERSION=1.71.0 \\\n \
apt-get install --no-install-recommends --no-install-suggests -y libclang-dev \\\n \
\ \ \ \&\& export RUST_VERSION=1.76.0 \\\n \
\ \ \ \&\& export RUSTUP_HOME=/usr/src/unit/rustup \\\n \
\ \ \ \&\& export CARGO_HOME=/usr/src/unit/cargo \\\n \
\ \ \ \&\& export PATH=/usr/src/unit/cargo/bin:\$$PATH \\\n \
Expand Down
3 changes: 3 additions & 0 deletions src/nxt_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,9 @@ nxt_app_parse_type(u_char *p, size_t length)
} else if (nxt_str_eq(&str, "java", 4)) {
return NXT_APP_JAVA;

} else if (nxt_str_eq(&str, "wasm-wasi-component", 19)) {
return NXT_APP_WASM_WC;

} else if (nxt_str_eq(&str, "wasm", 4)) {
return NXT_APP_WASM;
}
Expand Down
9 changes: 9 additions & 0 deletions src/nxt_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef enum {
NXT_APP_RUBY,
NXT_APP_JAVA,
NXT_APP_WASM,
NXT_APP_WASM_WC,

NXT_APP_UNKNOWN,
} nxt_app_type_t;
Expand Down Expand Up @@ -104,6 +105,13 @@ typedef struct {
} nxt_wasm_app_conf_t;


typedef struct {
const char *component;

nxt_conf_value_t *access;
} nxt_wasm_wc_app_conf_t;


struct nxt_common_app_conf_s {
nxt_str_t name;
nxt_str_t type;
Expand Down Expand Up @@ -133,6 +141,7 @@ struct nxt_common_app_conf_s {
nxt_ruby_app_conf_t ruby;
nxt_java_app_conf_t java;
nxt_wasm_app_conf_t wasm;
nxt_wasm_wc_app_conf_t wasm_wc;
} u;

nxt_conf_value_t *self;
Expand Down
17 changes: 17 additions & 0 deletions src/nxt_conf_validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,22 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_members[] = {
};


static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_wc_members[] = {
{
.name = nxt_string("component"),
.type = NXT_CONF_VLDT_STRING,
.flags = NXT_CONF_VLDT_REQUIRED,
}, {
.name = nxt_string("access"),
.type = NXT_CONF_VLDT_OBJECT,
.validator = nxt_conf_vldt_object,
.u.members = nxt_conf_vldt_wasm_access_members,
},

NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)
};


static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_access_members[] = {
{
.name = nxt_string("filesystem"),
Expand Down Expand Up @@ -2660,6 +2676,7 @@ nxt_conf_vldt_app(nxt_conf_validation_t *vldt, nxt_str_t *name,
{ nxt_conf_vldt_object, nxt_conf_vldt_ruby_members },
{ nxt_conf_vldt_object, nxt_conf_vldt_java_members },
{ nxt_conf_vldt_object, nxt_conf_vldt_wasm_members },
{ nxt_conf_vldt_object, nxt_conf_vldt_wasm_wc_members },
};

ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_VLDT_OBJECT);
Expand Down
15 changes: 15 additions & 0 deletions src/nxt_main_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,20 @@ static nxt_conf_map_t nxt_wasm_app_conf[] = {
};


static nxt_conf_map_t nxt_wasm_wc_app_conf[] = {
{
nxt_string("component"),
NXT_CONF_MAP_CSTRZ,
offsetof(nxt_common_app_conf_t, u.wasm_wc.component),
},
{
nxt_string("access"),
NXT_CONF_MAP_PTR,
offsetof(nxt_common_app_conf_t, u.wasm_wc.access),
},
};


static nxt_conf_app_map_t nxt_app_maps[] = {
{ nxt_nitems(nxt_external_app_conf), nxt_external_app_conf },
{ nxt_nitems(nxt_python_app_conf), nxt_python_app_conf },
Expand All @@ -385,6 +399,7 @@ static nxt_conf_app_map_t nxt_app_maps[] = {
{ nxt_nitems(nxt_ruby_app_conf), nxt_ruby_app_conf },
{ nxt_nitems(nxt_java_app_conf), nxt_java_app_conf },
{ nxt_nitems(nxt_wasm_app_conf), nxt_wasm_app_conf },
{ nxt_nitems(nxt_wasm_wc_app_conf), nxt_wasm_wc_app_conf },
};


Expand Down
1 change: 1 addition & 0 deletions src/nxt_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ static const nxt_str_t *nxt_app_msg_prefix[] = {
[NXT_APP_RUBY] = &http_prefix,
[NXT_APP_JAVA] = &empty_prefix,
[NXT_APP_WASM] = &empty_prefix,
[NXT_APP_WASM_WC] = &empty_prefix,
};


Expand Down
1 change: 1 addition & 0 deletions src/wasm-wasi-component/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
Loading