2 # Autoconf macros for configuring the build of Python extension modules
9 # Look for Python and set the output variable 'PYTHON' if found,
12 # As the Python 3 transition happens and PEP 394 isn't updated, we
13 # need to cater to systems that don't have unversioned "python" by
14 # default. Some systems ship with "python3" by default and perhaps
15 # have "python" in an optional package. Some systems only have
16 # "python2" and "python3", in which case it's reasonable to prefer the
18 AC_DEFUN([PGAC_PATH_PYTHON],
19 [PGAC_PATH_PROGS(PYTHON, [python python3 python2])
20 AC_ARG_VAR(PYTHON, [Python program])dnl
21 if test x"$PYTHON" = x""; then
22 AC_MSG_ERROR([Python not found])
27 # _PGAC_CHECK_PYTHON_DIRS
28 # -----------------------
29 # Determine the name of various directories of a given Python installation,
30 # as well as the Python version.
31 AC_DEFUN([_PGAC_CHECK_PYTHON_DIRS],
32 [AC_REQUIRE([PGAC_PATH_PYTHON])
33 python_fullversion=`${PYTHON} -c "import sys; print(sys.version)" | sed q`
34 AC_MSG_NOTICE([using python $python_fullversion])
35 # python_fullversion is typically n.n.n plus some trailing junk
36 python_majorversion=`echo "$python_fullversion" | sed '[s/^\([0-9]*\).*/\1/]'`
37 python_minorversion=`echo "$python_fullversion" | sed '[s/^[0-9]*\.\([0-9]*\).*/\1/]'`
38 python_version=`echo "$python_fullversion" | sed '[s/^\([0-9]*\.[0-9]*\).*/\1/]'`
39 # Reject unsupported Python versions as soon as practical.
40 if test "$python_majorversion" -lt 3 -a "$python_minorversion" -lt 4; then
41 AC_MSG_ERROR([Python version $python_version is too old (version 2.4 or later is required)])
44 AC_MSG_CHECKING([for Python distutils module])
45 if "${PYTHON}" -c 'import distutils' 2>&AS_MESSAGE_LOG_FD
50 AC_MSG_ERROR([distutils module not found])
53 AC_MSG_CHECKING([Python configuration directory])
54 python_configdir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBPL'))))"`
55 AC_MSG_RESULT([$python_configdir])
57 AC_MSG_CHECKING([Python include directories])
58 python_includespec=`${PYTHON} -c "
59 import distutils.sysconfig
60 a = '-I' + distutils.sysconfig.get_python_inc(False)
61 b = '-I' + distutils.sysconfig.get_python_inc(True)
66 if test "$PORTNAME" = win32 ; then
67 python_includespec=`echo $python_includespec | sed 's,[[\]],/,g'`
69 AC_MSG_RESULT([$python_includespec])
71 AC_SUBST(python_majorversion)[]dnl
72 AC_SUBST(python_version)[]dnl
73 AC_SUBST(python_includespec)[]dnl
74 ])# _PGAC_CHECK_PYTHON_DIRS
77 # PGAC_CHECK_PYTHON_EMBED_SETUP
78 # -----------------------------
80 # Set python_libdir to the path of the directory containing the Python shared
81 # library. Set python_libspec to the -L/-l linker switches needed to link it.
82 # Set python_additional_libs to contain any additional linker switches needed
83 # for subsidiary libraries.
85 # In modern, well-configured Python installations, LIBDIR gives the correct
86 # directory name and LDLIBRARY is the file name of the shlib. But in older
87 # installations LDLIBRARY is frequently a useless path fragment, and it's also
88 # possible that the shlib is in a standard library directory such as /usr/lib
89 # so that LIBDIR is irrelevant. Also, some packagers put the .so symlink for
90 # the shlib in ${python_configdir} even though Python itself never does.
91 # We must also check that what we found is a shared library not a plain
92 # library, which we do by checking its extension. (We used to rely on
93 # Py_ENABLE_SHARED, but that only tells us that a shlib exists, not that
95 AC_DEFUN([PGAC_CHECK_PYTHON_EMBED_SETUP],
96 [AC_REQUIRE([_PGAC_CHECK_PYTHON_DIRS])
97 AC_MSG_CHECKING([how to link an embedded Python application])
99 python_libdir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBDIR'))))"`
100 python_ldlibrary=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LDLIBRARY'))))"`
102 # If LDLIBRARY exists and has a shlib extension, use it verbatim.
103 ldlibrary=`echo "${python_ldlibrary}" | sed -e 's/\.so$//' -e 's/\.dll$//' -e 's/\.dylib$//' -e 's/\.sl$//'`
104 if test -e "${python_libdir}/${python_ldlibrary}" -a x"${python_ldlibrary}" != x"${ldlibrary}"
106 ldlibrary=`echo "${ldlibrary}" | sed "s/^lib//"`
109 # Otherwise, guess the base name of the shlib.
110 # LDVERSION was added in Python 3.2, before that use VERSION,
111 # or failing that, $python_version from _PGAC_CHECK_PYTHON_DIRS.
112 python_ldversion=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LDVERSION'))))"`
113 if test x"${python_ldversion}" != x""; then
114 ldlibrary="python${python_ldversion}"
116 python_version_var=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('VERSION'))))"`
117 if test x"${python_version_var}" != x""; then
118 ldlibrary="python${python_version_var}"
120 ldlibrary="python${python_version}"
123 # Search for a likely-looking file.
125 for d in "${python_libdir}" "${python_configdir}" /usr/lib64 /usr/lib
127 # We don't know the platform DLSUFFIX here, so check 'em all.
128 for e in .so .dll .dylib .sl; do
129 if test -e "$d/lib${ldlibrary}$e"; then
136 # Some platforms (OpenBSD) require us to accept a bare versioned shlib
137 # (".so.n.n") as well. However, check this only after failing to find
138 # ".so" anywhere, because yet other platforms (Debian) put the .so
139 # symlink in a different directory from the underlying versioned shlib.
140 if test "$found_shlib" != 1; then
141 for d in "${python_libdir}" "${python_configdir}" /usr/lib64 /usr/lib
143 for f in "$d/lib${ldlibrary}.so."* ; do
144 if test -e "$f"; then
152 # As usual, Windows has its own ideas. Possible default library
153 # locations include c:/Windows/System32 and (for Cygwin) /usr/bin,
154 # and the "lib" prefix might not be there.
155 if test "$found_shlib" != 1 -a \( "$PORTNAME" = win32 -o "$PORTNAME" = cygwin \); then
156 for d in "${python_libdir}" "${python_configdir}" c:/Windows/System32 /usr/bin
158 for f in "$d/lib${ldlibrary}.dll" "$d/${ldlibrary}.dll" ; do
159 if test -e "$f"; then
168 if test "$found_shlib" != 1; then
169 AC_MSG_ERROR([could not find shared library for Python
170 You might have to rebuild your Python installation. Refer to the
171 documentation for details. Use --without-python to disable building
174 python_libspec="-L${python_libdir} -l${ldlibrary}"
176 python_additional_libs=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBS','LIBC','LIBM','BASEMODLIBS'))))"`
178 AC_MSG_RESULT([${python_libspec} ${python_additional_libs}])
180 AC_SUBST(python_libdir)[]dnl
181 AC_SUBST(python_libspec)[]dnl
182 AC_SUBST(python_additional_libs)[]dnl
184 ])# PGAC_CHECK_PYTHON_EMBED_SETUP