Skip to content

Commit a9f3f81

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Android custom lib (#5501)
Summary: 1. Clean up the file to group all deps libs together. 2. Add an option `EXECUTORCH_JNI_CUSTOM_LIBRARY` for user to pass in their own library. Example: ``` echo "int my_function() { return 0; }" > my_source.cpp echo "int my_function2() { return 0; }" > my_source2.cpp ${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android27-clang -c my_source.cpp -o file1.o ${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android27-clang -c my_source2.cpp -o file2.o ar rcs libmystaticlib.a file1.o ar rcs libmystaticlib2.a file2.o rm my_source.cpp my_source2.cpp file1.o file2.o cmake extension/android \ -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \ -DANDROID_ABI=arm64-v8a \ -DANDROID_PLATFORM=android-23 \ -DCMAKE_INSTALL_PREFIX=cmake-out-android-arm64-v8a \ -DEXECUTORCH_ENABLE_LOGGING=ON \ -DEXECUTORCH_LOG_LEVEL=Info \ -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \ -DEXECUTORCH_BUILD_LLAMA_JNI=ON \ -DEXECUTORCH_JNI_CUSTOM_LIBRARY="/home/hsz/executorch/libmystaticlib.a;/home/hsz/executorch/libmystaticlib2.a" \ -DCMAKE_BUILD_TYPE=Release \ -Bcmake-out-android-arm64-v8a/extension/android cmake --build cmake-out-android-arm64-v8a/extension/android -j 10 --config Release nm -gDC ./cmake-out-android-arm64-v8a/extension/android/libexecutorch_jni.so | grep my_function ``` Pull Request resolved: #5501 Reviewed By: shoumikhin Differential Revision: D63057835 Pulled By: kirklandsign fbshipit-source-id: 000e493568db878c50ba85f72ad1ec3a48182a9f
1 parent 3512148 commit a9f3f81

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

extension/android/CMakeLists.txt

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../lib/cmake/ExecuTorch)
3030
find_package(executorch CONFIG REQUIRED)
3131
target_link_options_shared_lib(executorch)
3232

33+
add_library(executorch_jni SHARED jni/jni_layer.cpp)
34+
3335
set(link_libraries)
3436
list(
3537
APPEND
@@ -58,13 +60,21 @@ else()
5860
list(APPEND link_libraries portable_ops_lib portable_kernels)
5961
target_link_options_shared_lib(portable_ops_lib)
6062
endif()
63+
64+
if(TARGET quantized_kernels)
65+
list(APPEND link_libraries quantized_kernels quantized_ops_lib)
66+
target_link_options_shared_lib(quantized_ops_lib)
67+
endif()
68+
6169
if(TARGET qnn_executorch_backend)
6270
list(APPEND link_libraries qnn_executorch_backend)
6371
endif()
72+
6473
if(TARGET xnnpack_backend)
6574
target_link_options_shared_lib(xnnpack_backend)
6675
list(APPEND link_libraries xnnpack_backend XNNPACK pthreadpool cpuinfo)
6776
endif()
77+
6878
if(TARGET vulkan_backend)
6979
target_link_options_shared_lib(vulkan_backend)
7080
list(APPEND link_libraries vulkan_backend)
@@ -79,7 +89,27 @@ if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
7989
target_link_options_shared_lib(custom_ops)
8090
endif()
8191

82-
add_library(executorch_jni SHARED jni/jni_layer.cpp)
92+
if(TARGET pthreadpool)
93+
target_compile_definitions(executorch_jni PRIVATE ET_USE_THREADPOOL=1)
94+
target_include_directories(
95+
executorch_jni
96+
PUBLIC
97+
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/cpuinfo/include
98+
)
99+
target_include_directories(
100+
executorch_jni
101+
PUBLIC
102+
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/pthreadpool/include
103+
)
104+
endif()
105+
106+
if(EXECUTORCH_JNI_CUSTOM_LIBRARY)
107+
list(APPEND link_libraries ${EXECUTORCH_JNI_CUSTOM_LIBRARY})
108+
target_link_libraries(
109+
executorch_jni -Wl,--whole-archive ${EXECUTORCH_JNI_CUSTOM_LIBRARY}
110+
-Wl,--no-whole-archive
111+
)
112+
endif()
83113

84114
if(EXECUTORCH_BUILD_LLAMA_JNI)
85115
target_sources(executorch_jni PRIVATE jni/jni_layer_llama.cpp)
@@ -96,29 +126,10 @@ if(EXECUTORCH_BUILD_LLAMA_JNI)
96126
)
97127
endif()
98128

99-
if(TARGET quantized_kernels)
100-
list(APPEND link_libraries quantized_kernels quantized_ops_lib)
101-
target_link_options_shared_lib(quantized_ops_lib)
102-
endif()
103-
104129
target_include_directories(
105130
executorch_jni PRIVATE ${_common_include_directories}
106131
)
107132

108133
target_compile_options(executorch_jni PUBLIC ${_common_compile_options})
109134

110135
target_link_libraries(executorch_jni ${link_libraries})
111-
112-
if(TARGET pthreadpool)
113-
target_compile_definitions(executorch_jni PRIVATE ET_USE_THREADPOOL=1)
114-
target_include_directories(
115-
executorch_jni
116-
PUBLIC
117-
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/cpuinfo/include
118-
)
119-
target_include_directories(
120-
executorch_jni
121-
PUBLIC
122-
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/pthreadpool/include
123-
)
124-
endif()

0 commit comments

Comments
 (0)