]> git.proxmox.com Git - mirror_frr.git/blob - m4/ax_python.m4
build: add check for python-3.8
[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 if test x"${py_ver}" == x"3.8" || test x"{py_ver}" == x"3.9"; then
190 PYTHON_LIBS="`\"$pycfg\" --ldflags --embed`"
191 else
192 PYTHON_LIBS="`\"$pycfg\" --ldflags`"
193 fi
194
195 AC_MSG_CHECKING([whether ${pycfg} provides a working build environment])
196 _FRR_PYTHON_DEVENV([$py_hex], [
197 py_found=true
198 break
199 ])
200 else
201 AC_MSG_RESULT([no])
202 fi
203
204 pkg_failed=no
205 AC_MSG_CHECKING([whether pkg-config python-${tryver} is available])
206 unset PYTHON_CFLAGS
207 unset PYTHON_LIBS
208 pkg="python-${tryver}"
209 pkg="${pkg%-}"
210 _PKG_CONFIG([PYTHON_CFLAGS], [cflags], [${pkg}])
211 _PKG_CONFIG([PYTHON_LIBS], [libs], [${pkg}])
212 if test $pkg_failed = no; then
213 AC_MSG_RESULT([yes])
214
215 PYTHON_CFLAGS=$pkg_cv_PYTHON_CFLAGS
216 PYTHON_LIBS=$pkg_cv_PYTHON_LIBS
217
218 AC_MSG_CHECKING([whether pkg-config python-${tryver} provides a working build environment])
219 _FRR_PYTHON_DEVENV([$py_hex], [
220 py_found=true
221 break
222 ])
223 else
224 AC_MSG_RESULT([no])
225 fi
226 done
227
228 if $py_found; then
229 m4_default([$2], [:])
230 else
231 unset PYTHON_CFLAGS
232 unset PYTHON_LIBS
233 m4_default([$3], [:])
234 fi
235 ])
236
237 dnl just find python without checking headers/libs
238 AC_DEFUN([FRR_PYTHON], [
239 dnl user override
240 if test "x$PYTHON" != "x"; then
241 _FRR_PYTHON_INTERP([], [
242 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
243 ])
244 else
245 for frr_pyver in _FRR_PY_VERS; do
246 PYTHON="python${frr_pyver#_}"
247 _FRR_PYTHON_INTERP([break])
248 PYTHON=":"
249 done
250 if test "$PYTHON" = ":"; then
251 AC_MSG_ERROR([no working python version found])
252 fi
253 fi
254 AC_SUBST([PYTHON])
255 ])
256
257 dnl find python with checking headers/libs
258 AC_DEFUN([FRR_PYTHON_DEV], [dnl
259 AC_ARG_VAR([PYTHON_CFLAGS], [C compiler flags for Python])dnl
260 AC_ARG_VAR([PYTHON_LIBS], [linker flags for Python])dnl
261
262 dnl user override
263 if test "x$PYTHON" != "x"; then
264 _FRR_PYTHON_INTERP([], [
265 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
266 ])
267 _FRR_PYTHON_GETDEV([$PYTHON], [], [
268 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but development environment not working])
269 ])
270 else
271 for frr_pyver in _FRR_PY_VERS; do
272 PYTHON="python${frr_pyver#_}"
273 _FRR_PYTHON_INTERP([
274 _FRR_PYTHON_GETDEV([$PYTHON], [
275 break
276 ])
277 ])
278 PYTHON=":"
279 done
280 if test "$PYTHON" = ":"; then
281 AC_MSG_ERROR([no working python version found])
282 fi
283 fi
284
285 AC_SUBST([PYTHON_CFLAGS])
286 AC_SUBST([PYTHON_LIBS])
287 AC_SUBST([PYTHON])
288 ])