Rearrange the python scripting support a bit.
[screen-lua.git] / src / acinclude.m4
blob8cc86f7b615f0a75367f4797dba68d25d578f5b2
1 # Ripped from pygobject-2.0
3 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
4 # Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 # 02111-1307, USA.
21 # AM_PATH_PYTHON([MINIMUM-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
23 # Adds support for distributing Python modules and packages.  To
24 # install modules, copy them to $(pythondir), using the python_PYTHON
25 # automake variable.  To install a package with the same name as the
26 # automake package, install to $(pkgpythondir), or use the
27 # pkgpython_PYTHON automake variable.
29 # The variables $(pyexecdir) and $(pkgpyexecdir) are provided as
30 # locations to install python extension modules (shared libraries).
31 # Another macro is required to find the appropriate flags to compile
32 # extension modules.
34 # If your package is configured with a different prefix to python,
35 # users will have to add the install directory to the PYTHONPATH
36 # environment variable, or create a .pth file (see the python
37 # documentation for details).
39 # If the MINIMUM-VERSION argument is passed, AM_PATH_PYTHON will
40 # cause an error if the version of python installed on the system
41 # doesn't meet the requirement.  MINIMUM-VERSION should consist of
42 # numbers and dots only.
44 AC_DEFUN([AM_PATH_PYTHON],
45  [
46   dnl Find a Python interpreter.  Python versions prior to 1.5 are not
47   dnl supported because the default installation locations changed from
48   dnl $prefix/lib/site-python in 1.4 to $prefix/lib/python1.5/site-packages
49   dnl in 1.5.
50   m4_define([_AM_PYTHON_INTERPRETER_LIST],
51             [python2.5 python python2 python2.4 python2.3 python2.2 dnl
52 python2.1 python2.0 python1.6 python1.5])
54   m4_if([$1],[],[
55     dnl No version check is needed.
56     # Find any Python interpreter.
57     if test -z "$PYTHON"; then
58       PYTHON=:
59       AC_PATH_PROGS([PYTHON], _AM_PYTHON_INTERPRETER_LIST)
60     fi
61     am_display_PYTHON=python
62   ], [
63     dnl A version check is needed.
64     if test -n "$PYTHON"; then
65       # If the user set $PYTHON, use it and don't search something else.
66       AC_MSG_CHECKING([whether $PYTHON version >= $1])
67       AM_PYTHON_CHECK_VERSION([$PYTHON], [$1],
68                               [AC_MSG_RESULT(yes)],
69                               [AC_MSG_ERROR(too old)])
70       am_display_PYTHON=$PYTHON
71     else
72       # Otherwise, try each interpreter until we find one that satisfies
73       # VERSION.
74       AC_CACHE_CHECK([for a Python interpreter with version >= $1],
75         [am_cv_pathless_PYTHON],[
76         for am_cv_pathless_PYTHON in _AM_PYTHON_INTERPRETER_LIST none; do
77           test "$am_cv_pathless_PYTHON" = none && break
78           AM_PYTHON_CHECK_VERSION([$am_cv_pathless_PYTHON], [$1], [break])
79         done])
80       # Set $PYTHON to the absolute path of $am_cv_pathless_PYTHON.
81       if test "$am_cv_pathless_PYTHON" = none; then
82         PYTHON=:
83       else
84         AC_PATH_PROG([PYTHON], [$am_cv_pathless_PYTHON])
85       fi
86       am_display_PYTHON=$am_cv_pathless_PYTHON
87     fi
88   ])
90   if test "$PYTHON" = :; then
91   dnl Run any user-specified action, or abort.
92     m4_default([$3], [AC_MSG_ERROR([no suitable Python interpreter found])])
93   else
95   dnl Query Python for its version number.  Getting [:3] seems to be
96   dnl the best way to do this; it's what "site.py" does in the standard
97   dnl library.
99   AC_CACHE_CHECK([for $am_display_PYTHON version], [am_cv_python_version],
100     [am_cv_python_version=`$PYTHON -c "import sys; print sys.version[[:3]]"`])
101   AC_SUBST([PYTHON_VERSION], [$am_cv_python_version])
103   dnl Use the values of $prefix and $exec_prefix for the corresponding
104   dnl values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.  These are made
105   dnl distinct variables so they can be overridden if need be.  However,
106   dnl general consensus is that you shouldn't need this ability.
108   AC_SUBST([PYTHON_PREFIX], ['${prefix}'])
109   AC_SUBST([PYTHON_EXEC_PREFIX], ['${exec_prefix}'])
111   dnl At times (like when building shared libraries) you may want
112   dnl to know which OS platform Python thinks this is.
114   AC_CACHE_CHECK([for $am_display_PYTHON platform], [am_cv_python_platform],
115     [am_cv_python_platform=`$PYTHON -c "import sys; print sys.platform"`])
116   AC_SUBST([PYTHON_PLATFORM], [$am_cv_python_platform])
119   dnl Set up 4 directories:
121   dnl pythondir -- where to install python scripts.  This is the
122   dnl   site-packages directory, not the python standard library
123   dnl   directory like in previous automake betas.  This behavior
124   dnl   is more consistent with lispdir.m4 for example.
125   dnl Query distutils for this directory.  distutils does not exist in
126   dnl Python 1.5, so we fall back to the hardcoded directory if it
127   dnl doesn't work.
128   AC_CACHE_CHECK([for $am_display_PYTHON script directory],
129     [am_cv_python_pythondir],
130     [am_cv_python_pythondir=`$PYTHON -c "from distutils import sysconfig; print sysconfig.get_python_lib(0,0,prefix='$PYTHON_PREFIX')" 2>/dev/null ||
131      echo "$PYTHON_PREFIX/lib/python$PYTHON_VERSION/site-packages"`])
132   AC_SUBST([pythondir], [$am_cv_python_pythondir])
134   dnl pkgpythondir -- $PACKAGE directory under pythondir.  Was
135   dnl   PYTHON_SITE_PACKAGE in previous betas, but this naming is
136   dnl   more consistent with the rest of automake.
138   AC_SUBST([pkgpythondir], [\${pythondir}/$PACKAGE])
140   dnl pyexecdir -- directory for installing python extension modules
141   dnl   (shared libraries)
142   dnl Query distutils for this directory.  distutils does not exist in
143   dnl Python 1.5, so we fall back to the hardcoded directory if it
144   dnl doesn't work.
145   AC_CACHE_CHECK([for $am_display_PYTHON extension module directory],
146     [am_cv_python_pyexecdir],
147     [am_cv_python_pyexecdir=`$PYTHON -c "from distutils import sysconfig; print sysconfig.get_python_lib(1,0,prefix='$PYTHON_EXEC_PREFIX')" 2>/dev/null ||
148      echo "${PYTHON_EXEC_PREFIX}/lib/python${PYTHON_VERSION}/site-packages"`])
149   AC_SUBST([pyexecdir], [$am_cv_python_pyexecdir])
151   dnl pkgpyexecdir -- $(pyexecdir)/$(PACKAGE)
153   AC_SUBST([pkgpyexecdir], [\${pyexecdir}/$PACKAGE])
155   dnl Run any user-specified action.
156   $2
157   fi
162 # AM_PYTHON_CHECK_VERSION(PROG, VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
163 # ---------------------------------------------------------------------------
164 # Run ACTION-IF-TRUE if the Python interpreter PROG has version >= VERSION.
165 # Run ACTION-IF-FALSE otherwise.
166 # This test uses sys.hexversion instead of the string equivalent (first
167 # word of sys.version), in order to cope with versions such as 2.2c1.
168 # hexversion has been introduced in Python 1.5.2; it's probably not
169 # worth to support older versions (1.5.1 was released on October 31, 1998).
170 AC_DEFUN([AM_PYTHON_CHECK_VERSION],
171  [prog="import sys, string
172 # split strings by '.' and convert to numeric.  Append some zeros
173 # because we need at least 4 digits for the hex conversion.
174 minver = map(int, string.split('$2', '.')) + [[0, 0, 0]]
175 minverhex = 0
176 for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
177 sys.exit(sys.hexversion < minverhex)"
178   AS_IF([AM_RUN_LOG([$1 -c "$prog"])], [$3], [$4])])
180 dnl a macro to check for ability to create python extensions
181 dnl  AM_CHECK_PYTHON_HEADERS([ACTION-IF-POSSIBLE], [ACTION-IF-NOT-POSSIBLE])
182 dnl function also defines PYTHON_INCLUDES
183 AC_DEFUN([AM_CHECK_PYTHON_HEADERS],
184 [AC_REQUIRE([AM_PATH_PYTHON])
185 AC_MSG_CHECKING(for headers required to compile python extensions)
186 dnl deduce PYTHON_INCLUDES
187 py_prefix=`$PYTHON -c "import sys; print sys.prefix"`
188 py_exec_prefix=`$PYTHON -c "import sys; print sys.exec_prefix"`
189 if test -x "$PYTHON-config"; then
190 PYTHON_INCLUDES=`$PYTHON-config --includes 2>/dev/null`
191 PYTHON_LIBS=`$PYTHON-config --libs 2>/dev/null`
192 else
193 PYTHON_INCLUDES="-I${py_prefix}/include/python${PYTHON_VERSION}"
194 if test "$py_prefix" != "$py_exec_prefix"; then
195   PYTHON_INCLUDES="$PYTHON_INCLUDES -I${py_exec_prefix}/include/python${PYTHON_VERSION}"
198 AC_SUBST(PYTHON_INCLUDES)
199 AC_SUBST(PYTHON_LIBS)
200 dnl check if the headers exist:
201 save_CPPFLAGS="$CPPFLAGS"
202 CPPFLAGS="$CPPFLAGS $PYTHON_INCLUDES"
203 AC_TRY_CPP([#include <Python.h>],dnl
204 [AC_MSG_RESULT(found)
205 $1],dnl
206 [AC_MSG_RESULT(not found)
207 $2])
208 CPPFLAGS="$save_CPPFLAGS"