1 # ===========================================================================
2 # https://www.gnu.org/software/autoconf-archive/ax_python_devel.html
3 # ===========================================================================
7 # AX_PYTHON_DEVEL([version], [action-if-not-found])
11 # Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
12 # in your configure.ac.
14 # Note: this is a slightly modified version of the original AX_PYTHON_DEVEL
15 # macro which accepts an additional [action-if-not-found] argument. This
16 # allow to detect if Python development is available without aborting the
17 # configure phase with an hard error in case it is not.
19 # This macro checks for Python and tries to get the include path to
20 # 'Python.h'. It provides the $(PYTHON_CPPFLAGS) and $(PYTHON_LIBS) output
21 # variables. It also exports $(PYTHON_EXTRA_LIBS) and
22 # $(PYTHON_EXTRA_LDFLAGS) for embedding Python in your code.
24 # You can search for some particular version of Python by passing a
25 # parameter to this macro, for example ">= '2.3.1'", or "== '2.4'". Please
26 # note that you *have* to pass also an operator along with the version to
27 # match, and pay special attention to the single quotes surrounding the
28 # version number. Don't use "PYTHON_VERSION" for this: that environment
29 # variable is declared as precious and thus reserved for the end-user.
31 # This macro should work for all versions of Python >= 2.1.0. As an end
32 # user, you can disable the check for the python version by setting the
33 # PYTHON_NOVERSIONCHECK environment variable to something else than the
36 # If you need to use this macro for an older Python version, please
37 # contact the authors. We're always open for feedback.
41 # Copyright (c) 2009 Sebastian Huber <sebastian-huber@web.de>
42 # Copyright (c) 2009 Alan W. Irwin
43 # Copyright (c) 2009 Rafael Laboissiere <rafael@laboissiere.net>
44 # Copyright (c) 2009 Andrew Collier
45 # Copyright (c) 2009 Matteo Settenvini <matteo@member.fsf.org>
46 # Copyright (c) 2009 Horst Knorr <hk_classes@knoda.org>
47 # Copyright (c) 2013 Daniel Mullner <muellner@math.stanford.edu>
48 # Copyright (c) 2018 loli10K <ezomori.nozomu@gmail.com>
50 # This program is free software: you can redistribute it and/or modify it
51 # under the terms of the GNU General Public License as published by the
52 # Free Software Foundation, either version 3 of the License, or (at your
53 # option) any later version.
55 # This program is distributed in the hope that it will be useful, but
56 # WITHOUT ANY WARRANTY; without even the implied warranty of
57 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
58 # Public License for more details.
60 # You should have received a copy of the GNU General Public License along
61 # with this program. If not, see <https://www.gnu.org/licenses/>.
63 # As a special exception, the respective Autoconf Macro's copyright owner
64 # gives unlimited permission to copy, distribute and modify the configure
65 # scripts that are the output of Autoconf when processing the Macro. You
66 # need not follow the terms of the GNU General Public License when using
67 # or distributing such scripts, even though portions of the text of the
68 # Macro appear in them. The GNU General Public License (GPL) does govern
69 # all other use of the material that constitutes the Autoconf Macro.
71 # This special exception to the GPL applies to versions of the Autoconf
72 # Macro released by the Autoconf Archive. When you make and distribute a
73 # modified version of the Autoconf Macro, you may extend this special
74 # exception to the GPL to apply to your modified version as well.
78 AU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
79 AC_DEFUN([AX_PYTHON_DEVEL],[
81 # Allow the use of a (user set) custom python version
83 AC_ARG_VAR([PYTHON_VERSION],[The installed Python
84 version to use, for example '2.3'. This string
85 will be appended to the Python interpreter
88 AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
89 if test -z "$PYTHON"; then
91 AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
97 # Check for a version of Python >= 2.1.0
99 AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
100 ac_supports_python_ver=`$PYTHON -c "import sys; \
101 ver = sys.version.split ()[[0]]; \
102 print (ver >= '2.1.0')"`
103 if test "$ac_supports_python_ver" != "True"; then
104 if test -z "$PYTHON_NOVERSIONCHECK"; then
107 This version of the AC@&t@_PYTHON_DEVEL macro
108 doesn't work properly with versions of Python before
109 2.1.0. You may need to re-run configure, setting the
110 variables PYTHON_CPPFLAGS, PYTHON_LIBS, PYTHON_SITE_PKG,
111 PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
112 Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
113 to something else than an empty string.
116 AC_MSG_RESULT([skip at user request])
123 # If the macro parameter ``version'' is set, honour it.
124 # A Python shim class, VPy, is used to implement correct version comparisons via
125 # string expressions, since e.g. a naive textual ">= 2.7.3" won't work for
126 # Python 2.7.10 (the ".1" being evaluated as less than ".3").
128 if test -n "$1"; then
129 AC_MSG_CHECKING([for a version of Python $1])
130 cat << EOF > ax_python_devel_vpy.py
133 return tuple(map(int, s.strip().replace("rc", ".").split(".")))
136 self.vpy = tuple(sys.version_info)
138 return self.vpy == self.vtup(s)
140 return self.vpy != self.vtup(s)
142 return self.vpy < self.vtup(s)
144 return self.vpy > self.vtup(s)
146 return self.vpy <= self.vtup(s)
148 return self.vpy >= self.vtup(s)
150 ac_supports_python_ver=`$PYTHON -c "import ax_python_devel_vpy; \
151 ver = ax_python_devel_vpy.VPy(); \
153 rm -rf ax_python_devel_vpy*.py* __pycache__/ax_python_devel_vpy*.py*
154 if test "$ac_supports_python_ver" = "True"; then
158 AC_MSG_ERROR([this package requires Python $1.
159 If you have it installed, but it isn't the default Python
160 interpreter in your system path, please pass the PYTHON_VERSION
161 variable to configure. See ``configure --help'' for reference.
168 # Check for Python include path
171 AC_MSG_CHECKING([for Python include path])
172 if test -z "$PYTHON_CPPFLAGS"; then
173 python_path=`$PYTHON -c "import sysconfig; \
174 print (sysconfig.get_path('include'));"`
175 plat_python_path=`$PYTHON -c "import sysconfig; \
176 print (sysconfig.get_path('platinclude'));"`
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"
181 python_path="-I$python_path"
184 PYTHON_CPPFLAGS=$python_path
186 AC_MSG_RESULT([$PYTHON_CPPFLAGS])
187 AC_SUBST([PYTHON_CPPFLAGS])
190 # Check for Python library path
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 -
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')
206 if test -z "$ac_python_version"; then
207 if test -n "$PYTHON_VERSION"; then
208 ac_python_version=$PYTHON_VERSION
210 ac_python_version=`$PYTHON -c "import sys; \
211 print ('.'.join(sys.version.split('.')[[:2]]))"`
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.])
219 # First, the library directory:
220 ac_python_libdir=`cat<<EOD | $PYTHON -
222 # There should be only one
224 e = sysconfig.get_config_var('LIBDIR')
229 # Now, for the library:
230 ac_python_library=`cat<<EOD | $PYTHON -
233 c = sysconfig.get_config_vars()
235 print ('python'+c[['LDVERSION']])
237 print ('python'+c[['VERSION']])
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... ?
244 if test -n "$ac_python_libdir" -a -n "$ac_python_library"
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"
250 # old way: use libpython from python_configdir
251 ac_python_libdir=`$PYTHON -c \
254 print (os.path.join(sysconfig.get_path('platstdlib'), 'config'));"`
255 PYTHON_LIBS="-L$ac_python_libdir -lpython$ac_python_version"
258 if test -z "PYTHON_LIBS"; then
259 m4_ifvaln([$2],[$2],[
261 Cannot determine location of your Python DSO. Please check it was installed with
262 dynamic libraries enabled, or try setting PYTHON_LIBS by hand.
267 AC_MSG_RESULT([$PYTHON_LIBS])
268 AC_SUBST([PYTHON_LIBS])
271 # Check for site packages
273 AC_MSG_CHECKING([for Python site-packages path])
274 if test -z "$PYTHON_SITE_PKG"; then
275 PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
276 print (distutils.sysconfig.get_python_lib(0,0));" 2>/dev/null || \
277 $PYTHON -c "import sysconfig; \
278 print (sysconfig.get_path('purelib'));"`
280 AC_MSG_RESULT([$PYTHON_SITE_PKG])
281 AC_SUBST([PYTHON_SITE_PKG])
284 # libraries which must be linked in when embedding
286 AC_MSG_CHECKING(python extra libraries)
287 if test -z "$PYTHON_EXTRA_LIBS"; then
288 PYTHON_EXTRA_LIBS=`$PYTHON -c "import sysconfig; \
289 conf = sysconfig.get_config_var; \
290 print (conf('LIBS') + ' ' + conf('SYSLIBS'))"`
292 AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
293 AC_SUBST(PYTHON_EXTRA_LIBS)
296 # linking flags needed when embedding
298 AC_MSG_CHECKING(python extra linking flags)
299 if test -z "$PYTHON_EXTRA_LDFLAGS"; then
300 PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import sysconfig; \
301 conf = sysconfig.get_config_var; \
302 print (conf('LINKFORSHARED'))"`
304 AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
305 AC_SUBST(PYTHON_EXTRA_LDFLAGS)
308 # final check to see if everything compiles alright
310 AC_MSG_CHECKING([consistency of all components of python development environment])
311 # save current global flags
313 ac_save_LDFLAGS="$LDFLAGS"
314 ac_save_CPPFLAGS="$CPPFLAGS"
315 LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS $PYTHON_EXTRA_LIBS"
316 LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS"
317 CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
320 AC_LANG_PROGRAM([[#include <Python.h>]],
321 [[Py_Initialize();]])
322 ],[pythonexists=yes],[pythonexists=no])
324 # turn back to default flags
325 CPPFLAGS="$ac_save_CPPFLAGS"
327 LDFLAGS="$ac_save_LDFLAGS"
329 AC_MSG_RESULT([$pythonexists])
331 if test ! "x$pythonexists" = "xyes"; then
332 m4_ifvaln([$2],[$2],[
334 Could not link test program to Python. Maybe the main Python library has been
335 installed in some non-standard library path. If so, pass it to configure,
336 via the LIBS environment variable.
337 Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
338 ============================================================================
340 You probably have to install the development version of the Python package
341 for your distribution. The exact name of this package varies among them.
342 ============================================================================