xref: /unit/auto/modules/python (revision 1489:4a3ec07f4b19)
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_EXEC=`${NXT_PYTHON_CONFIG} --exec-prefix`/bin/${NXT_PYTHON}
72    NXT_PYTHON_INCLUDE=`${NXT_PYTHON_CONFIG} --includes`
73    NXT_PYTHON_LIBS=`${NXT_PYTHON_CONFIG} --ldflags`
74
75    if [ "$NXT_PYTHON_LIB_PATH" != "" ]; then
76        # "python-config --ldflags" may not contain path to libpython.
77        NXT_PYTHON_LDFLAGS="-L$NXT_PYTHON_LIB_PATH -Wl,-rpath $NXT_PYTHON_LIB_PATH"
78    else
79        NXT_PYTHON_LDFLAGS=""
80    fi
81
82    nxt_feature="Python"
83    nxt_feature_name=""
84    nxt_feature_run=no
85    nxt_feature_incs="${NXT_PYTHON_INCLUDE}"
86    nxt_feature_libs="${NXT_PYTHON_LIBS} $NXT_PYTHON_LDFLAGS"
87    nxt_feature_test="
88        #include <Python.h>
89
90        int main() {
91            Py_Initialize();
92            return 0;
93        }"
94
95    . auto/feature
96
97else
98    $echo "checking for Python ... not found"
99fi
100
101if [ $nxt_found = no ]; then
102    $echo
103    $echo $0: error: no Python found.
104    $echo
105    exit 1;
106fi
107
108
109nxt_feature="Python version"
110nxt_feature_name=""
111nxt_feature_run=value
112nxt_feature_incs="${NXT_PYTHON_INCLUDE}"
113nxt_feature_libs="${NXT_PYTHON_LIBS} $NXT_PYTHON_LDFLAGS"
114nxt_feature_test="
115    #include <Python.h>
116    #include <stdio.h>
117
118    int main() {
119        printf(\"%s\", PY_VERSION);
120        return 0;
121    }"
122
123. auto/feature
124
125
126if grep ^$NXT_PYTHON_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
127    $echo
128    $echo $0: error: duplicate \"$NXT_PYTHON_MODULE\" module configured.
129    $echo
130    exit 1;
131fi
132
133
134NXT_PYTHON_MOUNTS_HEADER=$NXT_BUILD_DIR/nxt_python_mounts.h
135
136$NXT_PYTHON_EXEC -c 'import os.path
137import sys
138pyver = "python" + str(sys.version_info[0]) + "." + str(sys.version_info[1])
139
140print("static const nxt_fs_mount_t  nxt_python%d%d_mounts[] = {" % (sys.version_info[0], sys.version_info[1]))
141
142pattern = "{(u_char *) \"%s\", (u_char *) \"%s\", (u_char *) \"bind\", NXT_MS_BIND|NXT_MS_REC, NULL},"
143base = None
144for p in sys.path:
145    if len(p) > 0:
146        if os.path.basename(p) == pyver:
147            base = p
148
149if base is None:
150    raise Exception("failed to compute sys.path mount points")
151
152print(pattern % (base, base))
153
154for p in sys.path:
155    if len(p) > 0:
156        if not p.startswith(base):
157            print(pattern % (p, p))
158
159print("};\n\n")
160
161' >> $NXT_PYTHON_MOUNTS_HEADER
162
163
164$echo " + Python module: ${NXT_PYTHON_MODULE}.unit.so"
165
166. auto/cc/deps
167
168$echo >> $NXT_MAKEFILE
169
170NXT_PYTHON_MODULE_SRCS=" \
171    src/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	\$(CC) -c \$(CFLAGS) \$(NXT_INCS) $NXT_PYTHON_INCLUDE \\
190	$nxt_dep_flags \\
191	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
192	$nxt_dep_post
193
194-include $NXT_BUILD_DIR/$nxt_dep
195
196END
197
198done
199
200
201cat << END >> $NXT_MAKEFILE
202
203.PHONY: ${NXT_PYTHON_MODULE}
204.PHONY: ${NXT_PYTHON_MODULE}-install
205.PHONY: ${NXT_PYTHON_MODULE}-uninstall
206
207all: ${NXT_PYTHON_MODULE}
208
209${NXT_PYTHON_MODULE}:	$NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so
210
211$NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so:	$nxt_objs
212	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so \\
213	$nxt_objs $NXT_PYTHON_LIBS $NXT_PYTHON_LDFLAGS $NXT_LD_OPT
214
215
216install: ${NXT_PYTHON_MODULE}-install
217
218${NXT_PYTHON_MODULE}-install: ${NXT_PYTHON_MODULE} install-check
219	install -d \$(DESTDIR)$NXT_MODULES
220	install -p $NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so \\
221		\$(DESTDIR)$NXT_MODULES/
222
223
224uninstall: ${NXT_PYTHON_MODULE}-uninstall
225
226${NXT_PYTHON_MODULE}-uninstall:
227	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_PYTHON_MODULE}.unit.so
228	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
229
230END
231