root/vtcross/trunk/config/ac_python_devel.m4 @ 323

Revision 323, 11.1 KB (checked in by trnewman, 15 years ago)

Added proper macros and cleaned up lib linking issues.

Line 
1# ===========================================================================
2#         http://www.nongnu.org/autoconf-archive/ac_python_devel.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7#   AC_PYTHON_DEVEL([version])
8#
9# DESCRIPTION
10#
11#   Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
12#   in your configure.ac.
13#
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.
18#
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.
25#
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
29#   empty string.
30#
31#   If you need to use this macro for an older Python version, please
32#   contact the authors. We're always open for feedback.
33#
34# LICENSE
35#
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>
42#
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.
47#
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.
52#
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/>.
55#
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.
63#
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.
68
69AC_DEFUN([AC_PYTHON_DEVEL],[
70        #
71        # Allow the use of a (user set) custom python version
72        #
73        AC_ARG_VAR([PYTHON_VERSION],[The installed Python
74                version to use, for example '2.3'. This string
75                will be appended to the Python interpreter
76                canonical name.])
77
78        AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
79        if test -z "$PYTHON"; then
80           AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
81           PYTHON_VERSION=""
82        fi
83
84        #
85        # Check for a version of Python >= 2.1.0
86        #
87        AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
88        ac_supports_python_ver=`$PYTHON -c "import sys; \
89                ver = sys.version.split ()[[0]]; \
90                print (ver >= '2.1.0')"`
91        if test "$ac_supports_python_ver" != "True"; then
92                if test -z "$PYTHON_NOVERSIONCHECK"; then
93                        AC_MSG_RESULT([no])
94                        AC_MSG_FAILURE([
95This version of the AC@&t@_PYTHON_DEVEL macro
96doesn't work properly with versions of Python before
972.1.0. You may need to re-run configure, setting the
98variables PYTHON_CPPFLAGS, PYTHON_LDFLAGS, PYTHON_SITE_PKG,
99PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
100Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
101to something else than an empty string.
102])
103                else
104                        AC_MSG_RESULT([skip at user request])
105                fi
106        else
107                AC_MSG_RESULT([yes])
108        fi
109
110        #
111        # if the macro parameter ``version'' is set, honour it
112        #
113        if test -n "$1"; then
114                AC_MSG_CHECKING([for a version of Python $1])
115                ac_supports_python_ver=`$PYTHON -c "import sys; \
116                        ver = sys.version.split ()[[0]]; \
117                        print (ver $1)"`
118                if test "$ac_supports_python_ver" = "True"; then
119                   AC_MSG_RESULT([yes])
120                else
121                        AC_MSG_RESULT([no])
122                        AC_MSG_ERROR([this package requires Python $1.
123If you have it installed, but it isn't the default Python
124interpreter in your system path, please pass the PYTHON_VERSION
125variable to configure. See ``configure --help'' for reference.
126])
127                        PYTHON_VERSION=""
128                fi
129        fi
130
131        #
132        # Check if you have distutils, else fail
133        #
134        AC_MSG_CHECKING([for the distutils Python package])
135        ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
136        if test -z "$ac_distutils_result"; then
137                AC_MSG_RESULT([yes])
138        else
139                AC_MSG_RESULT([no])
140                AC_MSG_ERROR([cannot import Python module "distutils".
141Please check your Python installation. The error was:
142$ac_distutils_result])
143                PYTHON_VERSION=""
144        fi
145
146        #
147        # Check for Python include path
148        #
149        AC_MSG_CHECKING([for Python include path])
150        if test -z "$PYTHON_CPPFLAGS"; then
151                python_path=`$PYTHON -c "import distutils.sysconfig; \
152                        print (distutils.sysconfig.get_python_inc ());"`
153                if test -n "${python_path}"; then
154                        python_path="-I$python_path"
155                fi
156                PYTHON_CPPFLAGS=$python_path
157        fi
158        AC_MSG_RESULT([$PYTHON_CPPFLAGS])
159        AC_SUBST([PYTHON_CPPFLAGS])
160
161        #
162        # Check for Python library path
163        #
164        AC_MSG_CHECKING([for Python library path])
165        if test -z "$PYTHON_LDFLAGS"; then
166                # (makes two attempts to ensure we've got a version number
167                # from the interpreter)
168                ac_python_version=`cat<<EOD | $PYTHON -
169
170# join all versioning strings, on some systems
171# major/minor numbers could be in different list elements
172from distutils.sysconfig import *
173ret = ''
174for e in get_config_vars ('VERSION'):
175        if (e != None):
176                ret += e
177print (ret)
178EOD`
179
180                if test -z "$ac_python_version"; then
181                        if test -n "$PYTHON_VERSION"; then
182                                ac_python_version=$PYTHON_VERSION
183                        else
184                                ac_python_version=`$PYTHON -c "import sys; \
185                                        print (sys.version[[:3]])"`
186                        fi
187                fi
188
189                # Make the versioning information available to the compiler
190                AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
191                                   [If available, contains the Python version number currently in use.])
192
193                # First, the library directory:
194                ac_python_libdir=`cat<<EOD | $PYTHON -
195
196# There should be only one
197import distutils.sysconfig
198for e in distutils.sysconfig.get_config_vars ('LIBDIR'):
199        if e != None:
200                print (e)
201                break
202EOD`
203
204                # Before checking for libpythonX.Y, we need to know
205                # the extension the OS we're on uses for libraries
206                # (we take the first one, if there's more than one fix me!):
207                ac_python_soext=`$PYTHON -c \
208                  "import distutils.sysconfig; \
209                  print (distutils.sysconfig.get_config_vars('SO')[[0]])"`
210
211                # Now, for the library:
212                ac_python_soname=`$PYTHON -c \
213                  "import distutils.sysconfig; \
214                  print (distutils.sysconfig.get_config_vars('LDLIBRARY')[[0]])"`
215
216                # Strip away extension from the end to canonicalize its name:
217                ac_python_library=`echo "$ac_python_soname" | sed "s/${ac_python_soext}$//"`
218
219                # This small piece shamelessly adapted from PostgreSQL python macro;
220                # credits goes to momjian, I think. I'd like to put the right name
221                # in the credits, if someone can point me in the right direction... ?
222                #
223                if test -n "$ac_python_libdir" -a -n "$ac_python_library" \
224                        -a x"$ac_python_library" != x"$ac_python_soname"
225                then
226                        # use the official shared library
227                        ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
228                        PYTHON_LDFLAGS="-L$ac_python_libdir -l$ac_python_library"
229                else
230                        # old way: use libpython from python_configdir
231                        ac_python_libdir=`$PYTHON -c \
232                          "from distutils.sysconfig import get_python_lib as f; \
233                          import os; \
234                          print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
235                        PYTHON_LDFLAGS="-L$ac_python_libdir -lpython$ac_python_version"
236                fi
237
238                if test -z "PYTHON_LDFLAGS"; then
239                        AC_MSG_ERROR([
240  Cannot determine location of your Python DSO. Please check it was installed with
241  dynamic libraries enabled, or try setting PYTHON_LDFLAGS by hand.
242                        ])
243                fi
244        fi
245        AC_MSG_RESULT([$PYTHON_LDFLAGS])
246        AC_SUBST([PYTHON_LDFLAGS])
247
248        #
249        # Check for site packages
250        #
251        AC_MSG_CHECKING([for Python site-packages path])
252        if test -z "$PYTHON_SITE_PKG"; then
253                PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
254                        print (distutils.sysconfig.get_python_lib(0,0));"`
255        fi
256        AC_MSG_RESULT([$PYTHON_SITE_PKG])
257        AC_SUBST([PYTHON_SITE_PKG])
258
259        #
260        # libraries which must be linked in when embedding
261        #
262        AC_MSG_CHECKING(python extra libraries)
263        if test -z "$PYTHON_EXTRA_LIBS"; then
264           PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
265                conf = distutils.sysconfig.get_config_var; \
266                print (conf('LOCALMODLIBS') + ' ' + conf('LIBS'))"`
267        fi
268        AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
269        AC_SUBST(PYTHON_EXTRA_LIBS)
270
271        #
272        # linking flags needed when embedding
273        #
274        AC_MSG_CHECKING(python extra linking flags)
275        if test -z "$PYTHON_EXTRA_LDFLAGS"; then
276                PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
277                        conf = distutils.sysconfig.get_config_var; \
278                        print (conf('LINKFORSHARED'))"`
279        fi
280        AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
281        AC_SUBST(PYTHON_EXTRA_LDFLAGS)
282
283        #
284        # final check to see if everything compiles alright
285        #
286        AC_MSG_CHECKING([consistency of all components of python development environment])
287        # save current global flags
288        ac_save_LIBS="$LIBS"
289        ac_save_CPPFLAGS="$CPPFLAGS"
290        LIBS="$ac_save_LIBS $PYTHON_LDFLAGS $PYTHON_EXTRA_LDFLAGS $PYTHON_EXTRA_LIBS"
291        CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
292        AC_LANG_PUSH([C])
293        AC_LINK_IFELSE([
294                AC_LANG_PROGRAM([[#include <Python.h>]],
295                                [[Py_Initialize();]])
296                ],[pythonexists=yes],[pythonexists=no])
297        AC_LANG_POP([C])
298        # turn back to default flags
299        CPPFLAGS="$ac_save_CPPFLAGS"
300        LIBS="$ac_save_LIBS"
301
302        AC_MSG_RESULT([$pythonexists])
303
304        if test ! "x$pythonexists" = "xyes"; then
305           AC_MSG_FAILURE([
306  Could not link test program to Python. Maybe the main Python library has been
307  installed in some non-standard library path. If so, pass it to configure,
308  via the LDFLAGS environment variable.
309  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
310  ============================================================================
311   ERROR!
312   You probably have to install the development version of the Python package
313   for your distribution.  The exact name of this package varies among them.
314  ============================================================================
315           ])
316          PYTHON_VERSION=""
317        fi
318
319        #
320        # all done!
321        #
322])
Note: See TracBrowser for help on using the browser.