Skip to content

Commit b51c26e

Browse files
committed
lapack/scipy: support NDK r21e, x86/64 archs
1 parent e524c0b commit b51c26e

File tree

5 files changed

+40
-55
lines changed

5 files changed

+40
-55
lines changed

ci/makefiles/android.mk

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Those android NDK/SDK variables can be override when running the file
44
ANDROID_NDK_VERSION ?= 23b
5-
ANDROID_NDK_VERSION_LEGACY ?= 19c
5+
ANDROID_NDK_VERSION_LEGACY ?= 21e
66
ANDROID_SDK_TOOLS_VERSION ?= 6514223
77
ANDROID_SDK_BUILD_TOOLS_VERSION ?= 29.0.3
88
ANDROID_HOME ?= $(HOME)/.android
@@ -93,12 +93,14 @@ extract_android_ndk_legacy:
9393
&& rm -f $(ANDROID_NDK_ARCHIVE_LEGACY)
9494

9595
extract_android_ndk_gfortran:
96-
rm -rf $(ANDROID_NDK_HOME_LEGACY)/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/
97-
mkdir $(ANDROID_NDK_HOME_LEGACY)/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/
98-
tar -xvf $(ANDROID_NDK_GFORTRAN_ARCHIVE_ARM64) -C $(ANDROID_NDK_HOME_LEGACY)/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/ --strip-components 1
99-
rm -rf $(ANDROID_NDK_HOME_LEGACY)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/
100-
mkdir $(ANDROID_NDK_HOME_LEGACY)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/
101-
tar -xvf $(ANDROID_NDK_GFORTRAN_ARCHIVE_ARM) -C $(ANDROID_NDK_HOME_LEGACY)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/ --strip-components 1
96+
rm -rf $(ANDROID_NDK_HOME_LEGACY)/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/ \
97+
&& mkdir $(ANDROID_NDK_HOME_LEGACY)/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/ \
98+
&& tar -xf $(ANDROID_NDK_GFORTRAN_ARCHIVE_ARM64) -C $(ANDROID_NDK_HOME_LEGACY)/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/ --strip-components 1 \
99+
&& rm -f $(ANDROID_NDK_GFORTRAN_ARCHIVE_ARM64) \
100+
&& rm -rf $(ANDROID_NDK_HOME_LEGACY)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/ \
101+
&& mkdir $(ANDROID_NDK_HOME_LEGACY)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/ \
102+
&& tar -xf $(ANDROID_NDK_GFORTRAN_ARCHIVE_ARM) -C $(ANDROID_NDK_HOME_LEGACY)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/ --strip-components 1 \
103+
&& rm -f $(ANDROID_NDK_GFORTRAN_ARCHIVE_ARM)
102104

103105

104106

