summaryrefslogtreecommitdiffstats
path: root/m4/ax_python.m4
blob: f5e603b96b16f7c30507f19e7d7c4932da6d856a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
dnl FRR Python autoconf magic
dnl 2019 David Lamparter for NetDEF, Inc.
dnl SPDX-License-Identifier: GPL-2.0-or-later

dnl the _ at the beginning will be cut off (to support the empty version string)
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])

dnl check basic interpreter properties (py2/py3)
dnl doubles as simple check whether the interpreter actually works
dnl also swaps in the full path to the interpreter
dnl arg1: if-true, arg2: if-false
AC_DEFUN([_FRR_PYTHON_INTERP], [dnl
AC_ARG_VAR([PYTHON], [Python interpreter to use])dnl
  AC_MSG_CHECKING([python interpreter $PYTHON])
  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))'])
  py2=$ac_status
  _py2_full="`cat conftest.pyver 2>/dev/null`"
  rm -f "conftest.pyver" >/dev/null 2>/dev/null

  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))'])
  py3=$ac_status
  _py3_full="`cat conftest.pyver 2>/dev/null`"
  rm -f "conftest.pyver" >/dev/null 2>/dev/null

  case "p${py2}p${py3}" in
  p0p1) frr_cv_python=python2
        _python_full="$_py2_full" ;;
  p1p0) frr_cv_python=python3
        _python_full="$_py3_full" ;;
  *)    frr_cv_python=none ;;
  esac

  if test "$frr_cv_python" = none; then
    AC_MSG_RESULT([not working])
    $2
  else
    test -n "$_python_full" -a -x "$_python_full" && PYTHON="$_python_full"
    AC_MSG_RESULT([$PYTHON ($frr_cv_python)])
    $1
  fi

  dnl return value
  test "$frr_cv_python" != none
])

dnl check whether $PYTHON has modules available
dnl arg1: list of modules (space separated)
dnl arg2: if all true, arg3: if any missing
dnl also sets frr_py_mod_<name> to "true" or "false"
AC_DEFUN([FRR_PYTHON_MODULES], [
  result=true
  for pymod in $1; do
    AC_MSG_CHECKING([whether $PYTHON module $pymod is available])
    AC_RUN_LOG(["$PYTHON" -c "import $pymod"])
    sane="`echo \"$pymod\" | tr -c '[a-zA-Z0-9\n]' '_'`"
    if test "$ac_status" -eq 0; then
      AC_MSG_RESULT([yes])
      eval frr_py_mod_$sane=true
    else
      AC_MSG_RESULT([no])
      eval frr_py_mod_$sane=false
      result=false
    fi
  done
  if $result; then
    m4_default([$2], [:])
  else
    m4_default([$3], [:])
  fi
  $result
])

dnl check whether $PYTHON has modules available
dnl arg1: list of modules (space separated)
dnl arg2: command line parameters for executing
dnl arg3: if all true, arg4: if any missing
dnl also sets frr_py_modexec_<name> to "true" or "false"
AC_DEFUN([FRR_PYTHON_MOD_EXEC], [
  result=true
  for pymod in $1; do
    AC_MSG_CHECKING([whether $PYTHON module $pymod is executable])
    AC_RUN_LOG(["$PYTHON" -m "$pymod" $2 > /dev/null])
    sane="`echo \"$pymod\" | tr -c '[a-zA-Z0-9\n]' '_'`"
    if test "$ac_status" -eq 0; then
      AC_MSG_RESULT([yes])
      eval frr_py_modexec_$sane=true
    else
      AC_MSG_RESULT([no])
      eval frr_py_modexec_$sane=false
      result=false
    fi
  done
  if $result; then
    m4_default([$3], [:])
  else
    m4_default([$4], [:])
  fi
  $result
])

dnl check whether we can build & link python bits
dnl input: PYTHON_CFLAGS and PYTHON_LIBS
AC_DEFUN([_FRR_PYTHON_DEVENV], [
  result=true
  AC_LINK_IFELSE_FLAGS([$PYTHON_CFLAGS], [$PYTHON_LIBS], [AC_LANG_PROGRAM([
#include <Python.h>
#if PY_VERSION_HEX < 0x02070000
#error python too old
#endif
int main(void);
],
[
{
  Py_Initialize();
  return 0;
}
])], [
    # some python installs are missing the zlib dependency...
    PYTHON_LIBS="${PYTHON_LIBS} -lz"
    AC_LINK_IFELSE_FLAGS([$PYTHON_CFLAGS], [$PYTHON_LIBS], [AC_LANG_PROGRAM([
#include <Python.h>
#if PY_VERSION_HEX < 0x02070000
#error python too old
#endif
int main(void);
],
[
{
  Py_Initialize();
  return 0;
}
])], [
      result=false
      AC_MSG_RESULT([no])
    ], [:])
  ], [:])

  if $result; then
    AC_LINK_IFELSE_FLAGS([$PYTHON_CFLAGS], [$PYTHON_LIBS], [AC_LANG_PROGRAM([
#include <Python.h>
#if PY_VERSION_HEX != $1
#error python version mismatch
#endif
int main(void);
],
[
{
  Py_Initialize();
  return 0;
}
])], [
      result=false
      AC_MSG_RESULT([version mismatch])
    ], [
      AC_MSG_RESULT([yes])
    ])
  fi

  if $result; then
    m4_default([$2], [:])
  else
    m4_default([$3], [
      unset PYTHON_LIBS
      unset PYTHON_CFLAGS
    ])
  fi
])

