Skip to content

Commit 2b4a7ee

Browse files
committed
Wasm: Wire the Wasm language module up to the build system.
This allows to configure the Wasm module, e.g ./configure wasm --include-path=/path/to/wasmtime-v11.0.0-x86_64-linux-c-api/include --lib-path=/path/to/wasmtime-v11.0.0-x86_64-linux-c-api/lib --rpath --rpath as above says to set the rpath to the value of --lib-path. You can alternatively specify a directory to use as the rpath. Or simply omit the option to not have an rpath set. This is mostly useful for during development where you may not have the Wasmtime stuff installed to system directories or you want to test with newer/different versions. See ./configure wasm --help for a full list of options. Reviewed-by: Alejandro Colomar <[email protected]> Signed-off-by: Andrew Clayton <[email protected]>
1 parent 6a211e2 commit 2b4a7ee

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

auto/help

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,7 @@ cat << END
7575
java OPTIONS configure Java module
7676
run "./configure java --help" to see available options
7777

78+
wasm OPTIONS configure WebAssembly module
79+
run "./configure wasm --help" to see available options
80+
7881
END

auto/modules/conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ case "$nxt_module" in
3333
. auto/modules/java
3434
;;
3535

36+
wasm)
37+
. auto/modules/wasm
38+
;;
39+
3640
*)
3741
echo
3842
echo $0: error: invalid module \"$nxt_module\".

