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