1 dnl FRR Python autoconf magic
2 dnl 2019 David Lamparter for NetDEF, Inc.
3 dnl SPDX-License-Identifier: GPL-2.0-or-later
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])
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))'])
17 _py2_full="`cat conftest.pyver 2>/dev/null`"
18 rm -f "conftest.pyver" >/dev/null 2>/dev/null
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))'])
22 _py3_full="`cat conftest.pyver 2>/dev/null`"
23 rm -f "conftest.pyver" >/dev/null 2>/dev/null
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 ;;
33 if test "$frr_cv_python" = none; then
34 AC_MSG_RESULT([not working])
37 test -n "$_python_full" -a -x "$_python_full" && PYTHON="$_python_full"
38 AC_MSG_RESULT([$PYTHON ($frr_cv_python)])
43 test "$frr_cv_python" != none
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], [
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
58 eval frr_py_mod_$sane=true
61 eval frr_py_mod_$sane=false
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], [
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
86 eval frr_py_modexec_$sane=true
89 eval frr_py_modexec_$sane=false
101 dnl check whether we can build & link python bits
102 dnl input: PYTHON_CFLAGS and PYTHON_LIBS
103 AC_DEFUN([_FRR_PYTHON_DEVENV], [
105 AC_LINK_IFELSE_FLAGS([$PYTHON_CFLAGS], [$PYTHON_LIBS], [AC_LANG_PROGRAM([
107 #if PY_VERSION_HEX < 0x02070000
108 #error python too old
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([
122 #if PY_VERSION_HEX < 0x02070000
123 #error python too old
139 AC_LINK_IFELSE_FLAGS([$PYTHON_CFLAGS], [$PYTHON_LIBS], [AC_LANG_PROGRAM([
141 #if PY_VERSION_HEX != $1
142 #error python version mismatch
153 AC_MSG_RESULT([version mismatch])
160 m4_default([$2], [:])
169 AC_DEFUN([_FRR_PYTHON_GETDEV], [dnl
170 AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
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
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
188 PYTHON_CFLAGS="`\"$pycfg\" --includes`"
189 minor_ver=${py_ver#*\.}
190 if test $((minor_ver)) -gt 7; then
191 PYTHON_LIBS="`\"$pycfg\" --ldflags --embed`"
193 PYTHON_LIBS="`\"$pycfg\" --ldflags`"
196 AC_MSG_CHECKING([whether ${pycfg} provides a working build environment])
197 _FRR_PYTHON_DEVENV([$py_hex], [
206 AC_MSG_CHECKING([whether pkg-config python-${tryver} is available])
209 pkg="python-${tryver}"
211 _PKG_CONFIG([PYTHON_CFLAGS], [cflags], [${pkg}])
212 _PKG_CONFIG([PYTHON_LIBS], [libs], [${pkg}])
213 if test $pkg_failed = no; then
216 PYTHON_CFLAGS=$pkg_cv_PYTHON_CFLAGS
217 PYTHON_LIBS=$pkg_cv_PYTHON_LIBS
219 AC_MSG_CHECKING([whether pkg-config python-${tryver} provides a working build environment])
220 _FRR_PYTHON_DEVENV([$py_hex], [
230 m4_default([$2], [:])
234 m4_default([$3], [:])
238 dnl just find python without checking headers/libs
239 AC_DEFUN([FRR_PYTHON], [
241 if test "x$PYTHON" != "x"; then
242 _FRR_PYTHON_INTERP([], [
243 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
246 for frr_pyver in _FRR_PY_VERS; do
247 PYTHON="python${frr_pyver#_}"
248 _FRR_PYTHON_INTERP([break])
251 if test "$PYTHON" = ":"; then
252 AC_MSG_ERROR([no working python version found])
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
264 if test "x$PYTHON" != "x"; then
265 _FRR_PYTHON_INTERP([], [
266 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
268 _FRR_PYTHON_GETDEV([$PYTHON], [], [
269 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but development environment not working])
272 for frr_pyver in _FRR_PY_VERS; do
273 PYTHON="python${frr_pyver#_}"
275 _FRR_PYTHON_GETDEV([$PYTHON], [
281 if test "$PYTHON" = ":"; then
282 AC_MSG_ERROR([no working python version found])
286 AC_SUBST([PYTHON_CFLAGS])
287 AC_SUBST([PYTHON_LIBS])