xref: /unit/auto/modules/ruby (revision 1539:33581f923180)
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    NXT_RUBY_GEMDIR=`gem environment gemdir`
66    NXT_RUBY_GEMPATH=`gem environment gempath`
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},
160    {(u_char *) "$NXT_RUBY_ARCHHDRDIR", (u_char *) "$NXT_RUBY_ARCHHDRDIR",
161     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
162    {(u_char *) "$NXT_RUBY_SITEDIR", (u_char *) "$NXT_RUBY_SITEDIR",
163     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
164    {(u_char *) "$NXT_RUBY_LIBDIR", (u_char *) "$NXT_RUBY_LIBDIR",
165     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
166    {(u_char *) "$NXT_RUBY_GEMDIR", (u_char *) "$NXT_RUBY_GEMDIR",
167     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
168    {(u_char *) "$NXT_RUBY_TOPDIR", (u_char *) "$NXT_RUBY_TOPDIR",
169     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
170    {(u_char *) "$NXT_RUBY_PREFIXDIR", (u_char *) "$NXT_RUBY_PREFIXDIR",
171     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
172
173END
174
175for path in `echo $NXT_RUBY_GEMPATH | tr ':' '\n'`; do
176   $echo "{(u_char *) \"$path\", (u_char *) \"$path\"," >> $NXT_RUBY_MOUNTS_PATH
177   $echo "(u_char *) \"bind\", NXT_MS_BIND | NXT_MS_REC, NULL}," >> $NXT_RUBY_MOUNTS_PATH
178done
179
180$echo "};" >> $NXT_RUBY_MOUNTS_PATH
181
182
183$echo " + Ruby module: ${NXT_RUBY_MODULE}.unit.so"
184
185. auto/cc/deps
186
187$echo >> $NXT_MAKEFILE
188
189NXT_RUBY_MODULE_SRCS=" \
190    src/ruby/nxt_ruby.c \
191    src/ruby/nxt_ruby_stream_io.c
192"
193
194# The Ruby module object files.
195
196nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o
197
198for nxt_src in $NXT_RUBY_MODULE_SRCS; do
199
200    nxt_obj=${nxt_src%.c}-$NXT_RUBY_MODULE.o
201    nxt_dep=${nxt_src%.c}-$NXT_RUBY_MODULE.dep
202    nxt_dep_flags=`nxt_gen_dep_flags`
203    nxt_dep_post=`nxt_gen_dep_post`
204    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
205
206    cat << END >> $NXT_MAKEFILE
207
208$NXT_BUILD_DIR/$nxt_obj:	$nxt_src $NXT_VERSION_H
209	mkdir -p $NXT_BUILD_DIR/src/ruby
210	\$(CC) -c \$(CFLAGS) -DNXT_RUBY_MOUNTS_H=\"$NXT_RUBY_MOUNTS_HEADER\" \\
211           \$(NXT_INCS) $NXT_RUBY_INCPATH \\
212	$nxt_dep_flags \\
213	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
214	$nxt_dep_post
215
216-include $NXT_BUILD_DIR/$nxt_dep
217
218END
219
220done
221
222cat << END >> $NXT_MAKEFILE
223
224.PHONY: ${NXT_RUBY_MODULE}
225.PHONY: ${NXT_RUBY_MODULE}-install
226.PHONY: ${NXT_RUBY_MODULE}-uninstall
227
228all: ${NXT_RUBY_MODULE}
229
230${NXT_RUBY_MODULE}:	$NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so
231
232$NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so:	$nxt_objs
233	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so \\
234	$nxt_objs $NXT_RUBY_LIBS $NXT_LD_OPT
235
236
237install: ${NXT_RUBY_MODULE}-install
238
239${NXT_RUBY_MODULE}-install: ${NXT_RUBY_MODULE} install-check
240	install -d \$(DESTDIR)$NXT_MODULES
241	install -p $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so \\
242		\$(DESTDIR)$NXT_MODULES/
243
244
245uninstall: ${NXT_RUBY_MODULE}-uninstall
246
247${NXT_RUBY_MODULE}-uninstall:
248	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_RUBY_MODULE}.unit.so
249	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
250
251END
252