Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android build system, NEON and non-NEON builds

I want to build my library for armv6, and there is some neon code that I enable at runtime if the device supports it. The neon code uses neon intrinsics, and to be able to compile it, I must enable armeabi-v7a, but that affects regular c-code (it becomes broken on some low-end devices).

So, if the android build system wasn't excessively intrusive, I wouldn't have to ask questions, but it seems that there is no way for me to compile one file for armv6 and the other file for arm7-neon.

Can anybody give any clues if that's doable?

Edit
Before trying to reply and wasting internet-ink, it should be clear that these are the main points:
1) make only ONE lib.
2) make build that runs on armv6 (pre neon devices, e.g. armeabi).
3) allow this build to also contain NEON code (which could be executed based on run-time cpu detection; cpu detection is outside the scope of the question).
4) NEON code comes from a c/cpp file and is written using neon intrinsics.

Omitting any part of these requirements totally loses the point of the question

like image 590
Pavel P Avatar asked Oct 06 '11 19:10

Pavel P


People also ask

What is Android Neon?

Neon provides scalar/vector instructions and registers (shared with the FPU) comparable to MMX/SSE/3DNow! in the x86 world. Not all ARMv7-based Android devices support Neon, but devices that do may benefit significantly from its support for scalar/vector instructions. All ARMv8-based devices support Neon.

What is NDK build?

To compile and debug native code for your app, you need the following components: The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android. CMake: an external build tool that works alongside Gradle to build your native library.

What is NDK tools in Android Studio?

The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android. CMake: an external build tool that works alongside Gradle to build your native library. You do not need this component if you only plan to use ndk-build.


1 Answers

I have recently found another way to work around the limitations of NDK. My case was not related to NEON, but for you the same hack could do the job.

The trick is to use the existing "tag" mechanism of NDK to specify special CFLAGS for a bunch of files. This is how you do it:

First, list the neon-specific sources. You cannot use the .neon suffix as described in docs/CPU-ARM-NEON.html because build-binary.mk will find that you are not targeting armeabi-v7a. I use the following technique:

LOCAL_NEON_SRC_FILES := imgproc/neon_utils.c \
                        videoproc/usingneon.cpp
LOCAL_SRC_FILES := main.c \
                   imgproc/img.c \
                   videoproc/video.cpp

LOCAL_SRC_FILES += $(LOCAL_NEON_SRC_FILES)

Now, define the CFLAGS for NEON:

LOCAL_NEON_CFLAGS := -mfloat-abi=softfp -mfpu=neon -march=armv7

Finally, add the following magical line to your Android.mk:

TARGET-process-src-files-tags += $(call add-src-files-target-cflags, $(LOCAL_NEON_SRC_FILES), $(LOCAL_NEON_CFLAGS))

If you have more than one binary to build, you will probably want $(LOCAL_NEON_SRC_FILES) to be reset by

include $(CLEAR_VARS)

For this, add the following to your Android.mk or to Application.mk:

modules-LOCALS += NEON_SRC_FILES

Note: I have not tried this magic for NEON, I needed it for entirely different purposes. You may need some adaptations to achieve the desired compilation options for your files, and for your project. I am using NDK r.8b, and I did not check if this would work on earlier (or later) versions.

like image 73
Alex Cohn Avatar answered Oct 14 '22 01:10

Alex Cohn