-
Notifications
You must be signed in to change notification settings - Fork 957
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
251 lines (218 loc) · 8.41 KB
/
CMakeLists.txt
File metadata and controls
251 lines (218 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
cmake_minimum_required(VERSION 3.24) # 3.24 is required for WHOLE_ARCHIVE
project(executorch_jni)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
if(NOT ANDROID)
message(FATAL_ERROR "This directory is for Android build only")
endif()
set(EXECUTORCH_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../..")
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
set(_common_compile_options
$<$<CXX_COMPILER_ID:MSVC>:/wd4996>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations -fPIC>
)
if(NOT ANDROID_PLATFORM)
set(ANDROID_PLATFORM android-30)
endif()
# We need to download fbjni library from maven, and use its "prefab" library and
# headers, and link executorch library against that fbjni library. We don't know
# which NDK is used to compile fbjni, and we need to link our executorch library
# to the version which Android APK links against for runtime to ensure the
# libc++ dependencies are consistent. WARNING # Users need to use the SAME fbjni
# version here and in app gradle dependency for runtime compatibility!
if(NOT FBJNI_VERSION)
set(FBJNI_VERSION 0.7.0)
endif()
set(FBJNI_AAR_URL
https://repo1.maven.org/maven2/com/facebook/fbjni/fbjni/${FBJNI_VERSION}/fbjni-${FBJNI_VERSION}.aar
)
set(FBJNI_DOWNLOAD_PATH ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/fbjni.aar)
if(NOT EXISTS "${FBJNI_DOWNLOAD_PATH}")
file(DOWNLOAD "${FBJNI_AAR_URL}" "${FBJNI_DOWNLOAD_PATH}")
endif()
add_custom_command(
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"
COMMAND unzip -o ${FBJNI_DOWNLOAD_PATH} -d
${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni
DEPENDS "${FBJNI_DOWNLOAD_PATH}"
)
add_custom_target(
fbjni_prefab
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"
)
add_library(fbjni SHARED IMPORTED)
add_dependencies(fbjni fbjni_prefab)
set_target_properties(
fbjni
PROPERTIES
IMPORTED_LOCATION
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
)
executorch_target_link_options_shared_lib(executorch)
add_library(
executorch_jni SHARED jni/jni_layer.cpp jni/log.cpp jni/jni_layer_runtime.cpp
jni/jni_helper.cpp
)
set(link_libraries)
list(
APPEND
link_libraries
executorch
extension_data_loader
extension_flat_tensor
extension_module
extension_runner_util
extension_tensor
extension_threadpool
fbjni
)
if(EXECUTORCH_ANDROID_PROFILING)
list(APPEND link_libraries etdump flatccrt)
target_compile_definitions(
executorch_jni PUBLIC EXECUTORCH_ANDROID_PROFILING=1
)
endif()
if(TARGET optimized_native_cpu_ops_lib)
list(APPEND link_libraries optimized_native_cpu_ops_lib)
executorch_target_link_options_shared_lib(optimized_native_cpu_ops_lib)
else()
list(APPEND link_libraries portable_ops_lib portable_kernels)
executorch_target_link_options_shared_lib(portable_ops_lib)
endif()
if(TARGET quantized_kernels)
list(APPEND link_libraries quantized_kernels quantized_ops_lib)
executorch_target_link_options_shared_lib(quantized_ops_lib)
endif()
if(TARGET qnn_executorch_backend)
list(APPEND link_libraries qnn_executorch_backend)
endif()
if(TARGET xnnpack_backend)
executorch_target_link_options_shared_lib(xnnpack_backend)
list(
APPEND
link_libraries
xnnpack_backend
XNNPACK
pthreadpool
cpuinfo
xnnpack-microkernels-prod
)
if(TARGET kleidiai)
list(APPEND link_libraries kleidiai)
endif()
endif()
if(TARGET vulkan_backend)
executorch_target_link_options_shared_lib(vulkan_backend)
list(APPEND link_libraries vulkan_backend)
endif()
if(EXECUTORCH_BUILD_KERNELS_LLM)
list(APPEND link_libraries $<LINK_LIBRARY:WHOLE_ARCHIVE,custom_ops>)
endif()
if(TARGET pthreadpool)
target_include_directories(
executorch_jni
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/cpuinfo/include
)
target_include_directories(
executorch_jni
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/pthreadpool/include
)
endif()
if(EXECUTORCH_JNI_CUSTOM_LIBRARY)
list(APPEND link_libraries ${EXECUTORCH_JNI_CUSTOM_LIBRARY})
target_link_libraries(
executorch_jni -Wl,--whole-archive ${EXECUTORCH_JNI_CUSTOM_LIBRARY}
-Wl,--no-whole-archive
)
endif()
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
target_sources(executorch_jni PRIVATE jni/jni_layer_training.cpp)
list(APPEND link_libraries extension_training)
target_compile_definitions(
executorch_jni PUBLIC EXECUTORCH_BUILD_EXTENSION_TRAINING=1
)
endif()
if(EXECUTORCH_BUILD_LLAMA_JNI)
target_sources(
executorch_jni PRIVATE jni/jni_layer_llama.cpp jni/jni_layer_asr.cpp
)
list(APPEND link_libraries extension_llm_runner extension_asr_runner)
target_compile_definitions(executorch_jni PUBLIC EXECUTORCH_BUILD_LLAMA_JNI=1)
if(QNN_SDK_ROOT)
target_sources(
executorch_jni
PRIVATE
${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner/runner.cpp
${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner/decoder_runner.cpp
${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner/prompt_processor.cpp
${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner/token_generator.cpp
${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner/lhd_token_generator.cpp
${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner/attention_sink_rope_runner.cpp
${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner/rpc_mem.cpp
${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner/kv_manager.cpp
)
target_include_directories(
executorch_jni
PRIVATE ${EXECUTORCH_ROOT}/examples/qualcomm/oss_scripts/llama/runner
)
target_compile_definitions(executorch_jni PRIVATE EXECUTORCH_BUILD_QNN=1)
endif()
if(NEURON_BUFFER_ALLOCATOR_LIB)
target_sources(
executorch_jni
PRIVATE
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/mtk_llama_runner.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/LlamaModelChunk.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/LlamaRuntime.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/ModelChunk.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/MultiModelLoader.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/mask_builder.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/rotary_embedding.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/token_embedding.cpp
)
target_include_directories(
executorch_jni
PRIVATE ${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner
)
add_library(libneuron_buffer_allocator SHARED IMPORTED)
set_property(
TARGET libneuron_buffer_allocator PROPERTY IMPORTED_LOCATION
${NEURON_BUFFER_ALLOCATOR_LIB}
)
list(APPEND link_libraries neuron_backend libneuron_buffer_allocator)
target_compile_definitions(
executorch_jni PRIVATE EXECUTORCH_BUILD_MEDIATEK=1
)
endif()
endif()
# Hook for extended build targets (e.g., internal or partner-specific JNI
# extensions). This is a no-op when extended_targets/ does not exist.
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/fb/extended_targets/CMakeLists.txt")
include("${CMAKE_CURRENT_SOURCE_DIR}/fb/extended_targets/CMakeLists.txt")
endif()
target_include_directories(
executorch_jni
PRIVATE
${_common_include_directories}
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
)
target_compile_options(executorch_jni PUBLIC ${_common_compile_options})
target_link_options(
executorch_jni PRIVATE
"LINKER:--version-script,${CMAKE_CURRENT_SOURCE_DIR}/jni/version_script.txt"
)
target_link_options_gc_sections(executorch_jni)
target_link_libraries(executorch_jni ${link_libraries} log)