auto/modules/wasm

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
# Copyright (C) Andrew Clayton
2+
# Copyright (C) F5, Inc.
3+
4+
5+
NXT_WASM_RUNTIME=wasmtime
6+
7+
shift
8+
9+
for nxt_option; do
10+
11+
case "$nxt_option" in
12+
-*=*) value=`echo "$nxt_option" | sed -e 's/[-_a-zA-Z0-9]*=//'` ;;
13+
*) value="" ;;
14+
esac
15+
16+
case "$nxt_option" in
17+
18+
--runtime=*) NXT_WASM_RUNTIME="$value" ;;
19+
--module=*) NXT_WASM_MODULE="$value" ;;
20+
--include-path=*) NXT_WASM_INCLUDE_PATH="$value" ;;
21+
--lib-path=*) NXT_WASM_LIB_PATH="$value" ;;
22+
--rpath*) NXT_WASM_RPATH="$value" ;;
23+
24+
--help)
25+
cat << END
26+
27+
--runtime=RUNTIME set the WASM runtime to use (default: wasmtime)
28+
--module=NAME set Unit WASM module name (default: wasm)
29+
--include-path=DIRECTORY set directory path to wasmtime includes
30+
--lib-path=DIRECTORY set directory path to libwasmtime.so library
31+
--rpath[=DIRECTORY] set the rpath (default: --lib-path)
32+
33+
END
34+
exit 0
35+
;;
36+
37+
*)
38+
echo
39+
echo $0: error: invalid wasm option \"$nxt_option\"
40+
echo
41+
exit 1
42+
;;
43+
esac
44+
45+
done
46+
47+
48+
if [ ! -f $NXT_AUTOCONF_DATA ]; then
49+
echo
50+
echo Please run common $0 before configuring module \"$nxt_module\".
51+
echo
52+
exit 1
53+
fi
54+
55+
. $NXT_AUTOCONF_DATA
56+
57+
NXT_WASM=wasm
58+
NXT_WASM_MODULE=${NXT_WASM_MODULE=${NXT_WASM##*/}}
59+
60+
NXT_WASM_INCLUDE_PATH=${NXT_WASM_INCLUDE_PATH=}
61+
NXT_WASM_LIB_PATH=${NXT_WASM_LIB_PATH=}
62+
NXT_WASM_LDFLAGS=
63+
if [ "$NXT_WASM_RUNTIME" = "wasmtime" ]; then
64+
NXT_WASM_LDFLAGS=-lwasmtime
65+
fi
66+
NXT_WASM_ADDITIONAL_FLAGS="-fno-strict-aliasing \
67+
-Wno-missing-field-initializers \
68+
-DNXT_HAVE_WASM_$(echo ${NXT_WASM_RUNTIME} | tr 'a-z' 'A-Z') \
69+
"
70+
71+
# Set the RPATH/RUNPATH.
72+
#
73+
# We temporarily disable warning on unbound variables here as
74+
# NXT_WASM_RPATH may be legitimately unset, in which case we
75+
# don't set a RPATH.
76+
#
77+
# If NXT_WASM_RPATH is set but null then we set a RPATH of the
78+
# value of $NXT_WASM_LIB (--lib-path) otherwise use the value
79+
# provided.
80+
set +u
81+
if [ "${NXT_WASM_RPATH+set}" = set ]; then
82+
if [ "$NXT_WASM_RPATH" = "" ]; then
83+
NXT_WASM_RPATH=$NXT_WASM_LIB_PATH
84+
fi
85+
86+
NXT_WASM_LDFLAGS="-Wl,-rpath,$NXT_WASM_RPATH $NXT_WASM_LDFLAGS"
87+
fi
88+
set -u
89+
90+
$echo "configuring WASM module"
91+
$echo "configuring WASM module ..." >> $NXT_AUTOCONF_ERR
92+
93+
nxt_found=no
94+
95+
if [ "$NXT_WASM_RUNTIME" = "wasmtime" ]; then
96+
nxt_feature="wasmtime"
97+
nxt_feature_name=""
98+
nxt_feature_run=no
99+
nxt_feature_incs="-I${NXT_WASM_INCLUDE_PATH}"
100+
nxt_feature_libs="-L${NXT_WASM_LIB_PATH} $NXT_WASM_LDFLAGS"
101+
nxt_feature_test="
102+
#include <wasm.h>
103+
#include <wasi.h>
104+
#include <wasmtime.h>
105+
106+
int main(void) {
107+
wasm_config_t *c;
108+
109+
c = wasm_config_new();
110+
wasm_config_delete(c);
111+
112+
return 0;
113+
}"
114+
115+
. auto/feature
116+
fi
117+
118+
if [ $nxt_found = no ]; then
119+
$echo
120+
$echo $0: error: no $NXT_WASM_RUNTIME found.
121+
$echo
122+
exit 1;
123+
fi
124+
125+
126+
if grep ^$NXT_WASM_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
127+
$echo
128+
$echo $0: error: duplicate \"$NXT_WASM_MODULE\" module configured.
129+
$echo
130+
exit 1;
131+
fi
132+
133+
134+
$echo " + WASM module: ${NXT_WASM_MODULE}.unit.so"
135+
136+
. auto/cc/deps
137+
138+
$echo >> $NXT_MAKEFILE
139+
140+
NXT_WASM_MODULE_SRCS=" \
141+
src/wasm/nxt_wasm.c \
142+
"
143+
144+
if [ "$NXT_WASM_RUNTIME" = "wasmtime" ]; then
145+
NXT_WASM_MODULE_SRCS="$NXT_WASM_MODULE_SRCS src/wasm/nxt_rt_wasmtime.c"
146+
fi
147+
148+
149+
# The wasm module object files.
150+
151+
nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o
152+
153+
for nxt_src in $NXT_WASM_MODULE_SRCS; do
154+
155+
nxt_obj=${nxt_src%.c}-$NXT_WASM_MODULE.o
156+
nxt_dep=${nxt_src%.c}-$NXT_WASM_MODULE.dep
157+
nxt_dep_flags=`nxt_gen_dep_flags`
158+
nxt_dep_post=`nxt_gen_dep_post`
159+
nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
160+
161+
cat << END >> $NXT_MAKEFILE
162+
163+
$NXT_BUILD_DIR/$nxt_obj: $nxt_src $NXT_VERSION_H
164+
mkdir -p $NXT_BUILD_DIR/src/wasm
165+
\$(CC) -c \$(CFLAGS) $NXT_WASM_ADDITIONAL_FLAGS \$(NXT_INCS) \\
166+
-I$NXT_WASM_INCLUDE_PATH \\
167+
$nxt_dep_flags \\
168+
-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
169+
$nxt_dep_post
170+
171+
-include $NXT_BUILD_DIR/$nxt_dep
172+
173+
END
174+
175+
done
176+
177+
178+
cat << END >> $NXT_MAKEFILE
179+
180+
.PHONY: ${NXT_WASM_MODULE}
181+
.PHONY: ${NXT_WASM_MODULE}-install
182+
.PHONY: ${NXT_WASM_MODULE}-uninstall
183+
184+
all: ${NXT_WASM_MODULE}
185+
186+
${NXT_WASM_MODULE}: $NXT_BUILD_DIR/lib/unit/modules/${NXT_WASM_MODULE}.unit.so
187+
188+
$NXT_BUILD_DIR/lib/unit/modules/${NXT_WASM_MODULE}.unit.so: $nxt_objs
189+
\$(NXT_MODULE_LINK) -o \$@ \\
190+
$nxt_objs -L${NXT_WASM_LIB_PATH} ${NXT_WASM_LDFLAGS} $NXT_LD_OPT
191+
192+
193+
install: ${NXT_WASM_MODULE}-install
194+
195+
${NXT_WASM_MODULE}-install: ${NXT_WASM_MODULE} install-check
196+
install -d \$(DESTDIR)$NXT_MODULESDIR
197+
install -p $NXT_BUILD_DIR/lib/unit/modules/${NXT_WASM_MODULE}.unit.so \\
198+
\$(DESTDIR)$NXT_MODULESDIR/
199+
200+
201+
uninstall: ${NXT_WASM_MODULE}-uninstall
202+
203+
${NXT_WASM_MODULE}-uninstall:
204+
rm -f \$(DESTDIR)$NXT_MODULESDIR/${NXT_WASM_MODULE}.unit.so
205+
@rmdir -p \$(DESTDIR)$NXT_MODULESDIR 2>/dev/null || true
206+
207+
END

0 commit comments

Comments
 (0)