xref: /unit/auto/modules/ruby (revision 1585:e941d77852d1)
1
2# Copyright (C) Alexander Borisov
3# Copyright (C) NGINX, Inc.
4
5
6shift
7
8for nxt_option; do
9
10    case "$nxt_option" in
11        -*=*) value=`echo "$nxt_option" | sed -e 's/[-_a-zA-Z0-9]*=//'`     ;;
12           *) value="" ;;
13    esac
14
15    case "$nxt_option" in
16        --ruby=*)    NXT_RUBY="$value"                                    ;;
17        --module=*)  NXT_RUBY_MODULE="$value"                             ;;
18
19        --help)
20            cat << END
21
22    --ruby=FILE    set ruby executable, default: ruby
23    --module=NAME  set unit ruby module name
24
25END
26            exit 0
27        ;;
28
29        *)
30            echo
31            echo $0: error: invalid Ruby option \"$nxt_option\"
32            echo
33            exit 1
34        ;;
35    esac
36
37done
38
39
40if [ ! -f $NXT_AUTOCONF_DATA ]; then
41   echo
42   echo Please run common $0 before configuring module \"$nxt_module\".
43   echo
44   exit 1
45fi
46
47. $NXT_AUTOCONF_DATA
48
49$echo "configuring Ruby module"
50$echo "configuring Ruby module ..." >> $NXT_AUTOCONF_ERR
51
52NXT_RUBY=${NXT_RUBY=ruby}
53NXT_RUBY_MODULE=${NXT_RUBY_MODULE=${NXT_RUBY}}
54
55nxt_found=no
56
57if /bin/sh -c "$NXT_RUBY -v" >> $NXT_AUTOCONF_ERR 2>&1; then
58
59    NXT_RUBY_RUBYHDRDIR=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["rubyhdrdir"]'`
60    NXT_RUBY_ARCHHDRDIR=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["rubyarchhdrdir"]'`
61    NXT_RUBY_SITEDIR=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["sitedir"]'`
62    NXT_RUBY_LIBDIR=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["rubylibdir"]'`
63    NXT_RUBY_TOPDIR=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["topdir"]'`
64    NXT_RUBY_PREFIXDIR=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["rubylibprefix"]'`
65
66    NXT_RUBY_GEMPATH=`$NXT_RUBY -rrubygems -e 'print Gem.default_path().join(":")'`
67
68    NXT_RUBY_INCPATH="-I$NXT_RUBY_ARCHHDRDIR -I$NXT_RUBY_RUBYHDRDIR"
69
70    NXT_RUBY_LIBNAME=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["RUBY_SO_NAME"]'`
71    NXT_RUBY_LIBSCONF=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["LIBS"]'`
72    NXT_RUBY_LIBPATH=`$NXT_RUBY -rrbconfig -e 'print RbConfig::CONFIG["libdir"]'`
73    NXT_RUBY_LIBS="-l$NXT_RUBY_LIBNAME $NXT_RUBY_LIBSCONF"
74
75    nxt_feature="Ruby library"
76    nxt_feature_name=""
77    nxt_feature_run=value
78    nxt_feature_incs="${NXT_RUBY_INCPATH}"
79    nxt_feature_libs="${NXT_RUBY_LIBS}"
80    nxt_feature_test="
81        #include <ruby.h>
82
83        int main() {
84            static const char *argv[3] = {
85                \"NGINX_Unit\", \"-rrbconfig\",
86                \"-eprint RbConfig::CONFIG['libdir']\"
87            };
88
89            RUBY_INIT_STACK;
90            ruby_init();
91            return ruby_run_node(ruby_options(3, (char **) argv));
92        }"
93
94    . auto/feature
95
96    if [ "$nxt_feature_value" != "$NXT_RUBY_LIBPATH" ]; then
97        NXT_RUBY_LIBS="-L$NXT_RUBY_LIBPATH -Wl,-rpath,${NXT_RUBY_LIBPATH} $NXT_RUBY_LIBS"
98
99        nxt_feature="Ruby library in $NXT_RUBY_LIBPATH"
100        nxt_feature_name=""
101        nxt_feature_run=no
102        nxt_feature_incs="${NXT_RUBY_INCPATH}"
103        nxt_feature_libs="${NXT_RUBY_LIBS}"
104        nxt_feature_test="
105            #include <ruby.h>
106
107            int main() {
108                ruby_init();
109                return ruby_cleanup(0);
110            }"
111
112        . auto/feature
113    fi
114
115else
116    $echo "checking for Ruby ... not found"
117fi
118
119if [ $nxt_found = no ]; then
120    $echo
121    $echo $0: error: no Ruby found.
122    $echo
123    exit 1;
124fi
125
126
127nxt_feature="Ruby version"
128nxt_feature_name=""
129nxt_feature_run=value
130nxt_feature_incs="${NXT_RUBY_INCPATH}"
131nxt_feature_libs="${NXT_RUBY_LIBS}"
132nxt_feature_test="
133    #include <ruby.h>
134    #include <ruby/version.h>
135
136    int main() {
137        printf(\"%s\", ruby_version);
138        return 0;
139    }"
140
141. auto/feature
142
143
144if grep ^$NXT_RUBY_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
145    $echo
146    $echo $0: error: duplicate \"$NXT_RUBY_MODULE\" module configured.
147    $echo
148    exit 1;
149fi
150
151
152NXT_RUBY_MOUNTS_HEADER=nxt_${NXT_RUBY_MODULE}_mounts.h
153NXT_RUBY_MOUNTS_PATH=$NXT_BUILD_DIR/$NXT_RUBY_MOUNTS_HEADER
154
155cat << END > $NXT_RUBY_MOUNTS_PATH
156
157static const nxt_fs_mount_t  nxt_ruby_mounts[] = {
158    {(u_char *) "$NXT_RUBY_RUBYHDRDIR", (u_char *) "$NXT_RUBY_RUBYHDRDIR",
159     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL, 1},
160    {(u_char *) "$NXT_RUBY_ARCHHDRDIR", (u_char *) "$NXT_RUBY_ARCHHDRDIR",
161     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL, 1},
162    {(u_char *) "$NXT_RUBY_SITEDIR", (u_char *) "$NXT_RUBY_SITEDIR",
163     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL, 1},
164    {(u_char *) "$NXT_RUBY_LIBDIR", (u_char *) "$NXT_RUBY_LIBDIR",
165     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL, 1},
166    {(u_char *) "$NXT_RUBY_TOPDIR", (u_char *) "$NXT_RUBY_TOPDIR",
167     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL, 1},
168    {(u_char *) "$NXT_RUBY_PREFIXDIR", (u_char *) "$NXT_RUBY_PREFIXDIR",
169     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL, 1},
170
171END
172
173for path in `echo $NXT_RUBY_GEMPATH | tr ':' '\n'`; do
174   $echo "{(u_char *) \"$path\", (u_char *) \"$path\"," >> $NXT_RUBY_MOUNTS_PATH
175   $echo "(u_char *) \"bind\", NXT_MS_BIND | NXT_MS_REC, NULL, 1}," >> $NXT_RUBY_MOUNTS_PATH
176done
177
178$echo "};" >> $NXT_RUBY_MOUNTS_PATH
179
180
181$echo " + Ruby module: ${NXT_RUBY_MODULE}.unit.so"
182
183. auto/cc/deps
184
185$echo >> $NXT_MAKEFILE
186
187NXT_RUBY_MODULE_SRCS=" \
188    src/ruby/nxt_ruby.c \
189    src/ruby/nxt_ruby_stream_io.c
190"
191
192# The Ruby module object files.
193
194nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o
195
196for nxt_src in $NXT_RUBY_MODULE_SRCS; do
197
198    nxt_obj=${nxt_src%.c}-$NXT_RUBY_MODULE.o
199    nxt_dep=${nxt_src%.c}-$NXT_RUBY_MODULE.dep
200    nxt_dep_flags=`nxt_gen_dep_flags`
201    nxt_dep_post=`nxt_gen_dep_post`
202    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
203
204    cat << END >> $NXT_MAKEFILE
205
206$NXT_BUILD_DIR/$nxt_obj:	$nxt_src $NXT_VERSION_H
207	mkdir -p $NXT_BUILD_DIR/src/ruby
208	\$(CC) -c \$(CFLAGS) -DNXT_RUBY_MOUNTS_H=\"$NXT_RUBY_MOUNTS_HEADER\" \\
209           \$(NXT_INCS) $NXT_RUBY_INCPATH \\
210	$nxt_dep_flags \\
211	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
212	$nxt_dep_post
213
214-include $NXT_BUILD_DIR/$nxt_dep
215
216END
217
218done
219
220cat << END >> $NXT_MAKEFILE
221
222.PHONY: ${NXT_RUBY_MODULE}
223.PHONY: ${NXT_RUBY_MODULE}-install
224.PHONY: ${NXT_RUBY_MODULE}-uninstall
225
226all: ${NXT_RUBY_MODULE}
227
228${NXT_RUBY_MODULE}:	$NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so
229
230$NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so:	$nxt_objs
231	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so \\
232	$nxt_objs $NXT_RUBY_LIBS $NXT_LD_OPT
233
234
235install: ${NXT_RUBY_MODULE}-install
236
237${NXT_RUBY_MODULE}-install: ${NXT_RUBY_MODULE} install-check
238	install -d \$(DESTDIR)$NXT_MODULES
239	install -p $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so \\
240		\$(DESTDIR)$NXT_MODULES/
241
242
243uninstall: ${NXT_RUBY_MODULE}-uninstall
244
245${NXT_RUBY_MODULE}-uninstall:
246	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_RUBY_MODULE}.unit.so
247	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
248
249END
250