Skip to content

Commit

Permalink
fix(android): multi-targets & x86 / x86_64 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Aug 30, 2023
1 parent 96a5f16 commit 788567b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
11 changes: 9 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["RNWhisper_" + name]).toInteger()
}

def reactNativeArchitectures() {
def value = project.getProperties().get("reactNativeArchitectures")
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

android {
ndkVersion getExtOrDefault("ndkVersion")
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
Expand All @@ -38,8 +43,10 @@ android {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
ndk {
abiFilters 'arm64-v8a', 'armeabi-v7a'
externalNativeBuild {
cmake {
abiFilters (*reactNativeArchitectures())
}
}
}
externalNativeBuild {
Expand Down
59 changes: 32 additions & 27 deletions android/src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,43 @@ set(
${CMAKE_SOURCE_DIR}/jni.cpp
)

if (${ANDROID_ABI} STREQUAL "arm64-v8a")
set(RNWHISPER_LIBRARY_NAME whisper_v8fp16_va)
elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a")
set(RNWHISPER_LIBRARY_NAME whisper_vfpv4)
endif ()
find_library(LOG_LIB log)

add_library(
${RNWHISPER_LIBRARY_NAME}
SHARED
${SOURCE_FILES}
)
function(build_library target_name)
add_library(
${target_name}
SHARED
${SOURCE_FILES}
)

target_link_libraries(${target_name} ${LOG_LIB} android)

if (${ANDROID_ABI} STREQUAL "arm64-v8a")
target_compile_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -march=armv8.2-a+fp16)
elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a")
target_compile_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -mfpu=neon-vfpv4)
endif ()
if (${ANDROID_ABI} STREQUAL "arm64-v8a")
target_compile_options(${target_name} PRIVATE -march=armv8.2-a+fp16)
elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a")
target_compile_options(${target_name} PRIVATE -mfpu=neon-vfpv4)
endif ()

target_link_libraries(${RNWHISPER_LIBRARY_NAME} log android)
include_directories(${RNWHISPER_LIB_DIR})
# NOTE: If you want to debug the native code, you can uncomment if and endif
# if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")

target_compile_options(${target_name} PRIVATE -O3 -DNDEBUG -pthread)
target_compile_options(${target_name} PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
target_compile_options(${target_name} PRIVATE -ffunction-sections -fdata-sections)

target_compile_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -DLM_GGML_USE_K_QUANTS -pthread)
target_link_options(${target_name} PRIVATE -Wl,--gc-sections)
target_link_options(${target_name} PRIVATE -Wl,--exclude-libs,ALL)
target_link_options(${target_name} PRIVATE -flto)

# NOTE: If you want to debug the native code, you can uncomment if and endif
# if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
# endif ()
endfunction()

target_compile_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -O3 -DNDEBUG)
target_compile_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
target_compile_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -ffunction-sections -fdata-sections)
build_library("whisper") # Default target

target_link_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -Wl,--gc-sections)
target_link_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -Wl,--exclude-libs,ALL)
target_link_options(${RNWHISPER_LIBRARY_NAME} PRIVATE -flto)
if (${ANDROID_ABI} STREQUAL "arm64-v8a")
build_library("whisper_v8fp16_va")
elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a")
build_library("whisper_vfpv4")
endif ()

# endif ()
include_directories(${RNWHISPER_LIB_DIR})

0 comments on commit 788567b

Please sign in to comment.