1AC_DEFUN([AC_PYTHON_DEVEL],[
2        #
3        # Allow the use of a (user set) custom python version
4        #
5        AC_ARG_VAR([PYTHON_VERSION],[The installed Python
6                version to use, for example '2.3'. This string
7                will be appended to the Python interpreter
8                canonical name.])
9
10        AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
11        if test -z "$PYTHON"; then
12           AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
13           PYTHON_VERSION=""
14        fi
15
16        if test -z "$PYTHON_VERSION"; then
17		PYTHON_VERSION=`$PYTHON -c "import sys; \
18			print(sys.version.split()[[0]])"`
19	fi
20	# calculate the version number components.
21	[
22	v="$PYTHON_VERSION"
23	PYTHON_VERSION_MAJOR=`echo $v | sed 's/[^0-9].*//'`
24	if test -z "$PYTHON_VERSION_MAJOR"; then PYTHON_VERSION_MAJOR="0"; fi
25	v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
26	PYTHON_VERSION_MINOR=`echo $v | sed 's/[^0-9].*//'`
27	if test -z "$PYTHON_VERSION_MINOR"; then PYTHON_VERSION_MINOR="0"; fi
28	v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
29	PYTHON_VERSION_PATCH=`echo $v | sed 's/[^0-9].*//'`
30	if test -z "$PYTHON_VERSION_PATCH"; then PYTHON_VERSION_PATCH="0"; fi
31	]
32
33	# For some systems, sysconfig exists, but has the wrong paths,
34	# on Debian 10, for python 2.7 and 3.7. So, we check the version,
35	# and for older versions try distutils.sysconfig first. For newer
36	# versions>=3.10, where distutils.sysconfig is deprecated, use
37	# sysconfig first and then attempt the other one.
38	py_distutils_first="no"
39	if test $PYTHON_VERSION_MAJOR -lt 3; then
40		py_distutils_first="yes"
41	fi
42	if test $PYTHON_VERSION_MAJOR -eq 3 -a $PYTHON_VERSION_MINOR -lt 10; then
43		py_distutils_first="yes"
44	fi
45
46	# Check if you have the first module
47	if test "$py_distutils_first" = "yes"; then m="distutils"; else m="sysconfig"; fi
48	sysconfig_module=""
49	AC_MSG_CHECKING([for the $m Python module])
50        if ac_modulecheck_result1=`$PYTHON -c "import $m" 2>&1`; then
51                AC_MSG_RESULT([yes])
52		sysconfig_module="$m"
53	else
54                AC_MSG_RESULT([no])
55	fi
56
57	# if not found, try the other one.
58	if test -z "$sysconfig_module"; then
59		if test "$py_distutils_first" = "yes"; then m2="sysconfig"; else m2="distutils"; fi
60		AC_MSG_CHECKING([for the $m2 Python module])
61		if ac_modulecheck_result2=`$PYTHON -c "import $m2" 2>&1`; then
62			AC_MSG_RESULT([yes])
63			sysconfig_module="$m2"
64		else
65			AC_MSG_RESULT([no])
66			AC_MSG_ERROR([cannot import Python module "$m", or "$m2".
67	Please check your Python installation. The errors are:
68	$m
69	$ac_modulecheck_result1
70	$m2
71	$ac_modulecheck_result2])
72			PYTHON_VERSION=""
73		fi
74	fi
75	if test "$sysconfig_module" = "distutils"; then sysconfig_module="distutils.sysconfig"; fi
76
77        #
78        # Check for Python include path
79        #
80        AC_MSG_CHECKING([for Python include path])
81        if test -z "$PYTHON_CPPFLAGS"; then
82		if test "$sysconfig_module" = "sysconfig"; then
83			python_path=`$PYTHON -c 'import sysconfig; \
84				print(sysconfig.get_path("include"));'`
85		else
86			python_path=`$PYTHON -c "import distutils.sysconfig; \
87				print(distutils.sysconfig.get_python_inc());"`
88		fi
89                if test -n "${python_path}"; then
90                        python_path="-I$python_path"
91                fi
92                PYTHON_CPPFLAGS=$python_path
93        fi
94        AC_MSG_RESULT([$PYTHON_CPPFLAGS])
95        AC_SUBST([PYTHON_CPPFLAGS])
96
97        #
98        # Check for Python library path
99        #
100        AC_MSG_CHECKING([for Python library path])
101        if test -z "$PYTHON_LDFLAGS"; then
102                PYTHON_LDFLAGS=`$PYTHON -c "from $sysconfig_module import *; \
103                        print('-L'+get_config_var('LIBDIR')+' -L'+get_config_var('LIBDEST')+' '+get_config_var('BLDLIBRARY'));"`
104        fi
105        AC_MSG_RESULT([$PYTHON_LDFLAGS])
106        AC_SUBST([PYTHON_LDFLAGS])
107
108        if test -z "$PYTHON_LIBDIR"; then
109                PYTHON_LIBDIR=`$PYTHON -c "from $sysconfig_module import *; \
110                        print(get_config_var('LIBDIR'));"`
111        fi
112
113        #
114        # Check for site packages
115        #
116        AC_MSG_CHECKING([for Python site-packages path])
117        if test -z "$PYTHON_SITE_PKG"; then
118		if test "$sysconfig_module" = "sysconfig"; then
119			PYTHON_SITE_PKG=`$PYTHON -c 'import sysconfig; \
120				print(sysconfig.get_path("platlib"));'`
121		else
122			PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
123				print(distutils.sysconfig.get_python_lib(1,0));"`
124		fi
125        fi
126        AC_MSG_RESULT([$PYTHON_SITE_PKG])
127        AC_SUBST([PYTHON_SITE_PKG])
128
129        #
130        # final check to see if everything compiles alright
131        #
132        AC_MSG_CHECKING([consistency of all components of python development environment])
133        AC_LANG_PUSH([C])
134        # save current global flags
135        ac_save_LIBS="$LIBS"
136        ac_save_CPPFLAGS="$CPPFLAGS"
137
138        LIBS="$LIBS $PYTHON_LDFLAGS"
139        CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
140        AC_LINK_IFELSE([AC_LANG_PROGRAM([[
141                #include <Python.h>
142        ]],[[
143                Py_Initialize();
144        ]])],[pythonexists=yes],[pythonexists=no])
145
146        AC_MSG_RESULT([$pythonexists])
147
148        if test ! "$pythonexists" = "yes"; then
149           AC_MSG_ERROR([
150  Could not link test program to Python. Maybe the main Python library has been
151  installed in some non-standard library path. If so, pass it to configure,
152  via the LDFLAGS environment variable.
153  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
154  ============================================================================
155   ERROR!
156   You probably have to install the development version of the Python package
157   for your distribution.  The exact name of this package varies among them.
158  ============================================================================
159           ])
160          PYTHON_VERSION=""
161        fi
162        AC_LANG_POP
163        # turn back to default flags
164        CPPFLAGS="$ac_save_CPPFLAGS"
165        LIBS="$ac_save_LIBS"
166
167        #
168        # all done!
169        #
170])
171
172