ax_python_devel.m4 revision 238104
1238104Sdes# ===========================================================================
2238104Sdes#      http://www.gnu.org/software/autoconf-archive/ax_python_devel.html
3238104Sdes# ===========================================================================
4238104Sdes#
5238104Sdes# SYNOPSIS
6238104Sdes#
7238104Sdes#   AX_PYTHON_DEVEL([version])
8238104Sdes#
9238104Sdes# DESCRIPTION
10238104Sdes#
11238104Sdes#   Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
12238104Sdes#   in your configure.ac.
13238104Sdes#
14238104Sdes#   This macro checks for Python and tries to get the include path to
15238104Sdes#   'Python.h'. It provides the $(PYTHON_CPPFLAGS) and $(PYTHON_LDFLAGS)
16238104Sdes#   output variables. It also exports $(PYTHON_EXTRA_LIBS) and
17238104Sdes#   $(PYTHON_EXTRA_LDFLAGS) for embedding Python in your code.
18238104Sdes#
19238104Sdes#   You can search for some particular version of Python by passing a
20238104Sdes#   parameter to this macro, for example ">= '2.3.1'", or "== '2.4'". Please
21238104Sdes#   note that you *have* to pass also an operator along with the version to
22238104Sdes#   match, and pay special attention to the single quotes surrounding the
23238104Sdes#   version number. Don't use "PYTHON_VERSION" for this: that environment
24238104Sdes#   variable is declared as precious and thus reserved for the end-user.
25238104Sdes#
26238104Sdes#   This macro should work for all versions of Python >= 2.1.0. As an end
27238104Sdes#   user, you can disable the check for the python version by setting the
28238104Sdes#   PYTHON_NOVERSIONCHECK environment variable to something else than the
29238104Sdes#   empty string.
30238104Sdes#
31238104Sdes#   If you need to use this macro for an older Python version, please
32238104Sdes#   contact the authors. We're always open for feedback.
33238104Sdes#
34238104Sdes# LICENSE
35238104Sdes#
36238104Sdes#   Copyright (c) 2009 Sebastian Huber <sebastian-huber@web.de>
37238104Sdes#   Copyright (c) 2009 Alan W. Irwin <irwin@beluga.phys.uvic.ca>
38238104Sdes#   Copyright (c) 2009 Rafael Laboissiere <rafael@laboissiere.net>
39238104Sdes#   Copyright (c) 2009 Andrew Collier <colliera@ukzn.ac.za>
40238104Sdes#   Copyright (c) 2009 Matteo Settenvini <matteo@member.fsf.org>
41238104Sdes#   Copyright (c) 2009 Horst Knorr <hk_classes@knoda.org>
42238104Sdes#
43238104Sdes#   This program is free software: you can redistribute it and/or modify it
44238104Sdes#   under the terms of the GNU General Public License as published by the
45238104Sdes#   Free Software Foundation, either version 3 of the License, or (at your
46238104Sdes#   option) any later version.
47238104Sdes#
48238104Sdes#   This program is distributed in the hope that it will be useful, but
49238104Sdes#   WITHOUT ANY WARRANTY; without even the implied warranty of
50238104Sdes#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
51238104Sdes#   Public License for more details.
52238104Sdes#
53238104Sdes#   You should have received a copy of the GNU General Public License along
54238104Sdes#   with this program. If not, see <http://www.gnu.org/licenses/>.
55238104Sdes#
56238104Sdes#   As a special exception, the respective Autoconf Macro's copyright owner
57238104Sdes#   gives unlimited permission to copy, distribute and modify the configure
58238104Sdes#   scripts that are the output of Autoconf when processing the Macro. You
59238104Sdes#   need not follow the terms of the GNU General Public License when using
60238104Sdes#   or distributing such scripts, even though portions of the text of the
61238104Sdes#   Macro appear in them. The GNU General Public License (GPL) does govern
62238104Sdes#   all other use of the material that constitutes the Autoconf Macro.
63238104Sdes#
64238104Sdes#   This special exception to the GPL applies to versions of the Autoconf
65238104Sdes#   Macro released by the Autoconf Archive. When you make and distribute a
66238104Sdes#   modified version of the Autoconf Macro, you may extend this special
67238104Sdes#   exception to the GPL to apply to your modified version as well.
68238104Sdes
69238104Sdes#serial 8
70238104Sdes
71238104SdesAU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
72238104SdesAC_DEFUN([AX_PYTHON_DEVEL],[
73238104Sdes	#
74238104Sdes	# Allow the use of a (user set) custom python version
75238104Sdes	#
76238104Sdes	AC_ARG_VAR([PYTHON_VERSION],[The installed Python
77238104Sdes		version to use, for example '2.3'. This string
78238104Sdes		will be appended to the Python interpreter
79238104Sdes		canonical name.])
80238104Sdes
81238104Sdes	AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
82238104Sdes	if test -z "$PYTHON"; then
83238104Sdes	   AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
84238104Sdes	   PYTHON_VERSION=""
85238104Sdes	fi
86238104Sdes
87238104Sdes	#
88238104Sdes	# Check for a version of Python >= 2.1.0
89238104Sdes	#
90238104Sdes	AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
91238104Sdes	ac_supports_python_ver=`$PYTHON -c "import sys; \
92238104Sdes		ver = sys.version.split ()[[0]]; \
93238104Sdes		print (ver >= '2.1.0')"`
94238104Sdes	if test "$ac_supports_python_ver" != "True"; then
95238104Sdes		if test -z "$PYTHON_NOVERSIONCHECK"; then
96238104Sdes			AC_MSG_RESULT([no])
97238104Sdes			AC_MSG_FAILURE([
98238104SdesThis version of the AC@&t@_PYTHON_DEVEL macro
99238104Sdesdoesn't work properly with versions of Python before
100238104Sdes2.1.0. You may need to re-run configure, setting the
101238104Sdesvariables PYTHON_CPPFLAGS, PYTHON_LDFLAGS, PYTHON_SITE_PKG,
102238104SdesPYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
103238104SdesMoreover, to disable this check, set PYTHON_NOVERSIONCHECK
104238104Sdesto something else than an empty string.
105238104Sdes])
106238104Sdes		else
107238104Sdes			AC_MSG_RESULT([skip at user request])
108238104Sdes		fi
109238104Sdes	else
110238104Sdes		AC_MSG_RESULT([yes])
111238104Sdes	fi
112238104Sdes
113238104Sdes	#
114238104Sdes	# if the macro parameter ``version'' is set, honour it
115238104Sdes	#
116238104Sdes	if test -n "$1"; then
117238104Sdes		AC_MSG_CHECKING([for a version of Python $1])
118238104Sdes		ac_supports_python_ver=`$PYTHON -c "import sys; \
119238104Sdes			ver = sys.version.split ()[[0]]; \
120238104Sdes			print (ver $1)"`
121238104Sdes		if test "$ac_supports_python_ver" = "True"; then
122238104Sdes		   AC_MSG_RESULT([yes])
123238104Sdes		else
124238104Sdes			AC_MSG_RESULT([no])
125238104Sdes			AC_MSG_ERROR([this package requires Python $1.
126238104SdesIf you have it installed, but it isn't the default Python
127238104Sdesinterpreter in your system path, please pass the PYTHON_VERSION
128238104Sdesvariable to configure. See ``configure --help'' for reference.
129238104Sdes])
130238104Sdes			PYTHON_VERSION=""
131238104Sdes		fi
132238104Sdes	fi
133238104Sdes
134238104Sdes	#
135238104Sdes	# Check if you have distutils, else fail
136238104Sdes	#
137238104Sdes	AC_MSG_CHECKING([for the distutils Python package])
138238104Sdes	ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
139238104Sdes	if test -z "$ac_distutils_result"; then
140238104Sdes		AC_MSG_RESULT([yes])
141238104Sdes	else
142238104Sdes		AC_MSG_RESULT([no])
143238104Sdes		AC_MSG_ERROR([cannot import Python module "distutils".
144238104SdesPlease check your Python installation. The error was:
145238104Sdes$ac_distutils_result])
146238104Sdes		PYTHON_VERSION=""
147238104Sdes	fi
148238104Sdes
149238104Sdes	#
150238104Sdes	# Check for Python include path
151238104Sdes	#
152238104Sdes	AC_MSG_CHECKING([for Python include path])
153238104Sdes	if test -z "$PYTHON_CPPFLAGS"; then
154238104Sdes		python_path=`$PYTHON -c "import distutils.sysconfig; \
155238104Sdes			print (distutils.sysconfig.get_python_inc ());"`
156238104Sdes		if test -n "${python_path}"; then
157238104Sdes			python_path="-I$python_path"
158238104Sdes		fi
159238104Sdes		PYTHON_CPPFLAGS=$python_path
160238104Sdes	fi
161238104Sdes	AC_MSG_RESULT([$PYTHON_CPPFLAGS])
162238104Sdes	AC_SUBST([PYTHON_CPPFLAGS])
163238104Sdes
164238104Sdes	#
165238104Sdes	# Check for Python library path
166238104Sdes	#
167238104Sdes	AC_MSG_CHECKING([for Python library path])
168238104Sdes	if test -z "$PYTHON_LDFLAGS"; then
169238104Sdes		# (makes two attempts to ensure we've got a version number
170238104Sdes		# from the interpreter)
171238104Sdes		ac_python_version=`cat<<EOD | $PYTHON -
172238104Sdes
173238104Sdes# join all versioning strings, on some systems
174238104Sdes# major/minor numbers could be in different list elements
175238104Sdesfrom distutils.sysconfig import *
176238104Sdesret = ''
177238104Sdesfor e in get_config_vars ('VERSION'):
178238104Sdes	if (e != None):
179238104Sdes		ret += e
180238104Sdesprint (ret)
181238104SdesEOD
182238104Sdes`
183238104Sdes
184238104Sdes		if test -z "$ac_python_version"; then
185238104Sdes			if test -n "$PYTHON_VERSION"; then
186238104Sdes				ac_python_version=$PYTHON_VERSION
187238104Sdes			else
188238104Sdes				ac_python_version=`$PYTHON -c "import sys; \
189238104Sdes					print (sys.version[[:3]])"`
190238104Sdes			fi
191238104Sdes		fi
192238104Sdes
193238104Sdes		# Make the versioning information available to the compiler
194238104Sdes		AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
195238104Sdes                                   [If available, contains the Python version number currently in use.])
196238104Sdes
197238104Sdes		# First, the library directory:
198238104Sdes		ac_python_libdir=`cat<<EOD | $PYTHON -
199238104Sdes
200238104Sdes# There should be only one
201238104Sdesimport distutils.sysconfig
202238104Sdesfor e in distutils.sysconfig.get_config_vars ('LIBDIR'):
203238104Sdes	if e != None:
204238104Sdes		print (e)
205238104Sdes		break
206238104SdesEOD
207238104Sdes`
208238104Sdes
209238104Sdes		# Before checking for libpythonX.Y, we need to know
210238104Sdes		# the extension the OS we're on uses for libraries
211238104Sdes		# (we take the first one, if there's more than one fix me!):
212238104Sdes		ac_python_soext=`$PYTHON -c \
213238104Sdes		  "import distutils.sysconfig; \
214238104Sdes		  print (distutils.sysconfig.get_config_vars('SO')[[0]])"`
215238104Sdes
216238104Sdes		# Now, for the library:
217238104Sdes		ac_python_soname=`$PYTHON -c \
218238104Sdes		  "import distutils.sysconfig; \
219238104Sdes		  print (distutils.sysconfig.get_config_vars('LDLIBRARY')[[0]])"`
220238104Sdes
221238104Sdes		# Strip away extension from the end to canonicalize its name:
222238104Sdes		ac_python_library=`echo "$ac_python_soname" | sed "s/${ac_python_soext}$//"`
223238104Sdes
224238104Sdes		# This small piece shamelessly adapted from PostgreSQL python macro;
225238104Sdes		# credits goes to momjian, I think. I'd like to put the right name
226238104Sdes		# in the credits, if someone can point me in the right direction... ?
227238104Sdes		#
228238104Sdes		if test -n "$ac_python_libdir" -a -n "$ac_python_library" \
229238104Sdes			-a x"$ac_python_library" != x"$ac_python_soname"
230238104Sdes		then
231238104Sdes			# use the official shared library
232238104Sdes			ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
233238104Sdes			PYTHON_LDFLAGS="-L$ac_python_libdir -l$ac_python_library"
234238104Sdes		else
235238104Sdes			# old way: use libpython from python_configdir
236238104Sdes			ac_python_libdir=`$PYTHON -c \
237238104Sdes			  "from distutils.sysconfig import get_python_lib as f; \
238238104Sdes			  import os; \
239238104Sdes			  print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
240238104Sdes			PYTHON_LDFLAGS="-L$ac_python_libdir -lpython$ac_python_version"
241238104Sdes		fi
242238104Sdes
243238104Sdes		if test -z "PYTHON_LDFLAGS"; then
244238104Sdes			AC_MSG_ERROR([
245238104Sdes  Cannot determine location of your Python DSO. Please check it was installed with
246238104Sdes  dynamic libraries enabled, or try setting PYTHON_LDFLAGS by hand.
247238104Sdes			])
248238104Sdes		fi
249238104Sdes	fi
250238104Sdes	AC_MSG_RESULT([$PYTHON_LDFLAGS])
251238104Sdes	AC_SUBST([PYTHON_LDFLAGS])
252238104Sdes
253238104Sdes	#
254238104Sdes	# Check for site packages
255238104Sdes	#
256238104Sdes	AC_MSG_CHECKING([for Python site-packages path])
257238104Sdes	if test -z "$PYTHON_SITE_PKG"; then
258238104Sdes		PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
259238104Sdes			print (distutils.sysconfig.get_python_lib(1,0));"`
260238104Sdes	fi
261238104Sdes	AC_MSG_RESULT([$PYTHON_SITE_PKG])
262238104Sdes	AC_SUBST([PYTHON_SITE_PKG])
263238104Sdes
264238104Sdes	#
265238104Sdes	# libraries which must be linked in when embedding
266238104Sdes	#
267238104Sdes	AC_MSG_CHECKING(python extra libraries)
268238104Sdes	if test -z "$PYTHON_EXTRA_LIBS"; then
269238104Sdes	   PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
270238104Sdes                conf = distutils.sysconfig.get_config_var; \
271238104Sdes                print (conf('LOCALMODLIBS') + ' ' + conf('LIBS'))"`
272238104Sdes	fi
273238104Sdes	AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
274238104Sdes	AC_SUBST(PYTHON_EXTRA_LIBS)
275238104Sdes
276238104Sdes	#
277238104Sdes	# linking flags needed when embedding
278238104Sdes	#
279238104Sdes	AC_MSG_CHECKING(python extra linking flags)
280238104Sdes	if test -z "$PYTHON_EXTRA_LDFLAGS"; then
281238104Sdes		PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
282238104Sdes			conf = distutils.sysconfig.get_config_var; \
283238104Sdes			print (conf('LINKFORSHARED'))"`
284238104Sdes	fi
285238104Sdes	AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
286238104Sdes	AC_SUBST(PYTHON_EXTRA_LDFLAGS)
287238104Sdes
288238104Sdes	#
289238104Sdes	# final check to see if everything compiles alright
290238104Sdes	#
291238104Sdes	AC_MSG_CHECKING([consistency of all components of python development environment])
292238104Sdes	# save current global flags
293238104Sdes	ac_save_LIBS="$LIBS"
294238104Sdes	ac_save_CPPFLAGS="$CPPFLAGS"
295238104Sdes	LIBS="$ac_save_LIBS $PYTHON_LDFLAGS $PYTHON_EXTRA_LDFLAGS $PYTHON_EXTRA_LIBS"
296238104Sdes	CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
297238104Sdes	AC_LANG_PUSH([C])
298238104Sdes	AC_LINK_IFELSE([
299238104Sdes		AC_LANG_PROGRAM([[#include <Python.h>]],
300238104Sdes				[[Py_Initialize();]])
301238104Sdes		],[pythonexists=yes],[pythonexists=no])
302238104Sdes	AC_LANG_POP([C])
303238104Sdes	# turn back to default flags
304238104Sdes	CPPFLAGS="$ac_save_CPPFLAGS"
305238104Sdes	LIBS="$ac_save_LIBS"
306238104Sdes
307238104Sdes	AC_MSG_RESULT([$pythonexists])
308238104Sdes
309238104Sdes        if test ! "x$pythonexists" = "xyes"; then
310238104Sdes	   AC_MSG_FAILURE([
311238104Sdes  Could not link test program to Python. Maybe the main Python library has been
312238104Sdes  installed in some non-standard library path. If so, pass it to configure,
313238104Sdes  via the LDFLAGS environment variable.
314238104Sdes  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
315238104Sdes  ============================================================================
316238104Sdes   ERROR!
317238104Sdes   You probably have to install the development version of the Python package
318238104Sdes   for your distribution.  The exact name of this package varies among them.
319238104Sdes  ============================================================================
320238104Sdes	   ])
321238104Sdes	  PYTHON_VERSION=""
322238104Sdes	fi
323238104Sdes
324238104Sdes	#
325238104Sdes	# all done!
326238104Sdes	#
327238104Sdes])
328