xref: /freebsd/contrib/unbound/acx_python.m4 (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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
21	# Check if you have sysconfig
22	AC_MSG_CHECKING([for the sysconfig Python module])
23        if ac_sysconfig_result=`$PYTHON -c "import sysconfig" 2>&1`; then
24                AC_MSG_RESULT([yes])
25		sysconfig_module="sysconfig"
26		# if yes, use sysconfig, because distutils is deprecated.
27	else
28                AC_MSG_RESULT([no])
29		# if no, try to use distutils
30
31		#
32		# Check if you have distutils, else fail
33		#
34		AC_MSG_CHECKING([for the distutils Python package])
35		if ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`; then
36			AC_MSG_RESULT([yes])
37		else
38			AC_MSG_RESULT([no])
39			AC_MSG_ERROR([cannot import Python module "distutils".
40	Please check your Python installation. The error was:
41	$ac_distutils_result])
42			PYTHON_VERSION=""
43		fi
44
45		sysconfig_module="distutils.sysconfig"
46	fi
47
48        #
49        # Check for Python include path
50        #
51        AC_MSG_CHECKING([for Python include path])
52        if test -z "$PYTHON_CPPFLAGS"; then
53		if test "$sysconfig_module" = "sysconfig"; then
54			python_path=`$PYTHON -c 'import sysconfig; \
55				print(sysconfig.get_path("include"));'`
56		else
57			python_path=`$PYTHON -c "import distutils.sysconfig; \
58				print(distutils.sysconfig.get_python_inc());"`
59		fi
60                if test -n "${python_path}"; then
61                        python_path="-I$python_path"
62                fi
63                PYTHON_CPPFLAGS=$python_path
64        fi
65        AC_MSG_RESULT([$PYTHON_CPPFLAGS])
66        AC_SUBST([PYTHON_CPPFLAGS])
67
68        #
69        # Check for Python library path
70        #
71        AC_MSG_CHECKING([for Python library path])
72        if test -z "$PYTHON_LDFLAGS"; then
73                PYTHON_LDFLAGS=`$PYTHON -c "from $sysconfig_module import *; \
74                        print('-L'+get_config_var('LIBDIR')+' -L'+get_config_var('LIBDEST')+' '+get_config_var('BLDLIBRARY'));"`
75        fi
76        AC_MSG_RESULT([$PYTHON_LDFLAGS])
77        AC_SUBST([PYTHON_LDFLAGS])
78
79        if test -z "$PYTHON_LIBDIR"; then
80                PYTHON_LIBDIR=`$PYTHON -c "from $sysconfig_module import *; \
81                        print(get_config_var('LIBDIR'));"`
82        fi
83
84        #
85        # Check for site packages
86        #
87        AC_MSG_CHECKING([for Python site-packages path])
88        if test -z "$PYTHON_SITE_PKG"; then
89		if test "$sysconfig_module" = "sysconfig"; then
90			PYTHON_SITE_PKG=`$PYTHON -c 'import sysconfig; \
91				print(sysconfig.get_path("platlib"));'`
92		else
93			PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
94				print(distutils.sysconfig.get_python_lib(1,0));"`
95		fi
96        fi
97        AC_MSG_RESULT([$PYTHON_SITE_PKG])
98        AC_SUBST([PYTHON_SITE_PKG])
99
100        #
101        # final check to see if everything compiles alright
102        #
103        AC_MSG_CHECKING([consistency of all components of python development environment])
104        AC_LANG_PUSH([C])
105        # save current global flags
106        ac_save_LIBS="$LIBS"
107        ac_save_CPPFLAGS="$CPPFLAGS"
108
109        LIBS="$LIBS $PYTHON_LDFLAGS"
110        CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
111        AC_LINK_IFELSE([AC_LANG_PROGRAM([[
112                #include <Python.h>
113        ]],[[
114                Py_Initialize();
115        ]])],[pythonexists=yes],[pythonexists=no])
116
117        AC_MSG_RESULT([$pythonexists])
118
119        if test ! "$pythonexists" = "yes"; then
120           AC_MSG_ERROR([
121  Could not link test program to Python. Maybe the main Python library has been
122  installed in some non-standard library path. If so, pass it to configure,
123  via the LDFLAGS environment variable.
124  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
125  ============================================================================
126   ERROR!
127   You probably have to install the development version of the Python package
128   for your distribution.  The exact name of this package varies among them.
129  ============================================================================
130           ])
131          PYTHON_VERSION=""
132        fi
133        AC_LANG_POP
134        # turn back to default flags
135        CPPFLAGS="$ac_save_CPPFLAGS"
136        LIBS="$ac_save_LIBS"
137
138        #
139        # all done!
140        #
141])
142
143