]> git.proxmox.com Git - mirror_frr.git/blob - m4/ax_python.m4
Merge pull request #4483 from donaldsharp/pim_mroute_debug_detail
[mirror_frr.git] / m4 / ax_python.m4
1 dnl FRR Python autoconf magic
2 dnl 2019 David Lamparter for NetDEF, Inc.
3 dnl SPDX-License-Identifier: GPL-2.0-or-later
4
5 dnl the _ at the beginning will be cut off (to support the empty version string)
6 m4_define_default([_FRR_PY_VERS], [_3 _ _2 _3.7 _3.6 _3.5 _3.4 _3.3 _3.2 _2.7])
7
8 dnl check basic interpreter properties (py2/py3)
9 dnl doubles as simple check whether the interpreter actually works
10 dnl also swaps in the full path to the interpreter
11 dnl arg1: if-true, arg2: if-false
12 AC_DEFUN([_FRR_PYTHON_INTERP], [dnl
13 AC_ARG_VAR([PYTHON], [Python interpreter to use])dnl
14 AC_MSG_CHECKING([python interpreter $PYTHON])
15 AC_RUN_LOG(["$PYTHON" -c 'import sys; open("conftest.pyver", "w").write(sys.executable or ""); sys.exit(not (sys.version_info.major == 2 and sys.version_info.minor >= 7))'])
16 py2=$ac_status
17 _py2_full="`cat conftest.pyver 2>/dev/null`"
18 rm -f "conftest.pyver" >/dev/null 2>/dev/null
19
20 AC_RUN_LOG(["$PYTHON" -c 'import sys; open("conftest.pyver", "w").write(sys.executable or ""); sys.exit(not ((sys.version_info.major == 3 and sys.version_info.minor >= 2) or sys.version_info.major > 3))'])
21 py3=$ac_status
22 _py3_full="`cat conftest.pyver 2>/dev/null`"
23 rm -f "conftest.pyver" >/dev/null 2>/dev/null
24
25 case "p${py2}p${py3}" in
26 p0p1) frr_cv_python=python2
27 _python_full="$_py2_full" ;;
28 p1p0) frr_cv_python=python3
29 _python_full="$_py3_full" ;;
30 *) frr_cv_python=none ;;
31 esac
32
33 if test "$frr_cv_python" = none; then
34 AC_MSG_RESULT([not working])
35 $2
36 else
37 test -n "$_python_full" -a -x "$_python_full" && PYTHON="$_python_full"
38 AC_MSG_RESULT([$PYTHON ($frr_cv_python)])
39 $1
40 fi
41
42 dnl return value
43 test "$frr_cv_python" != none
44 ])
45
46 dnl check whether $PYTHON has modules available
47 dnl arg1: list of modules (space separated)
48 dnl arg2: if all true, arg3: if any missing
49 dnl also sets frr_py_mod_<name> to "true" or "false"
50 AC_DEFUN([FRR_PYTHON_MODULES], [
51 result=true
52 for pymod in $1; do
53 AC_MSG_CHECKING([whether $PYTHON module $pymod is available])
54 AC_RUN_LOG(["$PYTHON" -c "import $pymod"])
55 sane="`echo \"$pymod\" | tr -c '[a-zA-Z0-9\n]' '_'`"
56 if test "$ac_status" -eq 0; then
57 AC_MSG_RESULT([yes])
58 eval frr_py_mod_$sane=true
59 else
60 AC_MSG_RESULT([no])
61 eval frr_py_mod_$sane=false
62 result=false
63 fi
64 done
65 if $result; then
66 m4_default([$2], [:])
67 else
68 m4_default([$3], [:])
69 fi
70 $result
71 ])
72
73 dnl check whether $PYTHON has modules available
74 dnl arg1: list of modules (space separated)
75 dnl arg2: command line parameters for executing
76 dnl arg3: if all true, arg4: if any missing
77 dnl also sets frr_py_modexec_<name> to "true" or "false"
78 AC_DEFUN([FRR_PYTHON_MOD_EXEC], [
79 result=true
80 for pymod in $1; do
81 AC_MSG_CHECKING([whether $PYTHON module $pymod is executable])
82 AC_RUN_LOG(["$PYTHON" -m "$pymod" $2 > /dev/null])
83 sane="`echo \"$pymod\" | tr -c '[a-zA-Z0-9\n]' '_'`"
84 if test "$ac_status" -eq 0; then
85 AC_MSG_RESULT([yes])
86 eval frr_py_modexec_$sane=true
87 else
88 AC_MSG_RESULT([no])
89 eval frr_py_modexec_$sane=false
90 result=false
91 fi
92 done
93 if $result; then
94 m4_default([$3], [:])
95 else
96 m4_default([$4], [:])
97 fi
98 $result
99 ])
100
101 dnl check whether we can build & link python bits
102 dnl input: PYTHON_CFLAGS and PYTHON_LIBS
103 AC_DEFUN([_FRR_PYTHON_DEVENV], [
104 result=true
105 AC_LINK_IFELSE_FLAGS([$PYTHON_CFLAGS], [$PYTHON_LIBS], [AC_LANG_PROGRAM([
106 #include <Python.h>
107 #if PY_VERSION_HEX < 0x02070000
108 #error python too old
109 #endif
110 int main(void);
111 ],
112 [
113 {
114 Py_Initialize();
115 return 0;
116 }
117 ])], [
118 # some python installs are missing the zlib dependency...
119 PYTHON_LIBS="${PYTHON_LIBS} -lz"
120 AC_LINK_IFELSE_FLAGS([$PYTHON_CFLAGS], [$PYTHON_LIBS], [AC_LANG_PROGRAM([
121 #include <Python.h>
122 #if PY_VERSION_HEX < 0x02070000
123 #error python too old
124 #endif
125 int main(void);
126 ],
127 [
128 {
129 Py_Initialize();
130 return 0;
131 }
132 ])], [
133 result=false
134 AC_MSG_RESULT([no])
135 ], [:])
136 ], [:])
137
138 if $result; then
139 AC_LINK_IFELSE_FLAGS([$PYTHON_CFLAGS], [$PYTHON_LIBS], [AC_LANG_PROGRAM([
140 #include <Python.h>
141 #if PY_VERSION_HEX != $1
142 #error python version mismatch
143 #endif
144 int main(void);
145 ],
146 [
147 {
148 Py_Initialize();
149 return 0;
150 }
151 ])], [
152 result=false
153 AC_MSG_RESULT([version mismatch])
154 ], [
155 AC_MSG_RESULT([yes])
156 ])
157 fi
158
159 if $result; then
160 m4_default([$2], [:])
161 else
162 m4_default([$3], [
163 unset PYTHON_LIBS
164 unset PYTHON_CFLAGS
165 ])
166 fi
167 ])
168
169 AC_DEFUN([_FRR_PYTHON_GETDEV], [dnl
170 AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
171
172 py_abi="` \"$1\" -c \"import sys; print(getattr(sys, 'abiflags', ''))\"`"
173 py_hex="` \"$1\" -c \"import sys; print(hex(sys.hexversion))\"`"
174 py_ldver="` \"$1\" -c \"import sysconfig; print(sysconfig.get_config_var('LDVERSION') or '')\"`"
175 py_ver="` \"$1\" -c \"import sysconfig; print(sysconfig.get_config_var('VERSION') or '')\"`"
176 py_bindir="`\"$1\" -c \"import sysconfig; print(sysconfig.get_config_var('BINDIR') or '')\"`"
177 test -z "$py_bindir" || py_bindir="$py_bindir/"
178 echo "py_abi=${py_abi} py_ldver=${py_ldver} py_ver=${py_ver} py_bindir=${py_bindir}" >&AS_MESSAGE_LOG_FD
179
180 py_found=false
181
182 for tryver in "${py_ldver}" "${py_ver}"; do
183 pycfg="${py_bindir}python${tryver}-config"
184 AC_MSG_CHECKING([whether ${pycfg} is available])
185 if "$pycfg" --configdir >/dev/null 2>/dev/null; then
186 AC_MSG_RESULT([yes])
187
188 PYTHON_CFLAGS="`\"$pycfg\" --includes`"
189 PYTHON_LIBS="`\"$pycfg\" --ldflags`"
190
191 AC_MSG_CHECKING([whether ${pycfg} provides a working build environment])
192 _FRR_PYTHON_DEVENV([$py_hex], [
193 py_found=true
194 break
195 ])
196 else
197 AC_MSG_RESULT([no])
198 fi
199
200 pkg_failed=no
201 AC_MSG_CHECKING([whether pkg-config python-${tryver} is available])
202 unset PYTHON_CFLAGS
203 unset PYTHON_LIBS
204 pkg="python-${tryver}"
205 pkg="${pkg%-}"
206 _PKG_CONFIG([PYTHON_CFLAGS], [cflags], [${pkg}])
207 _PKG_CONFIG([PYTHON_LIBS], [libs], [${pkg}])
208 if test $pkg_failed = no; then
209 AC_MSG_RESULT([yes])
210
211 PYTHON_CFLAGS=$pkg_cv_PYTHON_CFLAGS
212 PYTHON_LIBS=$pkg_cv_PYTHON_LIBS
213
214 AC_MSG_CHECKING([whether pkg-config python-${tryver} provides a working build environment])
215 _FRR_PYTHON_DEVENV([$py_hex], [
216 py_found=true
217 break
218 ])
219 else
220 AC_MSG_RESULT([no])
221 fi
222 done
223
224 if $py_found; then
225 m4_default([$2], [:])
226 else
227 unset PYTHON_CFLAGS
228 unset PYTHON_LIBS
229 m4_default([$3], [:])
230 fi
231 ])
232
233 dnl just find python without checking headers/libs
234 AC_DEFUN([FRR_PYTHON], [
235 dnl user override
236 if test "x$PYTHON" != "x"; then
237 _FRR_PYTHON_INTERP([], [
238 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
239 ])
240 else
241 for frr_pyver in _FRR_PY_VERS; do
242 PYTHON="python${frr_pyver#_}"
243 _FRR_PYTHON_INTERP([break])
244 PYTHON=":"
245 done
246 if test "$PYTHON" = ":"; then
247 AC_MSG_ERROR([no working python version found])
248 fi
249 fi
250 AC_SUBST([PYTHON])
251 ])
252
253 dnl find python with checking headers/libs
254 AC_DEFUN([FRR_PYTHON_DEV], [dnl
255 AC_ARG_VAR([PYTHON_CFLAGS], [C compiler flags for Python])dnl
256 AC_ARG_VAR([PYTHON_LIBS], [linker flags for Python])dnl
257
258 dnl user override
259 if test "x$PYTHON" != "x"; then
260 _FRR_PYTHON_INTERP([], [
261 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
262 ])
263 _FRR_PYTHON_GETDEV([$PYTHON], [], [
264 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but development environment not working])
265 ])
266 else
267 for frr_pyver in _FRR_PY_VERS; do
268 PYTHON="python${frr_pyver#_}"
269 _FRR_PYTHON_INTERP([
270 _FRR_PYTHON_GETDEV([$PYTHON], [
271 break
272 ])
273 ])
274 PYTHON=":"
275 done
276 if test "$PYTHON" = ":"; then
277 AC_MSG_ERROR([no working python version found])
278 fi
279 fi
280
281 AC_SUBST([PYTHON_CFLAGS])
282 AC_SUBST([PYTHON_LIBS])
283 AC_SUBST([PYTHON])
284 ])