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