1## ------------------------                                 -*- Autoconf -*-
2## Python file handling
3## From Andrew Dalke
4## Updated by James Henstridge and other contributors.
5## ------------------------
6# Copyright (C) 1999-2021 Free Software Foundation, Inc.
7#
8# This file is free software; the Free Software Foundation
9# gives unlimited permission to copy and/or distribute it,
10# with or without modifications, as long as this notice is preserved.
11
12
13# AM_PATH_PYTHON([MINIMUM-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
14# ---------------------------------------------------------------------------
15# Adds support for distributing Python modules and packages.  To
16# install modules, copy them to $(pythondir), using the python_PYTHON
17# automake variable.  To install a package with the same name as the
18# automake package, install to $(pkgpythondir), or use the
19# pkgpython_PYTHON automake variable.
20#
21# The variables $(pyexecdir) and $(pkgpyexecdir) are provided as
22# locations to install python extension modules (shared libraries).
23# Another macro is required to find the appropriate flags to compile
24# extension modules.
25#
26# If your package is configured with a different prefix to python,
27# users will have to add the install directory to the PYTHONPATH
28# environment variable, or create a .pth file (see the python
29# documentation for details).
30#
31# If the MINIMUM-VERSION argument is passed, AM_PATH_PYTHON will
32# cause an error if the version of python installed on the system
33# doesn't meet the requirement.  MINIMUM-VERSION should consist of
34# numbers and dots only.
35AC_DEFUN([AM_PATH_PYTHON],
36 [
37  dnl Find a Python interpreter.  Python versions prior to 2.0 are not
38  dnl supported. (2.0 was released on October 16, 2000).
39  m4_define_default([_AM_PYTHON_INTERPRETER_LIST],
40[python python2 python3 dnl
41 python3.9 python3.8 python3.7 python3.6 python3.5 python3.4 python3.3 dnl
42 python3.2 python3.1 python3.0 dnl
43 python2.7 python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 dnl
44 python2.0])
45
46  AC_ARG_VAR([PYTHON], [the Python interpreter])
47
48  m4_if([$1],[],[
49    dnl No version check is needed.
50    # Find any Python interpreter.
51    if test -z "$PYTHON"; then
52      AC_PATH_PROGS([PYTHON], _AM_PYTHON_INTERPRETER_LIST, :)
53    fi
54    am_display_PYTHON=python
55  ], [
56    dnl A version check is needed.
57    if test -n "$PYTHON"; then
58      # If the user set $PYTHON, use it and don't search something else.
59      AC_MSG_CHECKING([whether $PYTHON version is >= $1])
60      AM_PYTHON_CHECK_VERSION([$PYTHON], [$1],
61			      [AC_MSG_RESULT([yes])],
62			      [AC_MSG_RESULT([no])
63			       AC_MSG_ERROR([Python interpreter is too old])])
64      am_display_PYTHON=$PYTHON
65    else
66      # Otherwise, try each interpreter until we find one that satisfies
67      # VERSION.
68      AC_CACHE_CHECK([for a Python interpreter with version >= $1],
69	[am_cv_pathless_PYTHON],[
70	for am_cv_pathless_PYTHON in _AM_PYTHON_INTERPRETER_LIST none; do
71	  test "$am_cv_pathless_PYTHON" = none && break
72	  AM_PYTHON_CHECK_VERSION([$am_cv_pathless_PYTHON], [$1], [break])
73	done])
74      # Set $PYTHON to the absolute path of $am_cv_pathless_PYTHON.
75      if test "$am_cv_pathless_PYTHON" = none; then
76	PYTHON=:
77      else
78        AC_PATH_PROG([PYTHON], [$am_cv_pathless_PYTHON])
79      fi
80      am_display_PYTHON=$am_cv_pathless_PYTHON
81    fi
82  ])
83
84  if test "$PYTHON" = :; then
85    dnl Run any user-specified action, or abort.
86    m4_default([$3], [AC_MSG_ERROR([no suitable Python interpreter found])])
87  else
88
89  dnl Query Python for its version number.  Although site.py simply uses
90  dnl sys.version[:3], printing that failed with Python 3.10, since the
91  dnl trailing zero was eliminated. So now we output just the major
92  dnl and minor version numbers, as numbers. Apparently the tertiary
93  dnl version is not of interest.
94  dnl
95  AC_CACHE_CHECK([for $am_display_PYTHON version], [am_cv_python_version],
96    [am_cv_python_version=`$PYTHON -c "import sys; print ('%u.%u' % sys.version_info[[:2]])"`])
97  AC_SUBST([PYTHON_VERSION], [$am_cv_python_version])
98
99  dnl At times, e.g., when building shared libraries, you may want
100  dnl to know which OS platform Python thinks this is.
101  dnl
102  AC_CACHE_CHECK([for $am_display_PYTHON platform], [am_cv_python_platform],
103    [am_cv_python_platform=`$PYTHON -c "import sys; sys.stdout.write(sys.platform)"`])
104  AC_SUBST([PYTHON_PLATFORM], [$am_cv_python_platform])
105
106  dnl emacs-page
107  dnl If --with-python-sys-prefix is given, use the values of sys.prefix
108  dnl and sys.exec_prefix for the corresponding values of PYTHON_PREFIX
109  dnl and PYTHON_EXEC_PREFIX. Otherwise, use the GNU ${prefix} and
110  dnl ${exec_prefix} variables.
111  dnl
112  dnl The two are made distinct variables so they can be overridden if
113  dnl need be, although general consensus is that you shouldn't need
114  dnl this separation.
115  dnl
116  dnl Also allow directly setting the prefixes via configure options,
117  dnl overriding any default.
118  dnl
119  if test "x$prefix" = xNONE; then
120    am__usable_prefix=$ac_default_prefix
121  else
122    am__usable_prefix=$prefix
123  fi
124
125  # Allow user to request using sys.* values from Python,
126  # instead of the GNU $prefix values.
127  AC_ARG_WITH([python-sys-prefix],
128  [AS_HELP_STRING([--with-python-sys-prefix],
129                  [use Python's sys.prefix and sys.exec_prefix values])],
130  [am_use_python_sys=:],
131  [am_use_python_sys=false])
132
133  # Allow user to override whatever the default Python prefix is.
134  AC_ARG_WITH([python_prefix],
135  [AS_HELP_STRING([--with-python_prefix],
136                  [override the default PYTHON_PREFIX])],
137  [am_python_prefix_subst=$withval
138   am_cv_python_prefix=$withval
139   AC_MSG_CHECKING([for explicit $am_display_PYTHON prefix])
140   AC_MSG_RESULT([$am_cv_python_prefix])],
141  [
142   if $am_use_python_sys; then
143     # using python sys.prefix value, not GNU
144     AC_CACHE_CHECK([for python default $am_display_PYTHON prefix],
145     [am_cv_python_prefix],
146     [am_cv_python_prefix=`$PYTHON -c "import sys; sys.stdout.write(sys.prefix)"`])
147
148     dnl If sys.prefix is a subdir of $prefix, replace the literal value of
149     dnl $prefix with a variable reference so it can be overridden.
150     case $am_cv_python_prefix in
151     $am__usable_prefix*)
152       am__strip_prefix=`echo "$am__usable_prefix" | sed 's|.|.|g'`
153       am_python_prefix_subst=`echo "$am_cv_python_prefix" | sed "s,^$am__strip_prefix,\\${prefix},"`
154       ;;
155     *)
156       am_python_prefix_subst=$am_cv_python_prefix
157       ;;
158     esac
159   else # using GNU prefix value, not python sys.prefix
160     am_python_prefix_subst='${prefix}'
161     am_python_prefix=$am_python_prefix_subst
162     AC_MSG_CHECKING([for GNU default $am_display_PYTHON prefix])
163     AC_MSG_RESULT([$am_python_prefix])
164   fi])
165  # Substituting python_prefix_subst value.
166  AC_SUBST([PYTHON_PREFIX], [$am_python_prefix_subst])
167
168  # emacs-page Now do it all over again for Python exec_prefix, but with yet
169  # another conditional: fall back to regular prefix if that was specified.
170  AC_ARG_WITH([python_exec_prefix],
171  [AS_HELP_STRING([--with-python_exec_prefix],
172                  [override the default PYTHON_EXEC_PREFIX])],
173  [am_python_exec_prefix_subst=$withval
174   am_cv_python_exec_prefix=$withval
175   AC_MSG_CHECKING([for explicit $am_display_PYTHON exec_prefix])
176   AC_MSG_RESULT([$am_cv_python_exec_prefix])],
177  [
178   # no explicit --with-python_exec_prefix, but if
179   # --with-python_prefix was given, use its value for python_exec_prefix too.
180   AS_IF([test -n "$with_python_prefix"],
181   [am_python_exec_prefix_subst=$with_python_prefix
182    am_cv_python_exec_prefix=$with_python_prefix
183    AC_MSG_CHECKING([for python_prefix-given $am_display_PYTHON exec_prefix])
184    AC_MSG_RESULT([$am_cv_python_exec_prefix])],
185   [
186    # Set am__usable_exec_prefix whether using GNU or Python values,
187    # since we use that variable for pyexecdir.
188    if test "x$exec_prefix" = xNONE; then
189      am__usable_exec_prefix=$am__usable_prefix
190    else
191      am__usable_exec_prefix=$exec_prefix
192    fi
193    #
194    if $am_use_python_sys; then # using python sys.exec_prefix, not GNU
195      AC_CACHE_CHECK([for python default $am_display_PYTHON exec_prefix],
196      [am_cv_python_exec_prefix],
197      [am_cv_python_exec_prefix=`$PYTHON -c "import sys; sys.stdout.write(sys.exec_prefix)"`])
198      dnl If sys.exec_prefix is a subdir of $exec_prefix, replace the
199      dnl literal value of $exec_prefix with a variable reference so it can
200      dnl be overridden.
201      case $am_cv_python_exec_prefix in
202      $am__usable_exec_prefix*)
203        am__strip_prefix=`echo "$am__usable_exec_prefix" | sed 's|.|.|g'`
204        am_python_exec_prefix_subst=`echo "$am_cv_python_exec_prefix" | sed "s,^$am__strip_prefix,\\${exec_prefix},"`
205        ;;
206      *)
207        am_python_exec_prefix_subst=$am_cv_python_exec_prefix
208        ;;
209     esac
210   else # using GNU $exec_prefix, not python sys.exec_prefix
211     am_python_exec_prefix_subst='${exec_prefix}'
212     am_python_exec_prefix=$am_python_exec_prefix_subst
213     AC_MSG_CHECKING([for GNU default $am_display_PYTHON exec_prefix])
214     AC_MSG_RESULT([$am_python_exec_prefix])
215   fi])])
216  # Substituting python_exec_prefix_subst.
217  AC_SUBST([PYTHON_EXEC_PREFIX], [$am_python_exec_prefix_subst])
218
219  # Factor out some code duplication into this shell variable.
220  am_python_setup_sysconfig="\
221import sys
222# Prefer sysconfig over distutils.sysconfig, for better compatibility
223# with python 3.x.  See automake bug#10227.
224try:
225    import sysconfig
226except ImportError:
227    can_use_sysconfig = 0
228else:
229    can_use_sysconfig = 1
230# Can't use sysconfig in CPython 2.7, since it's broken in virtualenvs:
231# <https://github.com/pypa/virtualenv/issues/118>
232try:
233    from platform import python_implementation
234    if python_implementation() == 'CPython' and sys.version[[:3]] == '2.7':
235        can_use_sysconfig = 0
236except ImportError:
237    pass"
238
239  dnl emacs-page Set up 4 directories:
240
241  dnl 1. pythondir: where to install python scripts.  This is the
242  dnl    site-packages directory, not the python standard library
243  dnl    directory like in previous automake betas.  This behavior
244  dnl    is more consistent with lispdir.m4 for example.
245  dnl Query distutils for this directory.
246  dnl
247  AC_CACHE_CHECK([for $am_display_PYTHON script directory (pythondir)],
248  [am_cv_python_pythondir],
249  [if test "x$am_cv_python_prefix" = x; then
250     am_py_prefix=$am__usable_prefix
251   else
252     am_py_prefix=$am_cv_python_prefix
253   fi
254   am_cv_python_pythondir=`$PYTHON -c "
255$am_python_setup_sysconfig
256if can_use_sysconfig:
257  sitedir = sysconfig.get_path('purelib', vars={'base':'$am_py_prefix'})
258else:
259  from distutils import sysconfig
260  sitedir = sysconfig.get_python_lib(0, 0, prefix='$am_py_prefix')
261sys.stdout.write(sitedir)"`
262   #
263   case $am_cv_python_pythondir in
264   $am_py_prefix*)
265     am__strip_prefix=`echo "$am_py_prefix" | sed 's|.|.|g'`
266     am_cv_python_pythondir=`echo "$am_cv_python_pythondir" | sed "s,^$am__strip_prefix,\\${PYTHON_PREFIX},"`
267     ;;
268   *)
269     case $am_py_prefix in
270       /usr|/System*) ;;
271       *) am_cv_python_pythondir="\${PYTHON_PREFIX}/lib/python$PYTHON_VERSION/site-packages"
272          ;;
273     esac
274     ;;
275   esac
276  ])
277  AC_SUBST([pythondir], [$am_cv_python_pythondir])
278
279  dnl 2. pkgpythondir: $PACKAGE directory under pythondir.  Was
280  dnl    PYTHON_SITE_PACKAGE in previous betas, but this naming is
281  dnl    more consistent with the rest of automake.
282  dnl
283  AC_SUBST([pkgpythondir], [\${pythondir}/$PACKAGE])
284
285  dnl 3. pyexecdir: directory for installing python extension modules
286  dnl    (shared libraries).
287  dnl Query distutils for this directory.
288  dnl
289  AC_CACHE_CHECK([for $am_display_PYTHON extension module directory (pyexecdir)],
290  [am_cv_python_pyexecdir],
291  [if test "x$am_cv_python_exec_prefix" = x; then
292     am_py_exec_prefix=$am__usable_exec_prefix
293   else
294     am_py_exec_prefix=$am_cv_python_exec_prefix
295   fi
296   am_cv_python_pyexecdir=`$PYTHON -c "
297$am_python_setup_sysconfig
298if can_use_sysconfig:
299  sitedir = sysconfig.get_path('platlib', vars={'platbase':'$am_py_exec_prefix'})
300else:
301  from distutils import sysconfig
302  sitedir = sysconfig.get_python_lib(1, 0, prefix='$am_py_exec_prefix')
303sys.stdout.write(sitedir)"`
304   #
305   case $am_cv_python_pyexecdir in
306   $am_py_exec_prefix*)
307     am__strip_prefix=`echo "$am_py_exec_prefix" | sed 's|.|.|g'`
308     am_cv_python_pyexecdir=`echo "$am_cv_python_pyexecdir" | sed "s,^$am__strip_prefix,\\${PYTHON_EXEC_PREFIX},"`
309     ;;
310   *)
311     case $am_py_exec_prefix in
312       /usr|/System*) ;;
313       *) am_cv_python_pyexecdir="\${PYTHON_EXEC_PREFIX}/lib/python$PYTHON_VERSION/site-packages"
314          ;;
315     esac
316     ;;
317   esac
318  ])
319  AC_SUBST([pyexecdir], [$am_cv_python_pyexecdir])
320
321  dnl 4. pkgpyexecdir: $(pyexecdir)/$(PACKAGE)
322  dnl
323  AC_SUBST([pkgpyexecdir], [\${pyexecdir}/$PACKAGE])
324
325  dnl Run any user-specified action.
326  $2
327  fi
328])
329
330
331# AM_PYTHON_CHECK_VERSION(PROG, VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
332# ---------------------------------------------------------------------------
333# Run ACTION-IF-TRUE if the Python interpreter PROG has version >= VERSION.
334# Run ACTION-IF-FALSE otherwise.
335# This test uses sys.hexversion instead of the string equivalent (first
336# word of sys.version), in order to cope with versions such as 2.2c1.
337# This supports Python 2.0 or higher. (2.0 was released on October 16, 2000).
338AC_DEFUN([AM_PYTHON_CHECK_VERSION],
339 [prog="import sys
340# split strings by '.' and convert to numeric.  Append some zeros
341# because we need at least 4 digits for the hex conversion.
342# map returns an iterator in Python 3.0 and a list in 2.x
343minver = list(map(int, '$2'.split('.'))) + [[0, 0, 0]]
344minverhex = 0
345# xrange is not present in Python 3.0 and range returns an iterator
346for i in list(range(0, 4)): minverhex = (minverhex << 8) + minver[[i]]
347sys.exit(sys.hexversion < minverhex)"
348  AS_IF([AM_RUN_LOG([$1 -c "$prog"])], [$3], [$4])])
349