Whamcloud - gitweb
LU-10973 lnet: infrastructure to build the LUTF
[fs/lustre-release.git] / lustre / autoconf / ac_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 21
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_PROGS([PYTHON],[python3 python])
83         #AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
84         if test -z "$PYTHON"; then
85            #AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
86            AC_MSG_WARN([Cannot find python in your system path])
87            PYTHON_VERSION_CHECK="no"
88            AC_SUBST([PYTHON_VERSION_CHECK])
89            PYTHON_VERSION=""
90         fi
91
92         PYTHON_VERSION_CHECK="yes"
93         #
94         # Check for a version of Python >= 2.1.0
95         #
96         AC_MSG_CHECKING([for a version of Python >= '3.6.0'])
97         ac_supports_python_ver=`$PYTHON -c "import sys; \
98                 ver = sys.version.split ()[[0]]; \
99                 print (ver >= '3.0')"`
100         if test "$ac_supports_python_ver" != "True"; then
101                 if test -z "$PYTHON_NOVERSIONCHECK"; then
102                         AC_MSG_RESULT([no])
103                         PYTHON_VERSION_CHECK="no"
104                         AC_SUBST([PYTHON_VERSION_CHECK])
105                         AC_MSG_WARN([
106 This version of the AC@&t@_PYTHON_DEVEL macro
107 doesn't work properly with versions of Python before
108 3.6.0. You may need to re-run configure, setting the
109 variables PYTHON_CPPFLAGS, PYTHON_LIBS, PYTHON_SITE_PKG,
110 PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
111 Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
112 to something else than an empty string.
113 ])
114                 else
115                         AC_MSG_RESULT([skip at user request])
116                 fi
117         else
118                 AC_MSG_RESULT([yes])
119         fi
120
121         #
122         # if the macro parameter ``version'' is set, honour it
123         #
124         if test -n "$1" ; then
125                 AC_MSG_CHECKING([for a version of Python $1])
126                 ac_supports_python_ver=`$PYTHON -c "import sys; \
127                         ver = sys.version.split ()[[0]]; \
128                         print (ver $1)"`
129                 if test "$ac_supports_python_ver" = "True"; then
130                    AC_MSG_RESULT([yes])
131                 else
132                         AC_MSG_RESULT([no])
133                         PYTHON_VERSION_CHECK="no"
134                         AC_SUBST([PYTHON_VERSION_CHECK])
135                         AC_MSG_WARN([this package requires Python $1.
136 If you have it installed, but it isn't the default Python
137 interpreter in your system path, please pass the PYTHON_VERSION
138 variable to configure. See ``configure --help'' for reference.
139 ])
140                         PYTHON_VERSION=""
141                 fi
142         fi
143
144         #
145         # Check if you have distutils, else fail
146         #
147         if test "yes" = "$PYTHON_VERSION_CHECK" ; then
148                 AC_MSG_CHECKING([for the distutils Python package])
149                 ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
150                 if test $? -eq 0; then
151                         AC_MSG_RESULT([yes])
152                 else
153                         AC_MSG_RESULT([no])
154                         PYTHON_VERSION_CHECK="no"
155                         AC_SUBST([PYTHON_VERSION_CHECK])
156                         AC_MSG_WARN([cannot import Python module "distutils".
157         Please check your Python installation. The error was:
158         $ac_distutils_result])
159                         PYTHON_VERSION=""
160                 fi
161
162                 #
163                 # Check for Python include path
164                 #
165                 AC_MSG_CHECKING([for Python include path])
166                 if test -z "$PYTHON_CPPFLAGS"; then
167                         python_path=`$PYTHON -c "import distutils.sysconfig; \
168                                 print (distutils.sysconfig.get_python_inc ());"`
169                         plat_python_path=`$PYTHON -c "import distutils.sysconfig; \
170                                 print (distutils.sysconfig.get_python_inc (plat_specific=1));"`
171                         if test -n "${python_path}"; then
172                                 if test "${plat_python_path}" != "${python_path}"; then
173                                         python_path="-I$python_path -I$plat_python_path"
174                                 else
175                                         python_path="-I$python_path"
176                                 fi
177                         fi
178                         PYTHON_CPPFLAGS=$python_path
179                 fi
180                 AC_MSG_RESULT([$PYTHON_CPPFLAGS])
181                 AC_SUBST([PYTHON_CPPFLAGS])
182
183                 #
184                 # Check for Python library path
185                 #
186                 AC_MSG_CHECKING([for Python library path])
187                 if test -z "$PYTHON_LIBS"; then
188                         # (makes two attempts to ensure we've got a version number
189                         # from the interpreter)
190                         ac_python_version=`cat<<EOD | $PYTHON -
191
192 # join all versioning strings, on some systems
193 # major/minor numbers could be in different list elements
194 from distutils.sysconfig import *
195 e = get_config_var('VERSION')
196 if e is not None:
197         print(e)
198 EOD`
199
200                         if test -z "$ac_python_version"; then
201                                 if test -n "$PYTHON_VERSION"; then
202                                         ac_python_version=$PYTHON_VERSION
203                                 else
204                                         ac_python_version=`$PYTHON -c "import sys; \
205                                                 print (sys.version[[:3]])"`
206                                 fi
207                         fi
208
209                         # Make the versioning information available to the compiler
210                         AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
211                                         [If available, contains the Python version number currently in use.])
212
213                         # First, the library directory:
214                         ac_python_libdir=`cat<<EOD | $PYTHON -
215
216 # There should be only one
217 import distutils.sysconfig
218 e = distutils.sysconfig.get_config_var('LIBDIR')
219 if e is not None:
220         print (e)
221 EOD`
222
223                         # Now, for the library:
224                         ac_python_library=`cat<<EOD | $PYTHON -
225
226 import distutils.sysconfig
227 c = distutils.sysconfig.get_config_vars()
228 if 'LDVERSION' in c:
229         print ('python'+c[['LDVERSION']])
230 else:
231         print ('python'+c[['VERSION']])
232 EOD`
233
234                         # This small piece shamelessly adapted from PostgreSQL python macro;
235                         # credits goes to momjian, I think. I'd like to put the right name
236                         # in the credits, if someone can point me in the right direction... ?
237                         #
238                         if test -n "$ac_python_libdir" -a -n "$ac_python_library"
239                         then
240                                 # use the official shared library
241                                 ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
242                                 PYTHON_LIBS="-L$ac_python_libdir -l$ac_python_library"
243                         else
244                                 # old way: use libpython from python_configdir
245                                 ac_python_libdir=`$PYTHON -c \
246                                 "from distutils.sysconfig import get_python_lib as f; \
247                                 import os; \
248                                 print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
249                                 PYTHON_LIBS="-L$ac_python_libdir -lpython$ac_python_version"
250                         fi
251
252                         if test -z "PYTHON_LIBS"; then
253                                 PYTHON_VERSION_CHECK="no"
254                                 AC_SUBST([PYTHON_VERSION_CHECK])
255                                 AC_MSG_WARN([
256         Cannot determine location of your Python DSO. Please check it was installed with
257         dynamic libraries enabled, or try setting PYTHON_LIBS by hand.
258                                 ])
259                         fi
260                 fi
261                 AC_MSG_RESULT([$PYTHON_LIBS])
262                 AC_SUBST([PYTHON_LIBS])
263
264                 #
265                 # Check for site packages
266                 #
267                 AC_MSG_CHECKING([for Python site-packages path])
268                 if test -z "$PYTHON_SITE_PKG"; then
269                         PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
270                                 print (distutils.sysconfig.get_python_lib(0,0));"`
271                 fi
272                 AC_MSG_RESULT([$PYTHON_SITE_PKG])
273                 AC_SUBST([PYTHON_SITE_PKG])
274
275                 #
276                 # libraries which must be linked in when embedding
277                 #
278                 AC_MSG_CHECKING(python extra libraries)
279                 if test -z "$PYTHON_EXTRA_LIBS"; then
280                 PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
281                         conf = distutils.sysconfig.get_config_var; \
282                         print (conf('LIBS') + ' ' + conf('SYSLIBS'))"`
283                 fi
284                 AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
285                 AC_SUBST(PYTHON_EXTRA_LIBS)
286
287                 #
288                 # linking flags needed when embedding
289                 #
290                 AC_MSG_CHECKING(python extra linking flags)
291                 if test -z "$PYTHON_EXTRA_LDFLAGS"; then
292                         PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
293                                 conf = distutils.sysconfig.get_config_var; \
294                                 print (conf('LINKFORSHARED'))"`
295                 fi
296                 AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
297                 AC_SUBST(PYTHON_EXTRA_LDFLAGS)
298
299                 #
300                 # final check to see if everything compiles alright
301                 #
302                 AC_MSG_CHECKING([consistency of all components of python development environment])
303                 # save current global flags
304                 ac_save_LIBS="$LIBS"
305                 ac_save_LDFLAGS="$LDFLAGS"
306                 ac_save_CPPFLAGS="$CPPFLAGS"
307                 LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS $PYTHON_EXTRA_LIBS"
308                 LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS"
309                 CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
310                 AC_LANG_PUSH([C])
311                 AC_LINK_IFELSE([
312                         AC_LANG_PROGRAM([[#include <Python.h>]],
313                                         [[Py_Initialize();]])
314                         ],[pythonexists=yes],[pythonexists=no])
315                 AC_LANG_POP([C])
316                 # turn back to default flags
317                 CPPFLAGS="$ac_save_CPPFLAGS"
318                 LIBS="$ac_save_LIBS"
319                 LDFLAGS="$ac_save_LDFLAGS"
320
321                 AC_MSG_RESULT([$pythonexists])
322
323                 if test ! "x$pythonexists" = "xyes"; then
324                 PYTHON_VERSION_CHECK="no"
325                 AC_SUBST([PYTHON_VERSION_CHECK])
326                 AC_MSG_WARN([
327         Could not link test program to Python. Maybe the main Python library has been
328         installed in some non-standard library path. If so, pass it to configure,
329         via the LIBS environment variable.
330         Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
331         ============================================================================
332         ERROR!
333         You probably have to install the development version of the Python package
334         for your distribution.  The exact name of this package varies among them.
335         ============================================================================
336                 ])
337                 PYTHON_VERSION=""
338                 fi
339         fi
340         #
341         # all done!
342         #
343 ])