]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-71052: Fix several Android build issues (#115955)
authorMalcolm Smith <smith@chaquo.com>
Thu, 29 Feb 2024 21:58:20 +0000 (21:58 +0000)
committerGitHub <noreply@github.com>
Thu, 29 Feb 2024 21:58:20 +0000 (22:58 +0100)
This change is part of the work on PEP-738: Adding Android as a
supported platform.

* Remove the "1.0" suffix from libpython's filename on Android, which
  would prevent Gradle from packaging it into an app.
* Simplify the build command in the Makefile so that libpython always
  gets given an SONAME with the `-Wl-h` argument, even if the SONAME is
  identical to the actual filename.
* Disable a number of functions on Android which can be compiled and
  linked against, but always fail at runtime. As a result, the native
  _multiprocessing module is no longer built for Android.
* gh-115390 (bee7bb331) added some pre-determined results to the
  configure script for things that can't be autodetected when
  cross-compiling; this change adds Android to these where appropriate.
* Add a couple more pre-determined results for Android, and making them
  cover iOS as well. This means the --enable-ipv6 configure option will
  no longer be required on either platform.

Lib/ctypes/__init__.py
Makefile.pre.in
Misc/NEWS.d/next/Build/2024-02-26-14-54-58.gh-issue-71052.XvFay1.rst [new file with mode: 0644]
Modules/_multiprocessing/multiprocessing.c
configure
configure.ac

index 141142a57dcb3e22d406758cab9ec3a8c41d2bd8..d54ee05b15f5bda8e816ff8b4cea0808894bafff 100644 (file)
@@ -468,6 +468,8 @@ pydll = LibraryLoader(PyDLL)
 
 if _os.name == "nt":
     pythonapi = PyDLL("python dll", None, _sys.dllhandle)
+elif hasattr(_sys, "getandroidapilevel"):
+    pythonapi = PyDLL("libpython%d.%d.so" % _sys.version_info[:2])
 elif _sys.platform == "cygwin":
     pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
 else:
index 7533a49b4392f04ed63272af2120a4a7515c0c71..ee65ecd918ce2adcff05834015cc8d3baf0c97d7 100644 (file)
@@ -859,11 +859,9 @@ $(LIBRARY): $(LIBRARY_OBJS)
        $(AR) $(ARFLAGS) $@ $(LIBRARY_OBJS)
 
 libpython$(LDVERSION).so: $(LIBRARY_OBJS) $(DTRACE_OBJS)
-       if test $(INSTSONAME) != $(LDLIBRARY); then \
-               $(BLDSHARED) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \
+       $(BLDSHARED) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM)
+       if test $(INSTSONAME) != $@; then \
                $(LN) -f $(INSTSONAME) $@; \
-       else \
-               $(BLDSHARED) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \
        fi
 
 libpython3.so: libpython$(LDVERSION).so
