xref: /unit/auto/modules/python (revision 1237:59eaa56b8f5c)
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 2>/dev/null; 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$echo " + Python module: ${NXT_PYTHON_MODULE}.unit.so"
133
134. auto/cc/deps
135
136$echo >> $NXT_MAKEFILE
137
138NXT_PYTHON_MODULE_SRCS=" \
139    src/nxt_python_wsgi.c \
140"
141
142# The python module object files.
143
144nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o
145
146for nxt_src in $NXT_PYTHON_MODULE_SRCS; do
147
148    nxt_obj=${nxt_src%.c}-$NXT_PYTHON_MODULE.o
149    nxt_dep=${nxt_src%.c}-$NXT_PYTHON_MODULE.dep
150    nxt_dep_flags=`nxt_gen_dep_flags`
151    nxt_dep_post=`nxt_gen_dep_post`
152    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
153
154    cat << END >> $NXT_MAKEFILE
155
156$NXT_BUILD_DIR/$nxt_obj:	$nxt_src $NXT_VERSION_H
157	\$(CC) -c \$(CFLAGS) \$(NXT_INCS) $NXT_PYTHON_INCLUDE \\
158	$nxt_dep_flags \\
159	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
160	$nxt_dep_post
161
162-include $NXT_BUILD_DIR/$nxt_dep
163
164END
165
166done
167
168
169cat << END >> $NXT_MAKEFILE
170
171.PHONY: ${NXT_PYTHON_MODULE}
172.PHONY: ${NXT_PYTHON_MODULE}-install
173.PHONY: ${NXT_PYTHON_MODULE}-uninstall
174
175all: ${NXT_PYTHON_MODULE}
176
177${NXT_PYTHON_MODULE}:	$NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so
178
179$NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so:	$nxt_objs
180	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so \\
181	$nxt_objs $NXT_PYTHON_LIBS $NXT_PYTHON_LDFLAGS $NXT_LD_OPT
182
183
184install: ${NXT_PYTHON_MODULE}-install
185
186${NXT_PYTHON_MODULE}-install: ${NXT_PYTHON_MODULE} install-check
187	install -d \$(DESTDIR)$NXT_MODULES
188	install -p $NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so \\
189		\$(DESTDIR)$NXT_MODULES/
190
191
192uninstall: ${NXT_PYTHON_MODULE}-uninstall
193
194${NXT_PYTHON_MODULE}-uninstall:
195	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_PYTHON_MODULE}.unit.so
196	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
197
198END
199