1 # ===========================================================================
2 # http://www.gnu.org/software/autoconf-archive/ax_python_devel.html
3 # ===========================================================================
7 # AX_PYTHON_DEVEL([version])
11 # Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
12 # in your configure.ac.
14 # This macro checks for Python and tries to get the include path to
15 # 'Python.h'. It provides the $(PYTHON_CPPFLAGS) and $(PYTHON_LDFLAGS)
16 # output variables. It also exports $(PYTHON_EXTRA_LIBS) and
17 # $(PYTHON_EXTRA_LDFLAGS) for embedding Python in your code.
19 # You can search for some particular version of Python by passing a
20 # parameter to this macro, for example ">= '2.3.1'", or "== '2.4'". Please
21 # note that you *have* to pass also an operator along with the version to
22 # match, and pay special attention to the single quotes surrounding the
23 # version number. Don't use "PYTHON_VERSION" for this: that environment
24 # variable is declared as precious and thus reserved for the end-user.
26 # This macro should work for all versions of Python >= 2.1.0. As an end
27 # user, you can disable the check for the python version by setting the
28 # PYTHON_NOVERSIONCHECK environment variable to something else than the
31 # If you need to use this macro for an older Python version, please
32 # contact the authors. We're always open for feedback.
36 # Copyright (c) 2009 Sebastian Huber <sebastian-huber@web.de>
37 # Copyright (c) 2009 Alan W. Irwin <irwin@beluga.phys.uvic.ca>
38 # Copyright (c) 2009 Rafael Laboissiere <rafael@laboissiere.net>
39 # Copyright (c) 2009 Andrew Collier <colliera@ukzn.ac.za>
40 # Copyright (c) 2009 Matteo Settenvini <matteo@member.fsf.org>
41 # Copyright (c) 2009 Horst Knorr <hk_classes@knoda.org>
43 # This program is free software: you can redistribute it and/or modify it
44 # under the terms of the GNU General Public License as published by the
45 # Free Software Foundation, either version 3 of the License, or (at your
46 # option) any later version.
48 # This program is distributed in the hope that it will be useful, but
49 # WITHOUT ANY WARRANTY; without even the implied warranty of
50 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
51 # Public License for more details.
53 # You should have received a copy of the GNU General Public License along
54 # with this program. If not, see <http://www.gnu.org/licenses/>.
56 # As a special exception, the respective Autoconf Macro's copyright owner
57 # gives unlimited permission to copy, distribute and modify the configure
58 # scripts that are the output of Autoconf when processing the Macro. You
59 # need not follow the terms of the GNU General Public License when using
60 # or distributing such scripts, even though portions of the text of the
61 # Macro appear in them. The GNU General Public License (GPL) does govern
62 # all other use of the material that constitutes the Autoconf Macro.
64 # This special exception to the GPL applies to versions of the Autoconf
65 # Macro released by the Autoconf Archive. When you make and distribute a
66 # modified version of the Autoconf Macro, you may extend this special
67 # exception to the GPL to apply to your modified version as well.
71 AU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
72 AC_DEFUN([AX_PYTHON_DEVEL],[
74 # Allow the use of a (user set) custom python version
76 AC_ARG_VAR([PYTHON_VERSION],[The installed Python
77 version to use, for example '2.3'. This string
78 will be appended to the Python interpreter
81 AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
82 if test -z "$PYTHON"; then
83 AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
88 # Check for a version of Python >= 2.1.0
90 AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
91 ac_supports_python_ver=`$PYTHON -c "import sys; \
92 ver = sys.version.split ()[[0]]; \
93 print (ver >= '2.1.0')"`
94 if test "$ac_supports_python_ver" != "True"; then
95 if test -z "$PYTHON_NOVERSIONCHECK"; then
98 This version of the AC@&t@_PYTHON_DEVEL macro
99 doesn't work properly with versions of Python before
100 2.1.0. You may need to re-run configure, setting the
101 variables PYTHON_CPPFLAGS, PYTHON_LDFLAGS, PYTHON_SITE_PKG,
102 PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
103 Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
104 to something else than an empty string.
107 AC_MSG_RESULT([skip at user request])
114 # if the macro parameter ``version'' is set, honour it
116 if test -n "$1"; then
117 AC_MSG_CHECKING([for a version of Python $1])
118 ac_supports_python_ver=`$PYTHON -c "import sys; \
119 ver = sys.version.split ()[[0]]; \
121 if test "$ac_supports_python_ver" = "True"; then
125 AC_MSG_ERROR([this package requires Python $1.
126 If you have it installed, but it isn't the default Python
127 interpreter in your system path, please pass the PYTHON_VERSION
128 variable to configure. See ``configure --help'' for reference.
135 # Check if you have distutils, else fail
137 AC_MSG_CHECKING([for the distutils Python package])
138 ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
139 if test -z "$ac_distutils_result"; then
143 AC_MSG_ERROR([cannot import Python module "distutils".
144 Please check your Python installation. The error was:
145 $ac_distutils_result])
150 # Check for Python include path
152 AC_MSG_CHECKING([for Python include path])
153 if test -z "$PYTHON_CPPFLAGS"; then
154 python_path=`$PYTHON -c "import distutils.sysconfig; \
155 print (distutils.sysconfig.get_python_inc ());"`
156 if test -n "${python_path}"; then
157 python_path="-I$python_path"
159 PYTHON_CPPFLAGS=$python_path
161 AC_MSG_RESULT([$PYTHON_CPPFLAGS])
162 AC_SUBST([PYTHON_CPPFLAGS])
165 # Check for Python library path
167 AC_MSG_CHECKING([for Python library path])
168 if test -z "$PYTHON_LDFLAGS"; then
169 # (makes two attempts to ensure we've got a version number
170 # from the interpreter)
171 ac_python_version=`cat<<EOD | $PYTHON -
173 # join all versioning strings, on some systems
174 # major/minor numbers could be in different list elements
175 from distutils.sysconfig import *
177 for e in get_config_vars ('VERSION'):
183 if test -z "$ac_python_version"; then
184 if test -n "$PYTHON_VERSION"; then
185 ac_python_version=$PYTHON_VERSION
187 ac_python_version=`$PYTHON -c "import sys; \
188 print (sys.version[[:3]])"`
192 # Make the versioning information available to the compiler
193 AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
194 [If available, contains the Python version number currently in use.])
196 # First, the library directory:
197 ac_python_libdir=`cat<<EOD | $PYTHON -
199 # There should be only one
200 import distutils.sysconfig
201 for e in distutils.sysconfig.get_config_vars ('LIBDIR'):
207 # Before checking for libpythonX.Y, we need to know
208 # the extension the OS we're on uses for libraries
209 # (we take the first one, if there's more than one fix me!):
210 ac_python_soext=`$PYTHON -c \
211 "import distutils.sysconfig; \
212 print (distutils.sysconfig.get_config_vars('SO')[[0]])"`
214 # Now, for the library:
215 ac_python_soname=`$PYTHON -c \
216 "import distutils.sysconfig; \
217 print (distutils.sysconfig.get_config_vars('LDLIBRARY')[[0]])"`
219 # If we're on OS X and this is a .dylib then the distutils will
220 # identify the .so extension incorrectly. So we check if the
221 # the extension is .dylib
222 PYTHON_LIB_IS_NOT_OSX_DYLIB=`echo "$ac_python_soname" | sed "s/^.*\.dylib$//"`
223 if test -z "$PYTHON_LIB_IS_NOT_OSX_DYLIB"; then
224 ac_python_soext=".dylib"
227 # Strip away extension from the end to canonicalize its name:
228 ac_python_library=`echo "$ac_python_soname" | sed "s/${ac_python_soext}$//"`
230 # This small piece shamelessly adapted from PostgreSQL python macro;
231 # credits goes to momjian, I think. I'd like to put the right name
232 # in the credits, if someone can point me in the right direction... ?
234 if test -n "$ac_python_libdir" -a -n "$ac_python_library" \
235 -a x"$ac_python_library" != x"$ac_python_soname"
237 # use the official shared library
238 ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
239 PYTHON_LDFLAGS="-L$ac_python_libdir -l$ac_python_library"
241 # old way: use libpython from python_configdir
242 ac_python_libdir=`$PYTHON -c \
243 "from distutils.sysconfig import get_python_lib as f; \
245 print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
246 PYTHON_LDFLAGS="-L$ac_python_libdir -lpython$ac_python_version"
249 if test -z "$PYTHON_LDFLAGS"; then
251 Cannot determine location of your Python DSO. Please check it was installed with
252 dynamic libraries enabled, or try setting PYTHON_LDFLAGS by hand.
256 AC_MSG_RESULT([$PYTHON_LDFLAGS])
257 AC_SUBST([PYTHON_LDFLAGS])
260 # Check for site packages
262 AC_MSG_CHECKING([for Python site-packages path])
263 if test -z "$PYTHON_SITE_PKG"; then
264 PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
265 print (distutils.sysconfig.get_python_lib(0,0));"`
267 AC_MSG_RESULT([$PYTHON_SITE_PKG])
268 AC_SUBST([PYTHON_SITE_PKG])
271 # libraries which must be linked in when embedding
273 AC_MSG_CHECKING(python extra libraries)
274 if test -z "$PYTHON_EXTRA_LIBS"; then
275 PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
276 conf = distutils.sysconfig.get_config_var; \
277 print (conf('LOCALMODLIBS') + ' ' + conf('LIBS'))"`
279 AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
280 AC_SUBST(PYTHON_EXTRA_LIBS)
283 # linking flags needed when embedding
285 AC_MSG_CHECKING(python extra linking flags)
286 if test -z "$PYTHON_EXTRA_LDFLAGS"; then
287 PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
288 conf = distutils.sysconfig.get_config_var; \
289 print (conf('LINKFORSHARED'))"`
291 AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
292 AC_SUBST(PYTHON_EXTRA_LDFLAGS)
295 # final check to see if everything compiles alright
297 AC_MSG_CHECKING([consistency of all components of python development environment])
298 # save current global flags
300 ac_save_CPPFLAGS="$CPPFLAGS"
301 LIBS="$ac_save_LIBS $PYTHON_LDFLAGS $PYTHON_EXTRA_LDFLAGS $PYTHON_EXTRA_LIBS"
302 CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
305 AC_LANG_PROGRAM([[#include <Python.h>]],
306 [[Py_Initialize();]])
307 ],[pythonexists=yes],[pythonexists=no])
309 # turn back to default flags
310 CPPFLAGS="$ac_save_CPPFLAGS"
313 AC_MSG_RESULT([$pythonexists])
315 if test ! "x$pythonexists" = "xyes"; then
317 Could not link test program to Python. Maybe the main Python library has been
318 installed in some non-standard library path. If so, pass it to configure,
319 via the LDFLAGS environment variable.
320 Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
321 ============================================================================
323 You probably have to install the development version of the Python package
324 for your distribution. The exact name of this package varies among them.
325 ============================================================================