1# ===========================================================================
2#     https://www.gnu.org/software/autoconf-archive/ax_python_devel.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7#   AX_PYTHON_DEVEL([version], [action-if-not-found])
8#
9# DESCRIPTION
10#
11#   Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
12#   in your configure.ac.
13#
14#   Note: this is a slightly modified version of the original AX_PYTHON_DEVEL
15#   macro which accepts an additional [action-if-not-found] argument. This
16#   allow to detect if Python development is available without aborting the
17#   configure phase with an hard error in case it is not.
18#
19#   This macro checks for Python and tries to get the include path to
20#   'Python.h'. It provides the $(PYTHON_CPPFLAGS) and $(PYTHON_LIBS) output
21#   variables. It also exports $(PYTHON_EXTRA_LIBS) and
22#   $(PYTHON_EXTRA_LDFLAGS) for embedding Python in your code.
23#
24#   You can search for some particular version of Python by passing a
25#   parameter to this macro, for example ">= '2.3.1'", or "== '2.4'". Please
26#   note that you *have* to pass also an operator along with the version to
27#   match, and pay special attention to the single quotes surrounding the
28#   version number. Don't use "PYTHON_VERSION" for this: that environment
29#   variable is declared as precious and thus reserved for the end-user.
30#
31#   This macro should work for all versions of Python >= 2.1.0. As an end
32#   user, you can disable the check for the python version by setting the
33#   PYTHON_NOVERSIONCHECK environment variable to something else than the
34#   empty string.
35#
36#   If you need to use this macro for an older Python version, please
37#   contact the authors. We're always open for feedback.
38#
39# LICENSE
40#
41#   Copyright (c) 2009 Sebastian Huber <sebastian-huber@web.de>
42#   Copyright (c) 2009 Alan W. Irwin
43#   Copyright (c) 2009 Rafael Laboissiere <rafael@laboissiere.net>
44#   Copyright (c) 2009 Andrew Collier
45#   Copyright (c) 2009 Matteo Settenvini <matteo@member.fsf.org>
46#   Copyright (c) 2009 Horst Knorr <hk_classes@knoda.org>
47#   Copyright (c) 2013 Daniel Mullner <muellner@math.stanford.edu>
48#   Copyright (c) 2018 loli10K <ezomori.nozomu@gmail.com>
49#
50#   This program is free software: you can redistribute it and/or modify it
51#   under the terms of the GNU General Public License as published by the
52#   Free Software Foundation, either version 3 of the License, or (at your
53#   option) any later version.
54#
55#   This program is distributed in the hope that it will be useful, but
56#   WITHOUT ANY WARRANTY; without even the implied warranty of
57#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
58#   Public License for more details.
59#
60#   You should have received a copy of the GNU General Public License along
61#   with this program. If not, see <https://www.gnu.org/licenses/>.
62#
63#   As a special exception, the respective Autoconf Macro's copyright owner
64#   gives unlimited permission to copy, distribute and modify the configure
65#   scripts that are the output of Autoconf when processing the Macro. You
66#   need not follow the terms of the GNU General Public License when using
67#   or distributing such scripts, even though portions of the text of the
68#   Macro appear in them. The GNU General Public License (GPL) does govern
69#   all other use of the material that constitutes the Autoconf Macro.
70#
71#   This special exception to the GPL applies to versions of the Autoconf
72#   Macro released by the Autoconf Archive. When you make and distribute a
73#   modified version of the Autoconf Macro, you may extend this special
74#   exception to the GPL to apply to your modified version as well.
75
76#serial 21
77
78AU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
79AC_DEFUN([AX_PYTHON_DEVEL],[
80	#
81	# Allow the use of a (user set) custom python version
82	#
83	AC_ARG_VAR([PYTHON_VERSION],[The installed Python
84		version to use, for example '2.3'. This string
85		will be appended to the Python interpreter
86		canonical name.])
87
88	AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
89	if test -z "$PYTHON"; then
90		m4_ifvaln([$2],[$2],[
91			AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
92			PYTHON_VERSION=""
93		])
94	fi
95
96	#
97	# Check for a version of Python >= 2.1.0
98	#
99	AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
100	ac_supports_python_ver=`$PYTHON -c "import sys; \
101		ver = sys.version.split ()[[0]]; \
102		print (ver >= '2.1.0')"`
103	if test "$ac_supports_python_ver" != "True"; then
104		if test -z "$PYTHON_NOVERSIONCHECK"; then
105			AC_MSG_RESULT([no])
106			m4_ifvaln([$2],[$2],[
107				AC_MSG_FAILURE([
108This version of the AC@&t@_PYTHON_DEVEL macro
109doesn't work properly with versions of Python before
1102.1.0. You may need to re-run configure, setting the
111variables PYTHON_CPPFLAGS, PYTHON_LIBS, PYTHON_SITE_PKG,
112PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
113Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
114to something else than an empty string.
115])
116			])
117		else
118			AC_MSG_RESULT([skip at user request])
119		fi
120	else
121		AC_MSG_RESULT([yes])
122	fi
123
124	#
125	# if the macro parameter ``version'' is set, honour it
126	#
127	if test -n "$1"; then
128		AC_MSG_CHECKING([for a version of Python $1])
129		ac_supports_python_ver=`$PYTHON -c "import sys; \
130			ver = sys.version.split ()[[0]]; \
131			print (ver $1)"`
132		if test "$ac_supports_python_ver" = "True"; then
133		   AC_MSG_RESULT([yes])
134		else
135			AC_MSG_RESULT([no])
136			m4_ifvaln([$2],[$2],[
137				AC_MSG_ERROR([this package requires Python $1.
138If you have it installed, but it isn't the default Python
139interpreter in your system path, please pass the PYTHON_VERSION
140variable to configure. See ``configure --help'' for reference.
141])
142				PYTHON_VERSION=""
143			])
144		fi
145	fi
146
147	#
148	# Check if you have distutils, else fail
149	#
150	AC_MSG_CHECKING([for the distutils Python package])
151	ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
152	if test $? -eq 0; then
153		AC_MSG_RESULT([yes])
154	else
155		AC_MSG_RESULT([no])
156		m4_ifvaln([$2],[$2],[
157			AC_MSG_ERROR([cannot import Python module "distutils".
158Please check your Python installation. The error was:
159$ac_distutils_result])
160			PYTHON_VERSION=""
161		])
162	fi
163
164	#
165	# Check for Python include path
166	#
167	AC_MSG_CHECKING([for Python include path])
168	if test -z "$PYTHON_CPPFLAGS"; then
169		python_path=`$PYTHON -c "import distutils.sysconfig; \
170			print (distutils.sysconfig.get_python_inc ());"`
171		plat_python_path=`$PYTHON -c "import distutils.sysconfig; \
172			print (distutils.sysconfig.get_python_inc (plat_specific=1));"`
173		if test -n "${python_path}"; then
174			if test "${plat_python_path}" != "${python_path}"; then
175				python_path="-I$python_path -I$plat_python_path"
176			else
177				python_path="-I$python_path"
178			fi
179		fi
180		PYTHON_CPPFLAGS=$python_path
181	fi
182	AC_MSG_RESULT([$PYTHON_CPPFLAGS])
183	AC_SUBST([PYTHON_CPPFLAGS])
184
185	#
186	# Check for Python library path
187	#
188	AC_MSG_CHECKING([for Python library path])
189	if test -z "$PYTHON_LIBS"; then
190		# (makes two attempts to ensure we've got a version number
191		# from the interpreter)
192		ac_python_version=`cat<<EOD | $PYTHON -
193
194# join all versioning strings, on some systems
195# major/minor numbers could be in different list elements
196from distutils.sysconfig import *
197e = get_config_var('VERSION')
198if e is not None:
199	print(e)
200EOD`
201
202		if test -z "$ac_python_version"; then
203			if test -n "$PYTHON_VERSION"; then
204				ac_python_version=$PYTHON_VERSION
205			else
206				ac_python_version=`$PYTHON -c "import sys; \
207					print (sys.version[[:3]])"`
208			fi
209		fi
210
211		# Make the versioning information available to the compiler
212		AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
213                                   [If available, contains the Python version number currently in use.])
214
215		# First, the library directory:
216		ac_python_libdir=`cat<<EOD | $PYTHON -
217
218# There should be only one
219import distutils.sysconfig
220e = distutils.sysconfig.get_config_var('LIBDIR')
221if e is not None:
222	print (e)
223EOD`
224
225		# Now, for the library:
226		ac_python_library=`cat<<EOD | $PYTHON -
227
228import distutils.sysconfig
229c = distutils.sysconfig.get_config_vars()
230if 'LDVERSION' in c:
231	print ('python'+c[['LDVERSION']])
232else:
233	print ('python'+c[['VERSION']])
234EOD`
235
236		# This small piece shamelessly adapted from PostgreSQL python macro;
237		# credits goes to momjian, I think. I'd like to put the right name
238		# in the credits, if someone can point me in the right direction... ?
239		#
240		if test -n "$ac_python_libdir" -a -n "$ac_python_library"
241		then
242			# use the official shared library
243			ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
244			PYTHON_LIBS="-L$ac_python_libdir -l$ac_python_library"
245		else
246			# old way: use libpython from python_configdir
247			ac_python_libdir=`$PYTHON -c \
248			  "from distutils.sysconfig import get_python_lib as f; \
249			  import os; \
250			  print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
251			PYTHON_LIBS="-L$ac_python_libdir -lpython$ac_python_version"
252		fi
253
254		if test -z "PYTHON_LIBS"; then
255			m4_ifvaln([$2],[$2],[
256				AC_MSG_ERROR([
257  Cannot determine location of your Python DSO. Please check it was installed with
258  dynamic libraries enabled, or try setting PYTHON_LIBS by hand.
259				])
260			])
261		fi
262	fi
263	AC_MSG_RESULT([$PYTHON_LIBS])
264	AC_SUBST([PYTHON_LIBS])
265
266	#
267	# Check for site packages
268	#
269	AC_MSG_CHECKING([for Python site-packages path])
270	if test -z "$PYTHON_SITE_PKG"; then
271		PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
272			print (distutils.sysconfig.get_python_lib(0,0));"`
273	fi
274	AC_MSG_RESULT([$PYTHON_SITE_PKG])
275	AC_SUBST([PYTHON_SITE_PKG])
276
277	#
278	# libraries which must be linked in when embedding
279	#
280	AC_MSG_CHECKING(python extra libraries)
281	if test -z "$PYTHON_EXTRA_LIBS"; then
282	   PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
283                conf = distutils.sysconfig.get_config_var; \
284                print (conf('LIBS') + ' ' + conf('SYSLIBS'))"`
285	fi
286	AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
287	AC_SUBST(PYTHON_EXTRA_LIBS)
288
289	#
290	# linking flags needed when embedding
291	#
292	AC_MSG_CHECKING(python extra linking flags)
293	if test -z "$PYTHON_EXTRA_LDFLAGS"; then
294		PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
295			conf = distutils.sysconfig.get_config_var; \
296			print (conf('LINKFORSHARED'))"`
297	fi
298	AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
299	AC_SUBST(PYTHON_EXTRA_LDFLAGS)
300
301	#
302	# final check to see if everything compiles alright
303	#
304	AC_MSG_CHECKING([consistency of all components of python development environment])
305	# save current global flags
306	ac_save_LIBS="$LIBS"
307	ac_save_LDFLAGS="$LDFLAGS"
308	ac_save_CPPFLAGS="$CPPFLAGS"
309	LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS $PYTHON_EXTRA_LIBS"
310	LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS"
311	CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
312	AC_LANG_PUSH([C])
313	AC_LINK_IFELSE([
314		AC_LANG_PROGRAM([[#include <Python.h>]],
315				[[Py_Initialize();]])
316		],[pythonexists=yes],[pythonexists=no])
317	AC_LANG_POP([C])
318	# turn back to default flags
319	CPPFLAGS="$ac_save_CPPFLAGS"
320	LIBS="$ac_save_LIBS"
321	LDFLAGS="$ac_save_LDFLAGS"
322
323	AC_MSG_RESULT([$pythonexists])
324
325        if test ! "x$pythonexists" = "xyes"; then
326		m4_ifvaln([$2],[$2],[
327			AC_MSG_FAILURE([
328  Could not link test program to Python. Maybe the main Python library has been
329  installed in some non-standard library path. If so, pass it to configure,
330  via the LIBS environment variable.
331  Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
332  ============================================================================
333   ERROR!
334   You probably have to install the development version of the Python package
335   for your distribution.  The exact name of this package varies among them.
336  ============================================================================
337	   ])
338			PYTHON_VERSION=""
339		])
340	fi
341
342	#
343	# all done!
344	#
345])
346