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>
37266114Sdes#   Copyright (c) 2009 Alan W. Irwin
38238104Sdes#   Copyright (c) 2009 Rafael Laboissiere <rafael@laboissiere.net>
39266114Sdes#   Copyright (c) 2009 Andrew Collier
40238104Sdes#   Copyright (c) 2009 Matteo Settenvini <matteo@member.fsf.org>
41238104Sdes#   Copyright (c) 2009 Horst Knorr <hk_classes@knoda.org>
42266114Sdes#   Copyright (c) 2013 Daniel Mullner <muellner@math.stanford.edu>
43238104Sdes#
44238104Sdes#   This program is free software: you can redistribute it and/or modify it
45238104Sdes#   under the terms of the GNU General Public License as published by the
46238104Sdes#   Free Software Foundation, either version 3 of the License, or (at your
47238104Sdes#   option) any later version.
48238104Sdes#
49238104Sdes#   This program is distributed in the hope that it will be useful, but
50238104Sdes#   WITHOUT ANY WARRANTY; without even the implied warranty of
51238104Sdes#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
52238104Sdes#   Public License for more details.
53238104Sdes#
54238104Sdes#   You should have received a copy of the GNU General Public License along
55238104Sdes#   with this program. If not, see <http://www.gnu.org/licenses/>.
56238104Sdes#
57238104Sdes#   As a special exception, the respective Autoconf Macro's copyright owner
58238104Sdes#   gives unlimited permission to copy, distribute and modify the configure
59238104Sdes#   scripts that are the output of Autoconf when processing the Macro. You
60238104Sdes#   need not follow the terms of the GNU General Public License when using
61238104Sdes#   or distributing such scripts, even though portions of the text of the
62238104Sdes#   Macro appear in them. The GNU General Public License (GPL) does govern
63238104Sdes#   all other use of the material that constitutes the Autoconf Macro.
64238104Sdes#
65238104Sdes#   This special exception to the GPL applies to versions of the Autoconf
66238104Sdes#   Macro released by the Autoconf Archive. When you make and distribute a
67238104Sdes#   modified version of the Autoconf Macro, you may extend this special
68238104Sdes#   exception to the GPL to apply to your modified version as well.
69238104Sdes
70266114Sdes#serial 16
71238104Sdes
72238104SdesAU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
73238104SdesAC_DEFUN([AX_PYTHON_DEVEL],[
74238104Sdes	#
75238104Sdes	# Allow the use of a (user set) custom python version
76238104Sdes	#
77238104Sdes	AC_ARG_VAR([PYTHON_VERSION],[The installed Python
78238104Sdes		version to use, for example '2.3'. This string
79238104Sdes		will be appended to the Python interpreter
80238104Sdes		canonical name.])
81238104Sdes
82238104Sdes	AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
83238104Sdes	if test -z "$PYTHON"; then
84238104Sdes	   AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
85238104Sdes	   PYTHON_VERSION=""
86238104Sdes	fi
87238104Sdes
88238104Sdes	#
89238104Sdes	# Check for a version of Python >= 2.1.0
90238104Sdes	#
91238104Sdes	AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
92238104Sdes	ac_supports_python_ver=`$PYTHON -c "import sys; \
93238104Sdes		ver = sys.version.split ()[[0]]; \
94238104Sdes		print (ver >= '2.1.0')"`
95238104Sdes	if test "$ac_supports_python_ver" != "True"; then
96238104Sdes		if test -z "$PYTHON_NOVERSIONCHECK"; then
97238104Sdes			AC_MSG_RESULT([no])
98238104Sdes			AC_MSG_FAILURE([
99238104SdesThis version of the AC@&t@_PYTHON_DEVEL macro
100238104Sdesdoesn't work properly with versions of Python before
101238104Sdes2.1.0. You may need to re-run configure, setting the
102238104Sdesvariables PYTHON_CPPFLAGS, PYTHON_LDFLAGS, PYTHON_SITE_PKG,
103238104SdesPYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
104238104SdesMoreover, to disable this check, set PYTHON_NOVERSIONCHECK
105238104Sdesto something else than an empty string.
106238104Sdes])
107238104Sdes		else
108238104Sdes			AC_MSG_RESULT([skip at user request])
109238104Sdes		fi
110238104Sdes	else
111238104Sdes		AC_MSG_RESULT([yes])
112238104Sdes	fi
113238104Sdes
114238104Sdes	#
115238104Sdes	# if the macro parameter ``version'' is set, honour it
116238104Sdes	#
117238104Sdes	if test -n "$1"; then
118238104Sdes		AC_MSG_CHECKING([for a version of Python $1])
119238104Sdes		ac_supports_python_ver=`$PYTHON -c "import sys; \
120238104Sdes			ver = sys.version.split ()[[0]]; \
121238104Sdes			print (ver $1)"`
122238104Sdes		if test "$ac_supports_python_ver" = "True"; then
123238104Sdes		   AC_MSG_RESULT([yes])
124238104Sdes		else
125238104Sdes			AC_MSG_RESULT([no])
126238104Sdes			AC_MSG_ERROR([this package requires Python $1.
127238104SdesIf you have it installed, but it isn't the default Python
128238104Sdesinterpreter in your system path, please pass the PYTHON_VERSION
129238104Sdesvariable to configure. See ``configure --help'' for reference.
130238104Sdes])
131238104Sdes			PYTHON_VERSION=""
132238104Sdes		fi
133238104Sdes	fi
134238104Sdes
135238104Sdes	#
136238104Sdes	# Check if you have distutils, else fail
137238104Sdes	#
138238104Sdes	AC_MSG_CHECKING([for the distutils Python package])
139238104Sdes	ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
140238104Sdes	if test -z "$ac_distutils_result"; then
141238104Sdes		AC_MSG_RESULT([yes])
142238104Sdes	else
143238104Sdes		AC_MSG_RESULT([no])
144238104Sdes		AC_MSG_ERROR([cannot import Python module "distutils".
145238104SdesPlease check your Python installation. The error was:
146238104Sdes$ac_distutils_result])
147238104Sdes		PYTHON_VERSION=""
148238104Sdes	fi
149238104Sdes
150238104Sdes	#
151238104Sdes	# Check for Python include path
152238104Sdes	#
153238104Sdes	AC_MSG_CHECKING([for Python include path])
154238104Sdes	if test -z "$PYTHON_CPPFLAGS"; then
155238104Sdes		python_path=`$PYTHON -c "import distutils.sysconfig; \
156238104Sdes			print (distutils.sysconfig.get_python_inc ());"`
157266114Sdes		plat_python_path=`$PYTHON -c "import distutils.sysconfig; \
158266114Sdes			print (distutils.sysconfig.get_python_inc (plat_specific=1));"`
159238104Sdes		if test -n "${python_path}"; then
160266114Sdes			if test "${plat_python_path}" != "${python_path}"; then
161266114Sdes				python_path="-I$python_path -I$plat_python_path"
162266114Sdes			else
163266114Sdes				python_path="-I$python_path"
164266114Sdes			fi
165238104Sdes		fi
166238104Sdes		PYTHON_CPPFLAGS=$python_path
167238104Sdes	fi
168238104Sdes	AC_MSG_RESULT([$PYTHON_CPPFLAGS])
169238104Sdes	AC_SUBST([PYTHON_CPPFLAGS])
170238104Sdes
171238104Sdes	#
172238104Sdes	# Check for Python library path
173238104Sdes	#
174238104Sdes	AC_MSG_CHECKING([for Python library path])
175238104Sdes	if test -z "$PYTHON_LDFLAGS"; then
176238104Sdes		# (makes two attempts to ensure we've got a version number
177238104Sdes		# from the interpreter)
178238104Sdes		ac_python_version=`cat<<EOD | $PYTHON -
179238104Sdes
180238104Sdes# join all versioning strings, on some systems
181238104Sdes# major/minor numbers could be in different list elements
182238104Sdesfrom distutils.sysconfig import *
183266114Sdese = get_config_var('VERSION')
184266114Sdesif e is not None:
185266114Sdes	print(e)
186266114SdesEOD`
187238104Sdes
188238104Sdes		if test -z "$ac_python_version"; then
189238104Sdes			if test -n "$PYTHON_VERSION"; then
190238104Sdes				ac_python_version=$PYTHON_VERSION
191238104Sdes			else
192238104Sdes				ac_python_version=`$PYTHON -c "import sys; \
193238104Sdes					print (sys.version[[:3]])"`
194238104Sdes			fi
195238104Sdes		fi
196238104Sdes
197238104Sdes		# Make the versioning information available to the compiler
198238104Sdes		AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
199238104Sdes                                   [If available, contains the Python version number currently in use.])
200238104Sdes
201238104Sdes		# First, the library directory:
202238104Sdes		ac_python_libdir=`cat<<EOD | $PYTHON -
203238104Sdes
204238104Sdes# There should be only one
205238104Sdesimport distutils.sysconfig
206266114Sdese = distutils.sysconfig.get_config_var('LIBDIR')
207266114Sdesif e is not None:
208266114Sdes	print (e)
209266114SdesEOD`
210238104Sdes
211238104Sdes		# Now, for the library:
212266114Sdes		ac_python_library=`cat<<EOD | $PYTHON -
213238104Sdes
214266114Sdesimport distutils.sysconfig
215266114Sdesc = distutils.sysconfig.get_config_vars()
216266114Sdesif 'LDVERSION' in c:
217266114Sdes	print ('python'+c[['LDVERSION']])
218266114Sdeselse:
219266114Sdes	print ('python'+c[['VERSION']])
220266114SdesEOD`
221238104Sdes
222238104Sdes		# This small piece shamelessly adapted from PostgreSQL python macro;
223238104Sdes		# credits goes to momjian, I think. I'd like to put the right name
224238104Sdes		# in the credits, if someone can point me in the right direction... ?
225238104Sdes		#
226266114Sdes		if test -n "$ac_python_libdir" -a -n "$ac_python_library"
227238104Sdes		then
228238104Sdes			# use the official shared library
229238104Sdes			ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
230238104Sdes			PYTHON_LDFLAGS="-L$ac_python_libdir -l$ac_python_library"
231238104Sdes		else
232238104Sdes			# old way: use libpython from python_configdir
233238104Sdes			ac_python_libdir=`$PYTHON -c \
234238104Sdes			  "from distutils.sysconfig import get_python_lib as f; \
235238104Sdes			  import os; \
236238104Sdes			  print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
237238104Sdes			PYTHON_LDFLAGS="-L$ac_python_libdir -lpython$ac_python_version"
238238104Sdes		fi
239238104Sdes
240238104Sdes		if test -z "PYTHON_LDFLAGS"; then
241238104Sdes			AC_MSG_ERROR([
242238104Sdes  Cannot determine location of your Python DSO. Please check it was installed with
243238104Sdes  dynamic libraries enabled, or try setting PYTHON_LDFLAGS by hand.
244238104Sdes			])
245238104Sdes		fi
246238104Sdes	fi
247238104Sdes	AC_MSG_RESULT([$PYTHON_LDFLAGS])
248238104Sdes	AC_SUBST([PYTHON_LDFLAGS])
249238104Sdes
250238104Sdes	#
251238104Sdes	# Check for site packages
252238104Sdes	#
253238104Sdes	AC_MSG_CHECKING([for Python site-packages path])
254238104Sdes	if test -z "$PYTHON_SITE_PKG"; then
255238104Sdes		PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
256238104Sdes			print (distutils.sysconfig.get_python_lib(1,0));"`
257238104Sdes	fi
258238104Sdes	AC_MSG_RESULT([$PYTHON_SITE_PKG])
259238104Sdes	AC_SUBST([PYTHON_SITE_PKG])
260238104Sdes
261238104Sdes	#
262238104Sdes	# libraries which must be linked in when embedding
263238104Sdes	#
264238104Sdes	AC_MSG_CHECKING(python extra libraries)
265238104Sdes	if test -z "$PYTHON_EXTRA_LIBS"; then
266238104Sdes	   PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
267238104Sdes                conf = distutils.sysconfig.get_config_var; \
268266114Sdes                print (conf('LIBS'))"`
269238104Sdes	fi
270238104Sdes	AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
271238104Sdes	AC_SUBST(PYTHON_EXTRA_LIBS)
272238104Sdes
273238104Sdes	#
274238104Sdes	# linking flags needed when embedding
275238104Sdes	#
276238104Sdes	AC_MSG_CHECKING(python extra linking flags)
277238104Sdes	if test -z "$PYTHON_EXTRA_LDFLAGS"; then
278238104Sdes		PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
279238104Sdes			conf = distutils.sysconfig.get_config_var; \
280238104Sdes			print (conf('LINKFORSHARED'))"`
281238104Sdes	fi
282238104Sdes	AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
283238104Sdes	AC_SUBST(PYTHON_EXTRA_LDFLAGS)
284238104Sdes
285238104Sdes	#
286238104Sdes	# final check to see if everything compiles alright
287238104Sdes	#
288238104Sdes	AC_MSG_CHECKING([consistency of all components of python development environment])
289238104Sdes	# save current global flags
290238104Sdes	ac_save_LIBS="$LIBS"
291238104Sdes	ac_save_CPPFLAGS="$CPPFLAGS"
292238104Sdes	LIBS="$ac_save_LIBS $PYTHON_LDFLAGS $PYTHON_EXTRA_LDFLAGS $PYTHON_EXTRA_LIBS"
293238104Sdes	CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
294238104Sdes	AC_LANG_PUSH([C])
295238104Sdes	AC_LINK_IFELSE([
296238104Sdes		AC_LANG_PROGRAM([[#include <Python.h>]],
297238104Sdes				[[Py_Initialize();]])
298238104Sdes		],[pythonexists=yes],[pythonexists=no])
299238104Sdes	AC_LANG_POP([C])
300238104Sdes	# turn back to default flags
301238104Sdes	CPPFLAGS="$ac_save_CPPFLAGS"
302238104Sdes	LIBS="$ac_save_LIBS"
303238104Sdes
304238104Sdes	AC_MSG_RESULT([$pythonexists])
305238104Sdes
306238104Sdes        if test ! "x$pythonexists" = "xyes"; then
307238104Sdes	   AC_MSG_FAILURE([
308238104Sdes  Could not link test program to Python. Maybe the main Python library has been
309238104Sdes  installed in some non-standard library path. If so, pass it to configure,
310238104Sdes  via the LDFLAGS environment variable.
311238104Sdes  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
312238104Sdes  ============================================================================
313238104Sdes   ERROR!
314238104Sdes   You probably have to install the development version of the Python package
315238104Sdes   for your distribution.  The exact name of this package varies among them.
316238104Sdes  ============================================================================
317238104Sdes	   ])
318238104Sdes	  PYTHON_VERSION=""
319238104Sdes	fi
320238104Sdes
321238104Sdes	#
322238104Sdes	# all done!
323238104Sdes	#
324238104Sdes])
325