diff --git a/Misc/NEWS.d/next/Build/2024-02-26-14-54-58.gh-issue-71052.XvFay1.rst b/Misc/NEWS.d/next/Build/2024-02-26-14-54-58.gh-issue-71052.XvFay1.rst
new file mode 100644 (file)
index 0000000..bda9133
--- /dev/null
@@ -0,0 +1 @@
+Fix several Android build issues
index 2e6d8eb68c02437ef9a1f5551ec22581137bddbc..1f6ab718a36984a961b34dcde86ee7ab2afadb5d 100644 (file)
@@ -181,7 +181,7 @@ static PyMethodDef module_methods[] = {
     _MULTIPROCESSING_RECV_METHODDEF
     _MULTIPROCESSING_SEND_METHODDEF
 #endif
-#if !defined(POSIX_SEMAPHORES_NOT_ENABLED) && !defined(__ANDROID__)
+#if !defined(POSIX_SEMAPHORES_NOT_ENABLED)
     _MULTIPROCESSING_SEM_UNLINK_METHODDEF
 #endif
     {NULL}
index f431c5dd15ec4a0c6cd33010db19f7a0c63a0a81..45f232164e7094a38612ddcde21ec573e3d84931 100755 (executable)
--- a/configure
+++ b/configure
@@ -7423,7 +7423,13 @@ printf "%s\n" "#define Py_ENABLE_SHARED 1" >>confdefs.h
       LDLIBRARY='libpython$(LDVERSION).so'
       BLDLIBRARY='-L. -lpython$(LDVERSION)'
       RUNSHARED=LD_LIBRARY_PATH=`pwd`${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
-      INSTSONAME="$LDLIBRARY".$SOVERSION
+
+      # The Android Gradle plugin will only package libraries whose names end
+      # with ".so".
+      if test "$ac_sys_system" != "Linux-android"; then
+          INSTSONAME="$LDLIBRARY".$SOVERSION
+      fi
+
       if test "$with_pydebug" != yes
       then
         PY3LIBRARY=libpython3.so
@@ -13726,7 +13732,14 @@ then :
 else $as_nop
   if test "$cross_compiling" = yes
 then :
+
+# "yes" changes the hash function to FNV, which causes problems with Numba
+# (https://github.com/numba/numba/blob/0.59.0/numba/cpython/hashing.py#L470).
+if test "$ac_sys_system" = "Linux-android"; then
+  ac_cv_aligned_required=no
+else
   ac_cv_aligned_required=yes
+fi
 else $as_nop
   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
@@ -17529,6 +17542,23 @@ else
 printf "%s\n" "$MACHDEP_OBJS" >&6; }
 fi
 
+if test "$ac_sys_system" = "Linux-android"; then
+  # When these functions are used in an unprivileged process, they crash rather
+  # than returning an error.
+  privileged_funcs="chroot initgroups setegid seteuid setgid setregid setresgid
+    setresuid setreuid setuid"
+
+  # These functions are unimplemented and always return an error.
+  unimplemented_funcs="sem_open sem_unlink"
+
+  for name in $privileged_funcs $unimplemented_funcs; do
+    as_func_var=`printf "%s\n" "ac_cv_func_$name" | $as_tr_sh`
+
+    eval "$as_func_var=no"
+
+  done
+fi
+
 # checks for library functions
 ac_fn_c_check_func "$LINENO" "accept4" "ac_cv_func_accept4"
 if test "x$ac_cv_func_accept4" = xyes
 
 done
 
-# On iOS, clock_settime can be linked (so it is found by
-# configure), but it raises a runtime error if used because apps can't change
-# the clock. Force the symbol off.
-if test "$ac_sys_system" != "iOS" ; then
+# On Android and iOS, clock_settime can be linked (so it is found by
+# configure), but when used in an unprivileged process, it crashes rather than
+# returning an error. Force the symbol off.
+if test "$ac_sys_system" != "Linux-android" && test "$ac_sys_system" != "iOS"
+then
 
   for ac_func in clock_settime
 do :
@@ -22295,7 +22326,9 @@ else $as_nop
   if test "$cross_compiling" = yes
 then :
 
-if test "${enable_ipv6+set}" = set; then
+if test "$ac_sys_system" = "Linux-android" || test "$ac_sys_system" = "iOS"; then
+  ac_cv_buggy_getaddrinfo="no"
+elif test "${enable_ipv6+set}" = set; then
   ac_cv_buggy_getaddrinfo="no -- configured with --(en|dis)able-ipv6"
 else
   ac_cv_buggy_getaddrinfo=yes
@@ -26968,7 +27001,7 @@ CPPFLAGS=$ac_save_cppflags
 { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for device files" >&5
 printf "%s\n" "$as_me: checking for device files" >&6;}
 
-if test "$ac_sys_system" = "iOS" ; then
+if test "$ac_sys_system" = "Linux-android" || test "$ac_sys_system" = "iOS"; then
   ac_cv_file__dev_ptmx=no
   ac_cv_file__dev_ptc=no
 else
index a5abb06a8294139435906310bb9ea6e93ef091dc..b7671facc65a8a9ec8e1102c352f41e93eb7ce3d 100644 (file)
@@ -1443,7 +1443,13 @@ if test $enable_shared = "yes"; then
       LDLIBRARY='libpython$(LDVERSION).so'
       BLDLIBRARY='-L. -lpython$(LDVERSION)'
       RUNSHARED=LD_LIBRARY_PATH=`pwd`${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
-      INSTSONAME="$LDLIBRARY".$SOVERSION
+
+      # The Android Gradle plugin will only package libraries whose names end
+      # with ".so".
+      if test "$ac_sys_system" != "Linux-android"; then
+          INSTSONAME="$LDLIBRARY".$SOVERSION
+      fi
+
       if test "$with_pydebug" != yes
       then
         PY3LIBRARY=libpython3.so
@@ -3675,7 +3681,14 @@ int main(void)
 }]])],
 [ac_cv_aligned_required=no],
 [ac_cv_aligned_required=yes],
