Skip to content

How to build OpenBLAS for Android

Martin Kroeker edited this page Jul 22, 2017 · 30 revisions

Building OpenBLAS without Fortran

The prebuilt Android NDK toolchains do not include Fortran, hence parts like LAPACK cannot be built. You can still build OpenBLAS without it. For instructions on how to build OpenBLAS with Fortran, see the next section.

To use easily the prebuilt toolchains, follow building a standalone toolchain for your desired architecture. This would be arm-linux-androideabi-gcc-4.9 for ARMV7 and aarch64-linux-android-gcc-4.9 for ARMV8.

You can build OpenBLAS (0.2.19 and earlier) with:

# Add the toolchain to your path
export PATH=/path/to/standalone-toolchain/bin:$PATH

# Build without Fortran for ARMV7
make TARGET=ARMV7 HOSTCC=gcc CC=arm-linux-androideabi-gcc NOFORTRAN=1 libs
# Build without Fortran for ARMV8
make TARGET=ARMV8 BINARY=64 HOSTCC=gcc CC=aarch64-linux-android-gcc NOFORTRAN=1 libs

Since we are cross-compiling, we make the libs recipe, not all. Otherwise you will get errors when trying to link/run tests as versions up to and including 0.2.19 cannot build a shared library for Android.

From 0.2.20 on, you can just leave off the "libs" to get a full build, and you may want to use the softfp ABI instead of the deprecated hardfp one on ARMV7 so you would use

# Add the toolchain to your path
export PATH=/path/to/standalone-toolchain/bin:$PATH

# Build without Fortran for ARMV7
make TARGET=ARMV7 ARM_SOFTFP_ABI=1 HOSTCC=gcc CC=arm-linux-androideabi-gcc NOFORTRAN=1
# Build without Fortran for ARMV8
make TARGET=ARMV8 BINARY=64 HOSTCC=gcc CC=aarch64-linux-android-gcc NOFORTRAN=1

When you are done, install OpenBLAS into the desired directory. Be sure to also use all command line options here that you specified for building, otherwise errors may occur as it tries to install things you did not build:

make PREFIX=/path/to/install-dir TARGET=... install

Building OpenBLAS with Fortran

Instructions on how to build the GNU toolchains with Fortran can be found here. The Releases section provides prebuilt versions, use the standalone one.

You can build OpenBLAS with:

# Add the toolchain to your path
export PATH=/path/to/standalone-toolchain-with-fortran/bin:$PATH

# Build with Fortran for ARMV7
make TARGET=ARMV7 HOSTCC=gcc CC=arm-linux-androideabi-gcc FC=arm-linux-androideabi-gfortran libs
# Build with LAPACK for ARMV8
make TARGET=ARMV8 BINARY=64 HOSTCC=gcc CC=aarch64-linux-android-gcc FC=aarch64-linux-android-gfortran libs

As mentioned above you can leave off the libs argument here when building 0.2.20 and later, and you may want to add ARM_SOFTFP_ABI=1 when building for ARMV7.

Linking OpenBLAS (0.2.19 and earlier) for ARMV7

If you are using ndk-build, you need to set the ABI to hard floating points in your Application.mk:

APP_ABI := armeabi-v7a-hard

This will set the appropriate flags for you. If you are not using ndk-build, you will want to add the following flags:

TARGET_CFLAGS += -mhard-float -D_NDK_MATH_NO_SOFTFP=1
TARGET_LDFLAGS += -Wl,--no-warn-mismatch -lm_hard

From 0.2.20 on, it is also possible to build for the softfp ABI by specifying ARM_SOFTFP_ABI=1 during the build. In that case, also make sure that all your dependencies are compiled with -mfloat-abi=softfp as well, as mixing "hard" and "soft" floating point ABIs in a program will make it crash.

Building with an NDK that does not support gcc

Most recent Android NDKs stopped supporting gcc, so you would substitute the version of clang that comes with the NDK for the CC=arm-linux-androideabi-gcc on the build lines above.