Skip to content

Commit 254b309

Browse files
xdegayevstinner
authored andcommitted
bpo-21536: On Android, C extensions are linked to libpython (pythonGH-12989)
1 parent b021ba5 commit 254b309

File tree

9 files changed

+47
-10
lines changed

9 files changed

+47
-10
lines changed

Doc/distutils/apiref.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ the full reference.
279279

280280
.. versionchanged:: 3.8
281281

282-
On Unix, C extensions are no longer linked to libpython.
282+
On Unix, C extensions are no longer linked to libpython except on
283+
Android.
283284

284285

285286
.. class:: Distribution

Doc/whatsnew/3.8.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,12 @@ Changes in the Python API
883883
Changes in the C API
884884
--------------------
885885

886-
* On Unix, C extensions are no longer linked to libpython. When Python is
887-
embedded, ``libpython`` must not be loaded with ``RTLD_LOCAL``, but
888-
``RTLD_GLOBAL`` instead. Previously, using ``RTLD_LOCAL``, it was already not
889-
possible to load C extensions which were not linked to ``libpython``, like C
890-
extensions of the standard library built by the ``*shared*`` section of
891-
``Modules/Setup``.
886+
* On Unix, C extensions are no longer linked to libpython except on
887+
Android. When Python is embedded, ``libpython`` must not be loaded with
888+
``RTLD_LOCAL``, but ``RTLD_GLOBAL`` instead. Previously, using
889+
``RTLD_LOCAL``, it was already not possible to load C extensions which were
890+
not linked to ``libpython``, like C extensions of the standard library built
891+
by the ``*shared*`` section of ``Modules/Setup``.
892892

893893
* Use of ``#`` variants of formats in parsing or building value (e.g.
894894
:c:func:`PyArg_ParseTuple`, :c:func:`Py_BuildValue`, :c:func:`PyObject_CallFunction`,

Lib/distutils/command/build_ext.py

+15
Original file line numberDiff line numberDiff line change
@@ -714,5 +714,20 @@ def get_libraries(self, ext):
714714
# don't extend ext.libraries, it may be shared with other
715715
# extensions, it is a reference to the original list
716716
return ext.libraries + [pythonlib]
717+
# On Android only the main executable and LD_PRELOADs are considered
718+
# to be RTLD_GLOBAL, all the dependencies of the main executable
719+
# remain RTLD_LOCAL and so the shared libraries must be linked with
720+
# libpython when python is built with a shared python library (issue
721+
# bpo-21536).
722+
else:
723+
from distutils.sysconfig import get_config_var
724+
if get_config_var('Py_ENABLE_SHARED'):
725+
# Either a native build on an Android device or the
726+
# cross-compilation of Python.
727+
if (hasattr(sys, 'getandroidapilevel') or
728+
('_PYTHON_HOST_PLATFORM' in os.environ and
729+
get_config_var('ANDROID_API_LEVEL') != 0)):
730+
ldversion = get_config_var('LDVERSION')
731+
return ext.libraries + ['python' + ldversion]
717732

718733
return ext.libraries

Makefile.pre.in

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ AR= @AR@
4141
READELF= @READELF@
4242
SOABI= @SOABI@
4343
LDVERSION= @LDVERSION@
44+
LIBPYTHON= @LIBPYTHON@
4445
GITVERSION= @GITVERSION@
4546
GITTAG= @GITTAG@
4647
GITBRANCH= @GITBRANCH@

Misc/NEWS.d/next/Build/2019-04-25-01-51-52.bpo-21536.ACQkiC.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
On Unix, C extensions are no longer linked to libpython.
1+
On Unix, C extensions are no longer linked to libpython except on Android.
22

33
It is now possible for a statically linked Python to load a C extension built
44
using a shared library Python.

Misc/python-config.in

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ for opt in opt_flags:
4747
print(' '.join(flags))
4848

4949
elif opt in ('--libs', '--ldflags'):
50-
libs = getvar('LIBS').split() + getvar('SYSLIBS').split()
50+
libpython = getvar('LIBPYTHON')
51+
libs = [libpython] if libpython else []
52+
libs.extend(getvar('LIBS').split() + getvar('SYSLIBS').split())
53+
5154
# add the prefix/lib/pythonX.Y/config dir, but only if there is no
5255
# shared library in prefix/lib/.
5356
if opt == '--ldflags':

Misc/python-config.sh.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LIBM="@LIBM@"
4141
LIBC="@LIBC@"
4242
SYSLIBS="$LIBM $LIBC"
4343
ABIFLAGS="@ABIFLAGS@"
44-
LIBS="@LIBS@ $SYSLIBS"
44+
LIBS="@LIBPYTHON@ @LIBS@ $SYSLIBS"
4545
BASECFLAGS="@BASECFLAGS@"
4646
LDLIBRARY="@LDLIBRARY@"
4747
OPT="@OPT@"

configure

+9
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ SRCDIRS
631631
THREADHEADERS
632632
LIBPL
633633
PY_ENABLE_SHARED
634+
LIBPYTHON
634635
EXT_SUFFIX
635636
ALT_SOABI
636637
SOABI
@@ -15154,6 +15155,14 @@ LDVERSION='$(VERSION)$(ABIFLAGS)'
1515415155
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $LDVERSION" >&5
1515515156
$as_echo "$LDVERSION" >&6; }
1515615157

15158+
# On Android the shared libraries must be linked with libpython.
15159+
15160+
if test -z "$ANDROID_API_LEVEL"; then
15161+
LIBPYTHON=''
15162+
else
15163+
LIBPYTHON="-lpython${VERSION}${ABIFLAGS}"
15164+
fi
15165+
1515715166

1515815167
if test x$PLATFORM_TRIPLET = x; then
1515915168
LIBPL='$(prefix)'"/lib/python${VERSION}/config-${LDVERSION}"

configure.ac

+8
Original file line numberDiff line numberDiff line change
@@ -4648,6 +4648,14 @@ AC_MSG_CHECKING(LDVERSION)
46484648
LDVERSION='$(VERSION)$(ABIFLAGS)'
46494649
AC_MSG_RESULT($LDVERSION)
46504650

4651+
# On Android the shared libraries must be linked with libpython.
4652+
AC_SUBST(LIBPYTHON)
4653+
if test -z "$ANDROID_API_LEVEL"; then
4654+
LIBPYTHON=''
4655+
else
4656+
LIBPYTHON="-lpython${VERSION}${ABIFLAGS}"
4657+
fi
4658+
46514659
dnl define LIBPL after ABIFLAGS and LDVERSION is defined.
46524660
AC_SUBST(PY_ENABLE_SHARED)
46534661
if test x$PLATFORM_TRIPLET = x; then

0 commit comments

Comments
 (0)