We recently added cpu-features to detect platform features like ARMV8 and CRC, AES and SHA. We caught a bug report for building against the latest NDK. When we attempt to include <cpu-features.h> in an armeabi project it results in:
$ make -f GNUmakefile-cross
arm-linux-androideabi-g++ -DNDEBUG -g2 -O3 -fPIC -pipe -march=armv5te -mtune=xscale -mthumb -msoft-float
-funwind-tables -fexceptions -frtti -DANDROID --sysroot=/opt/android-ndk/platforms/android-21/arch-arm
-Wa,--noexecstack -I/opt/android-ndk/sources/cxx-stl/gnu-libstdc++/4.9/include
-I/opt/android-ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include -c cpu.cpp
In file included from cpu.cpp:26:0:
/opt/android-ndk/platforms/android-21/arch-arm/usr/include/machine/cpu-features.h:52:6: error:
# error Unknown or unsupported ARM architecture
^
cpu.cpp: In function 'bool CryptoPP::CPU_QueryNEON()':
cpu.cpp:402:29: error: 'android_getCpuFeatures' was not declared in this scope
if (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON)
^
cpu.cpp:402:33: error: 'ANDROID_CPU_ARM_FEATURE_NEON' was not declared in this scope
if (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON)
^
make: *** [cpu.o] Error 1
The CXXFLAGS we are using come directly from a JNI project I created a few years ago. I used ndk-build to examine the flags Android's build system sets, and then transferred them to our script. We are not making them up; they are official NDK compiler options.
When I cat the header cpu-features.h I see an ominous sign:
/* __ARM_ARCH__ is a number corresponding to the ARM revision
* we're going to support. Our toolchain doesn't define __ARM_ARCH__
* so try to guess it.
*/
#ifndef __ARM_ARCH__
# if defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ || \
defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__
# define __ARM_ARCH__ 7
# elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ || \
defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6Z__ || \
defined __ARM_ARCH_6KZ__ || defined __ARM_ARCH_6T2__
# define __ARM_ARCH__ 6
# else
# error Unknown or unsupported ARM architecture
# endif
#endif
We seem to be missing something in our process and implementation. First, all the definitions needed are provided by the preprocessor (see below). The preprocessor provides __ARM_ARCH, but Android's header checks for __ARM_ARCH__.
Second, <machine/cpu-features.h> does not seem to include needed declarations:
$ cat /opt/android-ndk/platforms/android-21/arch-arm/usr/include/machine/cpu-features.h | grep -i android_getCpuFeatures
$
We don't use Android's build system, so very little of this applies: The cpufeatures Library. Its also too high level and lacks some of the details we need.
My question is, how do we use cpu-features in a regular native library? What are we missing for the first and second problems?
Our script sets CXX, CXXFLAGS, etc. They are available for use once the script is sourced. In this case:
$ echo $CXX
arm-linux-androideabi-g++
And:
$ $CXX -dM -E - </dev/null | sort
#define __ACCUM_EPSILON__ 0x1P-15K
#define __ACCUM_FBIT__ 15
#define __ACCUM_IBIT__ 16
#define __ACCUM_MAX__ 0X7FFFFFFFP-15K
#define __ACCUM_MIN__ (-0X1P15K-0X1P15K)
#define __ANDROID__ 1
#define __APCS_32__ 1
#define __arm__ 1
#define __ARM_32BIT_STATE 1
#define __ARM_ARCH 5
#define __ARM_ARCH_5TE__ 1
#define __ARM_ARCH_ISA_ARM 1
#define __ARM_ARCH_ISA_THUMB 1
#define __ARM_EABI__ 1
#define __ARMEL__ 1
#define __ARM_FEATURE_CLZ 1
#define __ARM_FEATURE_DSP 1
#define __ARM_FEATURE_QBIT 1
#define __ARM_FP 12
#define __ARM_NEON_FP 4
#define __ARM_PCS 1
#define __ARM_SIZEOF_MINIMAL_ENUM 4
#define __ARM_SIZEOF_WCHAR_T 4
#define __ATOMIC_ACQ_REL 4
#define __ATOMIC_ACQUIRE 2
#define __ATOMIC_CONSUME 1
#define __ATOMIC_RELAXED 0
#define __ATOMIC_RELEASE 3
#define __ATOMIC_SEQ_CST 5
#define __BIGGEST_ALIGNMENT__ 8
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
...
None of the cpu-features seem to be declared or defined:
$ echo $AOSP_SYSROOT
/opt/android-ndk/platforms/android-21/arch-arm
$ grep -IR android_getCpuFeatures $AOSP_SYSROOT
$ grep -IR ANDROID_CPU_ARM_FEATURE_NEON $AOSP_SYSROOT
$