xref: /openbsd-src/usr.sbin/unbound/acx_python.m4 (revision 98bc733b08604094f4138174a0ee0bb9faaca4bd)
1933707f3SsthenAC_DEFUN([AC_PYTHON_DEVEL],[
2933707f3Ssthen        #
3933707f3Ssthen        # Allow the use of a (user set) custom python version
4933707f3Ssthen        #
5933707f3Ssthen        AC_ARG_VAR([PYTHON_VERSION],[The installed Python
6933707f3Ssthen                version to use, for example '2.3'. This string
7933707f3Ssthen                will be appended to the Python interpreter
8933707f3Ssthen                canonical name.])
9933707f3Ssthen
10933707f3Ssthen        AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
11933707f3Ssthen        if test -z "$PYTHON"; then
12*98bc733bSsthen           AC_MSG_ERROR([Cannot find 'python$PYTHON_VERSION' in your system path. You can use the environment variable 'PYTHON_VERSION=version_number' for an explicit version.])
13933707f3Ssthen           PYTHON_VERSION=""
14933707f3Ssthen        fi
15933707f3Ssthen
16933707f3Ssthen        if test -z "$PYTHON_VERSION"; then
1798f3ca02Sbrad		PYTHON_VERSION=`$PYTHON -c "import sys; \
1898f3ca02Sbrad			print(sys.version.split()[[0]])"`
19933707f3Ssthen	fi
208b7325afSsthen	# calculate the version number components.
218b7325afSsthen	[
228b7325afSsthen	v="$PYTHON_VERSION"
238b7325afSsthen	PYTHON_VERSION_MAJOR=`echo $v | sed 's/[^0-9].*//'`
248b7325afSsthen	if test -z "$PYTHON_VERSION_MAJOR"; then PYTHON_VERSION_MAJOR="0"; fi
258b7325afSsthen	v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
268b7325afSsthen	PYTHON_VERSION_MINOR=`echo $v | sed 's/[^0-9].*//'`
278b7325afSsthen	if test -z "$PYTHON_VERSION_MINOR"; then PYTHON_VERSION_MINOR="0"; fi
288b7325afSsthen	v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
298b7325afSsthen	PYTHON_VERSION_PATCH=`echo $v | sed 's/[^0-9].*//'`
308b7325afSsthen	if test -z "$PYTHON_VERSION_PATCH"; then PYTHON_VERSION_PATCH="0"; fi
318b7325afSsthen	]
32933707f3Ssthen
338b7325afSsthen	# For some systems, sysconfig exists, but has the wrong paths,
348b7325afSsthen	# on Debian 10, for python 2.7 and 3.7. So, we check the version,
358b7325afSsthen	# and for older versions try distutils.sysconfig first. For newer
368b7325afSsthen	# versions>=3.10, where distutils.sysconfig is deprecated, use
378b7325afSsthen	# sysconfig first and then attempt the other one.
388b7325afSsthen	py_distutils_first="no"
398b7325afSsthen	if test $PYTHON_VERSION_MAJOR -lt 3; then
408b7325afSsthen		py_distutils_first="yes"
418b7325afSsthen	fi
428b7325afSsthen	if test $PYTHON_VERSION_MAJOR -eq 3 -a $PYTHON_VERSION_MINOR -lt 10; then
438b7325afSsthen		py_distutils_first="yes"
448b7325afSsthen	fi
458b7325afSsthen
468b7325afSsthen	# Check if you have the first module
478b7325afSsthen	if test "$py_distutils_first" = "yes"; then m="distutils"; else m="sysconfig"; fi
488b7325afSsthen	sysconfig_module=""
498b7325afSsthen	AC_MSG_CHECKING([for the $m Python module])
508b7325afSsthen        if ac_modulecheck_result1=`$PYTHON -c "import $m" 2>&1`; then
510bdb4f62Ssthen                AC_MSG_RESULT([yes])
528b7325afSsthen		sysconfig_module="$m"
530bdb4f62Ssthen	else
540bdb4f62Ssthen                AC_MSG_RESULT([no])
558b7325afSsthen	fi
560bdb4f62Ssthen
578b7325afSsthen	# if not found, try the other one.
588b7325afSsthen	if test -z "$sysconfig_module"; then
598b7325afSsthen		if test "$py_distutils_first" = "yes"; then m2="sysconfig"; else m2="distutils"; fi
608b7325afSsthen		AC_MSG_CHECKING([for the $m2 Python module])
618b7325afSsthen		if ac_modulecheck_result2=`$PYTHON -c "import $m2" 2>&1`; then
62933707f3Ssthen			AC_MSG_RESULT([yes])
638b7325afSsthen			sysconfig_module="$m2"
64933707f3Ssthen		else
65933707f3Ssthen			AC_MSG_RESULT([no])
668b7325afSsthen			AC_MSG_ERROR([cannot import Python module "$m", or "$m2".
678b7325afSsthen	Please check your Python installation. The errors are:
688b7325afSsthen	$m
698b7325afSsthen	$ac_modulecheck_result1
708b7325afSsthen	$m2
718b7325afSsthen	$ac_modulecheck_result2])
72933707f3Ssthen			PYTHON_VERSION=""
73933707f3Ssthen		fi
740bdb4f62Ssthen	fi
758b7325afSsthen	if test "$sysconfig_module" = "distutils"; then sysconfig_module="distutils.sysconfig"; fi
760bdb4f62Ssthen
77933707f3Ssthen        #
78933707f3Ssthen        # Check for Python include path
79933707f3Ssthen        #
80933707f3Ssthen        AC_MSG_CHECKING([for Python include path])
81933707f3Ssthen        if test -z "$PYTHON_CPPFLAGS"; then
820bdb4f62Ssthen		if test "$sysconfig_module" = "sysconfig"; then
830bdb4f62Ssthen			python_path=`$PYTHON -c 'import sysconfig; \
840bdb4f62Ssthen				print(sysconfig.get_path("include"));'`
850bdb4f62Ssthen		else
86933707f3Ssthen			python_path=`$PYTHON -c "import distutils.sysconfig; \
8798f3ca02Sbrad				print(distutils.sysconfig.get_python_inc());"`
880bdb4f62Ssthen		fi
89933707f3Ssthen                if test -n "${python_path}"; then
90933707f3Ssthen                        python_path="-I$python_path"
91933707f3Ssthen                fi
92933707f3Ssthen                PYTHON_CPPFLAGS=$python_path
93933707f3Ssthen        fi
94933707f3Ssthen        AC_MSG_RESULT([$PYTHON_CPPFLAGS])
95933707f3Ssthen        AC_SUBST([PYTHON_CPPFLAGS])
96933707f3Ssthen
97933707f3Ssthen        #
98933707f3Ssthen        # Check for Python library path
99933707f3Ssthen        #
100933707f3Ssthen        AC_MSG_CHECKING([for Python library path])
101933707f3Ssthen        if test -z "$PYTHON_LDFLAGS"; then
1020bdb4f62Ssthen                PYTHON_LDFLAGS=`$PYTHON -c "from $sysconfig_module import *; \
10377079be7Ssthen                        print('-L'+get_config_var('LIBDIR')+' -L'+get_config_var('LIBDEST')+' '+get_config_var('BLDLIBRARY'));"`
104933707f3Ssthen        fi
105933707f3Ssthen        AC_MSG_RESULT([$PYTHON_LDFLAGS])
106933707f3Ssthen        AC_SUBST([PYTHON_LDFLAGS])
107933707f3Ssthen
108a3167c07Ssthen        if test -z "$PYTHON_LIBDIR"; then
1090bdb4f62Ssthen                PYTHON_LIBDIR=`$PYTHON -c "from $sysconfig_module import *; \
110a3167c07Ssthen                        print(get_config_var('LIBDIR'));"`
111a3167c07Ssthen        fi
112a3167c07Ssthen
113933707f3Ssthen        #
114933707f3Ssthen        # Check for site packages
115933707f3Ssthen        #
116933707f3Ssthen        AC_MSG_CHECKING([for Python site-packages path])
117933707f3Ssthen        if test -z "$PYTHON_SITE_PKG"; then
1180bdb4f62Ssthen		if test "$sysconfig_module" = "sysconfig"; then
1190bdb4f62Ssthen			PYTHON_SITE_PKG=`$PYTHON -c 'import sysconfig; \
1200bdb4f62Ssthen				print(sysconfig.get_path("platlib"));'`
1210bdb4f62Ssthen		else
122933707f3Ssthen			PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
12398f3ca02Sbrad				print(distutils.sysconfig.get_python_lib(1,0));"`
124933707f3Ssthen		fi
1250bdb4f62Ssthen        fi
126933707f3Ssthen        AC_MSG_RESULT([$PYTHON_SITE_PKG])
127933707f3Ssthen        AC_SUBST([PYTHON_SITE_PKG])
128933707f3Ssthen
129933707f3Ssthen        #
130933707f3Ssthen        # final check to see if everything compiles alright
131933707f3Ssthen        #
132933707f3Ssthen        AC_MSG_CHECKING([consistency of all components of python development environment])
133933707f3Ssthen        AC_LANG_PUSH([C])
134933707f3Ssthen        # save current global flags
1353dcb24b8Ssthen        ac_save_LIBS="$LIBS"
1363dcb24b8Ssthen        ac_save_CPPFLAGS="$CPPFLAGS"
1373dcb24b8Ssthen
1383dcb24b8Ssthen        LIBS="$LIBS $PYTHON_LDFLAGS"
1393dcb24b8Ssthen        CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
1409982a05dSsthen        AC_LINK_IFELSE([AC_LANG_PROGRAM([[
141933707f3Ssthen                #include <Python.h>
1429982a05dSsthen        ]],[[
143933707f3Ssthen                Py_Initialize();
1449982a05dSsthen        ]])],[pythonexists=yes],[pythonexists=no])
145933707f3Ssthen
146933707f3Ssthen        AC_MSG_RESULT([$pythonexists])
147933707f3Ssthen
148933707f3Ssthen        if test ! "$pythonexists" = "yes"; then
149933707f3Ssthen           AC_MSG_ERROR([
150933707f3Ssthen  Could not link test program to Python. Maybe the main Python library has been
151933707f3Ssthen  installed in some non-standard library path. If so, pass it to configure,
152933707f3Ssthen  via the LDFLAGS environment variable.
153933707f3Ssthen  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
154933707f3Ssthen  ============================================================================
155933707f3Ssthen   ERROR!
156933707f3Ssthen   You probably have to install the development version of the Python package
157933707f3Ssthen   for your distribution.  The exact name of this package varies among them.
158933707f3Ssthen  ============================================================================
159933707f3Ssthen           ])
160933707f3Ssthen          PYTHON_VERSION=""
161933707f3Ssthen        fi
162933707f3Ssthen        AC_LANG_POP
163933707f3Ssthen        # turn back to default flags
164933707f3Ssthen        CPPFLAGS="$ac_save_CPPFLAGS"
165933707f3Ssthen        LIBS="$ac_save_LIBS"
166933707f3Ssthen
167933707f3Ssthen        #
168933707f3Ssthen        # all done!
169933707f3Ssthen        #
170933707f3Ssthen])
171933707f3Ssthen
172