Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / m4 / ax_python_devel.m4
1 # ===========================================================================
2 #     https://www.gnu.org/software/autoconf-archive/ax_python_devel.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 #   AX_PYTHON_DEVEL([version])
8 #
9 # DESCRIPTION
10 #
11 #   Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
12 #   in your configure.ac.
13 #
14 #   This macro checks for Python and tries to get the include path to
15 #   'Python.h'. It provides the $(PYTHON_CPPFLAGS) and $(PYTHON_LIBS) output
16 #   variables. It also exports $(PYTHON_EXTRA_LIBS) and
17 #   $(PYTHON_EXTRA_LDFLAGS) for embedding Python in your code.
18 #
19 #   You can search for some particular version of Python by passing a
20 #   parameter to this macro, for example ">= '2.3.1'", or "== '2.4'". Please
21 #   note that you *have* to pass also an operator along with the version to
22 #   match, and pay special attention to the single quotes surrounding the
23 #   version number. Don't use "PYTHON_VERSION" for this: that environment
24 #   variable is declared as precious and thus reserved for the end-user.
25 #
26 #   This macro should work for all versions of Python >= 2.1.0. As an end
27 #   user, you can disable the check for the python version by setting the
28 #   PYTHON_NOVERSIONCHECK environment variable to something else than the
29 #   empty string.
30 #
31 #   If you need to use this macro for an older Python version, please
32 #   contact the authors. We're always open for feedback.
33 #
34 # LICENSE
35 #
36 #   Copyright (c) 2009 Sebastian Huber <sebastian-huber@web.de>
37 #   Copyright (c) 2009 Alan W. Irwin
38 #   Copyright (c) 2009 Rafael Laboissiere <rafael@laboissiere.net>
39 #   Copyright (c) 2009 Andrew Collier
40 #   Copyright (c) 2009 Matteo Settenvini <matteo@member.fsf.org>
41 #   Copyright (c) 2009 Horst Knorr <hk_classes@knoda.org>
42 #   Copyright (c) 2013 Daniel Mullner <muellner@math.stanford.edu>
43 #
44 #   This program is free software: you can redistribute it and/or modify it
45 #   under the terms of the GNU General Public License as published by the
46 #   Free Software Foundation, either version 3 of the License, or (at your
47 #   option) any later version.
48 #
49 #   This program is distributed in the hope that it will be useful, but
50 #   WITHOUT ANY WARRANTY; without even the implied warranty of
51 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
52 #   Public License for more details.
53 #
54 #   You should have received a copy of the GNU General Public License along
55 #   with this program. If not, see <https://www.gnu.org/licenses/>.
56 #
57 #   As a special exception, the respective Autoconf Macro's copyright owner
58 #   gives unlimited permission to copy, distribute and modify the configure
59 #   scripts that are the output of Autoconf when processing the Macro. You
60 #   need not follow the terms of the GNU General Public License when using
61 #   or distributing such scripts, even though portions of the text of the
62 #   Macro appear in them. The GNU General Public License (GPL) does govern
63 #   all other use of the material that constitutes the Autoconf Macro.
64 #
65 #   This special exception to the GPL applies to versions of the Autoconf
66 #   Macro released by the Autoconf Archive. When you make and distribute a
67 #   modified version of the Autoconf Macro, you may extend this special
68 #   exception to the GPL to apply to your modified version as well.
69
70 #serial 23
71
72 AU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
73 AC_DEFUN([AX_PYTHON_DEVEL],[
74         #
75         # Allow the use of a (user set) custom python version
76         #
77         AC_ARG_VAR([PYTHON_VERSION],[The installed Python
78                 version to use, for example '2.3'. This string
79                 will be appended to the Python interpreter
80                 canonical name.])
81
82         AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
83         if test -z "$PYTHON"; then
84            AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
85            PYTHON_VERSION=""
86         fi
87
88         #
89         # Check for a version of Python >= 2.1.0
90         #
91         AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
92         ac_supports_python_ver=`$PYTHON -c "import sys; \
93                 ver = sys.version.split ()[[0]]; \
94                 print (ver >= '2.1.0')"`
95         if test "$ac_supports_python_ver" != "True"; then
96                 if test -z "$PYTHON_NOVERSIONCHECK"; then
97                         AC_MSG_RESULT([no])
98                         AC_MSG_FAILURE([
99 This version of the AC@&t@_PYTHON_DEVEL macro
100 doesn't work properly with versions of Python before
101 2.1.0. You may need to re-run configure, setting the
102 variables PYTHON_CPPFLAGS, PYTHON_LIBS, PYTHON_SITE_PKG,
103 PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
104 Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
105 to something else than an empty string.
106 ])
107                 else
108                         AC_MSG_RESULT([skip at user request])
109                 fi
110         else
111                 AC_MSG_RESULT([yes])
112         fi
113
114         #
115         # if the macro parameter ``version'' is set, honour it
116         #
117         if test -n "$1"; then
118                 AC_MSG_CHECKING([for a version of Python $1])
119                 ac_supports_python_ver=`$PYTHON -c "import sys; \
120                         ver = sys.version.split ()[[0]]; \
121                         print (ver $1)"`
122                 if test "$ac_supports_python_ver" = "True"; then
123                    AC_MSG_RESULT([yes])
124                 else
125                         AC_MSG_RESULT([no])
126                         AC_MSG_ERROR([this package requires Python $1.
127 If you have it installed, but it isn't the default Python
128 interpreter in your system path, please pass the PYTHON_VERSION
129 variable to configure. See ``configure --help'' for reference.
130 ])
131                         PYTHON_VERSION=""
132                 fi
133         fi
134
135         #
136         # Check if you have distutils, else fail
137         #
138         AC_MSG_CHECKING([for the sysconfig Python package])
139         ac_sysconfig_result=`$PYTHON -c "import sysconfig" 2>&1`
140         if test $? -eq 0; then
141                 AC_MSG_RESULT([yes])
142                 IMPORT_SYSCONFIG="import sysconfig"
143         else
144                 AC_MSG_RESULT([no])
145
146                 AC_MSG_CHECKING([for the distutils Python package])
147                 ac_sysconfig_result=`$PYTHON -c "from distutils import sysconfig" 2>&1`
148                 if test $? -eq 0; then
149                         AC_MSG_RESULT([yes])
150                         IMPORT_SYSCONFIG="from distutils import sysconfig"
151                 else
152                         AC_MSG_ERROR([cannot import Python module "distutils".
153 Please check your Python installation. The error was:
154 $ac_sysconfig_result])
155                         PYTHON_VERSION=""
156                 fi
157         fi
158
159         #
160         # Check for Python include path
161         #
162         AC_MSG_CHECKING([for Python include path])
163         if test -z "$PYTHON_CPPFLAGS"; then
164                 if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
165                         # sysconfig module has different functions
166                         python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
167                                 print (sysconfig.get_path ('include'));"`
168                         plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
169                                 print (sysconfig.get_path ('platinclude'));"`
170                 else
171                         # old distutils way
172                         python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
173                                 print (sysconfig.get_python_inc ());"`
174                         plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
175                                 print (sysconfig.get_python_inc (plat_specific=1));"`
176                 fi
177                 if test -n "${python_path}"; then
178                         if test "${plat_python_path}" != "${python_path}"; then
179                                 python_path="-I$python_path -I$plat_python_path"
180                         else
181                                 python_path="-I$python_path"
182                         fi
183                 fi
184                 PYTHON_CPPFLAGS=$python_path
185         fi
186         AC_MSG_RESULT([$PYTHON_CPPFLAGS])
187         AC_SUBST([PYTHON_CPPFLAGS])
188
189         #
190         # Check for Python library path
191         #
192         AC_MSG_CHECKING([for Python library path])
193         if test -z "$PYTHON_LIBS"; then
194                 # (makes two attempts to ensure we've got a version number
195                 # from the interpreter)
196                 ac_python_version=`cat<<EOD | $PYTHON -
197
198 # join all versioning strings, on some systems
199 # major/minor numbers could be in different list elements
200 from sysconfig import *
201 e = get_config_var('VERSION')
202 if e is not None:
203         print(e)
204 EOD`
205
206                 if test -z "$ac_python_version"; then
207                         if test -n "$PYTHON_VERSION"; then
208                                 ac_python_version=$PYTHON_VERSION
209                         else
210                                 ac_python_version=`$PYTHON -c "import sys; \
211                                         print (sys.version[[:3]])"`
212                         fi
213                 fi
214
215                 # Make the versioning information available to the compiler
216                 AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
217                                    [If available, contains the Python version number currently in use.])
218
219                 # First, the library directory:
220                 ac_python_libdir=`cat<<EOD | $PYTHON -
221
222 # There should be only one
223 $IMPORT_SYSCONFIG
224 e = sysconfig.get_config_var('LIBDIR')
225 if e is not None:
226         print (e)
227 EOD`
228
229                 # Now, for the library:
230                 ac_python_library=`cat<<EOD | $PYTHON -
231
232 $IMPORT_SYSCONFIG
233 c = sysconfig.get_config_vars()
234 if 'LDVERSION' in c:
235         print ('python'+c[['LDVERSION']])
236 else:
237         print ('python'+c[['VERSION']])
238 EOD`
239
240                 # This small piece shamelessly adapted from PostgreSQL python macro;
241                 # credits goes to momjian, I think. I'd like to put the right name
242                 # in the credits, if someone can point me in the right direction... ?
243                 #
244                 if test -n "$ac_python_libdir" -a -n "$ac_python_library"
245                 then
246                         # use the official shared library
247                         ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
248                         PYTHON_LIBS="-L$ac_python_libdir -l$ac_python_library"
249                 else
250                         # old way: use libpython from python_configdir
251                         ac_python_libdir=`$PYTHON -c \
252                           "from sysconfig import get_python_lib as f; \
253                           import os; \
254                           print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
255                         PYTHON_LIBS="-L$ac_python_libdir -lpython$ac_python_version"
256                 fi
257
258                 if test -z "PYTHON_LIBS"; then
259                         AC_MSG_ERROR([
260   Cannot determine location of your Python DSO. Please check it was installed with
261   dynamic libraries enabled, or try setting PYTHON_LIBS by hand.
262                         ])
263                 fi
264         fi
265         AC_MSG_RESULT([$PYTHON_LIBS])
266         AC_SUBST([PYTHON_LIBS])
267
268         #
269         # Check for site packages
270         #
271         AC_MSG_CHECKING([for Python site-packages path])
272         if test -z "$PYTHON_SITE_PKG"; then
273                 if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
274                         PYTHON_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
275                                 print (sysconfig.get_path('purelib'));"`
276                 else
277                         # distutils.sysconfig way
278                         PYTHON_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
279                                 print (sysconfig.get_python_lib(0,0));"`
280                 fi
281         fi
282         AC_MSG_RESULT([$PYTHON_SITE_PKG])
283         AC_SUBST([PYTHON_SITE_PKG])
284
285         #
286         # Check for platform-specific site packages
287         #
288         AC_MSG_CHECKING([for Python platform specific site-packages path])
289         if test -z "$PYTHON_SITE_PKG"; then
290                 if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
291                         PYTHON_PLATFORM_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
292                                 print (sysconfig.get_path('platlib'));"`
293                 else
294                         # distutils.sysconfig way
295                         PYTHON_PLATFORM_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
296                                 print (sysconfig.get_python_lib(1,0));"`
297                 fi
298         fi
299         AC_MSG_RESULT([$PYTHON_PLATFORM_SITE_PKG])
300         AC_SUBST([PYTHON_PLATFORM_SITE_PKG])
301
302         #
303         # libraries which must be linked in when embedding
304         #
305         AC_MSG_CHECKING(python extra libraries)
306         if test -z "$PYTHON_EXTRA_LIBS"; then
307            PYTHON_EXTRA_LIBS=`$PYTHON -c "$IMPORT_SYSCONFIG; \
308                 conf = sysconfig.get_config_var; \
309                 print (conf('LIBS') + ' ' + conf('SYSLIBS'))"`
310         fi
311         AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
312         AC_SUBST(PYTHON_EXTRA_LIBS)
313
314         #
315         # linking flags needed when embedding
316         #
317         AC_MSG_CHECKING(python extra linking flags)
318         if test -z "$PYTHON_EXTRA_LDFLAGS"; then
319                 PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "$IMPORT_SYSCONFIG; \
320                         conf = sysconfig.get_config_var; \
321                         print (conf('LINKFORSHARED'))"`
322         fi
323         AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
324         AC_SUBST(PYTHON_EXTRA_LDFLAGS)
325
326         #
327         # final check to see if everything compiles alright
328         #
329         AC_MSG_CHECKING([consistency of all components of python development environment])
330         # save current global flags
331         ac_save_LIBS="$LIBS"
332         ac_save_LDFLAGS="$LDFLAGS"
333         ac_save_CPPFLAGS="$CPPFLAGS"
334         LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS $PYTHON_EXTRA_LIBS"
335         LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS"
336         CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
337         AC_LANG_PUSH([C])
338         AC_LINK_IFELSE([
339                 AC_LANG_PROGRAM([[#include <Python.h>]],
340                                 [[Py_Initialize();]])
341                 ],[pythonexists=yes],[pythonexists=no])
342         AC_LANG_POP([C])
343         # turn back to default flags
344         CPPFLAGS="$ac_save_CPPFLAGS"
345         LIBS="$ac_save_LIBS"
346         LDFLAGS="$ac_save_LDFLAGS"
347
348         AC_MSG_RESULT([$pythonexists])
349
350         if test ! "x$pythonexists" = "xyes"; then
351            AC_MSG_FAILURE([
352   Could not link test program to Python. Maybe the main Python library has been
353   installed in some non-standard library path. If so, pass it to configure,
354   via the LIBS environment variable.
355   Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
356   ============================================================================
357    ERROR!
358    You probably have to install the development version of the Python package
359    for your distribution.  The exact name of this package varies among them.
360   ============================================================================
361            ])
362           PYTHON_VERSION=""
363         fi
364
365         #
366         # all done!
367         #
368 ])