xref: /unit/auto/modules/php (revision 1583:0d343e154c46)
1
2# Copyright (C) Max Romanov
3# Copyright (C) Igor Sysoev
4# Copyright (C) NGINX, Inc.
5
6
7shift
8
9for 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        --config=*)    NXT_PHP_CONFIG="$value"                               ;;
18        --module=*)    NXT_PHP_MODULE="$value"                               ;;
19        --lib-path=*)  NXT_PHP_LIB_PATH="$value"                             ;;
20        --lib-static)  NXT_PHP_LIB_STATIC=yes                                ;;
21
22        --help)
23            cat << END
24
25    --config=FILE         set php-config filename
26    --module=NAME         set unit php module name
27    --lib-path=DIRECTORY  set directory path to libphp.so library
28    --lib-static          enable linking with static libphp.a library
29
30END
31            exit 0
32        ;;
33
34        *)
35            echo
36            echo $0: error: invalid PHP option \"$nxt_option\"
37            echo
38            exit 1
39        ;;
40
41    esac
42
43done
44
45
46if [ ! -f $NXT_AUTOCONF_DATA ]; then
47   echo
48   echo Please run common $0 before configuring module \"$nxt_module\".
49   echo
50   exit 1
51fi
52
53. $NXT_AUTOCONF_DATA
54
55
56NXT_PHP_CONFIG=${NXT_PHP_CONFIG=php-config}
57NXT_PHP=${NXT_PHP_CONFIG%-config*}
58NXT_PHP_MODULE=${NXT_PHP_MODULE=${NXT_PHP##*/}}
59NXT_PHP_LIB_PATH=${NXT_PHP_LIB_PATH=}
60NXT_PHP_LIB_STATIC=${NXT_PHP_LIB_STATIC=no}
61NXT_PHP_ADDITIONAL_FLAGS=
62NXT_PHP_REALPATH=realpath
63
64
65if [ -z `which $NXT_PHP_REALPATH` ]; then
66    NXT_PHP_REALPATH="readlink -e"
67fi
68
69
70$echo "configuring PHP module"
71$echo "configuring PHP module ..." >> $NXT_AUTOCONF_ERR
72
73$echo -n "checking for PHP ..."
74$echo "checking for PHP ..." >> $NXT_AUTOCONF_ERR
75
76NXT_PHP_LDFLAGS=
77
78if /bin/sh -c "${NXT_PHP_CONFIG} --version" >> $NXT_AUTOCONF_ERR 2>&1; then
79
80    $echo " found"
81
82    NXT_PHP_VERSION="`${NXT_PHP_CONFIG} --version`"
83    NXT_PHP_EXT_DIR="`${NXT_PHP_CONFIG} --extension-dir`"
84    NXT_PHP_LIBC_DIR="`${CC} --print-file-name=libc.so`"
85    NXT_PHP_LIBC_DIR="`$NXT_PHP_REALPATH $NXT_PHP_LIBC_DIR`"
86    NXT_PHP_LIBC_DIR="`dirname $NXT_PHP_LIBC_DIR`"
87    NXT_PHP_SYSLIB_DIR="`${CC} --print-file-name=libtinfo.so`"
88    NXT_PHP_SYSLIB_DIR="`$NXT_PHP_REALPATH $NXT_PHP_SYSLIB_DIR`"
89    NXT_PHP_SYSLIB_DIR="`dirname $NXT_PHP_SYSLIB_DIR`"
90
91    $echo " + PHP SAPI: [`${NXT_PHP_CONFIG} --php-sapis`]"
92
93    NXT_PHP_MAJOR_VERSION=${NXT_PHP_VERSION%%.*}
94    NXT_PHP_MINOR_VERSION=${NXT_PHP_VERSION#*.}
95    NXT_PHP_MINOR_VERSION=${NXT_PHP_MINOR_VERSION%%.*}
96
97    if [ $NXT_PHP_MAJOR_VERSION = 5 -a $NXT_PHP_MINOR_VERSION -lt 4 ]; then
98        NXT_PHP_ADDITIONAL_FLAGS=-Wno-write-strings
99    fi
100
101    NXT_PHP_INCLUDE="`${NXT_PHP_CONFIG} --includes`"
102
103    if [ $NXT_PHP_LIB_STATIC = yes ]; then
104
105        if [ "$NXT_PHP_LIB_PATH" = "" ]; then
106            $echo
107            $echo $0: error: --lib-static option requires --lib-path option.
108            $echo
109            exit 1;
110        fi
111
112        NXT_PHP_LIB="$NXT_PHP_LIB_PATH/libphp${NXT_PHP_VERSION%%.*}.a"
113        NXT_PHP_LDFLAGS="`${NXT_PHP_CONFIG} --ldflags` \
114                         `${NXT_PHP_CONFIG} --libs`"
115
116    else
117        if [ $NXT_PHP_MAJOR_VERSION -ge 8 ]; then
118            NXT_PHP_LIB="-lphp"
119        else
120            NXT_PHP_LIB="-lphp${NXT_PHP_VERSION%%.*}"
121        fi
122
123        if [ "$NXT_PHP_LIB_PATH" != "" ]; then
124            # "php-config --ldflags" does not contain path to libphp, but
125            # contains usually path to libraries required by extensions.
126            NXT_PHP_LDFLAGS="-L${NXT_PHP_LIB_PATH} \
127                             -Wl,-rpath ${NXT_PHP_LIB_PATH}"
128        fi
129    fi
130
131else
132    $echo
133    $echo $0: error: no PHP found.
134    $echo
135    exit 1;
136fi
137
138
139nxt_feature="PHP version"
140nxt_feature_name=""
141nxt_feature_run=value
142nxt_feature_incs="${NXT_PHP_INCLUDE}"
143nxt_feature_libs="${NXT_PHP_LIB} ${NXT_PHP_LDFLAGS}"
144nxt_feature_test="
145    #include <php.h>
146
147    int main() {
148        printf(\"%s\", PHP_VERSION);
149        return 0;
150    }"
151
152. auto/feature
153
154
155nxt_feature="PHP embed SAPI"
156nxt_feature_name=""
157nxt_feature_run=no
158nxt_feature_incs="${NXT_PHP_INCLUDE}"
159nxt_feature_libs="${NXT_PHP_LIB} ${NXT_PHP_LDFLAGS}"
160nxt_feature_test="
161    #include <php.h>
162    #include <php_main.h>
163
164    int main() {
165        php_module_startup(NULL, NULL, 0);
166        return 0;
167    }"
168
169. auto/feature
170
171if [ $nxt_found = no ]; then
172    $echo
173    $echo $0: error: no PHP embed SAPI found.
174    $echo
175    exit 1;
176fi
177
178
179nxt_feature="PHP Zend Thread Safety"
180nxt_feature_name=""
181nxt_feature_run=no
182nxt_feature_incs="${NXT_PHP_INCLUDE}"
183nxt_feature_libs="${NXT_PHP_LIB} ${NXT_PHP_LDFLAGS}"
184nxt_feature_test="
185    #include <php.h>
186    #include <php_main.h>
187
188    int main() {
189        #ifndef ZTS
190        #error ZTS is not defined.
191        #endif
192        return 0;
193    }"
194
195. auto/feature
196
197
198# Bug #71041 (https://bugs.php.net/bug.php?id=71041).
199
200nxt_feature="PHP zend_signal_startup()"
201nxt_feature_name=""
202nxt_feature_run=no
203nxt_feature_incs="${NXT_PHP_INCLUDE}"
204nxt_feature_libs="${NXT_PHP_LIB} ${NXT_PHP_LDFLAGS}"
205nxt_feature_test="
206    #include <php.h>
207    #include <php_main.h>
208
209    int main() {
210        zend_signal_startup();
211        return 0;
212    }"
213
214. auto/feature
215
216if [ $nxt_found = yes ]; then
217    NXT_ZEND_SIGNAL_STARTUP=1
218else
219    NXT_ZEND_SIGNAL_STARTUP=0
220fi
221
222
223if grep ^$NXT_PHP_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
224    $echo
225    $echo $0: error: duplicate \"$NXT_PHP_MODULE\" module configured.
226    $echo
227    exit 1;
228fi
229
230
231NXT_PHP_MOUNTS_HEADER=nxt_${NXT_PHP_MODULE}_mounts.h
232
233cat << END > $NXT_BUILD_DIR/$NXT_PHP_MOUNTS_HEADER
234static const nxt_fs_mount_t  nxt_php_mounts[] = {
235    {(u_char *) "$NXT_PHP_EXT_DIR", (u_char *) "$NXT_PHP_EXT_DIR",
236     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
237    {(u_char *) "$NXT_PHP_LIBC_DIR", (u_char *) "$NXT_PHP_LIBC_DIR",
238     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
239    {(u_char *) "$NXT_PHP_SYSLIB_DIR", (u_char *) "$NXT_PHP_SYSLIB_DIR",
240     (u_char *) "bind", NXT_MS_BIND | NXT_MS_REC, NULL},
241};
242
243END
244
245
246$echo " + PHP module: ${NXT_PHP_MODULE}.unit.so"
247
248. auto/cc/deps
249
250$echo >> $NXT_MAKEFILE
251
252NXT_PHP_MODULE_SRCS=" \
253    src/nxt_php_sapi.c \
254"
255
256# The php module object files.
257
258nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o
259
260for nxt_src in $NXT_PHP_MODULE_SRCS; do
261
262    nxt_obj=${nxt_src%.c}-$NXT_PHP_MODULE.o
263    nxt_dep=${nxt_src%.c}-$NXT_PHP_MODULE.dep
264    nxt_dep_flags=`nxt_gen_dep_flags`
265    nxt_dep_post=`nxt_gen_dep_post`
266    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
267
268    cat << END >> $NXT_MAKEFILE
269
270$NXT_BUILD_DIR/$nxt_obj:	$nxt_src $NXT_VERSION_H
271	\$(CC) -c \$(CFLAGS) -DNXT_PHP_MOUNTS_H=\"$NXT_PHP_MOUNTS_HEADER\" \\
272    $NXT_PHP_ADDITIONAL_FLAGS \$(NXT_INCS) \\
273	$NXT_PHP_INCLUDE -DNXT_ZEND_SIGNAL_STARTUP=$NXT_ZEND_SIGNAL_STARTUP \\
274	$nxt_dep_flags \\
275	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
276	$nxt_dep_post
277
278-include $NXT_BUILD_DIR/$nxt_dep
279
280END
281
282done
283
284
285cat << END >> $NXT_MAKEFILE
286
287.PHONY: ${NXT_PHP_MODULE}
288.PHONY: ${NXT_PHP_MODULE}-install
289.PHONY: ${NXT_PHP_MODULE}-uninstall
290
291all: ${NXT_PHP_MODULE}
292
293${NXT_PHP_MODULE}:	$NXT_BUILD_DIR/${NXT_PHP_MODULE}.unit.so
294
295$NXT_BUILD_DIR/${NXT_PHP_MODULE}.unit.so:	$nxt_objs
296	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_PHP_MODULE}.unit.so \\
297	$nxt_objs ${NXT_PHP_LIB} ${NXT_PHP_LDFLAGS} $NXT_LD_OPT
298
299
300install: ${NXT_PHP_MODULE}-install
301
302${NXT_PHP_MODULE}-install: ${NXT_PHP_MODULE} install-check
303	install -d \$(DESTDIR)$NXT_MODULES
304	install -p $NXT_BUILD_DIR/${NXT_PHP_MODULE}.unit.so \\
305		\$(DESTDIR)$NXT_MODULES/
306
307
308uninstall: ${NXT_PHP_MODULE}-uninstall
309
310${NXT_PHP_MODULE}-uninstall:
311	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_PHP_MODULE}.unit.so
312	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
313
314END
315