1 # ===========================================================================
2 # https://www.gnu.org/software/autoconf-archive/ax_python_devel.html
3 # ===========================================================================
7 # AX_PYTHON_DEVEL([version[,optional]])
11 # Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
12 # in your configure.ac.
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.
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.
26 # By default this will fail if it does not detect a development version of
27 # python. If you want it to continue, set optional to true, like
28 # AX_PYTHON_DEVEL([], [true]). The ax_python_devel_found variable will be
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>
49 # This program is free software: you can redistribute it and/or modify it
50 # under the terms of the GNU General Public License as published by the
51 # Free Software Foundation, either version 3 of the License, or (at your
52 # option) any later version.
54 # This program is distributed in the hope that it will be useful, but
55 # WITHOUT ANY WARRANTY; without even the implied warranty of
56 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
57 # Public License for more details.
59 # You should have received a copy of the GNU General Public License along
60 # with this program. If not, see <https://www.gnu.org/licenses/>.
62 # As a special exception, the respective Autoconf Macro's copyright owner
63 # gives unlimited permission to copy, distribute and modify the configure
64 # scripts that are the output of Autoconf when processing the Macro. You
65 # need not follow the terms of the GNU General Public License when using
66 # or distributing such scripts, even though portions of the text of the
67 # Macro appear in them. The GNU General Public License (GPL) does govern
68 # all other use of the material that constitutes the Autoconf Macro.
70 # This special exception to the GPL applies to versions of the Autoconf
71 # Macro released by the Autoconf Archive. When you make and distribute a
72 # modified version of the Autoconf Macro, you may extend this special
73 # exception to the GPL to apply to your modified version as well.
77 AU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL])
78 AC_DEFUN([AX_PYTHON_DEVEL],[
79 # Get whether it's optional
81 ax_python_devel_optional=false
83 ax_python_devel_optional=$2
85 ax_python_devel_found=yes
88 # Allow the use of a (user set) custom python version
90 AC_ARG_VAR([PYTHON_VERSION],[The installed Python
91 version to use, for example '2.3'. This string
92 will be appended to the Python interpreter
95 AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
96 if test -z "$PYTHON"; then
97 AC_MSG_WARN([Cannot find python$PYTHON_VERSION in your system path])
98 if ! $ax_python_devel_optional; then
99 AC_MSG_ERROR([Giving up, python development not available])
101 ax_python_devel_found=no
105 if test $ax_python_devel_found = yes; then
107 # Check for a version of Python >= 2.1.0
109 AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
110 ac_supports_python_ver=`$PYTHON -c "import sys; \
111 ver = sys.version.split ()[[0]]; \
112 print (ver >= '2.1.0')"`
113 if test "$ac_supports_python_ver" != "True"; then
114 if test -z "$PYTHON_NOVERSIONCHECK"; then
117 This version of the AC@&t@_PYTHON_DEVEL macro
118 doesn't work properly with versions of Python before
119 2.1.0. You may need to re-run configure, setting the
120 variables PYTHON_CPPFLAGS, PYTHON_LIBS, PYTHON_SITE_PKG,
121 PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
122 Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
123 to something else than an empty string.
125 if ! $ax_python_devel_optional; then
126 AC_MSG_FAILURE([Giving up])
128 ax_python_devel_found=no
131 AC_MSG_RESULT([skip at user request])
138 if test $ax_python_devel_found = yes; then
140 # If the macro parameter ``version'' is set, honour it.
141 # A Python shim class, VPy, is used to implement correct version comparisons via
142 # string expressions, since e.g. a naive textual ">= 2.7.3" won't work for
143 # Python 2.7.10 (the ".1" being evaluated as less than ".3").
145 if test -n "$1"; then
146 AC_MSG_CHECKING([for a version of Python $1])
147 cat << EOF > ax_python_devel_vpy.py
150 return tuple(map(int, s.strip().replace("rc", ".").split(".")))
153 self.vpy = tuple(sys.version_info)[[:3]]
155 return self.vpy == self.vtup(s)
157 return self.vpy != self.vtup(s)
159 return self.vpy < self.vtup(s)
161 return self.vpy > self.vtup(s)
163 return self.vpy <= self.vtup(s)
165 return self.vpy >= self.vtup(s)
167 ac_supports_python_ver=`$PYTHON -c "import ax_python_devel_vpy; \
168 ver = ax_python_devel_vpy.VPy(); \
170 rm -rf ax_python_devel_vpy*.py* __pycache__/ax_python_devel_vpy*.py*
171 if test "$ac_supports_python_ver" = "True"; then
175 AC_MSG_WARN([this package requires Python $1.
176 If you have it installed, but it isn't the default Python
177 interpreter in your system path, please pass the PYTHON_VERSION
178 variable to configure. See ``configure --help'' for reference.
180 if ! $ax_python_devel_optional; then
181 AC_MSG_ERROR([Giving up])
183 ax_python_devel_found=no
189 if test $ax_python_devel_found = yes; then
191 # Check if you have distutils, else fail
193 AC_MSG_CHECKING([for the sysconfig Python package])
194 ac_sysconfig_result=`$PYTHON -c "import sysconfig" 2>&1`
195 if test $? -eq 0; then
197 IMPORT_SYSCONFIG="import sysconfig"
201 AC_MSG_CHECKING([for the distutils Python package])
202 ac_sysconfig_result=`$PYTHON -c "from distutils import sysconfig" 2>&1`
203 if test $? -eq 0; then
205 IMPORT_SYSCONFIG="from distutils import sysconfig"
207 AC_MSG_WARN([cannot import Python module "distutils".
208 Please check your Python installation. The error was:
209 $ac_sysconfig_result])
210 if ! $ax_python_devel_optional; then
211 AC_MSG_ERROR([Giving up])
213 ax_python_devel_found=no
219 if test $ax_python_devel_found = yes; then
221 # Check for Python include path
223 AC_MSG_CHECKING([for Python include path])
224 if test -z "$PYTHON_CPPFLAGS"; then
225 if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
226 # sysconfig module has different functions
227 python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
228 print (sysconfig.get_path ('include'));"`
229 plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
230 print (sysconfig.get_path ('platinclude'));"`
233 python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
234 print (sysconfig.get_python_inc ());"`
235 plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \
236 print (sysconfig.get_python_inc (plat_specific=1));"`
238 if test -n "${python_path}"; then
239 if test "${plat_python_path}" != "${python_path}"; then
240 python_path="-I$python_path -I$plat_python_path"
242 python_path="-I$python_path"
245 PYTHON_CPPFLAGS=$python_path
247 AC_MSG_RESULT([$PYTHON_CPPFLAGS])
248 AC_SUBST([PYTHON_CPPFLAGS])
251 # Check for Python library path
253 AC_MSG_CHECKING([for Python library path])
254 if test -z "$PYTHON_LIBS"; then
255 # (makes two attempts to ensure we've got a version number
256 # from the interpreter)
257 ac_python_version=`cat<<EOD | $PYTHON -
259 # join all versioning strings, on some systems
260 # major/minor numbers could be in different list elements
261 from sysconfig import *
262 e = get_config_var('VERSION')
267 if test -z "$ac_python_version"; then
268 if test -n "$PYTHON_VERSION"; then
269 ac_python_version=$PYTHON_VERSION
271 ac_python_version=`$PYTHON -c "import sys; \
272 print ("%d.%d" % sys.version_info[[:2]])"`
276 # Make the versioning information available to the compiler
277 AC_DEFINE_UNQUOTED([HAVE_PYTHON], ["$ac_python_version"],
278 [If available, contains the Python version number currently in use.])
280 # First, the library directory:
281 ac_python_libdir=`cat<<EOD | $PYTHON -
283 # There should be only one
285 e = sysconfig.get_config_var('LIBDIR')
290 # Now, for the library:
291 ac_python_library=`cat<<EOD | $PYTHON -
294 c = sysconfig.get_config_vars()
296 print ('python'+c[['LDVERSION']])
298 print ('python'+c[['VERSION']])
301 # This small piece shamelessly adapted from PostgreSQL python macro;
302 # credits goes to momjian, I think. I'd like to put the right name
303 # in the credits, if someone can point me in the right direction... ?
305 if test -n "$ac_python_libdir" -a -n "$ac_python_library"
307 # use the official shared library
308 ac_python_library=`echo "$ac_python_library" | sed "s/^lib//"`
309 PYTHON_LIBS="-L$ac_python_libdir -l$ac_python_library"
311 # old way: use libpython from python_configdir
312 ac_python_libdir=`$PYTHON -c \
313 "from sysconfig import get_python_lib as f; \
315 print (os.path.join(f(plat_specific=1, standard_lib=1), 'config'));"`
316 PYTHON_LIBS="-L$ac_python_libdir -lpython$ac_python_version"
319 if test -z "PYTHON_LIBS"; then
321 Cannot determine location of your Python DSO. Please check it was installed with
322 dynamic libraries enabled, or try setting PYTHON_LIBS by hand.
324 if ! $ax_python_devel_optional; then
325 AC_MSG_ERROR([Giving up])
327 ax_python_devel_found=no
333 if test $ax_python_devel_found = yes; then
334 AC_MSG_RESULT([$PYTHON_LIBS])
335 AC_SUBST([PYTHON_LIBS])
338 # Check for site packages
340 AC_MSG_CHECKING([for Python site-packages path])
341 if test -z "$PYTHON_SITE_PKG"; then
342 if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
343 PYTHON_SITE_PKG=`$PYTHON -c "
345 if hasattr(sysconfig, 'get_default_scheme'):
346 scheme = sysconfig.get_default_scheme()
348 scheme = sysconfig._get_default_scheme()
349 if scheme == 'posix_local':
350 # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
351 scheme = 'posix_prefix'
354 prefix = '$ac_default_prefix'
355 sitedir = sysconfig.get_path('purelib', scheme, vars={'base': prefix})
358 # distutils.sysconfig way
359 PYTHON_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
360 print (sysconfig.get_python_lib(0,0));"`
363 AC_MSG_RESULT([$PYTHON_SITE_PKG])
364 AC_SUBST([PYTHON_SITE_PKG])
367 # Check for platform-specific site packages
369 AC_MSG_CHECKING([for Python platform specific site-packages path])
370 if test -z "$PYTHON_PLATFORM_SITE_PKG"; then
371 if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then
372 PYTHON_PLATFORM_SITE_PKG=`$PYTHON -c "
374 if hasattr(sysconfig, 'get_default_scheme'):
375 scheme = sysconfig.get_default_scheme()
377 scheme = sysconfig._get_default_scheme()
378 if scheme == 'posix_local':
379 # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
380 scheme = 'posix_prefix'
383 prefix = '$ac_default_prefix'
384 sitedir = sysconfig.get_path('platlib', scheme, vars={'platbase': prefix})
387 # distutils.sysconfig way
388 PYTHON_PLATFORM_SITE_PKG=`$PYTHON -c "$IMPORT_SYSCONFIG; \
389 print (sysconfig.get_python_lib(1,0));"`
392 AC_MSG_RESULT([$PYTHON_PLATFORM_SITE_PKG])
393 AC_SUBST([PYTHON_PLATFORM_SITE_PKG])
396 # libraries which must be linked in when embedding
398 AC_MSG_CHECKING(python extra libraries)
399 if test -z "$PYTHON_EXTRA_LIBS"; then
400 PYTHON_EXTRA_LIBS=`$PYTHON -c "$IMPORT_SYSCONFIG; \
401 conf = sysconfig.get_config_var; \
402 print (conf('LIBS') + ' ' + conf('SYSLIBS'))"`
404 AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
405 AC_SUBST(PYTHON_EXTRA_LIBS)
408 # linking flags needed when embedding
410 AC_MSG_CHECKING(python extra linking flags)
411 if test -z "$PYTHON_EXTRA_LDFLAGS"; then
412 PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "$IMPORT_SYSCONFIG; \
413 conf = sysconfig.get_config_var; \
414 print (conf('LINKFORSHARED'))"`
415 # Hack for macos, it sticks this in here.
416 PYTHON_EXTRA_LDFLAGS=`echo $PYTHON_EXTRA_LDFLAGS | sed 's/CoreFoundation.*$/CoreFoundation/'`
418 AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
419 AC_SUBST(PYTHON_EXTRA_LDFLAGS)
422 # final check to see if everything compiles alright
424 AC_MSG_CHECKING([consistency of all components of python development environment])
425 # save current global flags
427 ac_save_LDFLAGS="$LDFLAGS"
428 ac_save_CPPFLAGS="$CPPFLAGS"
429 LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS"
430 LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS"
431 CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
434 AC_LANG_PROGRAM([[#include <Python.h>]],
435 [[Py_Initialize();]])
436 ],[pythonexists=yes],[pythonexists=no])
438 # turn back to default flags
439 CPPFLAGS="$ac_save_CPPFLAGS"
441 LDFLAGS="$ac_save_LDFLAGS"
443 AC_MSG_RESULT([$pythonexists])
445 if test ! "x$pythonexists" = "xyes"; then
447 Could not link test program to Python. Maybe the main Python library has been
448 installed in some non-standard library path. If so, pass it to configure,
449 via the LIBS environment variable.
450 Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
451 ============================================================================
453 You probably have to install the development version of the Python package
454 for your distribution. The exact name of this package varies among them.
455 ============================================================================
457 if ! $ax_python_devel_optional; then
458 AC_MSG_ERROR([Giving up])
460 ax_python_devel_found=no