xref: /unit/auto/modules/python (revision 1592:9727c9b61f1c)
1
2# Copyright (C) Valentin V. Bartenev
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
18        --config=*)    NXT_PYTHON_CONFIG="$value"                            ;;
19        --module=*)    NXT_PYTHON_MODULE="$value"                            ;;
20        --lib-path=*)  NXT_PYTHON_LIB_PATH="$value"                          ;;
21
22        --help)
23            cat << END
24
25    --config=FILE         set python-config filename
26    --module=NAME         set unit python module name
27    --lib-path=DIRECTORY  set directory path to libpython.so library
28
29END
30            exit 0
31        ;;
32
33        *)
34            echo
35            echo $0: error: invalid Python option \"$nxt_option\"
36            echo
37            exit 1
38        ;;
39    esac
40
41done
42
43
44if [ ! -f $NXT_AUTOCONF_DATA ]; then
45   echo
46   echo Please run common $0 before configuring module \"$nxt_module\".
47   echo
48   exit 1
49fi
50
51. $NXT_AUTOCONF_DATA
52
53
54NXT_PYTHON_CONFIG=${NXT_PYTHON_CONFIG=python-config}
55NXT_PYTHON=${NXT_PYTHON_CONFIG%-config*}
56NXT_PYTHON_MODULE=${NXT_PYTHON_MODULE=${NXT_PYTHON##*/}}
57NXT_PYTHON_LIB_PATH=${NXT_PYTHON_LIB_PATH=}
58
59
60$echo "configuring Python module"
61$echo "configuring Python module ..." >> $NXT_AUTOCONF_ERR
62
63nxt_found=no
64
65if /bin/sh -c "$NXT_PYTHON_CONFIG --prefix" >> $NXT_AUTOCONF_ERR 2>&1; then
66
67    if ${NXT_PYTHON_CONFIG} --embed >/dev/null 2>&1; then
68        NXT_PYTHON_CONFIG="${NXT_PYTHON_CONFIG} --embed"
69    fi
70
71    NXT_PYTHON_INCLUDE=`${NXT_PYTHON_CONFIG} --includes`
72    NXT_PYTHON_LIBS=`${NXT_PYTHON_CONFIG} --ldflags`
73
74    if [ "$NXT_PYTHON_LIB_PATH" != "" ]; then
75        # "python-config --ldflags" may not contain path to libpython.
76        NXT_PYTHON_LDFLAGS="-L$NXT_PYTHON_LIB_PATH -Wl,-rpath $NXT_PYTHON_LIB_PATH"
77    else
78        NXT_PYTHON_LDFLAGS=""
79    fi
80
81    nxt_feature="Python"
82    nxt_feature_name=""
83    nxt_feature_run=no
84    nxt_feature_incs="${NXT_PYTHON_INCLUDE}"
85    nxt_feature_libs="${NXT_PYTHON_LIBS} $NXT_PYTHON_LDFLAGS"
86    nxt_feature_test="
87        #include <Python.h>
88
89        int main() {
90            Py_Initialize();
91            return 0;
92        }"
93
94    . auto/feature
95
96else
97    $echo "checking for Python ... not found"
98fi
99
100if [ $nxt_found = no ]; then
101    $echo
102    $echo $0: error: no Python found.
103    $echo
104    exit 1;
105fi
106
107
108nxt_feature="Python version"
109nxt_feature_name=""
110nxt_feature_run=value
111nxt_feature_incs="${NXT_PYTHON_INCLUDE}"
112nxt_feature_libs="${NXT_PYTHON_LIBS} $NXT_PYTHON_LDFLAGS"
113nxt_feature_test="
114    #include <Python.h>
115    #include <stdio.h>
116
117    int main() {
118        printf(\"%s\", PY_VERSION);
119        return 0;
120    }"
121
122. auto/feature
123
124
125if grep ^$NXT_PYTHON_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
126    $echo
127    $echo $0: error: duplicate \"$NXT_PYTHON_MODULE\" module configured.
128    $echo
129    exit 1;
130fi
131
132
133NXT_PYTHON_MOUNTS_HEADER=nxt_${NXT_PYTHON_MODULE}_mounts.h
134
135$NXT_PYTHON -c 'import os.path
136import sys
137pyver = "python" + str(sys.version_info[0]) + "." + str(sys.version_info[1])
138
139print("static const nxt_fs_mount_t  nxt_python_mounts[] = {")
140
141pattern = "{(u_char *) \"%s\", (u_char *) \"%s\", (u_char *) \"bind\", NXT_MS_BIND|NXT_MS_REC, NULL, 1},"
142base = None
143for p in sys.path:
144    if len(p) > 0:
145        if os.path.basename(p) == pyver:
146            base = p
147
148if base is None:
149    raise Exception("failed to compute sys.path mount points")
150
151print(pattern % (base, base))
152
153for p in sys.path:
154    if len(p) > 0:
155        if not p.startswith(base):
156            print(pattern % (p, p))
157
158print("};\n\n")
159
160' > $NXT_BUILD_DIR/$NXT_PYTHON_MOUNTS_HEADER
161
162
163$echo " + Python module: ${NXT_PYTHON_MODULE}.unit.so"
164
165. auto/cc/deps
166
167$echo >> $NXT_MAKEFILE
168
169NXT_PYTHON_MODULE_SRCS=" \
170    src/python/nxt_python.c \
171    src/python/nxt_python_wsgi.c \
172"
173
174# The python module object files.
175
176nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o
177
178for nxt_src in $NXT_PYTHON_MODULE_SRCS; do
179
180    nxt_obj=${nxt_src%.c}-$NXT_PYTHON_MODULE.o
181    nxt_dep=${nxt_src%.c}-$NXT_PYTHON_MODULE.dep
182    nxt_dep_flags=`nxt_gen_dep_flags`
183    nxt_dep_post=`nxt_gen_dep_post`
184    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
185
186    cat << END >> $NXT_MAKEFILE
187
188$NXT_BUILD_DIR/$nxt_obj:	$nxt_src $NXT_VERSION_H
189	mkdir -p $NXT_BUILD_DIR/src/python
190	\$(CC) -c \$(CFLAGS) -DNXT_PYTHON_MOUNTS_H=\"$NXT_PYTHON_MOUNTS_HEADER\" \\
191           \$(NXT_INCS) $NXT_PYTHON_INCLUDE \\
192	$nxt_dep_flags \\
193	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
194	$nxt_dep_post
195
196-include $NXT_BUILD_DIR/$nxt_dep
197
198END
199
200done
201
202
203cat << END >> $NXT_MAKEFILE
204
205.PHONY: ${NXT_PYTHON_MODULE}
206.PHONY: ${NXT_PYTHON_MODULE}-install
207.PHONY: ${NXT_PYTHON_MODULE}-uninstall
208
209all: ${NXT_PYTHON_MODULE}
210
211${NXT_PYTHON_MODULE}:	$NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so
212
213$NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so:	$nxt_objs
214	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so \\
215	$nxt_objs $NXT_PYTHON_LIBS $NXT_PYTHON_LDFLAGS $NXT_LD_OPT
216
217
218install: ${NXT_PYTHON_MODULE}-install
219
220${NXT_PYTHON_MODULE}-install: ${NXT_PYTHON_MODULE} install-check
221	install -d \$(DESTDIR)$NXT_MODULES
222	install -p $NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so \\
223		\$(DESTDIR)$NXT_MODULES/
224
225
226uninstall: ${NXT_PYTHON_MODULE}-uninstall
227
228${NXT_PYTHON_MODULE}-uninstall:
229	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_PYTHON_MODULE}.unit.so
230	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
231
232END
233