'Get architecture type (ABI) to C preprocessor for Android NDK

I'm trying to control my C code in an Android NDK project depending on the selected ABI library.

As a start, I want the NDK library method to answer, with a string, what ABI that is used. I did a few tries but all fails to deliver the result.

In Application.mk I define
APP_ABI := all

Android.mk looks like this:

LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_CFLAGS    += -D$(TARGET_ARCH)  
LOCAL_MODULE    := get_current_abi  
LOCAL_SRC_FILES := get_current_abi.c  
include $(BUILD_SHARED_LIBRARY)

And finally my C-file looks like this: #include
#include

jstring  
Java_com_example_abifinderlib_AbiFinder_getCurrentAbi(JNIEnv* env, jobject thiz)  
{  
#if defined armeabi  
    return (*env)->NewStringUTF(env, "armeabi");  
#elif defined armeabi-v7a  
    return (*env)->NewStringUTF(env, "armeabi-v7a");  
#elif defined mips  
    return (*env)->NewStringUTF(env, "mips");  
#elif defined x86  
    return (*env)->NewStringUTF(env, "x86");  
#else  
    return (*env)->NewStringUTF(env, "unknown");  
#endif  
}

Result string is always "unknown" for all architectures. (Tested arm v6 and arm v7 on HW and others on emulator)

Can I see the actual content of the $(TARGET_ARCH) in the make file? Can I run only the preprocessor on my c-file and see the preprocessed result?

Or am I straight of and need a totally different approach?



Solution 1:[1]

You can use the CPU Features library for this:

#include <cpu-features.h>

AndroidCpuFamily family = android_getCpuFamily();
uint64_t features = android_getCpuFeatures();

if (family == ANDROID_CPU_FAMILY_X86) {
} else if (family == ANDROID_CPU_FAMILY_MIPS) {
} else if (family == ANDROID_CPU_FAMILY_ARM) {
    if (features & ANDROID_CPU_ARM_FEATURE_ARMv7) {
    }
}

(reference)

Solution 2:[2]

You can use gcc defines like this:

#ifdef __i386__
// x86
#elif __x86_64__
// x86_64
#elif __arm__
// armeabi-v7a
#elif __aarch64__
// arm64-v8a
#else
// unknown 
#endif

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael
Solution 2 somega