Merge branch 'minor'
[automake.git] / m4 / python.m4
blob6d9e934010992688b79d40ad33bc0f261f477e69
1 ## ------------------------                                 -*- Autoconf -*-
2 ## Python file handling
3 ## From Andrew Dalke
4 ## Updated by James Henstridge
5 ## ------------------------
6 # Copyright (C) 1999-2017 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   dnl FIXME: Remove the need to hard-code Python versions here.
40   m4_define_default([_AM_PYTHON_INTERPRETER_LIST],
41 [python python2 python3 python3.5 python3.4 python3.3 python3.2 python3.1 python3.0 python2.7 dnl
42  python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 python2.0])
44   AC_ARG_VAR([PYTHON], [the Python interpreter])
46   m4_if([$1],[],[
47     dnl No version check is needed.
48     # Find any Python interpreter.
49     if test -z "$PYTHON"; then
50       AC_PATH_PROGS([PYTHON], _AM_PYTHON_INTERPRETER_LIST, :)
51     fi
52     am_display_PYTHON=python
53   ], [
54     dnl A version check is needed.
55     if test -n "$PYTHON"; then
56       # If the user set $PYTHON, use it and don't search something else.
57       AC_MSG_CHECKING([whether $PYTHON version is >= $1])
58       AM_PYTHON_CHECK_VERSION([$PYTHON], [$1],
59                               [AC_MSG_RESULT([yes])],
60                               [AC_MSG_RESULT([no])
61                                AC_MSG_ERROR([Python interpreter is too old])])
62       am_display_PYTHON=$PYTHON
63     else
64       # Otherwise, try each interpreter until we find one that satisfies
65       # VERSION.
66       AC_CACHE_CHECK([for a Python interpreter with version >= $1],
67         [am_cv_pathless_PYTHON],[
68         for am_cv_pathless_PYTHON in _AM_PYTHON_INTERPRETER_LIST none; do
69           test "$am_cv_pathless_PYTHON" = none && break
70           AM_PYTHON_CHECK_VERSION([$am_cv_pathless_PYTHON], [$1], [break])
71         done])
72       # Set $PYTHON to the absolute path of $am_cv_pathless_PYTHON.
73       if test "$am_cv_pathless_PYTHON" = none; then
74         PYTHON=:
75       else
76         AC_PATH_PROG([PYTHON], [$am_cv_pathless_PYTHON])
77       fi
78       am_display_PYTHON=$am_cv_pathless_PYTHON
79     fi
80   ])
82   if test "$PYTHON" = :; then
83   dnl Run any user-specified action, or abort.
84     m4_default([$3], [AC_MSG_ERROR([no suitable Python interpreter found])])
85   else
87   dnl Query Python for its version number.  Getting [:3] seems to be
88   dnl the best way to do this; it's what "site.py" does in the standard
89   dnl library.
91   AC_CACHE_CHECK([for $am_display_PYTHON version], [am_cv_python_version],
92     [am_cv_python_version=`$PYTHON -c "import sys; sys.stdout.write(sys.version[[:3]])"`])
93   AC_SUBST([PYTHON_VERSION], [$am_cv_python_version])
95   dnl Use the values of $prefix and $exec_prefix for the corresponding
96   dnl values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.  These are made
97   dnl distinct variables so they can be overridden if need be.  However,
98   dnl general consensus is that you shouldn't need this ability.
100   AC_SUBST([PYTHON_PREFIX], ['${prefix}'])
101   AC_SUBST([PYTHON_EXEC_PREFIX], ['${exec_prefix}'])
103   dnl At times (like when building shared libraries) you may want
104   dnl to know which OS platform Python thinks this is.
106   AC_CACHE_CHECK([for $am_display_PYTHON platform], [am_cv_python_platform],
107     [am_cv_python_platform=`$PYTHON -c "import sys; sys.stdout.write(sys.platform)"`])
108   AC_SUBST([PYTHON_PLATFORM], [$am_cv_python_platform])
110   # Just factor out some code duplication.
111   am_python_setup_sysconfig="\
112 import sys
113 # Prefer sysconfig over distutils.sysconfig, for better compatibility
114 # with python 3.x.  See automake bug#10227.
115 try:
116     import sysconfig
117 except ImportError:
118     can_use_sysconfig = 0
119 else:
120     can_use_sysconfig = 1
121 # Can't use sysconfig in CPython 2.7, since it's broken in virtualenvs:
122 # <https://github.com/pypa/virtualenv/issues/118>
123 try:
124     from platform import python_implementation
125     if python_implementation() == 'CPython' and sys.version[[:3]] == '2.7':
126         can_use_sysconfig = 0
127 except ImportError:
128     pass"
130   dnl Set up 4 directories:
132   dnl pythondir -- where to install python scripts.  This is the
133   dnl   site-packages directory, not the python standard library
134   dnl   directory like in previous automake betas.  This behavior
135   dnl   is more consistent with lispdir.m4 for example.
136   dnl Query distutils for this directory.
137   AC_CACHE_CHECK([for $am_display_PYTHON script directory],
138     [am_cv_python_pythondir],
139     [if test "x$prefix" = xNONE
140      then
141        am_py_prefix=$ac_default_prefix
142      else
143        am_py_prefix=$prefix
144      fi
145      am_cv_python_pythondir=`$PYTHON -c "
146 $am_python_setup_sysconfig
147 if can_use_sysconfig:
148     sitedir = sysconfig.get_path('purelib', vars={'base':'$am_py_prefix'})
149 else:
150     from distutils import sysconfig
151     sitedir = sysconfig.get_python_lib(0, 0, prefix='$am_py_prefix')
152 sys.stdout.write(sitedir)"`
153      case $am_cv_python_pythondir in
154      $am_py_prefix*)
155        am__strip_prefix=`echo "$am_py_prefix" | sed 's|.|.|g'`
156        am_cv_python_pythondir=`echo "$am_cv_python_pythondir" | sed "s,^$am__strip_prefix,$PYTHON_PREFIX,"`
157        ;;
158      *)
159        case $am_py_prefix in
160          /usr|/System*) ;;
161          *)
162           am_cv_python_pythondir=$PYTHON_PREFIX/lib/python$PYTHON_VERSION/site-packages
163           ;;
164        esac
165        ;;
166      esac
167     ])
168   AC_SUBST([pythondir], [$am_cv_python_pythondir])
170   dnl pkgpythondir -- $PACKAGE directory under pythondir.  Was
171   dnl   PYTHON_SITE_PACKAGE in previous betas, but this naming is
172   dnl   more consistent with the rest of automake.
174   AC_SUBST([pkgpythondir], [\${pythondir}/$PACKAGE])
176   dnl pyexecdir -- directory for installing python extension modules
177   dnl   (shared libraries)
178   dnl Query distutils for this directory.
179   AC_CACHE_CHECK([for $am_display_PYTHON extension module directory],
180     [am_cv_python_pyexecdir],
181     [if test "x$exec_prefix" = xNONE
182      then
183        am_py_exec_prefix=$am_py_prefix
184      else
185        am_py_exec_prefix=$exec_prefix
186      fi
187      am_cv_python_pyexecdir=`$PYTHON -c "
188 $am_python_setup_sysconfig
189 if can_use_sysconfig:
190     sitedir = sysconfig.get_path('platlib', vars={'platbase':'$am_py_prefix'})
191 else:
192     from distutils import sysconfig
193     sitedir = sysconfig.get_python_lib(1, 0, prefix='$am_py_prefix')
194 sys.stdout.write(sitedir)"`
195      case $am_cv_python_pyexecdir in
196      $am_py_exec_prefix*)
197        am__strip_prefix=`echo "$am_py_exec_prefix" | sed 's|.|.|g'`
198        am_cv_python_pyexecdir=`echo "$am_cv_python_pyexecdir" | sed "s,^$am__strip_prefix,$PYTHON_EXEC_PREFIX,"`
199        ;;
200      *)
201        case $am_py_exec_prefix in
202          /usr|/System*) ;;
203          *)
204            am_cv_python_pyexecdir=$PYTHON_EXEC_PREFIX/lib/python$PYTHON_VERSION/site-packages
205            ;;
206        esac
207        ;;
208      esac
209     ])
210   AC_SUBST([pyexecdir], [$am_cv_python_pyexecdir])
212   dnl pkgpyexecdir -- $(pyexecdir)/$(PACKAGE)
214   AC_SUBST([pkgpyexecdir], [\${pyexecdir}/$PACKAGE])
216   dnl Run any user-specified action.
217   $2
218   fi
223 # AM_PYTHON_CHECK_VERSION(PROG, VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
224 # ---------------------------------------------------------------------------
225 # Run ACTION-IF-TRUE if the Python interpreter PROG has version >= VERSION.
226 # Run ACTION-IF-FALSE otherwise.
227 # This test uses sys.hexversion instead of the string equivalent (first
228 # word of sys.version), in order to cope with versions such as 2.2c1.
229 # This supports Python 2.0 or higher. (2.0 was released on October 16, 2000).
230 AC_DEFUN([AM_PYTHON_CHECK_VERSION],
231  [prog="import sys
232 # split strings by '.' and convert to numeric.  Append some zeros
233 # because we need at least 4 digits for the hex conversion.
234 # map returns an iterator in Python 3.0 and a list in 2.x
235 minver = list(map(int, '$2'.split('.'))) + [[0, 0, 0]]
236 minverhex = 0
237 # xrange is not present in Python 3.0 and range returns an iterator
238 for i in list(range(0, 4)): minverhex = (minverhex << 8) + minver[[i]]
239 sys.exit(sys.hexversion < minverhex)"
240   AS_IF([AM_RUN_LOG([$1 -c "$prog"])], [$3], [$4])])