xref: /netbsd-src/external/bsd/unbound/dist/acx_python.m4 (revision 91f7d55fb697b5e0475da4718fa34c3a3ebeac85)
13b6c3722SchristosAC_DEFUN([AC_PYTHON_DEVEL],[
23b6c3722Schristos        #
33b6c3722Schristos        # Allow the use of a (user set) custom python version
43b6c3722Schristos        #
53b6c3722Schristos        AC_ARG_VAR([PYTHON_VERSION],[The installed Python
63b6c3722Schristos                version to use, for example '2.3'. This string
73b6c3722Schristos                will be appended to the Python interpreter
83b6c3722Schristos                canonical name.])
93b6c3722Schristos
103b6c3722Schristos        AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
113b6c3722Schristos        if test -z "$PYTHON"; then
123b6c3722Schristos           AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
133b6c3722Schristos           PYTHON_VERSION=""
143b6c3722Schristos        fi
153b6c3722Schristos
163b6c3722Schristos        if test -z "$PYTHON_VERSION"; then
173b6c3722Schristos		PYTHON_VERSION=`$PYTHON -c "import sys; \
183b6c3722Schristos			print(sys.version.split()[[0]])"`
193b6c3722Schristos	fi
20*91f7d55fSchristos	# calculate the version number components.
21*91f7d55fSchristos	[
22*91f7d55fSchristos	v="$PYTHON_VERSION"
23*91f7d55fSchristos	PYTHON_VERSION_MAJOR=`echo $v | sed 's/[^0-9].*//'`
24*91f7d55fSchristos	if test -z "$PYTHON_VERSION_MAJOR"; then PYTHON_VERSION_MAJOR="0"; fi
25*91f7d55fSchristos	v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
26*91f7d55fSchristos	PYTHON_VERSION_MINOR=`echo $v | sed 's/[^0-9].*//'`
27*91f7d55fSchristos	if test -z "$PYTHON_VERSION_MINOR"; then PYTHON_VERSION_MINOR="0"; fi
28*91f7d55fSchristos	v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
29*91f7d55fSchristos	PYTHON_VERSION_PATCH=`echo $v | sed 's/[^0-9].*//'`
30*91f7d55fSchristos	if test -z "$PYTHON_VERSION_PATCH"; then PYTHON_VERSION_PATCH="0"; fi
31*91f7d55fSchristos	]
323b6c3722Schristos
33*91f7d55fSchristos	# For some systems, sysconfig exists, but has the wrong paths,
34*91f7d55fSchristos	# on Debian 10, for python 2.7 and 3.7. So, we check the version,
35*91f7d55fSchristos	# and for older versions try distutils.sysconfig first. For newer
36*91f7d55fSchristos	# versions>=3.10, where distutils.sysconfig is deprecated, use
37*91f7d55fSchristos	# sysconfig first and then attempt the other one.
38*91f7d55fSchristos	py_distutils_first="no"
39*91f7d55fSchristos	if test $PYTHON_VERSION_MAJOR -lt 3; then
40*91f7d55fSchristos		py_distutils_first="yes"
41*91f7d55fSchristos	fi
42*91f7d55fSchristos	if test $PYTHON_VERSION_MAJOR -eq 3 -a $PYTHON_VERSION_MINOR -lt 10; then
43*91f7d55fSchristos		py_distutils_first="yes"
44*91f7d55fSchristos	fi
45*91f7d55fSchristos
46*91f7d55fSchristos	# Check if you have the first module
47*91f7d55fSchristos	if test "$py_distutils_first" = "yes"; then m="distutils"; else m="sysconfig"; fi
48*91f7d55fSchristos	sysconfig_module=""
49*91f7d55fSchristos	AC_MSG_CHECKING([for the $m Python module])
50*91f7d55fSchristos        if ac_modulecheck_result1=`$PYTHON -c "import $m" 2>&1`; then
517a540f2bSchristos                AC_MSG_RESULT([yes])
52*91f7d55fSchristos		sysconfig_module="$m"
537a540f2bSchristos	else
547a540f2bSchristos                AC_MSG_RESULT([no])
55*91f7d55fSchristos	fi
567a540f2bSchristos
57*91f7d55fSchristos	# if not found, try the other one.
58*91f7d55fSchristos	if test -z "$sysconfig_module"; then
59*91f7d55fSchristos		if test "$py_distutils_first" = "yes"; then m2="sysconfig"; else m2="distutils"; fi
60*91f7d55fSchristos		AC_MSG_CHECKING([for the $m2 Python module])
61*91f7d55fSchristos		if ac_modulecheck_result2=`$PYTHON -c "import $m2" 2>&1`; then
623b6c3722Schristos			AC_MSG_RESULT([yes])
63*91f7d55fSchristos			sysconfig_module="$m2"
643b6c3722Schristos		else
653b6c3722Schristos			AC_MSG_RESULT([no])
66*91f7d55fSchristos			AC_MSG_ERROR([cannot import Python module "$m", or "$m2".
67*91f7d55fSchristos	Please check your Python installation. The errors are:
68*91f7d55fSchristos	$m
69*91f7d55fSchristos	$ac_modulecheck_result1
70*91f7d55fSchristos	$m2
71*91f7d55fSchristos	$ac_modulecheck_result2])
723b6c3722Schristos			PYTHON_VERSION=""
733b6c3722Schristos		fi
747a540f2bSchristos	fi
75*91f7d55fSchristos	if test "$sysconfig_module" = "distutils"; then sysconfig_module="distutils.sysconfig"; fi
767a540f2bSchristos
773b6c3722Schristos        #
783b6c3722Schristos        # Check for Python include path
793b6c3722Schristos        #
803b6c3722Schristos        AC_MSG_CHECKING([for Python include path])
813b6c3722Schristos        if test -z "$PYTHON_CPPFLAGS"; then
827a540f2bSchristos		if test "$sysconfig_module" = "sysconfig"; then
837a540f2bSchristos			python_path=`$PYTHON -c 'import sysconfig; \
847a540f2bSchristos				print(sysconfig.get_path("include"));'`
857a540f2bSchristos		else
863b6c3722Schristos			python_path=`$PYTHON -c "import distutils.sysconfig; \
873b6c3722Schristos				print(distutils.sysconfig.get_python_inc());"`
887a540f2bSchristos		fi
893b6c3722Schristos                if test -n "${python_path}"; then
903b6c3722Schristos                        python_path="-I$python_path"
913b6c3722Schristos                fi
923b6c3722Schristos                PYTHON_CPPFLAGS=$python_path
933b6c3722Schristos        fi
943b6c3722Schristos        AC_MSG_RESULT([$PYTHON_CPPFLAGS])
953b6c3722Schristos        AC_SUBST([PYTHON_CPPFLAGS])
963b6c3722Schristos
973b6c3722Schristos        #
983b6c3722Schristos        # Check for Python library path
993b6c3722Schristos        #
1003b6c3722Schristos        AC_MSG_CHECKING([for Python library path])
1013b6c3722Schristos        if test -z "$PYTHON_LDFLAGS"; then
1027a540f2bSchristos                PYTHON_LDFLAGS=`$PYTHON -c "from $sysconfig_module import *; \
1030cd9f4ecSchristos                        print('-L'+get_config_var('LIBDIR')+' -L'+get_config_var('LIBDEST')+' '+get_config_var('BLDLIBRARY'));"`
1043b6c3722Schristos        fi
1053b6c3722Schristos        AC_MSG_RESULT([$PYTHON_LDFLAGS])
1063b6c3722Schristos        AC_SUBST([PYTHON_LDFLAGS])
1073b6c3722Schristos
108d0eba39bSchristos        if test -z "$PYTHON_LIBDIR"; then
1097a540f2bSchristos                PYTHON_LIBDIR=`$PYTHON -c "from $sysconfig_module import *; \
110d0eba39bSchristos                        print(get_config_var('LIBDIR'));"`
111d0eba39bSchristos        fi
112d0eba39bSchristos
1133b6c3722Schristos        #
1143b6c3722Schristos        # Check for site packages
1153b6c3722Schristos        #
1163b6c3722Schristos        AC_MSG_CHECKING([for Python site-packages path])
1173b6c3722Schristos        if test -z "$PYTHON_SITE_PKG"; then
1187a540f2bSchristos		if test "$sysconfig_module" = "sysconfig"; then
1197a540f2bSchristos			PYTHON_SITE_PKG=`$PYTHON -c 'import sysconfig; \
1207a540f2bSchristos				print(sysconfig.get_path("platlib"));'`
1217a540f2bSchristos		else
1223b6c3722Schristos			PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
1233b6c3722Schristos				print(distutils.sysconfig.get_python_lib(1,0));"`
1243b6c3722Schristos		fi
1257a540f2bSchristos        fi
1263b6c3722Schristos        AC_MSG_RESULT([$PYTHON_SITE_PKG])
1273b6c3722Schristos        AC_SUBST([PYTHON_SITE_PKG])
1283b6c3722Schristos
1293b6c3722Schristos        #
1303b6c3722Schristos        # final check to see if everything compiles alright
1313b6c3722Schristos        #
1323b6c3722Schristos        AC_MSG_CHECKING([consistency of all components of python development environment])
1333b6c3722Schristos        AC_LANG_PUSH([C])
1343b6c3722Schristos        # save current global flags
1353b6c3722Schristos        ac_save_LIBS="$LIBS"
1363b6c3722Schristos        ac_save_CPPFLAGS="$CPPFLAGS"
1373b6c3722Schristos
1383b6c3722Schristos        LIBS="$LIBS $PYTHON_LDFLAGS"
1393b6c3722Schristos        CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
140d0eba39bSchristos        AC_LINK_IFELSE([AC_LANG_PROGRAM([[
1413b6c3722Schristos                #include <Python.h>
142d0eba39bSchristos        ]],[[
1433b6c3722Schristos                Py_Initialize();
144d0eba39bSchristos        ]])],[pythonexists=yes],[pythonexists=no])
1453b6c3722Schristos
1463b6c3722Schristos        AC_MSG_RESULT([$pythonexists])
1473b6c3722Schristos
1483b6c3722Schristos        if test ! "$pythonexists" = "yes"; then
1493b6c3722Schristos           AC_MSG_ERROR([
1503b6c3722Schristos  Could not link test program to Python. Maybe the main Python library has been
1513b6c3722Schristos  installed in some non-standard library path. If so, pass it to configure,
1523b6c3722Schristos  via the LDFLAGS environment variable.
1533b6c3722Schristos  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
1543b6c3722Schristos  ============================================================================
1553b6c3722Schristos   ERROR!
1563b6c3722Schristos   You probably have to install the development version of the Python package
1573b6c3722Schristos   for your distribution.  The exact name of this package varies among them.
1583b6c3722Schristos  ============================================================================
1593b6c3722Schristos           ])
1603b6c3722Schristos          PYTHON_VERSION=""
1613b6c3722Schristos        fi
1623b6c3722Schristos        AC_LANG_POP
1633b6c3722Schristos        # turn back to default flags
1643b6c3722Schristos        CPPFLAGS="$ac_save_CPPFLAGS"
1653b6c3722Schristos        LIBS="$ac_save_LIBS"
1663b6c3722Schristos
1673b6c3722Schristos        #
1683b6c3722Schristos        # all done!
1693b6c3722Schristos        #
1703b6c3722Schristos])
1713b6c3722Schristos
172