Merge branch 'maint'
[platform/upstream/automake.git] / m4 / python.m4
1 ## ------------------------                                 -*- Autoconf -*-
2 ## Python file handling
3 ## From Andrew Dalke
4 ## Updated by James Henstridge
5 ## ------------------------
6 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009,
7 # 2011 Free Software Foundation, Inc.
8 #
9 # This file is free software; the Free Software Foundation
10 # gives unlimited permission to copy and/or distribute it,
11 # with or without modifications, as long as this notice is preserved.
12
13 # serial 9
14
15 # AM_PATH_PYTHON([MINIMUM-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
16 # ---------------------------------------------------------------------------
17 # Adds support for distributing Python modules and packages.  To
18 # install modules, copy them to $(pythondir), using the python_PYTHON
19 # automake variable.  To install a package with the same name as the
20 # automake package, install to $(pkgpythondir), or use the
21 # pkgpython_PYTHON automake variable.
22 #
23 # The variables $(pyexecdir) and $(pkgpyexecdir) are provided as
24 # locations to install python extension modules (shared libraries).
25 # Another macro is required to find the appropriate flags to compile
26 # extension modules.
27 #
28 # If your package is configured with a different prefix to python,
29 # users will have to add the install directory to the PYTHONPATH
30 # environment variable, or create a .pth file (see the python
31 # documentation for details).
32 #
33 # If the MINIMUM-VERSION argument is passed, AM_PATH_PYTHON will
34 # cause an error if the version of python installed on the system
35 # doesn't meet the requirement.  MINIMUM-VERSION should consist of
36 # numbers and dots only.
37 AC_DEFUN([AM_PATH_PYTHON],
38  [
39   dnl Find a Python interpreter.  Python versions prior to 2.0 are not
40   dnl supported. (2.0 was released on October 16, 2000).
41   m4_define_default([_AM_PYTHON_INTERPRETER_LIST],
42 [python python2 python3 python3.2 python3.1 python3.0 python2.7 dnl
43  python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 python2.0])
44
45   AC_ARG_VAR([PYTHON], [the Python interpreter])
46
47   m4_if([$1],[],[
48     dnl No version check is needed.
49     # Find any Python interpreter.
50     if test -z "$PYTHON"; then
51       AC_PATH_PROGS([PYTHON], _AM_PYTHON_INTERPRETER_LIST, :)
52     fi
53     am_display_PYTHON=python
54   ], [
55     dnl A version check is needed.
56     if test -n "$PYTHON"; then
57       # If the user set $PYTHON, use it and don't search something else.
58       AC_MSG_CHECKING([whether $PYTHON version is >= $1])
59       AM_PYTHON_CHECK_VERSION([$PYTHON], [$1],
60                               [AC_MSG_RESULT([yes])],
61                               [AC_MSG_RESULT([no])
62                                AC_MSG_ERROR([Python interpreter is too old])])
63       am_display_PYTHON=$PYTHON
64     else
65       # Otherwise, try each interpreter until we find one that satisfies
66       # VERSION.
67       AC_CACHE_CHECK([for a Python interpreter with version >= $1],
68         [am_cv_pathless_PYTHON],[
69         for am_cv_pathless_PYTHON in _AM_PYTHON_INTERPRETER_LIST none; do
70           test "$am_cv_pathless_PYTHON" = none && break
71           AM_PYTHON_CHECK_VERSION([$am_cv_pathless_PYTHON], [$1], [break])
72         done])
73       # Set $PYTHON to the absolute path of $am_cv_pathless_PYTHON.
74       if test "$am_cv_pathless_PYTHON" = none; then
75         PYTHON=:
76       else
77         AC_PATH_PROG([PYTHON], [$am_cv_pathless_PYTHON])
78       fi
79       am_display_PYTHON=$am_cv_pathless_PYTHON
80     fi
81   ])
82
83   if test "$PYTHON" = :; then
84   dnl Run any user-specified action, or abort.
85     m4_default([$3], [AC_MSG_ERROR([no suitable Python interpreter found])])
86   else
87
88   dnl Query Python for its version number.  Getting [:3] seems to be
89   dnl the best way to do this; it's what "site.py" does in the standard
90   dnl library.
91
92   AC_CACHE_CHECK([for $am_display_PYTHON version], [am_cv_python_version],
93     [am_cv_python_version=`$PYTHON -c "import sys; sys.stdout.write(sys.version[[:3]])"`])
94   AC_SUBST([PYTHON_VERSION], [$am_cv_python_version])
95
96   dnl Use the values of $prefix and $exec_prefix for the corresponding
97   dnl values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.  These are made
98   dnl distinct variables so they can be overridden if need be.  However,
99   dnl general consensus is that you shouldn't need this ability.
100
101   AC_SUBST([PYTHON_PREFIX], ['${prefix}'])
102   AC_SUBST([PYTHON_EXEC_PREFIX], ['${exec_prefix}'])
103
104   dnl At times (like when building shared libraries) you may want
105   dnl to know which OS platform Python thinks this is.
106
107   AC_CACHE_CHECK([for $am_display_PYTHON platform], [am_cv_python_platform],
108     [am_cv_python_platform=`$PYTHON -c "import sys; sys.stdout.write(sys.platform)"`])
109   AC_SUBST([PYTHON_PLATFORM], [$am_cv_python_platform])
110
111
112   dnl Set up 4 directories:
113
114   dnl pythondir -- where to install python scripts.  This is the
115   dnl   site-packages directory, not the python standard library
116   dnl   directory like in previous automake betas.  This behavior
117   dnl   is more consistent with lispdir.m4 for example.
118   dnl Query distutils for this directory.
119   AC_CACHE_CHECK([for $am_display_PYTHON script directory],
120     [am_cv_python_pythondir],
121     [if test "x$prefix" = xNONE
122      then
123        am_py_prefix=$ac_default_prefix
124      else
125        am_py_prefix=$prefix
126      fi
127      am_cv_python_pythondir=`$PYTHON -c "import sys; from distutils import sysconfig; sys.stdout.write(sysconfig.get_python_lib(0,0,prefix='$am_py_prefix'))" 2>/dev/null`
128      case $am_cv_python_pythondir in
129      $am_py_prefix*)
130        am__strip_prefix=`echo "$am_py_prefix" | sed 's|.|.|g'`
131        am_cv_python_pythondir=`echo "$am_cv_python_pythondir" | sed "s,^$am__strip_prefix,$PYTHON_PREFIX,"`
132        ;;
133      *)
134        case $am_py_prefix in
135          /usr|/System*) ;;
136          *)
137           am_cv_python_pythondir=$PYTHON_PREFIX/lib/python$PYTHON_VERSION/site-packages
138           ;;
139        esac
140        ;;
141      esac
142     ])
143   AC_SUBST([pythondir], [$am_cv_python_pythondir])
144
145   dnl pkgpythondir -- $PACKAGE directory under pythondir.  Was
146   dnl   PYTHON_SITE_PACKAGE in previous betas, but this naming is
147   dnl   more consistent with the rest of automake.
148
149   AC_SUBST([pkgpythondir], [\${pythondir}/$PACKAGE])
150
151   dnl pyexecdir -- directory for installing python extension modules
152   dnl   (shared libraries)
153   dnl Query distutils for this directory.
154   AC_CACHE_CHECK([for $am_display_PYTHON extension module directory],
155     [am_cv_python_pyexecdir],
156     [if test "x$exec_prefix" = xNONE
157      then
158        am_py_exec_prefix=$am_py_prefix
159      else
160        am_py_exec_prefix=$exec_prefix
161      fi
162      am_cv_python_pyexecdir=`$PYTHON -c "import sys; from distutils import sysconfig; sys.stdout.write(sysconfig.get_python_lib(1,0,prefix='$am_py_exec_prefix'))" 2>/dev/null`
163      case $am_cv_python_pyexecdir in
164      $am_py_exec_prefix*)
165        am__strip_prefix=`echo "$am_py_exec_prefix" | sed 's|.|.|g'`
166        am_cv_python_pyexecdir=`echo "$am_cv_python_pyexecdir" | sed "s,^$am__strip_prefix,$PYTHON_EXEC_PREFIX,"`
167        ;;
168      *)
169        case $am_py_exec_prefix in
170          /usr|/System*) ;;
171          *)
172            am_cv_python_pyexecdir=$PYTHON_EXEC_PREFIX/lib/python$PYTHON_VERSION/site-packages
173            ;;
174        esac
175        ;;
176      esac
177     ])
178   AC_SUBST([pyexecdir], [$am_cv_python_pyexecdir])
179
180   dnl pkgpyexecdir -- $(pyexecdir)/$(PACKAGE)
181
182   AC_SUBST([pkgpyexecdir], [\${pyexecdir}/$PACKAGE])
183
184   dnl Run any user-specified action.
185   $2
186   fi
187
188 ])
189
190
191 # AM_PYTHON_CHECK_VERSION(PROG, VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
192 # ---------------------------------------------------------------------------
193 # Run ACTION-IF-TRUE if the Python interpreter PROG has version >= VERSION.
194 # Run ACTION-IF-FALSE otherwise.
195 # This test uses sys.hexversion instead of the string equivalent (first
196 # word of sys.version), in order to cope with versions such as 2.2c1.
197 # This supports Python 2.0 or higher. (2.0 was released on October 16, 2000).
198 AC_DEFUN([AM_PYTHON_CHECK_VERSION],
199  [prog="import sys
200 # split strings by '.' and convert to numeric.  Append some zeros
201 # because we need at least 4 digits for the hex conversion.
202 # map returns an iterator in Python 3.0 and a list in 2.x
203 minver = list(map(int, '$2'.split('.'))) + [[0, 0, 0]]
204 minverhex = 0
205 # xrange is not present in Python 3.0 and range returns an iterator
206 for i in list(range(0, 4)): minverhex = (minverhex << 8) + minver[[i]]
207 sys.exit(sys.hexversion < minverhex)"
208   AS_IF([AM_RUN_LOG([$1 -c "$prog"])], [$3], [$4])])