xref: /unit/auto/modules/python (revision 415:7439704336c7)
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    NXT_PYTHON_INCLUDE=`${NXT_PYTHON_CONFIG} --includes`
68    NXT_PYTHON_LIBS=`${NXT_PYTHON_CONFIG} --ldflags`
69
70    if [ "$NXT_PYTHON_LIB_PATH" != "" ]; then
71        # "python-config --ldflags" may not contain path to libpython.
72        NXT_PYTHON_LDFLAGS="-L$NXT_PYTHON_LIB_PATH -Wl,-rpath $NXT_PYTHON_LIB_PATH"
73    else
74        NXT_PYTHON_LDFLAGS=""
75    fi
76
77    nxt_feature="Python"
78    nxt_feature_name=""
79    nxt_feature_run=no
80    nxt_feature_incs="${NXT_PYTHON_INCLUDE}"
81    nxt_feature_libs="${NXT_PYTHON_LIBS} $NXT_PYTHON_LDFLAGS"
82    nxt_feature_test="
83        #include <Python.h>
84
85        int main() {
86            Py_Initialize();
87            return 0;
88        }"
89
90    . auto/feature
91
92else
93    $echo "checking for Python ... not found"
94fi
95
96if [ $nxt_found = no ]; then
97    $echo
98    $echo $0: error: no Python found.
99    $echo
100    exit 1;
101fi
102
103
104nxt_feature="Python version"
105nxt_feature_name=""
106nxt_feature_run=value
107nxt_feature_incs="${NXT_PYTHON_INCLUDE}"
108nxt_feature_libs="${NXT_PYTHON_LIBS} $NXT_PYTHON_LDFLAGS"
109nxt_feature_test="
110    #include <Python.h>
111    #include <stdio.h>
112
113    int main() {
114        printf("PY_VERSION");
115        return 0;
116    }"
117
118. auto/feature
119
120
121if grep ^$NXT_PYTHON_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
122    $echo
123    $echo $0: error: duplicate \"$NXT_PYTHON_MODULE\" module configured.
124    $echo
125    exit 1;
126fi
127
128$echo " + Python module: ${NXT_PYTHON_MODULE}.unit.so"
129
130. auto/cc/deps
131
132$echo >> $NXT_MAKEFILE
133
134NXT_PYTHON_MODULE_SRCS=" \
135    src/nxt_python_wsgi.c \
136"
137
138# The python module object files.
139
140nxt_objs=
141
142for nxt_src in $NXT_PYTHON_MODULE_SRCS; do
143
144    nxt_obj=${nxt_src%.c}-$NXT_PYTHON_MODULE.o
145    nxt_dep=${nxt_src%.c}-$NXT_PYTHON_MODULE.dep
146    nxt_dep_flags=`nxt_gen_dep_flags`
147    nxt_dep_post=`nxt_gen_dep_post`
148    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
149
150    cat << END >> $NXT_MAKEFILE
151
152$NXT_BUILD_DIR/$nxt_obj:	$nxt_src
153	\$(CC) -c \$(CFLAGS) \$(NXT_INCS) $NXT_PYTHON_INCLUDE \\
154	$nxt_dep_flags \\
155	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
156	$nxt_dep_post
157
158-include $NXT_BUILD_DIR/$nxt_dep
159
160END
161
162done
163
164
165cat << END >> $NXT_MAKEFILE
166
167.PHONY: ${NXT_PYTHON_MODULE}
168.PHONY: ${NXT_PYTHON_MODULE}-install
169.PHONY: ${NXT_PYTHON_MODULE}-uninstall
170
171all: ${NXT_PYTHON_MODULE}
172
173${NXT_PYTHON_MODULE}:	$NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so
174
175$NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so:	$nxt_objs
176	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so \\
177	$nxt_objs $NXT_PYTHON_LIBS $NXT_PYTHON_LDFLAGS $NXT_LD_OPT
178
179
180install: ${NXT_PYTHON_MODULE}-install
181
182${NXT_PYTHON_MODULE}-install: ${NXT_PYTHON_MODULE}
183	install -d \$(DESTDIR)$NXT_MODULES
184	install -p $NXT_BUILD_DIR/${NXT_PYTHON_MODULE}.unit.so \\
185		\$(DESTDIR)$NXT_MODULES/
186
187
188uninstall: ${NXT_PYTHON_MODULE}-uninstall
189
190${NXT_PYTHON_MODULE}-uninstall:
191	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_PYTHON_MODULE}.unit.so
192	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
193
194END
195