xref: /freebsd/contrib/unbound/acx_python.m4 (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
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, string; \
18			print string.split(sys.version)[[0]]"`
19	fi
20
21        #
22        # Check for a version of Python >= 2.1.0
23        #
24        AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
25        ac_supports_python_ver=`$PYTHON -c "import sys, string; \
26                ver = string.split(sys.version)[[0]]; \
27                print ver >= '2.1.0'"`
28        if test "$ac_supports_python_ver" != "True"; then
29                if test -z "$PYTHON_NOVERSIONCHECK"; then
30                        AC_MSG_RESULT([no])
31                        AC_MSG_FAILURE([
32This version of the AC@&t@_PYTHON_DEVEL macro
33doesn't work properly with versions of Python before
342.1.0. You may need to re-run configure, setting the
35variables PYTHON_CPPFLAGS, PYTHON_LDFLAGS, PYTHON_SITE_PKG,
36PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
37Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
38to something else than an empty string.
39])
40                else
41                        AC_MSG_RESULT([skip at user request])
42                fi
43        else
44                AC_MSG_RESULT([yes])
45        fi
46
47        #
48        # if the macro parameter ``version'' is set, honour it
49        #
50        if test -n "$1"; then
51                AC_MSG_CHECKING([for a version of Python $1])
52                ac_supports_python_ver=`$PYTHON -c "import sys, string; \
53                        ver = string.split(sys.version)[[0]]; \
54                        print ver $1"`
55                if test "$ac_supports_python_ver" = "True"; then
56                   AC_MSG_RESULT([yes])
57                else
58                        AC_MSG_RESULT([no])
59                        AC_MSG_ERROR([this package requires Python $1.
60If you have it installed, but it isn't the default Python
61interpreter in your system path, please pass the PYTHON_VERSION
62variable to configure. See ``configure --help'' for reference.
63])
64                        PYTHON_VERSION=""
65                fi
66        fi
67
68        #
69        # Check if you have distutils, else fail
70        #
71        AC_MSG_CHECKING([for the distutils Python package])
72        ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
73        if test -z "$ac_distutils_result"; then
74                AC_MSG_RESULT([yes])
75        else
76                AC_MSG_RESULT([no])
77                AC_MSG_ERROR([cannot import Python module "distutils".
78Please check your Python installation. The error was:
79$ac_distutils_result])
80                PYTHON_VERSION=""
81        fi
82
83        #
84        # Check for Python include path
85        #
86        AC_MSG_CHECKING([for Python include path])
87        if test -z "$PYTHON_CPPFLAGS"; then
88                python_path=`$PYTHON -c "import distutils.sysconfig; \
89                        print distutils.sysconfig.get_python_inc();"`
90                if test -n "${python_path}"; then
91                        python_path="-I$python_path"
92                fi
93                PYTHON_CPPFLAGS=$python_path
94        fi
95        AC_MSG_RESULT([$PYTHON_CPPFLAGS])
96        AC_SUBST([PYTHON_CPPFLAGS])
97
98        #
99        # Check for Python library path
100        #
101        AC_MSG_CHECKING([for Python library path])
102        if test -z "$PYTHON_LDFLAGS"; then
103                # (makes two attempts to ensure we've got a version number
104                # from the interpreter)
105                py_version=`$PYTHON -c "from distutils.sysconfig import *; \
106                        from string import join; \
107                        print join(get_config_vars('VERSION'))"`
108                if test "$py_version" = "[None]"; then
109                        if test -n "$PYTHON_VERSION"; then
110                                py_version=$PYTHON_VERSION
111                        else
112                                py_version=`$PYTHON -c "import sys; \
113                                        print sys.version[[:3]]"`
114                        fi
115                fi
116
117                PYTHON_LDFLAGS=`$PYTHON -c "from distutils.sysconfig import *; \
118                        from string import join; \
119                        print '-L' + get_python_lib(0,1), \
120                        '-L' + os.path.dirname(get_python_lib(0,1)), \
121                        '-lpython';"`$py_version
122        fi
123        AC_MSG_RESULT([$PYTHON_LDFLAGS])
124        AC_SUBST([PYTHON_LDFLAGS])
125
126        #
127        # Check for site packages
128        #
129        AC_MSG_CHECKING([for Python site-packages path])
130        if test -z "$PYTHON_SITE_PKG"; then
131                PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
132                        print distutils.sysconfig.get_python_lib(1,0);"`
133        fi
134        AC_MSG_RESULT([$PYTHON_SITE_PKG])
135        AC_SUBST([PYTHON_SITE_PKG])
136
137        #
138        # libraries which must be linked in when embedding
139        #
140        AC_MSG_CHECKING(python extra libraries)
141        if test -z "$PYTHON_EXTRA_LIBS"; then
142           PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
143                conf = distutils.sysconfig.get_config_var; \
144                print conf('LOCALMODLIBS'), conf('LIBS')"`
145        fi
146        AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
147        AC_SUBST(PYTHON_EXTRA_LIBS)
148
149        #
150        # linking flags needed when embedding
151        #
152        AC_MSG_CHECKING(python extra linking flags)
153        if test -z "$PYTHON_EXTRA_LDFLAGS"; then
154                PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
155                        conf = distutils.sysconfig.get_config_var; \
156                        print conf('LINKFORSHARED')"`
157        fi
158        AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
159        AC_SUBST(PYTHON_EXTRA_LDFLAGS)
160
161        #
162        # final check to see if everything compiles alright
163        #
164        AC_MSG_CHECKING([consistency of all components of python development environment])
165        AC_LANG_PUSH([C])
166        # save current global flags
167        ac_save_LIBS="$LIBS"
168        ac_save_CPPFLAGS="$CPPFLAGS"
169
170        LIBS="$LIBS $PYTHON_LDFLAGS"
171        CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
172        AC_TRY_LINK([
173                #include <Python.h>
174        ],[
175                Py_Initialize();
176        ],[pythonexists=yes],[pythonexists=no])
177
178        AC_MSG_RESULT([$pythonexists])
179
180        if test ! "$pythonexists" = "yes"; then
181           AC_MSG_ERROR([
182  Could not link test program to Python. Maybe the main Python library has been
183  installed in some non-standard library path. If so, pass it to configure,
184  via the LDFLAGS environment variable.
185  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
186  ============================================================================
187   ERROR!
188   You probably have to install the development version of the Python package
189   for your distribution.  The exact name of this package varies among them.
190  ============================================================================
191           ])
192          PYTHON_VERSION=""
193        fi
194        AC_LANG_POP
195        # turn back to default flags
196        CPPFLAGS="$ac_save_CPPFLAGS"
197        LIBS="$ac_save_LIBS"
198
199        #
200        # all done!
201        #
202])
203
204