-[ac_cv_aligned_required=yes])
+[
+# "yes" changes the hash function to FNV, which causes problems with Numba
+# (https://github.com/numba/numba/blob/0.59.0/numba/cpython/hashing.py#L470).
+if test "$ac_sys_system" = "Linux-android"; then
+  ac_cv_aligned_required=no
+else
+  ac_cv_aligned_required=yes
+fi])
 ])
 if test "$ac_cv_aligned_required" = yes ; then
   AC_DEFINE([HAVE_ALIGNED_REQUIRED], [1],
@@ -4872,6 +4885,22 @@ else
   AC_MSG_RESULT([$MACHDEP_OBJS])
 fi
 
+if test "$ac_sys_system" = "Linux-android"; then
+  # When these functions are used in an unprivileged process, they crash rather
+  # than returning an error.
+  privileged_funcs="chroot initgroups setegid seteuid setgid setregid setresgid
+    setresuid setreuid setuid"
+
+  # These functions are unimplemented and always return an error.
+  unimplemented_funcs="sem_open sem_unlink"
+
+  for name in $privileged_funcs $unimplemented_funcs; do
+    AS_VAR_PUSHDEF([func_var], [ac_cv_func_$name])
+    AS_VAR_SET([func_var], [no])
+    AS_VAR_POPDEF([func_var])
+  done
+fi
+
 # checks for library functions
 AC_CHECK_FUNCS([ \
   accept4 alarm bind_textdomain_codeset chmod chown clock closefrom close_range confstr \
@@ -5216,10 +5245,11 @@ AC_CHECK_FUNCS([clock_getres], [], [
     ])
 ])
 
-# On iOS, clock_settime can be linked (so it is found by
-# configure), but it raises a runtime error if used because apps can't change
-# the clock. Force the symbol off.
-if test "$ac_sys_system" != "iOS" ; then
+# On Android and iOS, clock_settime can be linked (so it is found by
+# configure), but when used in an unprivileged process, it crashes rather than
+# returning an error. Force the symbol off.
+if test "$ac_sys_system" != "Linux-android" && test "$ac_sys_system" != "iOS"
+then
   AC_CHECK_FUNCS([clock_settime], [], [
       AC_CHECK_LIB([rt], [clock_settime], [
           AC_DEFINE([HAVE_CLOCK_SETTIME], [1])
@@ -5371,7 +5401,9 @@ int main(void)
 [ac_cv_buggy_getaddrinfo=no],
 [ac_cv_buggy_getaddrinfo=yes],
 [
-if test "${enable_ipv6+set}" = set; then
+if test "$ac_sys_system" = "Linux-android" || test "$ac_sys_system" = "iOS"; then
+  ac_cv_buggy_getaddrinfo="no"
+elif test "${enable_ipv6+set}" = set; then
   ac_cv_buggy_getaddrinfo="no -- configured with --(en|dis)able-ipv6"
 else
   ac_cv_buggy_getaddrinfo=yes
@@ -6589,9 +6621,9 @@ CPPFLAGS=$ac_save_cppflags
 AC_MSG_NOTICE([checking for device files])
 
 dnl NOTE: Inform user how to proceed with files when cross compiling.
-dnl iOS cross-compile builds are predictable; they won't ever
+dnl Some cross-compile builds are predictable; they won't ever
 dnl have /dev/ptmx or /dev/ptc, so we can set them explicitly.
-if test "$ac_sys_system" = "iOS" ; then
+if test "$ac_sys_system" = "Linux-android" || test "$ac_sys_system" = "iOS"; then
   ac_cv_file__dev_ptmx=no
   ac_cv_file__dev_ptc=no
 else