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 _ _2 _3.8 _3.7 _3.6 _3.5 _3.4 _3.3 _3.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 if test x"${py_ver}" = x"3.8" || test x"{py_ver}" = x"3.9"; then
190 PYTHON_LIBS="`\"$pycfg\" --ldflags --embed`"
192 PYTHON_LIBS="`\"$pycfg\" --ldflags`"
195 AC_MSG_CHECKING([whether ${pycfg} provides a working build environment])
196 _FRR_PYTHON_DEVENV([$py_hex], [
205 AC_MSG_CHECKING([whether pkg-config python-${tryver} is available])
208 pkg="python-${tryver}"
210 _PKG_CONFIG([PYTHON_CFLAGS], [cflags], [${pkg}])
211 _PKG_CONFIG([PYTHON_LIBS], [libs], [${pkg}])
212 if test $pkg_failed = no; then
215 PYTHON_CFLAGS=$pkg_cv_PYTHON_CFLAGS
216 PYTHON_LIBS=$pkg_cv_PYTHON_LIBS
218 AC_MSG_CHECKING([whether pkg-config python-${tryver} provides a working build environment])
219 _FRR_PYTHON_DEVENV([$py_hex], [
229 m4_default([$2], [:])
233 m4_default([$3], [:])
237 dnl just find python without checking headers/libs
238 AC_DEFUN([FRR_PYTHON], [
240 if test "x$PYTHON" != "x"; then
241 _FRR_PYTHON_INTERP([], [
242 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
245 for frr_pyver in _FRR_PY_VERS; do
246 PYTHON="python${frr_pyver#_}"
247 _FRR_PYTHON_INTERP([break])
250 if test "$PYTHON" = ":"; then
251 AC_MSG_ERROR([no working python version found])
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
263 if test "x$PYTHON" != "x"; then
264 _FRR_PYTHON_INTERP([], [
265 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
267 _FRR_PYTHON_GETDEV([$PYTHON], [], [
268 AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but development environment not working])
271 for frr_pyver in _FRR_PY_VERS; do
272 PYTHON="python${frr_pyver#_}"
274 _FRR_PYTHON_GETDEV([$PYTHON], [
280 if test "$PYTHON" = ":"; then
281 AC_MSG_ERROR([no working python version found])
285 AC_SUBST([PYTHON_CFLAGS])
286 AC_SUBST([PYTHON_LIBS])