36

I'm trying to configure Android.mk to cross compile native code to support different chipset namely armeabi, mips, and x86. I know I can configure Application.mk in the following way to compile the source code for different chip set:

APP_ABI := all

This will trigger Android-NDK's build script to compile the source code for all the chipsets. However, I want to dynamically tell Android.mk to look for different static library dependencies compiled with different chip set.

# Get the architecture info
ARCH := ????

include $(CLEAR_VARS)
LOCAL_MODULE:= mylib
LOCAL_SRC_FILES:= build/lib/libxxx_$(ARCH).a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)

Is this possible to do? If so, can anyone advice how to do so?

Update: I tried something like this in Application.mk:

 APP_ABI := armeabi armeabi-v7a mips x64

with Android.mk:

# Get the architecture info
ARCH := $(APP_ABI)

include $(CLEAR_VARS)
LOCAL_MODULE:= mylib
LOCAL_SRC_FILES:= build/lib/libxxx_$(ARCH).a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)

but it errors with the following:

 The LOCAL_SRC_FILES for a prebuilt static library should only contain one item

which makes sense. I want to pass APP_ABI := all in Application.mk and be able to dynamically reference it. Any ideas?

LuxuryMaster
  • 577
  • 1
  • 4
  • 11
  • If you look at ndk and other libs, they use defines such as `#ifdef __arm__` but I am not sure where it's defined or standardized – minsk Dec 08 '13 at 07:53
  • Hi , i want to do exactly the same thing. Can you give me example of Andorid.mk file and where to put it in the app?? – jeevs Jan 17 '16 at 14:39

2 Answers2

36

Check TARGET_ARCH_ABI:

ifeq($(TARGET_ARCH_ABI), armeabi-v7a)
  # v7a-specific stuff
endif
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 1
    $TARGET_ARCH_ABI simply returns whatever I set in APP_ABI. Is there any way I can pass either APP_ABI := armeabi armeabi-v7a mips x64 or APP_ABI := all? – LuxuryMaster Sep 27 '12 at 06:42
  • 4
    You must set `APP_ABI := all` in `Application.mk`, otherwise `ndk-build` will only use the default `armeabi` architecture. For multiple architectures, NDK will iteratively call your `Android.mk` file - every time setting `$(TARGET_ARCH_ABI)` differently. – Alex Cohn Sep 27 '12 at 09:26
  • 1
    I thought he was setting `APP_ABI` in `Android.mk`, but I misread. Sorry. – nneonneo Sep 27 '12 at 09:27
  • `ifneq (,$(filter $(TARGET_ARCH_ABI),armeabi armeabi-v7a))` for multi archs: http://stackoverflow.com/questions/7656425/makefile-ifeq-logical-or – Ciro Santilli OurBigBook.com Jun 24 '16 at 17:45
29

There is TARGET_ARCH variable that holds the value of the current ABI being built. You can use it the following way:

ifeq ($(TARGET_ARCH),x86)
    LOCAL_CFLAGS   := $(COMMON_FLAGS_LIST)
else
    LOCAL_CFLAGS   := -mfpu=vfp -mfloat-abi=softfp $(COMMON_FLAGS_LIST)
endif

If you specify APP_ABI := armeabi-v7a armeabi mips x86 or APP_ABI := all in your Application.mk you will get each and every separate ABI value.

Erik Ghonyan
  • 427
  • 4
  • 12
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
  • 1
    Hello Sergey, thanks for the answer. It worked like a charm! I also appreciate how you gave me an example on how I can better use this as well. I was also getting fp warning when compiling but this also resolved that warning as well :) – LuxuryMaster Sep 28 '12 at 04:19
  • 1
    FYI, not sure if u intended your code as extra help but I tried anyway because I was curious :) LOCAL_CFLAGS := -mfpu=vfp -mfloat-abi=softfp. I observed that it errors while compiling mips architecture, it errored with the following:cc1plus: error: unrecognized command line option "-mfpu=vfp" cc1plus: error: unrecognized command line option "-mfloat-abi=softfp". This means the correct code snippet would be LOCAL_CFLAGS would be ifeq ($(TARGET_ARCH),arm) LOCAL_CFLAGS := -mfpu=vfp -mfloat-abi=softfp $(COMMON_FLAGS_LIST) else LOCAL_CFLAGS := $(COMMON_FLAGS_LIST). endif. Right? – LuxuryMaster Sep 28 '12 at 04:51
  • 6
    Doesn't work for arm64-v8a, which returns arm64 instead. The other answer (TARGET_ARCH_ABI) is correct. – Karu Jan 28 '15 at 07:48
  • @Karu: this was answered 2.5 years ago. There was no arm64-v8a in sight at that time. – Sergey K. Jan 28 '15 at 15:44
  • 2
    That's fine, but I read it yesterday. It was incorrect yesterday, so I downvoted it yesterday. – Karu Jan 29 '15 at 04:30
  • armeabi-v7a and armeabi-v7 are both returning arm with TARGET_ARCH. armeabi-v7a was in sight at that time. – Marc Plano-Lesay Feb 12 '15 at 16:06