xref: /unit/auto/modules/ruby (revision 1537:b472ba224472)
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 -r rbconfig -e 'printf("%s",RbConfig::CONFIG["rubyhdrdir"])'`
60    NXT_RUBY_ARCHHDRDIR=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["rubyarchhdrdir"])'`
61    NXT_RUBY_SITEARCHDIR=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["sitearchhdrdir"])'`
62    NXT_RUBY_SITEDIR=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["sitedir"])'`
63    NXT_RUBY_LIBDIR=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["rubylibdir"])'`
64    NXT_RUBY_TOPDIR=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["topdir"])'`
65    NXT_RUBY_PREFIXDIR=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["rubylibprefix"])'`
66    NXT_RUBY_GEMDIR=`gem environment gemdir`
67    NXT_RUBY_GEMPATH=`gem environment gempath`
68
69    NXT_RUBY_INCPATH="-I$NXT_RUBY_ARCHHDRDIR -I$NXT_RUBY_RUBYHDRDIR"
70
71    NXT_RUBY_LIBNAME=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["RUBY_SO_NAME"])'`
72    NXT_RUBY_LIBSCONF=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["LIBS"])'`
73    NXT_RUBY_LIBPATH=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["libdir"])'`
74    NXT_RUBY_LIBS="-l$NXT_RUBY_LIBNAME $NXT_RUBY_LIBSCONF"
75
76    nxt_feature="Ruby library"
77    nxt_feature_name=""
78    nxt_feature_run=value
79    nxt_feature_incs="${NXT_RUBY_INCPATH}"
80    nxt_feature_libs="${NXT_RUBY_LIBS}"
81    nxt_feature_test="
82        #include <ruby.h>
83
84        int main() {
85            static const char *argv[3] = {
86                \"NGINX_Unit\", \"-rrbconfig\",
87                \"-eprint RbConfig::CONFIG['libdir']\"
88            };
89
90            RUBY_INIT_STACK;
91            ruby_init();
92            return ruby_run_node(ruby_options(3, (char **) argv));
93        }"
94
95    . auto/feature
96
97    if [ "$nxt_feature_value" != "$NXT_RUBY_LIBPATH" ]; then
98        NXT_RUBY_LIBS="-L$NXT_RUBY_LIBPATH -Wl,-rpath,${NXT_RUBY_LIBPATH} $NXT_RUBY_LIBS"
99
100        nxt_feature="Ruby library in $NXT_RUBY_LIBPATH"
101        nxt_feature_name=""
102        nxt_feature_run=no
103        nxt_feature_incs="${NXT_RUBY_INCPATH}"
104        nxt_feature_libs="${NXT_RUBY_LIBS}"
105        nxt_feature_test="
106            #include <ruby.h>
107
108            int main() {
109                ruby_init();
110                return ruby_cleanup(0);
111            }"
112
113        . auto/feature
114    fi
115
116else
117    $echo "checking for Ruby ... not found"
118fi
119
120if [ $nxt_found = no ]; then
121    $echo
122    $echo $0: error: no Ruby found.
123    $echo
124    exit 1;
125fi
126
127
128nxt_feature="Ruby version"
129nxt_feature_name=""
130nxt_feature_run=value
131nxt_feature_incs="${NXT_RUBY_INCPATH}"
132nxt_feature_libs="${NXT_RUBY_LIBS}"
133nxt_feature_test="
134    #include <ruby.h>
135    #include <ruby/version.h>
136
137    int main() {
138        printf(\"%s\", ruby_version);
139        return 0;
140    }"
141
142. auto/feature
143
144
145if grep ^$NXT_RUBY_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
146    $echo
147    $echo $0: error: duplicate \"$NXT_RUBY_MODULE\" module configured.
148    $echo
149    exit 1;
150fi
151
152
153NXT_RUBY_MOUNTS_HEADER=nxt_${NXT_RUBY_MODULE}_mounts.h
154NXT_RUBY_MOUNTS_PATH=$NXT_BUILD_DIR/$NXT_RUBY_MOUNTS_HEADER
155
156cat << END > $NXT_RUBY_MOUNTS_PATH
157
158static const nxt_fs_mount_t  nxt_ruby_mounts[] = {
159    {(u_char *) "$NXT_RUBY_RUBYHDRDIR", (u_char *) "$NXT_RUBY_RUBYHDRDIR",
160     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
161    {(u_char *) "$NXT_RUBY_ARCHHDRDIR", (u_char *) "$NXT_RUBY_ARCHHDRDIR",
162     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
163    {(u_char *) "$NXT_RUBY_SITEDIR", (u_char *) "$NXT_RUBY_SITEDIR",
164     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
165    {(u_char *) "$NXT_RUBY_LIBDIR", (u_char *) "$NXT_RUBY_LIBDIR",
166     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
167    {(u_char *) "$NXT_RUBY_GEMDIR", (u_char *) "$NXT_RUBY_GEMDIR",
168     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
169    {(u_char *) "$NXT_RUBY_TOPDIR", (u_char *) "$NXT_RUBY_TOPDIR",
170     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
171    {(u_char *) "$NXT_RUBY_PREFIXDIR", (u_char *) "$NXT_RUBY_PREFIXDIR",
172     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
173
174END
175
176for path in `echo $NXT_RUBY_GEMPATH | tr ':' '\n'`; do
177   $echo "{(u_char *) \"$path\", (u_char *) \"$path\"," >> $NXT_RUBY_MOUNTS_PATH
178   $echo "(u_char *) \"bind\", NXT_MS_BIND | NXT_MS_REC, NULL}," >> $NXT_RUBY_MOUNTS_PATH
179done
180
181$echo "};" >> $NXT_RUBY_MOUNTS_PATH
182
183
184$echo " + Ruby module: ${NXT_RUBY_MODULE}.unit.so"
185
186. auto/cc/deps
187
188$echo >> $NXT_MAKEFILE
189
190NXT_RUBY_MODULE_SRCS=" \
191    src/ruby/nxt_ruby.c \
192    src/ruby/nxt_ruby_stream_io.c
193"
194
195# The Ruby module object files.
196
197nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o
198
199for nxt_src in $NXT_RUBY_MODULE_SRCS; do
200
201    nxt_obj=${nxt_src%.c}-$NXT_RUBY_MODULE.o
202    nxt_dep=${nxt_src%.c}-$NXT_RUBY_MODULE.dep
203    nxt_dep_flags=`nxt_gen_dep_flags`
204    nxt_dep_post=`nxt_gen_dep_post`
205    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
206
207    cat << END >> $NXT_MAKEFILE
208
209$NXT_BUILD_DIR/$nxt_obj:	$nxt_src $NXT_VERSION_H
210	mkdir -p $NXT_BUILD_DIR/src/ruby
211	\$(CC) -c \$(CFLAGS) -DNXT_RUBY_MOUNTS_H=\"$NXT_RUBY_MOUNTS_HEADER\" \\
212           \$(NXT_INCS) $NXT_RUBY_INCPATH \\
213	$nxt_dep_flags \\
214	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
215	$nxt_dep_post
216
217-include $NXT_BUILD_DIR/$nxt_dep
218
219END
220
221done
222
223cat << END >> $NXT_MAKEFILE
224
225.PHONY: ${NXT_RUBY_MODULE}
226.PHONY: ${NXT_RUBY_MODULE}-install
227.PHONY: ${NXT_RUBY_MODULE}-uninstall
228
229all: ${NXT_RUBY_MODULE}
230
231${NXT_RUBY_MODULE}:	$NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so
232
233$NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so:	$nxt_objs
234	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so \\
235	$nxt_objs $NXT_RUBY_LIBS $NXT_LD_OPT
236
237
238install: ${NXT_RUBY_MODULE}-install
239
240${NXT_RUBY_MODULE}-install: ${NXT_RUBY_MODULE} install-check
241	install -d \$(DESTDIR)$NXT_MODULES
242	install -p $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so \\
243		\$(DESTDIR)$NXT_MODULES/
244
245
246uninstall: ${NXT_RUBY_MODULE}-uninstall
247
248${NXT_RUBY_MODULE}-uninstall:
249	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_RUBY_MODULE}.unit.so
250	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
251
252END
253