1##### http://autoconf-archive.cryp.to/ac_python_devel.html
2#
3# SYNOPSIS
4#
5#   AC_PYTHON_DEVEL([version])
6#
7# DESCRIPTION
8#
9#   Note: Defines as a precious variable "PYTHON_VERSION". Don't
10#   override it in your configure.ac.
11#
12#   This macro checks for Python and tries to get the include path to
13#   'Python.h'. It provides the $(PYTHON_CPPFLAGS) and
14#   $(PYTHON_LDFLAGS) output variables. It also exports
15#   $(PYTHON_EXTRA_LIBS) and $(PYTHON_EXTRA_LDFLAGS) for embedding
16#   Python in your code.
17#
18#   You can search for some particular version of Python by passing a
19#   parameter to this macro, for example ">= '2.3.1'", or "== '2.4'".
20#   Please note that you *have* to pass also an operator along with the
21#   version to match, and pay special attention to the single quotes
22#   surrounding the version number. Don't use "PYTHON_VERSION" for
23#   this: that environment variable is declared as precious and thus
24#   reserved for the end-user.
25#
26#   This macro should work for all versions of Python >= 2.1.0. As an
27#   end user, you can disable the check for the python version by
28#   setting the PYTHON_NOVERSIONCHECK environment variable to something
29#   else than the 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# LAST MODIFICATION
35#
36#   2006-10-22
37#
38# COPYLEFT
39#
40#   Copyright (c) 2006 Sebastian Huber <sebastian-huber@web.de>
41#   Copyright (c) 2006 Alan W. Irwin <irwin@beluga.phys.uvic.ca>
42#   Copyright (c) 2006 Rafael Laboissiere <rafael@laboissiere.net>
43#   Copyright (c) 2006 Andrew Collier <colliera@ukzn.ac.za>
44#   Copyright (c) 2006 Matteo Settenvini <matteo@member.fsf.org>
45#   Copyright (c) 2006 Horst Knorr <hk_classes@knoda.org>
46#
47#   This program is free software; you can redistribute it and/or
48#   modify it under the terms of the GNU General Public License as
49#   published by the Free Software Foundation; either version 2 of the
50#   License, or (at your option) any later version.
51#
52#   This program is distributed in the hope that it will be useful, but
53#   WITHOUT ANY WARRANTY; without even the implied warranty of
54#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55#   General Public License for more details.
56#
57#   You should have received a copy of the GNU General Public License
58#   along with this program; if not, write to the Free Software
59#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
60#   02111-1307, USA.
61#
62#   As a special exception, the respective Autoconf Macro's copyright
63#   owner gives unlimited permission to copy, distribute and modify the
64#   configure scripts that are the output of Autoconf when processing
65#   the Macro. You need not follow the terms of the GNU General Public
66#   License when using or distributing such scripts, even though
67#   portions of the text of the Macro appear in them. The GNU General
68#   Public License (GPL) does govern all other use of the material that
69#   constitutes the Autoconf Macro.
70#
71#   This special exception to the GPL applies to versions of the
72#   Autoconf Macro released by the Autoconf Macro Archive. When you
73#   make and distribute a modified version of the Autoconf Macro, you
74#   may extend this special exception to the GPL to apply to your
75#   modified version as well.
76
77AC_DEFUN([AC_PYTHON_DEVEL],[
78	#
79	# Allow the use of a (user set) custom python version
80	#
81	AC_ARG_VAR([PYTHON_VERSION],[The installed Python
82		version to use, for example '2.3'. This string
83		will be appended to the Python interpreter
84		canonical name.])
85
86	AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
87	if test -z "$PYTHON"; then
88	   AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
89	   PYTHON_VERSION=""
90	fi
91
92	#
93	# Check for a version of Python >= 2.1.0
94	#
95	AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
96	ac_supports_python_ver=`$PYTHON -c "import sys, string; \
97		ver = sys.version.split(); \
98		print(ver[[0]] >= '2.1.0')"`
99	if test "$ac_supports_python_ver" != "True"; then
100		if test -z "$PYTHON_NOVERSIONCHECK"; then
101			AC_MSG_RESULT([no])
102			AC_MSG_FAILURE([
103This version of the AC@&t@_PYTHON_DEVEL macro
104doesn't work properly with versions of Python before
1052.1.0. You may need to re-run configure, setting the
106variables PYTHON_CPPFLAGS, PYTHON_LDFLAGS, PYTHON_SITE_PKG,
107PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
108Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
109to something else than an empty string.
110])
111		else
112			AC_MSG_RESULT([skip at user request])
113		fi
114	else
115		AC_MSG_RESULT([yes])
116	fi
117
118	#
119	# if the macro parameter ``version'' is set, honour it
120	#
121	if test -n "$1"; then
122		AC_MSG_CHECKING([for a version of Python $1])
123		ac_supports_python_ver=`$PYTHON -c "import sys, string; \
124			ver = sys.version.split(); \
125			print(ver[[0]], $1)"`
126		if test "$ac_supports_python_ver" = "True"; then
127	   	   AC_MSG_RESULT([yes])
128		else
129			AC_MSG_RESULT([no])
130			AC_MSG_ERROR([this package requires Python $1.
131If you have it installed, but it isn't the default Python
132interpreter in your system path, please pass the PYTHON_VERSION
133variable to configure. See ``configure --help'' for reference.
134])
135			PYTHON_VERSION=""
136		fi
137	fi
138
139	#
140	# Check if you have distutils, else fail
141	#
142	AC_MSG_CHECKING([for the distutils Python package])
143	ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
144	if test -z "$ac_distutils_result"; then
145		AC_MSG_RESULT([yes])
146	else
147		AC_MSG_RESULT([no])
148		AC_MSG_ERROR([cannot import Python module "distutils".
149Please check your Python installation. The error was:
150$ac_distutils_result])
151		PYTHON_VERSION=""
152	fi
153
154	#
155	# Check for Python include path
156	#
157	AC_MSG_CHECKING([for Python include path])
158	if test -z "$PYTHON_CPPFLAGS"; then
159		python_path=`$PYTHON -c "import distutils.sysconfig; \
160           		print(distutils.sysconfig.get_python_inc());"`
161		if test -n "${python_path}"; then
162		   	python_path="-I$python_path"
163		fi
164		PYTHON_CPPFLAGS=$python_path
165	fi
166	AC_MSG_RESULT([$PYTHON_CPPFLAGS])
167	AC_SUBST([PYTHON_CPPFLAGS])
168
169	#
170	# Check for Python library path
171	#
172	AC_MSG_CHECKING([for Python library path])
173	if test -z "$PYTHON_LDFLAGS"; then
174		# (makes two attempts to ensure we've got a version number
175		# from the interpreter)
176		py_version=`$PYTHON -c "from distutils.sysconfig import *; \
177			print(' '.join(get_config_vars('VERSION')))"`
178		if test "$py_version" = "[None]"; then
179			if test -n "$PYTHON_VERSION"; then
180				py_version=$PYTHON_VERSION
181			else
182				py_version=`$PYTHON -c "import sys; \
183					print(sys.version[[:3]])"`
184			fi
185		fi
186
187		PYTHON_LDFLAGS=`$PYTHON -c "from distutils.sysconfig import *; \
188			print('-L', get_python_lib(0,1), \
189		      	'-lpython');"`$py_version
190	fi
191	AC_MSG_RESULT([$PYTHON_LDFLAGS])
192	AC_SUBST([PYTHON_LDFLAGS])
193
194	#
195	# Check for site packages
196	#
197	AC_MSG_CHECKING([for Python site-packages path])
198	if test -z "$PYTHON_SITE_PKG"; then
199		PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
200		        print(distutils.sysconfig.get_python_lib(0,0));"`
201	fi
202	AC_MSG_RESULT([$PYTHON_SITE_PKG])
203	AC_SUBST([PYTHON_SITE_PKG])
204
205	#
206	# libraries which must be linked in when embedding
207	#
208	AC_MSG_CHECKING(python extra libraries)
209	if test -z "$PYTHON_EXTRA_LIBS"; then
210	   PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
211                conf = distutils.sysconfig.get_config_var; \
212                print(conf('LOCALMODLIBS'), conf('LIBS'))"`
213	fi
214	AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
215	AC_SUBST(PYTHON_EXTRA_LIBS)
216
217	#
218	# linking flags needed when embedding
219	#
220	AC_MSG_CHECKING(python extra linking flags)
221	if test -z "$PYTHON_EXTRA_LDFLAGS"; then
222		PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
223			conf = distutils.sysconfig.get_config_var; \
224			print(conf('LINKFORSHARED'))"`
225	fi
226	AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
227	AC_SUBST(PYTHON_EXTRA_LDFLAGS)
228
229	#
230	# final check to see if everything compiles alright
231	#
232	AC_MSG_CHECKING([consistency of all components of python development environment])
233	AC_LANG_PUSH([C])
234	# save current global flags
235	LIBS="$ac_save_LIBS $PYTHON_LDFLAGS"
236	CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
237	AC_TRY_LINK([
238		#include <Python.h>
239	],[
240		Py_Initialize();
241	],[pythonexists=yes],[pythonexists=no])
242
243	AC_MSG_RESULT([$pythonexists])
244
245        if test ! "$pythonexists" = "yes"; then
246	   AC_MSG_ERROR([
247  Could not link test program to Python. Maybe the main Python library has been
248  installed in some non-standard library path. If so, pass it to configure,
249  via the LDFLAGS environment variable.
250  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
251  ============================================================================
252   ERROR!
253   You probably have to install the development version of the Python package
254   for your distribution.  The exact name of this package varies among them.
255  ============================================================================
256	   ])
257	  PYTHON_VERSION=""
258	fi
259	AC_LANG_POP
260	# turn back to default flags
261	CPPFLAGS="$ac_save_CPPFLAGS"
262	LIBS="$ac_save_LIBS"
263
264	#
265	# all done!
266	#
267])
268
269