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