Skip to content

Commit de9c9cb

Browse files
mhsmithwoodruffw
authored andcommitted
pythongh-71052: Fix several Android build issues (python#115955)
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. * pythongh-115390 (bee7bb3) 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.
1 parent f38537f commit de9c9cb

File tree

6 files changed

+87
-21
lines changed

6 files changed

+87
-21
lines changed

Lib/ctypes/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ def LoadLibrary(self, name):
468468

469469
if _os.name == "nt":
470470
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
471+
elif hasattr(_sys, "getandroidapilevel"):
472+
pythonapi = PyDLL("libpython%d.%d.so" % _sys.version_info[:2])
471473
elif _sys.platform == "cygwin":
472474
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
473475
else:

Makefile.pre.in

+2-4
Original file line numberDiff line numberDiff line change
@@ -859,11 +859,9 @@ $(LIBRARY): $(LIBRARY_OBJS)
859859
$(AR) $(ARFLAGS) $@ $(LIBRARY_OBJS)
860860

861861
libpython$(LDVERSION).so: $(LIBRARY_OBJS) $(DTRACE_OBJS)
862-
if test $(INSTSONAME) != $(LDLIBRARY); then \
863-
$(BLDSHARED) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \
862+
$(BLDSHARED) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM)
863+
if test $(INSTSONAME) != $@; then \
864864
$(LN) -f $(INSTSONAME) $@; \
865-
else \
866-
$(BLDSHARED) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \
867865
fi
868866

869867
libpython3.so: libpython$(LDVERSION).so
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix several Android build issues

Modules/_multiprocessing/multiprocessing.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static PyMethodDef module_methods[] = {
181181
_MULTIPROCESSING_RECV_METHODDEF
182182
_MULTIPROCESSING_SEND_METHODDEF
183183
#endif
184-
#if !defined(POSIX_SEMAPHORES_NOT_ENABLED) && !defined(__ANDROID__)
184+
#if !defined(POSIX_SEMAPHORES_NOT_ENABLED)
185185
_MULTIPROCESSING_SEM_UNLINK_METHODDEF
186186
#endif
187187
{NULL}

configure

+40-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+41-9
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,13 @@ if test $enable_shared = "yes"; then
14431443
LDLIBRARY='libpython$(LDVERSION).so'
14441444
BLDLIBRARY='-L. -lpython$(LDVERSION)'
14451445
RUNSHARED=LD_LIBRARY_PATH=`pwd`${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
1446-
INSTSONAME="$LDLIBRARY".$SOVERSION
1446+
1447+
# The Android Gradle plugin will only package libraries whose names end
1448+
# with ".so".
1449+
if test "$ac_sys_system" != "Linux-android"; then
1450+
INSTSONAME="$LDLIBRARY".$SOVERSION
1451+
fi
1452+
14471453
if test "$with_pydebug" != yes
14481454
then
14491455
PY3LIBRARY=libpython3.so
@@ -3675,7 +3681,14 @@ int main(void)
36753681
}]])],
36763682
[ac_cv_aligned_required=no],
36773683
[ac_cv_aligned_required=yes],
3678-
[ac_cv_aligned_required=yes])
3684+
[
3685+
# "yes" changes the hash function to FNV, which causes problems with Numba
3686+
# (https://github.com/numba/numba/blob/0.59.0/numba/cpython/hashing.py#L470).
3687+
if test "$ac_sys_system" = "Linux-android"; then
3688+
ac_cv_aligned_required=no
3689+
else
3690+
ac_cv_aligned_required=yes
3691+
fi])
36793692
])
36803693
if test "$ac_cv_aligned_required" = yes ; then
36813694
AC_DEFINE([HAVE_ALIGNED_REQUIRED], [1],
@@ -4872,6 +4885,22 @@ else
48724885
AC_MSG_RESULT([$MACHDEP_OBJS])
48734886
fi
48744887

4888+
if test "$ac_sys_system" = "Linux-android"; then
4889+
# When these functions are used in an unprivileged process, they crash rather
4890+
# than returning an error.
4891+
privileged_funcs="chroot initgroups setegid seteuid setgid setregid setresgid
4892+
setresuid setreuid setuid"
4893+
4894+
# These functions are unimplemented and always return an error.
4895+
unimplemented_funcs="sem_open sem_unlink"
4896+
4897+
for name in $privileged_funcs $unimplemented_funcs; do
4898+
AS_VAR_PUSHDEF([func_var], [ac_cv_func_$name])
4899+
AS_VAR_SET([func_var], [no])
4900+
AS_VAR_POPDEF([func_var])
4901+
done
4902+
fi
4903+
48754904
# checks for library functions
48764905
AC_CHECK_FUNCS([ \
48774906
accept4 alarm bind_textdomain_codeset chmod chown clock closefrom close_range confstr \
@@ -5216,10 +5245,11 @@ AC_CHECK_FUNCS([clock_getres], [], [
52165245
])
52175246
])
52185247

5219-
# On iOS, clock_settime can be linked (so it is found by
5220-
# configure), but it raises a runtime error if used because apps can't change
5221-
# the clock. Force the symbol off.
5222-
if test "$ac_sys_system" != "iOS" ; then
5248+
# On Android and iOS, clock_settime can be linked (so it is found by
5249+
# configure), but when used in an unprivileged process, it crashes rather than
5250+
# returning an error. Force the symbol off.
5251+
if test "$ac_sys_system" != "Linux-android" && test "$ac_sys_system" != "iOS"
5252+
then
52235253
AC_CHECK_FUNCS([clock_settime], [], [
52245254
AC_CHECK_LIB([rt], [clock_settime], [
52255255
AC_DEFINE([HAVE_CLOCK_SETTIME], [1])
@@ -5371,7 +5401,9 @@ int main(void)
53715401
[ac_cv_buggy_getaddrinfo=no],
53725402
[ac_cv_buggy_getaddrinfo=yes],
53735403
[
5374-
if test "${enable_ipv6+set}" = set; then
5404+
if test "$ac_sys_system" = "Linux-android" || test "$ac_sys_system" = "iOS"; then
5405+
ac_cv_buggy_getaddrinfo="no"
5406+
elif test "${enable_ipv6+set}" = set; then
53755407
ac_cv_buggy_getaddrinfo="no -- configured with --(en|dis)able-ipv6"
53765408
else
53775409
ac_cv_buggy_getaddrinfo=yes
@@ -6589,9 +6621,9 @@ CPPFLAGS=$ac_save_cppflags
65896621
AC_MSG_NOTICE([checking for device files])
65906622

65916623
dnl NOTE: Inform user how to proceed with files when cross compiling.
6592-
dnl iOS cross-compile builds are predictable; they won't ever
6624+
dnl Some cross-compile builds are predictable; they won't ever
65936625
dnl have /dev/ptmx or /dev/ptc, so we can set them explicitly.
6594-
if test "$ac_sys_system" = "iOS" ; then
6626+
if test "$ac_sys_system" = "Linux-android" || test "$ac_sys_system" = "iOS"; then
65956627
ac_cv_file__dev_ptmx=no
65966628
ac_cv_file__dev_ptc=no
65976629
else

0 commit comments

Comments
 (0)