xref: /freebsd/sys/contrib/openzfs/config/ax_python_devel.m4 (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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=`cat<<EOD | $PYTHON -
101from __future__ import print_function;
102import sys;
103try:
104	from packaging import version;
105except ImportError:
106	from distlib import version;
107ver = sys.version.split ()[[0]];
108(tst_cmp, tst_ver) = ">= '2.1.0'".split ();
109tst_ver = tst_ver.strip ("'");
110eval ("print (version.LegacyVersion (ver)"+ tst_cmp +"version.LegacyVersion (tst_ver))")
111EOD`
112	if test "$ac_supports_python_ver" != "True"; then
113		if test -z "$PYTHON_NOVERSIONCHECK"; then
114			AC_MSG_RESULT([no])
115			m4_ifvaln([$2],[$2],[
116				AC_MSG_FAILURE([
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			])
126		else
127			AC_MSG_RESULT([skip at user request])
128		fi
129	else
130		AC_MSG_RESULT([yes])
131	fi
132
133	#
134	# if the macro parameter ``version'' is set, honour it
135	#
136	if test -n "$1"; then
137		AC_MSG_CHECKING([for a version of Python $1])
138		# Why the strip ()?  Because if we don't, version.parse
139		# will, for example, report 3.10.0 >= '3.11.0'
140		ac_supports_python_ver=`cat<<EOD | $PYTHON -
141
142from __future__ import print_function;
143import sys;
144try:
145	from packaging import version;
146except ImportError:
147	from distlib import version;
148ver = sys.version.split ()[[0]];
149(tst_cmp, tst_ver) = "$1".split ();
150tst_ver = tst_ver.strip ("'");
151eval ("print (version.LegacyVersion (ver)"+ tst_cmp +"version.LegacyVersion (tst_ver))")
152EOD`
153		if test "$ac_supports_python_ver" = "True"; then
154		   AC_MSG_RESULT([yes])
155		else
156			AC_MSG_RESULT([no])
157			m4_ifvaln([$2],[$2],[
158				AC_MSG_ERROR([this package requires Python $1.
159If you have it installed, but it isn't the default Python
160interpreter in your system path, please pass the PYTHON_VERSION
161variable to configure. See ``configure --help'' for reference.
162])
163				PYTHON_VERSION=""
164			])
165		fi
166	fi
167
168	#
169	# Check if you have distutils, else fail
170	#
171	AC_MSG_CHECKING([for the distutils Python package])
172	if ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`; then
173		AC_MSG_RESULT([yes])
174	else
175		AC_MSG_RESULT([no])
176		m4_ifvaln([$2],[$2],[
177			AC_MSG_ERROR([cannot import Python module "distutils".
178Please check your Python installation. The error was:
179$ac_distutils_result])
180			PYTHON_VERSION=""
181		])
182	fi
183
184	#
185	# Check for Python include path
186	#
187	AC_MSG_CHECKING([for Python include path])
188	if test -z "$PYTHON_CPPFLAGS"; then
189		python_path=`$PYTHON -c "import distutils.sysconfig; \
190			print (distutils.sysconfig.get_python_inc ());"`
191		plat_python_path=`$PYTHON -c "import distutils.sysconfig; \
192			print (distutils.sysconfig.get_python_inc (plat_specific=1));"`
193		if test -n "${python_path}"; then
194			if test "${plat_python_path}" != "${python_path}"; then
195				python_path="-I$python_path -I$plat_python_path"
196			else
197				python_path="-I$python_path"
198			fi
199		fi
200		PYTHON_CPPFLAGS=$python_path
201	fi
202	AC_MSG_RESULT([$PYTHON_CPPFLAGS])
203	AC_SUBST([PYTHON_CPPFLAGS])
204
205	#
206	# Check for Python library path
207	#
208	AC_MSG_CHECKING([for Python library path])
209	if test -z "$PYTHON_LIBS"; then
210		# (makes two attempts to ensure we've got a version number
211		# from the interpreter)
212		ac_python_version=`cat<<EOD | $PYTHON -
213
214# join all versioning strings, on some systems
215# major/minor numbers could be in different list elements
216from distutils.sysconfig import *
217e = get_config_var('VERSION')
218if e is not None:
219	print(e)
220EOD`
221
222		if test -z "$ac_python_version"; then
223			if test -n "$PYTHON_VERSION"; then
224				ac_python_version=$PYTHON_VERSION
225			else
226				ac_python_version=`$PYTHON -c "import sys; \
227					print ('.'.join(sys.version.split('.')[[:2]]))"`
228			fi
229		fi
230
231		# Make the versioning information available to the compiler
232		AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
233                                   [If available, contains the Python version number currently in use.])
234
235		# First, the library directory:
236		ac_python_libdir=`cat<<EOD | $PYTHON -
237
238# There should be only one
239import distutils.sysconfig
240e = distutils.sysconfig.get_config_var('LIBDIR')
241if e is not None:
242	print (e)
243EOD`
244
245		# Now, for the library:
246		ac_python_library=`cat<<EOD | $PYTHON -
247
248import distutils.sysconfig
249c = distutils.sysconfig.get_config_vars()
250if 'LDVERSION' in c:
251	print ('python'+c[['LDVERSION']])
252else:
253	print ('python'+c[['VERSION']])
254EOD`
255
256		# This small piece shamelessly adapted from PostgreSQL python macro;
257		# credits goes to momjian, I think. I'd like to put the right name
258		# in the credits, if someone can point me in the right direction... ?
259		#
260		if test -n "$ac_python_libdir" -a -n "$ac_python_library"
261		then
262			# use the official shared library
263			ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
264			PYTHON_LIBS="-L$ac_python_libdir -l$ac_python_library"
265		else
266			# old way: use libpython from python_configdir
267			ac_python_libdir=`$PYTHON -c \
268			  "from distutils.sysconfig import get_python_lib as f; \
269			  import os; \
270			  print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
271			PYTHON_LIBS="-L$ac_python_libdir -lpython$ac_python_version"
272		fi
273
274		if test -z "PYTHON_LIBS"; then
275			m4_ifvaln([$2],[$2],[
276				AC_MSG_ERROR([
277  Cannot determine location of your Python DSO. Please check it was installed with
278  dynamic libraries enabled, or try setting PYTHON_LIBS by hand.
279				])
280			])
281		fi
282	fi
283	AC_MSG_RESULT([$PYTHON_LIBS])
284	AC_SUBST([PYTHON_LIBS])
285
286	#
287	# Check for site packages
288	#
289	AC_MSG_CHECKING([for Python site-packages path])
290	if test -z "$PYTHON_SITE_PKG"; then
291		PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
292			print (distutils.sysconfig.get_python_lib(0,0));"`
293	fi
294	AC_MSG_RESULT([$PYTHON_SITE_PKG])
295	AC_SUBST([PYTHON_SITE_PKG])
296
297	#
298	# libraries which must be linked in when embedding
299	#
300	AC_MSG_CHECKING(python extra libraries)
301	if test -z "$PYTHON_EXTRA_LIBS"; then
302	   PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
303                conf = distutils.sysconfig.get_config_var; \
304                print (conf('LIBS') + ' ' + conf('SYSLIBS'))"`
305	fi
306	AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
307	AC_SUBST(PYTHON_EXTRA_LIBS)
308
309	#
310	# linking flags needed when embedding
311	#
312	AC_MSG_CHECKING(python extra linking flags)
313	if test -z "$PYTHON_EXTRA_LDFLAGS"; then
314		PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
315			conf = distutils.sysconfig.get_config_var; \
316			print (conf('LINKFORSHARED'))"`
317	fi
318	AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
319	AC_SUBST(PYTHON_EXTRA_LDFLAGS)
320
321	#
322	# final check to see if everything compiles alright
323	#
324	AC_MSG_CHECKING([consistency of all components of python development environment])
325	# save current global flags
326	ac_save_LIBS="$LIBS"
327	ac_save_LDFLAGS="$LDFLAGS"
328	ac_save_CPPFLAGS="$CPPFLAGS"
329	LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS $PYTHON_EXTRA_LIBS"
330	LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS"
331	CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
332	AC_LANG_PUSH([C])
333	AC_LINK_IFELSE([
334		AC_LANG_PROGRAM([[#include <Python.h>]],
335				[[Py_Initialize();]])
336		],[pythonexists=yes],[pythonexists=no])
337	AC_LANG_POP([C])
338	# turn back to default flags
339	CPPFLAGS="$ac_save_CPPFLAGS"
340	LIBS="$ac_save_LIBS"
341	LDFLAGS="$ac_save_LDFLAGS"
342
343	AC_MSG_RESULT([$pythonexists])
344
345        if test ! "x$pythonexists" = "xyes"; then
346		m4_ifvaln([$2],[$2],[
347			AC_MSG_FAILURE([
348  Could not link test program to Python. Maybe the main Python library has been
349  installed in some non-standard library path. If so, pass it to configure,
350  via the LIBS environment variable.
351  Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
352  ============================================================================
353   ERROR!
354   You probably have to install the development version of the Python package
355   for your distribution.  The exact name of this package varies among them.
356  ============================================================================
357	   ])
358			PYTHON_VERSION=""
359		])
360	fi
361
362	#
363	# all done!
364	#
365])
366