AC_DEFUN([_FRR_PYTHON_GETDEV], [dnl
AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl

  py_abi="`   \"$1\" -c \"import sys; print(getattr(sys, 'abiflags', ''))\"`"
  py_hex="`   \"$1\" -c \"import sys; print(hex(sys.hexversion))\"`"
  py_ldver="` \"$1\" -c \"import sysconfig; print(sysconfig.get_config_var('LDVERSION') or '')\"`"
  py_ver="`   \"$1\" -c \"import sysconfig; print(sysconfig.get_config_var('VERSION') or '')\"`"
  py_bindir="`\"$1\" -c \"import sysconfig; print(sysconfig.get_config_var('BINDIR') or '')\"`"
  test -z "$py_bindir" || py_bindir="$py_bindir/"
  echo "py_abi=${py_abi} py_ldver=${py_ldver} py_ver=${py_ver} py_bindir=${py_bindir}" >&AS_MESSAGE_LOG_FD

  py_found=false

  for tryver in "${py_ldver}" "${py_ver}"; do
    pycfg="${py_bindir}python${tryver}-config"
    AC_MSG_CHECKING([whether ${pycfg} is available])
    if "$pycfg" --configdir >/dev/null 2>/dev/null; then
      AC_MSG_RESULT([yes])

      PYTHON_CFLAGS="`\"$pycfg\" --includes`"
      minor_ver=${py_ver#*\.}
      if test $((minor_ver)) -gt 7; then
        PYTHON_LIBS="`\"$pycfg\" --ldflags --embed`"
      else
        PYTHON_LIBS="`\"$pycfg\" --ldflags`"
      fi

      AC_MSG_CHECKING([whether ${pycfg} provides a working build environment])
      _FRR_PYTHON_DEVENV([$py_hex], [
        py_found=true
        break
      ])
    else
      AC_MSG_RESULT([no])
    fi

    pkg_failed=no
    AC_MSG_CHECKING([whether pkg-config python-${tryver} is available])
    unset PYTHON_CFLAGS
    unset PYTHON_LIBS
    pkg="python-${tryver}-embed"
    pkg="${pkg%-}"
    _PKG_CONFIG([PYTHON_CFLAGS], [cflags], [${pkg}])
    _PKG_CONFIG([PYTHON_LIBS], [libs], [${pkg}])
    if test $pkg_failed = no; then
      AC_MSG_RESULT([yes])

      PYTHON_CFLAGS=$pkg_cv_PYTHON_CFLAGS
      PYTHON_LIBS=$pkg_cv_PYTHON_LIBS

      AC_MSG_CHECKING([whether pkg-config python-${tryver} provides a working build environment])
      _FRR_PYTHON_DEVENV([$py_hex], [
        py_found=true
        break
      ])
    else
      AC_MSG_RESULT([no])
    fi
  done

  if $py_found; then
    m4_default([$2], [:])
  else
    unset PYTHON_CFLAGS
    unset PYTHON_LIBS
    m4_default([$3], [:])
  fi
])

dnl just find python without checking headers/libs
AC_DEFUN([FRR_PYTHON], [
  dnl user override
  if test "x$PYTHON" != "x"; then
    _FRR_PYTHON_INTERP([], [
      AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
    ])
  else
    for frr_pyver in _FRR_PY_VERS; do
      PYTHON="python${frr_pyver#_}"
      _FRR_PYTHON_INTERP([break])
      PYTHON=":"
    done
    if test "$PYTHON" = ":"; then
      AC_MSG_ERROR([no working python version found])
    fi
  fi
  AC_SUBST([PYTHON])
])

dnl find python with checking headers/libs
AC_DEFUN([FRR_PYTHON_DEV], [dnl
AC_ARG_VAR([PYTHON_CFLAGS], [C compiler flags for Python])dnl
AC_ARG_VAR([PYTHON_LIBS], [linker flags for Python])dnl

  dnl user override
  if test "x$PYTHON" != "x"; then
    _FRR_PYTHON_INTERP([], [
      AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but not working])
    ])
    _FRR_PYTHON_GETDEV([$PYTHON], [], [
      AC_MSG_ERROR([PYTHON ($PYTHON) explicitly specified but development environment not working])
    ])
  else
    for frr_pyver in _FRR_PY_VERS; do
      PYTHON="python${frr_pyver#_}"
      _FRR_PYTHON_INTERP([
        _FRR_PYTHON_GETDEV([$PYTHON], [
          break
        ])
      ])
      PYTHON=":"
    done
    if test "$PYTHON" = ":"; then
      AC_MSG_ERROR([no working python version found])
    fi
  fi

  AC_SUBST([PYTHON_CFLAGS])
  AC_SUBST([PYTHON_LIBS])
  AC_SUBST([PYTHON])
])