1# ===========================================================================
2#     https://www.gnu.org/software/autoconf-archive/ax_python_devel.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7#   AX_PYTHON_DEVEL([version[,optional]])
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_LIBS) output
16#   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#   By default this will fail if it does not detect a development version of
27#   python.  If you want it to continue, set optional to true, like
28#   AX_PYTHON_DEVEL([], [true]).  The ax_python_devel_found variable will be
29#   "no" if it fails.
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#
49#   This program is free software: you can redistribute it and/or modify it
50#   under the terms of the GNU General Public License as published by the
51#   Free Software Foundation, either version 3 of the License, or (at your
52#   option) any later version.
53#
54#   This program is distributed in the hope that it will be useful, but
55#   WITHOUT ANY WARRANTY; without even the implied warranty of
56#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
57#   Public License for more details.
58#
59#   You should have received a copy of the GNU General Public License along
60#   with this program. If not, see <https://www.gnu.org/licenses/>.
61#
62#   As a special exception, the respective Autoconf Macro's copyright owner
63#   gives unlimited permission to copy, distribute and modify the configure
64#   scripts that are the output of Autoconf when processing the Macro. You
65#   need not follow the terms of the GNU General Public License when using
66#   or distributing such scripts, even though portions of the text of the
67#   Macro appear in them. The GNU General Public License (GPL) does govern
68#   all other use of the material that constitutes the Autoconf Macro.
69#
70#   This special exception to the GPL applies to versions of the Autoconf
71#   Macro released by the Autoconf Archive. When you make and distribute a
72#   modified version of the Autoconf Macro, you may extend this special
73#   exception to the GPL to apply to your modified version as well.
74
75#serial 36
76
77AU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
78AC_DEFUN([AX_PYTHON_DEVEL],[
79	# Get whether it's optional
80	if test -z "$2"; then
81	   ax_python_devel_optional=false
82	else
83	   ax_python_devel_optional=$2
84	fi
85	ax_python_devel_found=yes
86
87	#
88	# Allow the use of a (user set) custom python version
89	#
90	AC_ARG_VAR([PYTHON_VERSION],[The installed Python
91		version to use, for example '2.3'. This string
92		will be appended to the Python interpreter
93		canonical name.])
94
95	AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
96	if test -z "$PYTHON"; then
97	   AC_MSG_WARN([Cannot find python$PYTHON_VERSION in your system path])
98	   if ! $ax_python_devel_optional; then
99	      AC_MSG_ERROR([Giving up, python development not available])
100	   fi
101	   ax_python_devel_found=no
102	   PYTHON_VERSION=""
103	fi
104
105	if test $ax_python_devel_found = yes; then
106	   #
107	   # Check for a version of Python >= 2.1.0
108	   #
109	   AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
110	   ac_supports_python_ver=`$PYTHON -c "import sys; \
111		ver = sys.version.split ()[[0]]; \
112		print (ver >= '2.1.0')"`
113	   if test "$ac_supports_python_ver" != "True"; then
114		if test -z "$PYTHON_NOVERSIONCHECK"; then
115			AC_MSG_RESULT([no])
116			AC_MSG_WARN([
117This version of the AC@&t@_PYTHON_DEVEL macro
118doesn't work properly with versions of Python before
1192.1.0. You may need to re-run configure, setting the
120variables PYTHON_CPPFLAGS, PYTHON_LIBS, PYTHON_SITE_PKG,
121PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
122Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
123to something else than an empty string.
124])
125			if ! $ax_python_devel_optional; then
126			   AC_MSG_FAILURE([Giving up])
127			fi
128			ax_python_devel_found=no
129			PYTHON_VERSION=""
130		else
131			AC_MSG_RESULT([skip at user request])
132		fi
133	   else
134		AC_MSG_RESULT([yes])
135	   fi
136	fi
137
138	if test $ax_python_devel_found = yes; then
139	   #
140	   # If the macro parameter ``version'' is set, honour it.
141	   # A Python shim class, VPy, is used to implement correct version comparisons via
142	   # string expressions, since e.g. a naive textual ">= 2.7.3" won't work for
143	   # Python 2.7.10 (the ".1" being evaluated as less than ".3").
144	   #
145	   if test -n "$1"; then
146		AC_MSG_CHECKING([for a version of Python $1])
147                cat << EOF > ax_python_devel_vpy.py
148class VPy:
149    def vtup(self, s):
150        return tuple(map(int, s.strip().replace("rc", ".").split(".")))
151    def __init__(self):
152        import sys
153        self.vpy = tuple(sys.version_info)[[:3]]
154    def __eq__(self, s):
155        return self.vpy == self.vtup(s)
156    def __ne__(self, s):
157        return self.vpy != self.vtup(s)
158    def __lt__(self, s):
159        return self.vpy < self.vtup(s)
160    def __gt__(self, s):
161        return self.vpy > self.vtup(s)
162    def __le__(self, s):
163        return self.vpy <= self.vtup(s)
164    def __ge__(self, s):
165        return self.vpy >= self.vtup(s)
166EOF
167		ac_supports_python_ver=`$PYTHON -c "import ax_python_devel_vpy; \
168                        ver = ax_python_devel_vpy.VPy(); \
169			print (ver $1)"`
170                rm -rf ax_python_devel_vpy*.py* __pycache__/ax_python_devel_vpy*.py*
171		if test "$ac_supports_python_ver" = "True"; then
172			AC_MSG_RESULT([yes])
173		else
174			AC_MSG_RESULT([no])
175			AC_MSG_WARN([this package requires Python $1.
176If you have it installed, but it isn't the default Python
177interpreter in your system path, please pass the PYTHON_VERSION
178variable to configure. See ``configure --help'' for reference.
179])
180			if ! $ax_python_devel_optional; then
181			   AC_MSG_ERROR([Giving up])
182			fi
183			ax_python_devel_found=no
184			PYTHON_VERSION=""
185		fi
186	   fi
187	fi
188
189	if test $ax_python_devel_found = yes; then
190	   #
191	   # Check if you have distutils, else fail
192	   #
193	   AC_MSG_CHECKING([for the sysconfig Python package])
194	   ac_sysconfig_result=`$PYTHON -c "import sysconfig" 2>&1`
195	   if test $? -eq 0; then
196		AC_MSG_RESULT([yes])
197		IMPORT_SYSCONFIG="import sysconfig"
198	   else
199		AC_MSG_RESULT([no])
200
201		AC_MSG_CHECKING([for the distutils Python package])
202		ac_sysconfig_result=`$PYTHON -c "from distutils import sysconfig" 2>&1`
203		if test $? -eq 0; then
204			AC_MSG_RESULT([yes])
205			IMPORT_SYSCONFIG="from distutils import sysconfig"
206		else
207			AC_MSG_WARN([cannot import Python module "distutils".
208Please check your Python installation. The error was:
209$ac_sysconfig_result])
210			if ! $ax_python_devel_optional; then
211			   AC_MSG_ERROR([Giving up])
212			fi
213			ax_python_devel_found=no
214			PYTHON_VERSION=""
215		fi
216	   fi
217	fi
218
219	if test $ax_python_devel_found = yes; then
220	   #
221	   # Check for Python include path
222	   #
223	   AC_MSG_CHECKING([for Python include path])
224	   if test -z "$PYTHON_CPPFLAGS"; then
225		if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
226			# sysconfig module has different functions
227			python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
228				print (sysconfig.get_path ('include'));"`
229			plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
230				print (sysconfig.get_path ('platinclude'));"`
231		else
232			# old distutils way
233			python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
234				print (sysconfig.get_python_inc ());"`
235			plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
236				print (sysconfig.get_python_inc (plat_specific=1));"`
237		fi
238		if test -n "${python_path}"; then
239			if test "${plat_python_path}" != "${python_path}"; then
240				python_path="-I$python_path -I$plat_python_path"
241			else
242				python_path="-I$python_path"
243			fi
244		fi
245		PYTHON_CPPFLAGS=$python_path
246	   fi
247	   AC_MSG_RESULT([$PYTHON_CPPFLAGS])
248	   AC_SUBST([PYTHON_CPPFLAGS])
249
250	   #
251	   # Check for Python library path
252	   #
253	   AC_MSG_CHECKING([for Python library path])
254	   if test -z "$PYTHON_LIBS"; then
255		# (makes two attempts to ensure we've got a version number
256		# from the interpreter)
257		ac_python_version=`cat<<EOD | $PYTHON -
258
259# join all versioning strings, on some systems
260# major/minor numbers could be in different list elements
261from sysconfig import *
262e = get_config_var('VERSION')
263if e is not None:
264	print(e)
265EOD`
266
267		if test -z "$ac_python_version"; then
268			if test -n "$PYTHON_VERSION"; then
269				ac_python_version=$PYTHON_VERSION
270			else
271				ac_python_version=`$PYTHON -c "import sys; \
272					print ("%d.%d" % sys.version_info[[:2]])"`
273			fi
274		fi
275
276		# Make the versioning information available to the compiler
277		AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
278                                   [If available, contains the Python version number currently in use.])
279
280		# First, the library directory:
281		ac_python_libdir=`cat<<EOD | $PYTHON -
282
283# There should be only one
284$IMPORT_SYSCONFIG
285e = sysconfig.get_config_var('LIBDIR')
286if e is not None:
287	print (e)
288EOD`
289
290		# Now, for the library:
291		ac_python_library=`cat<<EOD | $PYTHON -
292
293$IMPORT_SYSCONFIG
294c = sysconfig.get_config_vars()
295if 'LDVERSION' in c:
296	print ('python'+c[['LDVERSION']])
297else:
298	print ('python'+c[['VERSION']])
299EOD`
300
301		# This small piece shamelessly adapted from PostgreSQL python macro;
302		# credits goes to momjian, I think. I'd like to put the right name
303		# in the credits, if someone can point me in the right direction... ?
304		#
305		if test -n "$ac_python_libdir" -a -n "$ac_python_library"
306		then
307			# use the official shared library
308			ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
309			PYTHON_LIBS="-L$ac_python_libdir -l$ac_python_library"
310		else
311			# old way: use libpython from python_configdir
312			ac_python_libdir=`$PYTHON -c \
313			  "from sysconfig import get_python_lib as f; \
314			  import os; \
315			  print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
316			PYTHON_LIBS="-L$ac_python_libdir -lpython$ac_python_version"
317		fi
318
319		if test -z "PYTHON_LIBS"; then
320			AC_MSG_WARN([
321  Cannot determine location of your Python DSO. Please check it was installed with
322  dynamic libraries enabled, or try setting PYTHON_LIBS by hand.
323			])
324			if ! $ax_python_devel_optional; then
325			   AC_MSG_ERROR([Giving up])
326			fi
327			ax_python_devel_found=no
328			PYTHON_VERSION=""
329		fi
330	   fi
331	fi
332
333	if test $ax_python_devel_found = yes; then
334	   AC_MSG_RESULT([$PYTHON_LIBS])
335	   AC_SUBST([PYTHON_LIBS])
336
337	   #
338	   # Check for site packages
339	   #
340	   AC_MSG_CHECKING([for Python site-packages path])
341	   if test -z "$PYTHON_SITE_PKG"; then
342		if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
343			PYTHON_SITE_PKG=`$PYTHON -c "
344$IMPORT_SYSCONFIG;
345if hasattr(sysconfig, 'get_default_scheme'):
346    scheme = sysconfig.get_default_scheme()
347else:
348    scheme = sysconfig._get_default_scheme()
349if scheme == 'posix_local':
350    # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
351    scheme = 'posix_prefix'
352prefix = '$prefix'
353if prefix == 'NONE':
354    prefix = '$ac_default_prefix'
355sitedir = sysconfig.get_path('purelib', scheme, vars={'base': prefix})
356print(sitedir)"`
357		else
358			# distutils.sysconfig way
359			PYTHON_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
360				print (sysconfig.get_python_lib(0,0));"`
361		fi
362	   fi
363	   AC_MSG_RESULT([$PYTHON_SITE_PKG])
364	   AC_SUBST([PYTHON_SITE_PKG])
365
366	   #
367	   # Check for platform-specific site packages
368	   #
369	   AC_MSG_CHECKING([for Python platform specific site-packages path])
370	   if test -z "$PYTHON_PLATFORM_SITE_PKG"; then
371		if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
372			PYTHON_PLATFORM_SITE_PKG=`$PYTHON -c "
373$IMPORT_SYSCONFIG;
374if hasattr(sysconfig, 'get_default_scheme'):
375    scheme = sysconfig.get_default_scheme()
376else:
377    scheme = sysconfig._get_default_scheme()
378if scheme == 'posix_local':
379    # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
380    scheme = 'posix_prefix'
381prefix = '$prefix'
382if prefix == 'NONE':
383    prefix = '$ac_default_prefix'
384sitedir = sysconfig.get_path('platlib', scheme, vars={'platbase': prefix})
385print(sitedir)"`
386		else
387			# distutils.sysconfig way
388			PYTHON_PLATFORM_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
389				print (sysconfig.get_python_lib(1,0));"`
390		fi
391	   fi
392	   AC_MSG_RESULT([$PYTHON_PLATFORM_SITE_PKG])
393	   AC_SUBST([PYTHON_PLATFORM_SITE_PKG])
394
395	   #
396	   # libraries which must be linked in when embedding
397	   #
398	   AC_MSG_CHECKING(python extra libraries)
399	   if test -z "$PYTHON_EXTRA_LIBS"; then
400	      PYTHON_EXTRA_LIBS=`$PYTHON -c "$IMPORT_SYSCONFIG; \
401                conf = sysconfig.get_config_var; \
402                print (conf('LIBS') + ' ' + conf('SYSLIBS'))"`
403	   fi
404	   AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
405	   AC_SUBST(PYTHON_EXTRA_LIBS)
406
407	   #
408	   # linking flags needed when embedding
409	   #
410	   AC_MSG_CHECKING(python extra linking flags)
411	   if test -z "$PYTHON_EXTRA_LDFLAGS"; then
412		PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "$IMPORT_SYSCONFIG; \
413			conf = sysconfig.get_config_var; \
414			print (conf('LINKFORSHARED'))"`
415		# Hack for macos, it sticks this in here.
416		PYTHON_EXTRA_LDFLAGS=`echo $PYTHON_EXTRA_LDFLAGS | sed 's/CoreFoundation.*$/CoreFoundation/'`
417	   fi
418	   AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
419	   AC_SUBST(PYTHON_EXTRA_LDFLAGS)
420
421	   #
422	   # final check to see if everything compiles alright
423	   #
424	   AC_MSG_CHECKING([consistency of all components of python development environment])
425	   # save current global flags
426	   ac_save_LIBS="$LIBS"
427	   ac_save_LDFLAGS="$LDFLAGS"
428	   ac_save_CPPFLAGS="$CPPFLAGS"
429	   LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS"
430	   LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS"
431	   CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
432	   AC_LANG_PUSH([C])
433	   AC_LINK_IFELSE([
434		AC_LANG_PROGRAM([[#include <Python.h>]],
435				[[Py_Initialize();]])
436		],[pythonexists=yes],[pythonexists=no])
437	   AC_LANG_POP([C])
438	   # turn back to default flags
439	   CPPFLAGS="$ac_save_CPPFLAGS"
440	   LIBS="$ac_save_LIBS"
441	   LDFLAGS="$ac_save_LDFLAGS"
442
443	   AC_MSG_RESULT([$pythonexists])
444
445	   if test ! "x$pythonexists" = "xyes"; then
446	      AC_MSG_WARN([
447  Could not link test program to Python. Maybe the main Python library has been
448  installed in some non-standard library path. If so, pass it to configure,
449  via the LIBS environment variable.
450  Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
451  ============================================================================
452   ERROR!
453   You probably have to install the development version of the Python package
454   for your distribution.  The exact name of this package varies among them.
455  ============================================================================
456	      ])
457	      if ! $ax_python_devel_optional; then
458		 AC_MSG_ERROR([Giving up])
459	      fi
460	      ax_python_devel_found=no
461	      PYTHON_VERSION=""
462	   fi
463	fi
464
465	#
466	# all done!
467	#
468])
469