]> granicus.if.org Git - python/commitdiff
bpo-21536: On Android, C extensions are linked to libpython (GH-12989)
authorxdegaye <xdegaye@gmail.com>
Mon, 29 Apr 2019 07:27:40 +0000 (09:27 +0200)
committerVictor Stinner <vstinner@redhat.com>
Mon, 29 Apr 2019 07:27:40 +0000 (09:27 +0200)
Doc/distutils/apiref.rst
Doc/whatsnew/3.8.rst
Lib/distutils/command/build_ext.py
Makefile.pre.in
Misc/NEWS.d/next/Build/2019-04-25-01-51-52.bpo-21536.ACQkiC.rst
Misc/python-config.in
Misc/python-config.sh.in
configure
configure.ac

index c3cdfc8a0a8ec67430a34a53a052832371e167d0..1facc0408d5b0f6a4732e508404bde5f9648296b 100644 (file)
@@ -279,7 +279,8 @@ the full reference.
 
    .. 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
index 8d94a9ff5441868dde4a6c136cf3a4dcf7b481ca..8df7538f1c435e6612c31e4e28e628438031fa2c 100644 (file)
@@ -883,12 +883,12 @@ Changes in the Python API
 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`,
index 1672d02acf1f7a17496d45ab4f80c09844d6956c..c3b9602461f93ac049b199df835e1fc33f622a14 100644 (file)
@@ -714,5 +714,20 @@ class build_ext(Command):
                 # 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
index 89479ee7f1a36c2ec022c8e1919e0da50869d8b6..7f0d8d44b3d1727be801bf98a2d0df1486cf0c38 100644 (file)
@@ -41,6 +41,7 @@ AR=           @AR@
 READELF=       @READELF@
 SOABI=         @SOABI@
 LDVERSION=     @LDVERSION@
+LIBPYTHON=     @LIBPYTHON@
 GITVERSION=    @GITVERSION@
 GITTAG=                @GITTAG@
 GITBRANCH=     @GITBRANCH@
index 8cfad76ab172f945673216812ab6ddd453abde76..59efab83265f4bf8c1c8710a5c383f0558d25c80 100644 (file)
@@ -1,4 +1,4 @@
-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.
index 31ad55822e55a26e7381486f169c44e88fd86e93..1df30d261d8ebb0c6b8257a5fc92712a10b09be9 100644 (file)
@@ -47,7 +47,10 @@ for opt in opt_flags:
         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':
index ac1a467678e44ba4a7b76cd3fdc4a538869a2bc3..33991ef0c5d49a098e2c55588756179fe6edebba 100644 (file)
@@ -41,7 +41,7 @@ LIBM="@LIBM@"
 LIBC="@LIBC@"
 SYSLIBS="$LIBM $LIBC"
 ABIFLAGS="@ABIFLAGS@"
-LIBS="@LIBS@ $SYSLIBS"
+LIBS="@LIBPYTHON@ @LIBS@ $SYSLIBS"
 BASECFLAGS="@BASECFLAGS@"
 LDLIBRARY="@LDLIBRARY@"
 OPT="@OPT@"
index 7f26fab81fe5dd1bb0071bfd96d2d6fb46f881d0..8d9c73d1c386bdd98236a39e211bceb160bb2aaa 100755 (executable)
--- a/configure
+++ b/configure
@@ -631,6 +631,7 @@ SRCDIRS
 THREADHEADERS
 LIBPL
 PY_ENABLE_SHARED
+LIBPYTHON
 EXT_SUFFIX
 ALT_SOABI
 SOABI
@@ -15154,6 +15155,14 @@ LDVERSION='$(VERSION)$(ABIFLAGS)'
 { $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}"
index 157e9bf4067943ffdb7ea6c41b7c2a821199a71f..7d2eff0d94c34fc6c4840ceab1da5451e3667cd8 100644 (file)
@@ -4648,6 +4648,14 @@ AC_MSG_CHECKING(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