xref: /src/sys/contrib/openzfs/config/ax_python_devel.m4 (revision 8a62a2a5659d1839d8799b4274c04469d7f17c78)
1# SPDX-License-Identifier: GPL-3.0-or-later WITH Autoconf-exception-macro
2# ===========================================================================
3#     https://www.gnu.org/software/autoconf-archive/ax_python_devel.html
4# ===========================================================================
5#
6# SYNOPSIS
7#
8#   AX_PYTHON_DEVEL([version[,optional]])
9#
10# DESCRIPTION
11#
12#   Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
13#   in your configure.ac.
14#
15#   This macro checks for Python and tries to get the include path to
16#   'Python.h'. It provides the $(PYTHON_CPPFLAGS) and $(PYTHON_LIBS) output
17#   variables. It also exports $(PYTHON_EXTRA_LIBS) and
18#   $(PYTHON_EXTRA_LDFLAGS) for embedding Python in your code.
19#
20#   You can search for some particular version of Python by passing a
21#   parameter to this macro, for example ">= '2.3.1'", or "== '2.4'". Please
22#   note that you *have* to pass also an operator along with the version to
23#   match, and pay special attention to the single quotes surrounding the
24#   version number. Don't use "PYTHON_VERSION" for this: that environment
25#   variable is declared as precious and thus reserved for the end-user.
26#
27#   By default this will fail if it does not detect a development version of
28#   python.  If you want it to continue, set optional to true, like
29#   AX_PYTHON_DEVEL([], [true]).  The ax_python_devel_found variable will be
30#   "no" if it fails.
31#
32#   This macro should work for all versions of Python >= 2.1.0. As an end
33#   user, you can disable the check for the python version by setting the
34#   PYTHON_NOVERSIONCHECK environment variable to something else than the
35#   empty string.
36#
37#   If you need to use this macro for an older Python version, please
38#   contact the authors. We're always open for feedback.
39#
40# LICENSE
41#
42#   Copyright (c) 2009 Sebastian Huber <sebastian-huber@web.de>
43#   Copyright (c) 2009 Alan W. Irwin
44#   Copyright (c) 2009 Rafael Laboissiere <rafael@laboissiere.net>
45#   Copyright (c) 2009 Andrew Collier
46#   Copyright (c) 2009 Matteo Settenvini <matteo@member.fsf.org>
47#   Copyright (c) 2009 Horst Knorr <hk_classes@knoda.org>
48#   Copyright (c) 2013 Daniel Mullner <muellner@math.stanford.edu>
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 37
77
78AU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
79AC_DEFUN([AX_PYTHON_DEVEL],[
80	# Get whether it's optional
81	if test -z "$2"; then
82	   ax_python_devel_optional=false
83	else
84	   ax_python_devel_optional=$2
85	fi
86	ax_python_devel_found=yes
87
88	#
89	# Allow the use of a (user set) custom python version
90	#
91	AC_ARG_VAR([PYTHON_VERSION],[The installed Python
92		version to use, for example '2.3'. This string
93		will be appended to the Python interpreter
94		canonical name.])
95
96	AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
97	if test -z "$PYTHON"; then
98	   AC_MSG_WARN([Cannot find python$PYTHON_VERSION in your system path])
99	   if ! $ax_python_devel_optional; then
100	      AC_MSG_ERROR([Giving up, python development not available])
101	   fi
102	   ax_python_devel_found=no
103	   PYTHON_VERSION=""
104	fi
105
106	if test $ax_python_devel_found = yes; then
107	   #
108	   # Check for a version of Python >= 2.1.0
109	   #
110	   AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
111	   ac_supports_python_ver=`$PYTHON -c "import sys; \
112		ver = sys.version.split ()[[0]]; \
113		print (ver >= '2.1.0')"`
114	   if test "$ac_supports_python_ver" != "True"; then
115		if test -z "$PYTHON_NOVERSIONCHECK"; then
116			AC_MSG_RESULT([no])
117			AC_MSG_WARN([
118This version of the AC@&t@_PYTHON_DEVEL macro
119doesn't work properly with versions of Python before
1202.1.0. You may need to re-run configure, setting the
121variables PYTHON_CPPFLAGS, PYTHON_LIBS, PYTHON_SITE_PKG,
122PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
123Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
124to something else than an empty string.
125])
126			if ! $ax_python_devel_optional; then
127			   AC_MSG_FAILURE([Giving up])
128			fi
129			ax_python_devel_found=no
130			PYTHON_VERSION=""
131		else
132			AC_MSG_RESULT([skip at user request])
133		fi
134	   else
135		AC_MSG_RESULT([yes])
136	   fi
137	fi
138
139	if test $ax_python_devel_found = yes; then
140	   #
141	   # If the macro parameter ``version'' is set, honour it.
142	   # A Python shim class, VPy, is used to implement correct version comparisons via
143	   # string expressions, since e.g. a naive textual ">= 2.7.3" won't work for
144	   # Python 2.7.10 (the ".1" being evaluated as less than ".3").
145	   #
146	   if test -n "$1"; then
147		AC_MSG_CHECKING([for a version of Python $1])
148                cat << EOF > ax_python_devel_vpy.py
149class VPy:
150    def vtup(self, s):
151        return tuple(map(int, s.strip().replace("rc", ".").split(".")))
152    def __init__(self):
153        import sys
154        self.vpy = tuple(sys.version_info)[[:3]]
155    def __eq__(self, s):
156        return self.vpy == self.vtup(s)
157    def __ne__(self, s):
158        return self.vpy != self.vtup(s)
159    def __lt__(self, s):
160        return self.vpy < self.vtup(s)
161    def __gt__(self, s):
162        return self.vpy > self.vtup(s)
163    def __le__(self, s):
164        return self.vpy <= self.vtup(s)
165    def __ge__(self, s):
166        return self.vpy >= self.vtup(s)
167EOF
168		ac_supports_python_ver=`$PYTHON -c "import ax_python_devel_vpy; \
169                        ver = ax_python_devel_vpy.VPy(); \
170			print (ver $1)"`
171                rm -rf ax_python_devel_vpy*.py* __pycache__/ax_python_devel_vpy*.py*
172		if test "$ac_supports_python_ver" = "True"; then
173			AC_MSG_RESULT([yes])
174		else
175			AC_MSG_RESULT([no])
176			AC_MSG_WARN([this package requires Python $1.
177If you have it installed, but it isn't the default Python
178interpreter in your system path, please pass the PYTHON_VERSION
179variable to configure. See ``configure --help'' for reference.
180])
181			if ! $ax_python_devel_optional; then
182			   AC_MSG_ERROR([Giving up])
183			fi
184			ax_python_devel_found=no
185			PYTHON_VERSION=""
186		fi
187	   fi
188	fi
189
190	if test $ax_python_devel_found = yes; then
191	   #
192	   # Check if you have distutils, else fail
193	   #
194	   AC_MSG_CHECKING([for the sysconfig Python package])
195	   ac_sysconfig_result=`$PYTHON -c "import sysconfig" 2>&1`
196	   if test $? -eq 0; then
197		AC_MSG_RESULT([yes])
198		IMPORT_SYSCONFIG="import sysconfig"
199	   else
200		AC_MSG_RESULT([no])
201
202		AC_MSG_CHECKING([for the distutils Python package])
203		ac_sysconfig_result=`$PYTHON -c "from distutils import sysconfig" 2>&1`
204		if test $? -eq 0; then
205			AC_MSG_RESULT([yes])
206			IMPORT_SYSCONFIG="from distutils import sysconfig"
207		else
208			AC_MSG_WARN([cannot import Python module "distutils".
209Please check your Python installation. The error was:
210$ac_sysconfig_result])
211			if ! $ax_python_devel_optional; then
212			   AC_MSG_ERROR([Giving up])
213			fi
214			ax_python_devel_found=no
215			PYTHON_VERSION=""
216		fi
217	   fi
218	fi
219
220	if test $ax_python_devel_found = yes; then
221	   #
222	   # Check for Python include path
223	   #
224	   AC_MSG_CHECKING([for Python include path])
225	   if test -z "$PYTHON_CPPFLAGS"; then
226		if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
227			# sysconfig module has different functions
228			python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
229				print (sysconfig.get_path ('include'));"`
230			plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
231				print (sysconfig.get_path ('platinclude'));"`
232		else
233			# old distutils way
234			python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
235				print (sysconfig.get_python_inc ());"`
236			plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
237				print (sysconfig.get_python_inc (plat_specific=1));"`
238		fi
239		if test -n "${python_path}"; then
240			if test "${plat_python_path}" != "${python_path}"; then
241				python_path="-I$python_path -I$plat_python_path"
242			else
243				python_path="-I$python_path"
244			fi
245		fi
246		PYTHON_CPPFLAGS=$python_path
247	   fi
248	   AC_MSG_RESULT([$PYTHON_CPPFLAGS])
249	   AC_SUBST([PYTHON_CPPFLAGS])
250
251	   #
252	   # Check for Python library path
253	   #
254	   AC_MSG_CHECKING([for Python library path])
255	   if test -z "$PYTHON_LIBS"; then
256		# (makes two attempts to ensure we've got a version number
257		# from the interpreter)
258		ac_python_version=`cat<<EOD | $PYTHON -
259
260# join all versioning strings, on some systems
261# major/minor numbers could be in different list elements
262from sysconfig import *
263e = get_config_var('VERSION')
264if e is not None:
265	print(e)
266EOD`
267
268		if test -z "$ac_python_version"; then
269			if test -n "$PYTHON_VERSION"; then
270				ac_python_version=$PYTHON_VERSION
271			else
272				ac_python_version=`$PYTHON -c "import sys; \
273					print ("%d.%d" % sys.version_info[[:2]])"`
274			fi
275		fi
276
277		# Make the versioning information available to the compiler
278		AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
279                                   [If available, contains the Python version number currently in use.])
280
281		# First, the library directory:
282		ac_python_libdir=`cat<<EOD | $PYTHON -
283
284# There should be only one
285$IMPORT_SYSCONFIG
286e = sysconfig.get_config_var('LIBDIR')
287if e is not None:
288	print (e)
289EOD`
290
291		# Now, for the library:
292		ac_python_library=`cat<<EOD | $PYTHON -
293
294$IMPORT_SYSCONFIG
295c = sysconfig.get_config_vars()
296if 'LDVERSION' in c:
297	print ('python'+c[['LDVERSION']])
298else:
299	print ('python'+c[['VERSION']])
300EOD`
301
302		# This small piece shamelessly adapted from PostgreSQL python macro;
303		# credits goes to momjian, I think. I'd like to put the right name
304		# in the credits, if someone can point me in the right direction... ?
305		#
306		if test -n "$ac_python_libdir" -a -n "$ac_python_library"
307		then
308			# use the official shared library
309			ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
310			PYTHON_LIBS="-L$ac_python_libdir -l$ac_python_library"
311		else
312			# old way: use libpython from python_configdir
313			ac_python_libdir=`$PYTHON -c \
314			  "from sysconfig import get_python_lib as f; \
315			  import os; \
316			  print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
317			PYTHON_LIBS="-L$ac_python_libdir -lpython$ac_python_version"
318		fi
319
320		if test -z "$PYTHON_LIBS"; then
321			AC_MSG_WARN([
322  Cannot determine location of your Python DSO. Please check it was installed with
323  dynamic libraries enabled, or try setting PYTHON_LIBS by hand.
324			])
325			if ! $ax_python_devel_optional; then
326			   AC_MSG_ERROR([Giving up])
327			fi
328			ax_python_devel_found=no
329			PYTHON_VERSION=""
330		fi
331	   fi
332	fi
333
334	if test $ax_python_devel_found = yes; then
335	   AC_MSG_RESULT([$PYTHON_LIBS])
336	   AC_SUBST([PYTHON_LIBS])
337
338	   #
339	   # Check for site packages
340	   #
341	   AC_MSG_CHECKING([for Python site-packages path])
342	   if test -z "$PYTHON_SITE_PKG"; then
343		if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
344			PYTHON_SITE_PKG=`$PYTHON -c "
345$IMPORT_SYSCONFIG;
346if hasattr(sysconfig, 'get_default_scheme'):
347    scheme = sysconfig.get_default_scheme()
348else:
349    scheme = sysconfig._get_default_scheme()
350if scheme == 'posix_local':
351    # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
352    scheme = 'posix_prefix'
353prefix = '$prefix'
354if prefix == 'NONE':
355    prefix = '$ac_default_prefix'
356sitedir = sysconfig.get_path('purelib', scheme, vars={'base': prefix})
357print(sitedir)"`
358		else
359			# distutils.sysconfig way
360			PYTHON_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
361				print (sysconfig.get_python_lib(0,0));"`
362		fi
363	   fi
364	   AC_MSG_RESULT([$PYTHON_SITE_PKG])
365	   AC_SUBST([PYTHON_SITE_PKG])
366
367	   #
368	   # Check for platform-specific site packages
369	   #
370	   AC_MSG_CHECKING([for Python platform specific site-packages path])
371	   if test -z "$PYTHON_PLATFORM_SITE_PKG"; then
372		if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
373			PYTHON_PLATFORM_SITE_PKG=`$PYTHON -c "
374$IMPORT_SYSCONFIG;
375if hasattr(sysconfig, 'get_default_scheme'):
376    scheme = sysconfig.get_default_scheme()
377else:
378    scheme = sysconfig._get_default_scheme()
379if scheme == 'posix_local':
380    # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
381    scheme = 'posix_prefix'
382prefix = '$prefix'
383if prefix == 'NONE':
384    prefix = '$ac_default_prefix'
385sitedir = sysconfig.get_path('platlib', scheme, vars={'platbase': prefix})
386print(sitedir)"`
387		else
388			# distutils.sysconfig way
389			PYTHON_PLATFORM_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
390				print (sysconfig.get_python_lib(1,0));"`
391		fi
392	   fi
393	   AC_MSG_RESULT([$PYTHON_PLATFORM_SITE_PKG])
394	   AC_SUBST([PYTHON_PLATFORM_SITE_PKG])
395
396	   #
397	   # libraries which must be linked in when embedding
398	   #
399	   AC_MSG_CHECKING(python extra libraries)
400	   if test -z "$PYTHON_EXTRA_LIBS"; then
401	      PYTHON_EXTRA_LIBS=`$PYTHON -c "$IMPORT_SYSCONFIG; \
402                conf = sysconfig.get_config_var; \
403                print (conf('LIBS') + ' ' + conf('SYSLIBS'))"`
404	   fi
405	   AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
406	   AC_SUBST(PYTHON_EXTRA_LIBS)
407
408	   #
409	   # linking flags needed when embedding
410	   #
411	   AC_MSG_CHECKING(python extra linking flags)
412	   if test -z "$PYTHON_EXTRA_LDFLAGS"; then
413		PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "$IMPORT_SYSCONFIG; \
414			conf = sysconfig.get_config_var; \
415			print (conf('LINKFORSHARED'))"`
416		# Hack for macos, it sticks this in here.
417		PYTHON_EXTRA_LDFLAGS=`echo $PYTHON_EXTRA_LDFLAGS | sed 's/CoreFoundation.*$/CoreFoundation/'`
418	   fi
419	   AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
420	   AC_SUBST(PYTHON_EXTRA_LDFLAGS)
421
422	   #
423	   # final check to see if everything compiles alright
424	   #
425	   AC_MSG_CHECKING([consistency of all components of python development environment])
426	   # save current global flags
427	   ac_save_LIBS="$LIBS"
428	   ac_save_LDFLAGS="$LDFLAGS"
429	   ac_save_CPPFLAGS="$CPPFLAGS"
430	   LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS"
431	   LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS"
432	   CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
433	   AC_LANG_PUSH([C])
434	   AC_LINK_IFELSE([
435		AC_LANG_PROGRAM([[#include <Python.h>]],
436				[[Py_Initialize();]])
437		],[pythonexists=yes],[pythonexists=no])
438	   AC_LANG_POP([C])
439	   # turn back to default flags
440	   CPPFLAGS="$ac_save_CPPFLAGS"
441	   LIBS="$ac_save_LIBS"
442	   LDFLAGS="$ac_save_LDFLAGS"
443
444	   AC_MSG_RESULT([$pythonexists])
445
446	   if test ! "x$pythonexists" = "xyes"; then
447	      AC_MSG_WARN([
448  Could not link test program to Python. Maybe the main Python library has been
449  installed in some non-standard library path. If so, pass it to configure,
450  via the LIBS environment variable.
451  Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
452  ============================================================================
453   ERROR!
454   You probably have to install the development version of the Python package
455   for your distribution.  The exact name of this package varies among them.
456  ============================================================================
457	      ])
458	      if ! $ax_python_devel_optional; then
459		 AC_MSG_ERROR([Giving up])
460	      fi
461	      ax_python_devel_found=no
462	      PYTHON_VERSION=""
463	   fi
464	fi
465
466	#
467	# all done!
468	#
469])
470