From 20a80972f472dce2688090eb8cf62bb0536ad0d1 Mon Sep 17 00:00:00 2001 From: junkfood <69683722+JunkFood02@users.noreply.github.com> Date: Mon, 28 Aug 2023 00:35:16 +0800 Subject: [PATCH] whisper.android : migrate from ndk-build to CMake (#1204) --- examples/whisper.android/app/build.gradle | 7 ++- .../app/src/main/jni/whisper/Android.mk | 26 ----------- .../app/src/main/jni/whisper/Application.mk | 1 - .../app/src/main/jni/whisper/CMakeLists.txt | 44 +++++++++++++++++++ .../app/src/main/jni/whisper/Whisper.mk | 18 -------- 5 files changed, 49 insertions(+), 47 deletions(-) delete mode 100644 examples/whisper.android/app/src/main/jni/whisper/Android.mk delete mode 100644 examples/whisper.android/app/src/main/jni/whisper/Application.mk create mode 100644 examples/whisper.android/app/src/main/jni/whisper/CMakeLists.txt delete mode 100644 examples/whisper.android/app/src/main/jni/whisper/Whisper.mk diff --git a/examples/whisper.android/app/build.gradle b/examples/whisper.android/app/build.gradle index 66add99..ab49569 100644 --- a/examples/whisper.android/app/build.gradle +++ b/examples/whisper.android/app/build.gradle @@ -18,6 +18,9 @@ android { vectorDrawables { useSupportLibrary true } + ndk { + abiFilters 'arm64-v8a', 'armeabi-v7a' + } } buildTypes { @@ -42,8 +45,8 @@ android { } ndkVersion "25.1.8937393" externalNativeBuild { - ndkBuild { - path 'src/main/jni/whisper/Android.mk' + cmake { + path = file("src/main/jni/whisper/CMakeLists.txt") } } packagingOptions { diff --git a/examples/whisper.android/app/src/main/jni/whisper/Android.mk b/examples/whisper.android/app/src/main/jni/whisper/Android.mk deleted file mode 100644 index 22fc77f..0000000 --- a/examples/whisper.android/app/src/main/jni/whisper/Android.mk +++ /dev/null @@ -1,26 +0,0 @@ -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) -LOCAL_MODULE := libwhisper -include $(LOCAL_PATH)/Whisper.mk -include $(BUILD_SHARED_LIBRARY) - -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - include $(CLEAR_VARS) - LOCAL_MODULE := libwhisper_vfpv4 - include $(LOCAL_PATH)/Whisper.mk - # Allow building NEON FMA code. - # https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h - LOCAL_CFLAGS += -mfpu=neon-vfpv4 - include $(BUILD_SHARED_LIBRARY) -endif - -ifeq ($(TARGET_ARCH_ABI),arm64-v8a) - include $(CLEAR_VARS) - LOCAL_MODULE := libwhisper_v8fp16_va - include $(LOCAL_PATH)/Whisper.mk - # Allow building NEON FMA code. - # https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h - LOCAL_CFLAGS += -march=armv8.2-a+fp16 - include $(BUILD_SHARED_LIBRARY) -endif - diff --git a/examples/whisper.android/app/src/main/jni/whisper/Application.mk b/examples/whisper.android/app/src/main/jni/whisper/Application.mk deleted file mode 100644 index 067c76f..0000000 --- a/examples/whisper.android/app/src/main/jni/whisper/Application.mk +++ /dev/null @@ -1 +0,0 @@ -APP_STL := c++_static \ No newline at end of file diff --git a/examples/whisper.android/app/src/main/jni/whisper/CMakeLists.txt b/examples/whisper.android/app/src/main/jni/whisper/CMakeLists.txt new file mode 100644 index 0000000..0d33db2 --- /dev/null +++ b/examples/whisper.android/app/src/main/jni/whisper/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.10) + +project(whisper.cpp) + +set(CMAKE_CXX_STANDARD 11) +set(WHISPER_LIB_DIR ${CMAKE_SOURCE_DIR}/../../../../../../../) + +set( + SOURCE_FILES + ${WHISPER_LIB_DIR}/ggml.c + ${WHISPER_LIB_DIR}/whisper.cpp + ${CMAKE_SOURCE_DIR}/jni.c +) + +if (${ANDROID_ABI} STREQUAL "arm64-v8a") + set(WHISPER_LIBRARY_NAME whisper_v8fp16_va) +elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a") + set(WHISPER_LIBRARY_NAME whisper_vfpv4) +endif () + +add_library( + ${WHISPER_LIBRARY_NAME} + SHARED + ${SOURCE_FILES} +) + +if (${ANDROID_ABI} STREQUAL "arm64-v8a") + target_compile_options(${WHISPER_LIBRARY_NAME} PRIVATE -march=armv8.2-a+fp16) +elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a") + target_compile_options(${WHISPER_LIBRARY_NAME} PRIVATE -mfpu=neon-vfpv4) +endif () + + +target_link_libraries(${WHISPER_LIBRARY_NAME} log android) +include_directories(${WHISPER_LIB_DIR}) + +if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug") + target_compile_options(${WHISPER_LIBRARY_NAME} PRIVATE -O3) + target_compile_options(${WHISPER_LIBRARY_NAME} PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden) + target_compile_options(${WHISPER_LIBRARY_NAME} PRIVATE -ffunction-sections -fdata-sections) + target_link_options(${WHISPER_LIBRARY_NAME} PRIVATE -Wl,--gc-sections) + target_link_options(${WHISPER_LIBRARY_NAME} PRIVATE -Wl,--exclude-libs,ALL) + target_link_options(${WHISPER_LIBRARY_NAME} PRIVATE -flto) +endif () diff --git a/examples/whisper.android/app/src/main/jni/whisper/Whisper.mk b/examples/whisper.android/app/src/main/jni/whisper/Whisper.mk deleted file mode 100644 index 6d9113b..0000000 --- a/examples/whisper.android/app/src/main/jni/whisper/Whisper.mk +++ /dev/null @@ -1,18 +0,0 @@ -WHISPER_LIB_DIR := $(LOCAL_PATH)/../../../../../../../ -LOCAL_LDLIBS := -landroid -llog - -# Make the final output library smaller by only keeping the symbols referenced from the app. -ifneq ($(APP_OPTIM),debug) - LOCAL_CFLAGS += -O3 - LOCAL_CFLAGS += -fvisibility=hidden -fvisibility-inlines-hidden - LOCAL_CFLAGS += -ffunction-sections -fdata-sections - LOCAL_LDFLAGS += -Wl,--gc-sections - LOCAL_LDFLAGS += -Wl,--exclude-libs,ALL - LOCAL_LDFLAGS += -flto -endif - -LOCAL_CFLAGS += -DSTDC_HEADERS -std=c11 -I $(WHISPER_LIB_DIR) -LOCAL_CPPFLAGS += -std=c++11 -LOCAL_SRC_FILES := $(WHISPER_LIB_DIR)/ggml.c \ - $(WHISPER_LIB_DIR)/whisper.cpp \ - $(LOCAL_PATH)/jni.c \ No newline at end of file