pythonforandroid/recipes/lapack/__init__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
known to build with cmake version 3.19.2 and NDK r19c.
2+
known to build with cmake version 3.23.2 and NDK r21e.
33
See https://gitlab.kitware.com/cmake/cmake/-/issues/18739
44
'''
55

@@ -15,10 +15,16 @@
1515
arch_to_sysroot = {'armeabi': 'arm', 'armeabi-v7a': 'arm', 'arm64-v8a': 'arm64'}
1616

1717

18+
def arch_to_toolchain(arch):
19+
if 'arm' in arch.arch:
20+
return arch.command_prefix
21+
return arch.arch
22+
23+
1824
class LapackRecipe(Recipe):
1925

2026
name = 'lapack'
21-
version = 'v3.9.0'
27+
version = 'v3.10.1'
2228
url = 'https://github.com/Reference-LAPACK/lapack/archive/{version}.tar.gz'
2329
libdir = 'build/install/lib'
2430
built_libraries = {'libblas.so': libdir, 'liblapack.so': libdir, 'libcblas.so': libdir}
@@ -28,14 +34,14 @@ def get_recipe_env(self, arch):
2834

2935
ndk_dir = environ.get("LEGACY_NDK")
3036
if ndk_dir is None:
31-
raise BuildInterruptingException("Please set the environment variable 'LEGACY_NDK' to point to a NDK location with gcc/gfortran support (last tested NDK version was 'r19c')")
37+
raise BuildInterruptingException("Please set the environment variable 'LEGACY_NDK' to point to a NDK location with gcc/gfortran support (supported NDK version: 'r21e')")
3238

3339
GCC_VER = '4.9'
3440
HOST = build_platform
3541

3642
sysroot_suffix = arch_to_sysroot.get(arch.arch, arch.arch)
3743
sysroot = f"{ndk_dir}/platforms/{env['NDK_API']}/arch-{sysroot_suffix}"
38-
FC = f"{ndk_dir}/toolchains/{arch.command_prefix}-{GCC_VER}/prebuilt/{HOST}/bin/{arch.command_prefix}-gfortran"
44+
FC = f"{ndk_dir}/toolchains/{arch_to_toolchain(arch)}-{GCC_VER}/prebuilt/{HOST}/bin/{arch.command_prefix}-gfortran"
3945
env['FC'] = f'{FC} --sysroot={sysroot}'
4046
if sh.which(FC) is None:
4147
raise BuildInterruptingException(f"{FC} not found. See https://github.com/mzakharo/android-gfortran")
@@ -51,19 +57,20 @@ def build_arch(self, arch):
5157
env = self.get_recipe_env(arch)
5258
ndk_dir = environ["LEGACY_NDK"]
5359
shprint(sh.rm, '-rf', 'CMakeFiles/', 'CMakeCache.txt', _env=env)
54-
shprint(sh.cmake, source_dir,
60+
opts = [
5561
'-DCMAKE_SYSTEM_NAME=Android',
5662
'-DCMAKE_POSITION_INDEPENDENT_CODE=1',
5763
'-DCMAKE_ANDROID_ARCH_ABI={arch}'.format(arch=arch.arch),
5864
'-DCMAKE_ANDROID_NDK=' + ndk_dir,
65+
'-DCMAKE_ANDROID_API={api}'.format(api=self.ctx.ndk_api),
5966
'-DCMAKE_BUILD_TYPE=Release',
6067
'-DCMAKE_INSTALL_PREFIX={}'.format(install_target),
61-
'-DANDROID_ABI={arch}'.format(arch=arch.arch),
62-
'-DANDROID_ARM_NEON=ON',
63-
'-DENABLE_NEON=ON',
6468
'-DCBLAS=ON',
6569
'-DBUILD_SHARED_LIBS=ON',
66-
_env=env)
70+
]
71+
if arch.arch == 'armeabi-v7a':
72+
opts.append('-DCMAKE_ANDROID_ARM_NEON=ON')
73+
shprint(sh.cmake, source_dir, *opts, _env=env)
6774
shprint(sh.make, '-j' + str(cpu_count()), _env=env)
6875
shprint(sh.make, 'install', _env=env)
6976

pythonforandroid/recipes/numpy/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class NumpyRecipe(CompiledComponentsPythonRecipe):
1919
patches = [
2020
join("patches", "remove-default-paths.patch"),
2121
join("patches", "add_libm_explicitly_to_build.patch"),
22-
join("patches", "fix-missing-threads.h.patch"),
2322
]
2423

2524
def get_recipe_env(self, arch=None, with_flags_in_cc=True):

pythonforandroid/recipes/numpy/patches/fix-missing-threads.h.patch

Lines changed: 0 additions & 26 deletions
This file was deleted.

pythonforandroid/recipes/scipy/__init__.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from os import environ
55
from pythonforandroid.util import build_platform
66

7-
arch_to_sysroot = {'armeabi': 'arm', 'armeabi-v7a': 'arm', 'arm64-v8a': 'arm64'}
7+
8+
def arch_to_toolchain(arch):
9+
if 'arm' in arch.arch:
10+
return arch.command_prefix
11+
return arch.arch
812

913

1014
class ScipyRecipe(CompiledComponentsPythonRecipe):
@@ -35,16 +39,12 @@ def get_recipe_env(self, arch):
3539
suffix = '64' if '64' in arch.arch else ''
3640

3741
prefix = arch.command_prefix
38-
sysroot_suffix = arch_to_sysroot.get(arch.arch, arch.arch)
39-
sysroot = f"{ndk_dir}/platforms/{env['NDK_API']}/arch-{sysroot_suffix}"
40-
sysroot_include = f'{ndk_dir}/toolchains/llvm/prebuilt/{HOST}/sysroot/usr/include'
4142
CLANG_BIN = f'{ndk_dir}/toolchains/llvm/prebuilt/{HOST}/bin/'
42-
GCC = f'{ndk_dir}/toolchains/{prefix}-{GCC_VER}/prebuilt/{HOST}'
43+
GCC = f'{ndk_dir}/toolchains/{arch_to_toolchain(arch)}-{GCC_VER}/prebuilt/{HOST}'
4344
libgfortran = f'{GCC}/{prefix}/lib{suffix}'
44-
4545
numpylib = self.ctx.get_python_install_dir(arch.arch) + '/numpy'
46-
LDSHARED_opts = env['LDSHARED'].split('clang')[1]
4746
arch_cflags = ' '.join(arch.arch_cflags)
47+
LDSHARED_opts = f'-target {arch.target} {arch_cflags} ' + ' '.join(arch.common_ldshared)
4848

4949
# TODO: add pythran support
5050
env['SCIPY_USE_PYTHRAN'] = '0'
@@ -58,13 +58,16 @@ def get_recipe_env(self, arch):
5858
env['F90'] = f'{GCC}/bin/{prefix}-gfortran'
5959
env['CC'] = f'{CLANG_BIN}/clang -target {arch.target} {arch_cflags}'
6060
env['CXX'] = f'{CLANG_BIN}/clang++ -target {arch.target} {arch_cflags}'
61+
62+
# scipy expects ldshared to be a single executable without options
6163
env['LDSHARED'] = f'{CLANG_BIN}/clang'
6264

63-
# flags
64-
env['CPPFLAGS'] = f'-DANDROID -I{sysroot_include}/{prefix} --sysroot={sysroot} -I{sysroot_include}/c++/v1 -I{sysroot_include}'
65-
env['LDFLAGS'] += f' {LDSHARED_opts} --sysroot={sysroot} -L{libgfortran} -L{numpylib}/core/lib -L{numpylib}/random/lib'
66-
env['LDFLAGS'] += f' -l{self.stl_lib_name} '
67-
env['LDFLAGS'] += f' -L{ndk_dir}/sources/cxx-stl/llvm-libc++/libs/{arch.arch}/' # for arm32 - unwind
65+
# erase the default NDK C++ include options
66+
env['CPPFLAGS'] = '-DANDROID'
67+
68+
# configure linker
69+
env['LDFLAGS'] += f' {LDSHARED_opts} -L{libgfortran} -L{numpylib}/core/lib -L{numpylib}/random/lib'
70+
env['LDFLAGS'] += f' -l{self.stl_lib_name}'
6871
return env
6972

7073

0 commit comments

Comments
 (0)