summaryrefslogtreecommitdiffhomepage
path: root/auto/modules/wasm
blob: 1f388de6cdb55a24d79610cc331456f64152d0ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright (C) Andrew Clayton
# Copyright (C) F5, Inc.


NXT_WASM_RUNTIME=wasmtime

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

        --runtime=*)		NXT_WASM_RUNTIME="$value"		    ;;
        --module=*)		NXT_WASM_MODULE="$value"                    ;;
        --include-path=*)	NXT_WASM_INCLUDE_PATH="$value"		    ;;
        --lib-path=*)		NXT_WASM_LIB_PATH="$value"		    ;;
        --rpath*)		NXT_WASM_RPATH="$value"			    ;;

        --help)
            cat << END

    --runtime=RUNTIME		set the WASM runtime to use (default: wasmtime)
    --module=NAME		set Unit WASM module name (default: wasm)
    --include-path=DIRECTORY	set directory path to wasmtime includes
    --lib-path=DIRECTORY	set directory path to libwasmtime.so library
    --rpath[=DIRECTORY]		set the rpath (default: --lib-path)

END
            exit 0
        ;;

        *)
            echo
            echo $0: error: invalid wasm 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_WASM=wasm
NXT_WASM_MODULE=${NXT_WASM_MODULE=${NXT_WASM##*/}}

NXT_WASM_INCLUDE_PATH=${NXT_WASM_INCLUDE_PATH=}
NXT_WASM_LIB_PATH=${NXT_WASM_LIB_PATH=}
NXT_WASM_LDFLAGS=
if [ "$NXT_WASM_RUNTIME" = "wasmtime" ]; then
    NXT_WASM_LDFLAGS=-lwasmtime
fi
NXT_WASM_ADDITIONAL_FLAGS="-fno-strict-aliasing \
 -Wno-missing-field-initializers \
 -DNXT_HAVE_WASM_$(echo ${NXT_WASM_RUNTIME} | tr 'a-z' 'A-Z') \
"

# Set the RPATH/RUNPATH.
#
# We temporarily disable warning on unbound variables here as
# NXT_WASM_RPATH may be legitimately unset, in which case we
# don't set a RPATH.
#
# If NXT_WASM_RPATH is set but null then we set a RPATH of the
# value of $NXT_WASM_LIB (--lib-path) otherwise use the value
# provided.
set +u
if [ "${NXT_WASM_RPATH+set}" = set ]; then
    if [ "$NXT_WASM_RPATH" = "" ]; then
        NXT_WASM_RPATH=$NXT_WASM_LIB_PATH
    fi

    NXT_WASM_LDFLAGS="-Wl,-rpath,$NXT_WASM_RPATH $NXT_WASM_LDFLAGS"
fi
set -u

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

nxt_found=no

if [ "$NXT_WASM_RUNTIME" = "wasmtime" ]; then
    nxt_feature="wasmtime"
    nxt_feature_name=""
    nxt_feature_run=no
    nxt_feature_incs="-I${NXT_WASM_INCLUDE_PATH}"
    nxt_feature_libs="-L${NXT_WASM_LIB_PATH} $NXT_WASM_LDFLAGS"
    nxt_feature_test="
        #include <wasm.h>
        #include <wasi.h>
        #include <wasmtime.h>

        int main(void) {
            wasm_config_t *c;

            c = wasm_config_new();
            wasm_config_delete(c);

            return 0;
        }"

    . auto/feature
fi

if [ $nxt_found = no ]; then
    $echo
    $echo $0: error: no $NXT_WASM_RUNTIME found.
    $echo
    exit 1;
fi


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


$echo " + WASM module: ${NXT_WASM_MODULE}.unit.so"

. auto/cc/deps

$echo >> $NXT_MAKEFILE

NXT_WASM_MODULE_SRCS=" \
    src/wasm/nxt_wasm.c \
"

if [ "$NXT_WASM_RUNTIME" = "wasmtime" ]; then
    NXT_WASM_MODULE_SRCS="$NXT_WASM_MODULE_SRCS src/wasm/nxt_rt_wasmtime.c"
fi


# The wasm module object files.

nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o

for nxt_src in $NXT_WASM_MODULE_SRCS; do

    nxt_obj=${nxt_src%.c}-$NXT_WASM_MODULE.o
    nxt_dep=${nxt_src%.c}-$NXT_WASM_MODULE.dep
    nxt_dep_flags=`nxt_gen_dep_flags`
    nxt_dep_post=`nxt_gen_dep_post`
    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"

    cat << END >> $NXT_MAKEFILE

$NXT_BUILD_DIR/$nxt_obj:	$nxt_src $NXT_VERSION_H
	mkdir -p $NXT_BUILD_DIR/src/wasm
	\$(CC) -c \$(CFLAGS) $NXT_WASM_ADDITIONAL_FLAGS \$(NXT_INCS) \\
	    -I$NXT_WASM_INCLUDE_PATH \\
	    $nxt_dep_flags \\
	    -o $NXT_BUILD_DIR/$nxt_obj $nxt_src
	$nxt_dep_post

-include $NXT_BUILD_DIR/$nxt_dep

END

done


cat << END >> $NXT_MAKEFILE

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

all: ${NXT_WASM_MODULE}

${NXT_WASM_MODULE}:	$NXT_BUILD_DIR/lib/unit/modules/${NXT_WASM_MODULE}.unit.so

$NXT_BUILD_DIR/lib/unit/modules/${NXT_WASM_MODULE}.unit.so:	$nxt_objs
	\$(NXT_MODULE_LINK) -o \$@ \\
	    $nxt_objs -L${NXT_WASM_LIB_PATH} ${NXT_WASM_LDFLAGS} $NXT_LD_OPT


install: ${NXT_WASM_MODULE}-install

${NXT_WASM_MODULE}-install: ${NXT_WASM_MODULE} install-check
	install -d \$(DESTDIR)$NXT_MODULESDIR
	install -p $NXT_BUILD_DIR/lib/unit/modules/${NXT_WASM_MODULE}.unit.so \\
		\$(DESTDIR)$NXT_MODULESDIR/


uninstall: ${NXT_WASM_MODULE}-uninstall

${NXT_WASM_MODULE}-uninstall:
	rm -f \$(DESTDIR)$NXT_MODULESDIR/${NXT_WASM_MODULE}.unit.so
	@rmdir -p \$(DESTDIR)$NXT_MODULESDIR 2>/dev/null || true

END