xref: /aosp_15_r20/external/executorch/extension/android/CMakeLists.txt (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the BSD-style license found in the
5# LICENSE file in the root directory of this source tree.
6
7cmake_minimum_required(VERSION 3.19)
8
9project(executorch_jni)
10
11if(NOT CMAKE_CXX_STANDARD)
12  set(CMAKE_CXX_STANDARD 17)
13endif()
14
15if(NOT ANDROID)
16  message(FATAL_ERROR "This directory is for Android build only")
17endif()
18
19set(EXECUTORCH_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../..")
20include(${EXECUTORCH_ROOT}/build/Utils.cmake)
21set(_common_compile_options -Wno-deprecated-declarations -fPIC)
22set(_common_include_directories ${EXECUTORCH_ROOT}/..)
23if(NOT ANDROID_PLATFORM)
24  set(ANDROID_PLATFORM android-30)
25endif()
26
27# We need to download fbjni library from maven, and use its "prefab" library
28# and headers, and link executorch library against that fbjni library.
29# We don't know which NDK is used to compile fbjni, and we need to link our
30# executorch library to the version which Android APK links against for runtime
31# to ensure the libc++ dependencies are consistent.
32# WARNING #
33# Users need to use the SAME fbjni version here and in app gradle dependency
34# for runtime compatibility!
35if(NOT FBJNI_VERSION)
36  set(FBJNI_VERSION 0.5.1)
37endif()
38
39set(FBJNI_AAR_URL https://repo1.maven.org/maven2/com/facebook/fbjni/fbjni/${FBJNI_VERSION}/fbjni-${FBJNI_VERSION}.aar)
40set(FBJNI_DOWNLOAD_PATH ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/fbjni.aar)
41
42if(NOT EXISTS "${FBJNI_DOWNLOAD_PATH}")
43  file(DOWNLOAD "${FBJNI_AAR_URL}" "${FBJNI_DOWNLOAD_PATH}")
44endif()
45
46add_custom_command(
47  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/" "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
48  COMMAND unzip -o ${FBJNI_DOWNLOAD_PATH} -d ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni
49  DEPENDS "${FBJNI_DOWNLOAD_PATH}"
50)
51
52add_custom_target(
53  fbjni_prefab
54  DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/" "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
55)
56
57add_library(fbjni SHARED IMPORTED)
58add_dependencies(fbjni fbjni_prefab)
59set_target_properties(fbjni PROPERTIES
60  IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
61)
62
63set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../lib/cmake/ExecuTorch)
64find_package(executorch CONFIG REQUIRED)
65target_link_options_shared_lib(executorch)
66
67add_library(executorch_jni SHARED jni/jni_layer.cpp jni/log.cpp)
68
69set(link_libraries)
70list(
71  APPEND
72  link_libraries
73  executorch
74  extension_data_loader
75  extension_module
76  extension_runner_util
77  extension_tensor
78  extension_threadpool
79  fbjni
80)
81
82if(TARGET optimized_native_cpu_ops_lib)
83  list(
84    APPEND
85    link_libraries
86    optimized_native_cpu_ops_lib
87    optimized_kernels
88    portable_kernels
89    cpublas
90    eigen_blas
91  )
92  target_link_options_shared_lib(optimized_native_cpu_ops_lib)
93else()
94  list(APPEND link_libraries portable_ops_lib portable_kernels)
95  target_link_options_shared_lib(portable_ops_lib)
96endif()
97
98if(TARGET quantized_kernels)
99  list(APPEND link_libraries quantized_kernels quantized_ops_lib)
100  target_link_options_shared_lib(quantized_ops_lib)
101endif()
102
103if(TARGET qnn_executorch_backend)
104  list(APPEND link_libraries qnn_executorch_backend)
105endif()
106
107if(TARGET xnnpack_backend)
108  target_link_options_shared_lib(xnnpack_backend)
109  list(APPEND link_libraries xnnpack_backend XNNPACK pthreadpool cpuinfo microkernels-prod)
110endif()
111
112if(TARGET vulkan_backend)
113  target_link_options_shared_lib(vulkan_backend)
114  list(APPEND link_libraries vulkan_backend)
115endif()
116
117if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
118  add_subdirectory(
119    ${EXECUTORCH_ROOT}/extension/llm/custom_ops
120    ${CMAKE_CURRENT_BINARY_DIR}/../../extension/llm/custom_ops
121  )
122  list(APPEND link_libraries custom_ops)
123  target_link_options_shared_lib(custom_ops)
124endif()
125
126if(TARGET pthreadpool)
127  target_compile_definitions(executorch_jni PRIVATE ET_USE_THREADPOOL=1)
128  target_include_directories(
129    executorch_jni
130    PUBLIC
131      ${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/cpuinfo/include
132  )
133  target_include_directories(
134    executorch_jni
135    PUBLIC
136      ${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/pthreadpool/include
137  )
138endif()
139
140if(EXECUTORCH_JNI_CUSTOM_LIBRARY)
141  list(APPEND link_libraries ${EXECUTORCH_JNI_CUSTOM_LIBRARY})
142  target_link_libraries(
143    executorch_jni -Wl,--whole-archive ${EXECUTORCH_JNI_CUSTOM_LIBRARY}
144    -Wl,--no-whole-archive
145  )
146endif()
147
148if(EXECUTORCH_BUILD_LLAMA_JNI)
149  target_sources(executorch_jni PRIVATE jni/jni_layer_llama.cpp jni/log.cpp)
150  list(APPEND link_libraries llama_runner llava_runner)
151  target_compile_definitions(executorch_jni PUBLIC EXECUTORCH_BUILD_LLAMA_JNI=1)
152  add_subdirectory(
153    ${EXECUTORCH_ROOT}/examples/models/llava/runner
154    ${CMAKE_CURRENT_BINARY_DIR}/../../examples/models/llava/runner
155  )
156
157  add_subdirectory(
158    ${EXECUTORCH_ROOT}/examples/models/llama/runner
159    ${CMAKE_CURRENT_BINARY_DIR}/../../examples/models/llama/runner
160  )
161
162  if(NEURON_BUFFER_ALLOCATOR_LIB)
163      target_sources(
164      executorch_jni PRIVATE
165      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/mtk_llama_runner.cpp
166      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/LlamaModelChunk.cpp
167      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/LlamaRuntime.cpp
168      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/ModelChunk.cpp
169      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/MultiModelLoader.cpp
170      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/mask_builder.cpp
171      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/rotary_embedding.cpp
172      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/token_embedding.cpp
173    )
174    target_include_directories(
175      executorch_jni PRIVATE
176      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/
177      ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner
178    )
179    add_library(libneuron_buffer_allocator SHARED IMPORTED)
180    set_property(TARGET libneuron_buffer_allocator PROPERTY IMPORTED_LOCATION ${NEURON_BUFFER_ALLOCATOR_LIB})
181    list(APPEND link_libraries neuron_backend libneuron_buffer_allocator)
182    target_compile_definitions(executorch_jni PRIVATE EXECUTORCH_BUILD_MEDIATEK=1)
183  endif()
184endif()
185
186target_include_directories(
187  executorch_jni PRIVATE ${_common_include_directories}
188  "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
189)
190
191target_compile_options(executorch_jni PUBLIC ${_common_compile_options})
192
193target_link_libraries(executorch_jni ${link_libraries} log)
194