.. versionchanged:: 3.8
- On Unix, C extensions are no longer linked to libpython.
+ On Unix, C extensions are no longer linked to libpython except on
+ Android.
.. class:: Distribution
Changes in the C API
--------------------
-* On Unix, C extensions are no longer linked to libpython. When Python is
- embedded, ``libpython`` must not be loaded with ``RTLD_LOCAL``, but
- ``RTLD_GLOBAL`` instead. Previously, using ``RTLD_LOCAL``, it was already not
- possible to load C extensions which were not linked to ``libpython``, like C
- extensions of the standard library built by the ``*shared*`` section of
- ``Modules/Setup``.
+* On Unix, C extensions are no longer linked to libpython except on
+ Android. When Python is embedded, ``libpython`` must not be loaded with
+ ``RTLD_LOCAL``, but ``RTLD_GLOBAL`` instead. Previously, using
+ ``RTLD_LOCAL``, it was already not possible to load C extensions which were
+ not linked to ``libpython``, like C extensions of the standard library built
+ by the ``*shared*`` section of ``Modules/Setup``.
* Use of ``#`` variants of formats in parsing or building value (e.g.
:c:func:`PyArg_ParseTuple`, :c:func:`Py_BuildValue`, :c:func:`PyObject_CallFunction`,
# don't extend ext.libraries, it may be shared with other
# extensions, it is a reference to the original list
return ext.libraries + [pythonlib]
+ # On Android only the main executable and LD_PRELOADs are considered
+ # to be RTLD_GLOBAL, all the dependencies of the main executable
+ # remain RTLD_LOCAL and so the shared libraries must be linked with
+ # libpython when python is built with a shared python library (issue
+ # bpo-21536).
+ else:
+ from distutils.sysconfig import get_config_var
+ if get_config_var('Py_ENABLE_SHARED'):
+ # Either a native build on an Android device or the
+ # cross-compilation of Python.
+ if (hasattr(sys, 'getandroidapilevel') or
+ ('_PYTHON_HOST_PLATFORM' in os.environ and
+ get_config_var('ANDROID_API_LEVEL') != 0)):
+ ldversion = get_config_var('LDVERSION')
+ return ext.libraries + ['python' + ldversion]
return ext.libraries
READELF= @READELF@
SOABI= @SOABI@
LDVERSION= @LDVERSION@
+LIBPYTHON= @LIBPYTHON@
GITVERSION= @GITVERSION@
GITTAG= @GITTAG@
GITBRANCH= @GITBRANCH@
-On Unix, C extensions are no longer linked to libpython.
+On Unix, C extensions are no longer linked to libpython except on Android.
It is now possible for a statically linked Python to load a C extension built
using a shared library Python.
print(' '.join(flags))
elif opt in ('--libs', '--ldflags'):
- libs = getvar('LIBS').split() + getvar('SYSLIBS').split()
+ libpython = getvar('LIBPYTHON')
+ libs = [libpython] if libpython else []
+ libs.extend(getvar('LIBS').split() + getvar('SYSLIBS').split())
+
# add the prefix/lib/pythonX.Y/config dir, but only if there is no
# shared library in prefix/lib/.
if opt == '--ldflags':
LIBC="@LIBC@"
SYSLIBS="$LIBM $LIBC"
ABIFLAGS="@ABIFLAGS@"
-LIBS="@LIBS@ $SYSLIBS"
+LIBS="@LIBPYTHON@ @LIBS@ $SYSLIBS"
BASECFLAGS="@BASECFLAGS@"
LDLIBRARY="@LDLIBRARY@"
OPT="@OPT@"
THREADHEADERS
LIBPL
PY_ENABLE_SHARED
+LIBPYTHON
EXT_SUFFIX
ALT_SOABI
SOABI
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $LDVERSION" >&5
$as_echo "$LDVERSION" >&6; }
+# On Android the shared libraries must be linked with libpython.
+
+if test -z "$ANDROID_API_LEVEL"; then
+ LIBPYTHON=''
+else
+ LIBPYTHON="-lpython${VERSION}${ABIFLAGS}"
+fi
+
if test x$PLATFORM_TRIPLET = x; then
LIBPL='$(prefix)'"/lib/python${VERSION}/config-${LDVERSION}"
LDVERSION='$(VERSION)$(ABIFLAGS)'
AC_MSG_RESULT($LDVERSION)
+# On Android the shared libraries must be linked with libpython.
+AC_SUBST(LIBPYTHON)
+if test -z "$ANDROID_API_LEVEL"; then
+ LIBPYTHON=''
+else
+ LIBPYTHON="-lpython${VERSION}${ABIFLAGS}"
+fi
+
dnl define LIBPL after ABIFLAGS and LDVERSION is defined.
AC_SUBST(PY_ENABLE_SHARED)
if test x$PLATFORM_TRIPLET = x; then