|
1 | | -cmake_minimum_required(VERSION 3.10) |
| 1 | +cmake_minimum_required(VERSION 3.13) |
2 | 2 | project(RnExecutorch) |
3 | 3 |
|
4 | 4 | set(CMAKE_VERBOSE_MAKEFILE ON) |
5 | 5 | set(CMAKE_CXX_STANDARD 20) |
6 | 6 |
|
7 | | -set(EXEC_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../third-party/android/jniLibs/${ANDROID_ABI}/libexecutorch.so") |
8 | | -set(OPENCV_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../third-party/android/jniLibs/${ANDROID_ABI}/libopencv_java4.so") |
| 7 | +set(THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party") |
| 8 | +set(LIBS_DIR "${THIRD_PARTY_DIR}/android/libs") |
| 9 | +set(INCLUDE_DIR "${THIRD_PARTY_DIR}/include") |
| 10 | +set(CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../cpp") |
9 | 11 |
|
10 | | -add_library(executorch SHARED IMPORTED) |
11 | | -set_target_properties(executorch PROPERTIES IMPORTED_LOCATION ${EXEC_LIB_PATH}) |
12 | | - |
13 | | -add_library(opencv SHARED IMPORTED) |
14 | | -set_target_properties(opencv PROPERTIES IMPORTED_LOCATION ${OPENCV_LIB_PATH}) |
| 12 | +# Feature flags forwarded from build.gradle.kts (which reads rne-build-config.json |
| 13 | +# written by scripts/download-libs.js). Default ON so a manual/full provisioning |
| 14 | +# still builds everything. |
| 15 | +option(RNE_ENABLE_OPENCV "Compile + link OpenCV-dependent sources" ON) |
| 16 | +option(RNE_ENABLE_PHONEMIS "Compile phonemis (TTS) sources" ON) |
| 17 | +option(RNE_ENABLE_XNNPACK "Link the XNNPACK backend shared library" ON) |
| 18 | +option(RNE_ENABLE_VULKAN "Link the Vulkan backend shared library" ON) |
15 | 19 |
|
16 | 20 | find_package(ReactAndroid REQUIRED CONFIG) |
17 | 21 |
|
18 | | -file(GLOB CORE_SOURCES ../cpp/core/*.cpp) |
19 | | -file(GLOB MATH_SOURCES ../cpp/extensions/math/*.cpp) |
20 | | -file(GLOB CV_SOURCES ../cpp/extensions/cv/*.cpp) |
21 | | -file(GLOB NLP_SOURCES ../cpp/extensions/nlp/*.cpp) |
| 22 | +# --- Source groups --- |
| 23 | +# core/math/nlp are backend-agnostic and always compiled. OPENCV_SOURCES is the |
| 24 | +# explicit, extensible list of opencv-dependent sources: when multimodal LLM |
| 25 | +# lands in nlp, its vision-encoder sources should be appended here too. |
| 26 | +file(GLOB CORE_SOURCES ${CPP_DIR}/core/*.cpp) |
| 27 | +file(GLOB MATH_SOURCES ${CPP_DIR}/extensions/math/*.cpp) |
| 28 | +file(GLOB NLP_SOURCES ${CPP_DIR}/extensions/nlp/*.cpp) |
| 29 | +file(GLOB OPENCV_SOURCES ${CPP_DIR}/extensions/cv/*.cpp) |
22 | 30 |
|
23 | | -add_library(${CMAKE_PROJECT_NAME} SHARED |
24 | | - ../cpp/RnExecutorch.cpp |
| 31 | +set(RNE_SOURCES |
| 32 | + ${CPP_DIR}/RnExecutorch.cpp |
25 | 33 | ${CORE_SOURCES} |
26 | | - ${CV_SOURCES} |
27 | 34 | ${MATH_SOURCES} |
28 | 35 | ${NLP_SOURCES} |
29 | 36 | cpp-adapter.cpp |
30 | 37 | ) |
31 | 38 |
|
| 39 | +if(RNE_ENABLE_OPENCV) |
| 40 | + list(APPEND RNE_SOURCES ${OPENCV_SOURCES}) |
| 41 | +endif() |
| 42 | + |
| 43 | +add_library(${CMAKE_PROJECT_NAME} SHARED ${RNE_SOURCES}) |
| 44 | + |
32 | 45 | target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE |
33 | 46 | C10_USING_CUSTOM_GENERATED_MACROS=1 |
34 | 47 | EXECUTORCH_ENABLE_EXECUTION_PROFILING=1 |
35 | 48 | ) |
36 | 49 |
|
| 50 | +# Propagate feature flags as preprocessor defines so C++ can guard includes/calls. |
| 51 | +if(RNE_ENABLE_OPENCV) |
| 52 | + target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE RNE_ENABLE_OPENCV) |
| 53 | +endif() |
| 54 | +if(RNE_ENABLE_PHONEMIS) |
| 55 | + target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE RNE_ENABLE_PHONEMIS) |
| 56 | +endif() |
| 57 | + |
37 | 58 | target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE |
38 | | - ../cpp |
39 | | - ${CMAKE_CURRENT_SOURCE_DIR}/../third-party/include |
40 | | - ${CMAKE_CURRENT_SOURCE_DIR}/../third-party/include/executorch/extension/llm/tokenizers/include |
41 | | - ${CMAKE_CURRENT_SOURCE_DIR}/../third-party/include/executorch/extension/llm/tokenizers/third-party/json/include |
42 | | - ${CMAKE_CURRENT_SOURCE_DIR}/../third-party/include/executorch/extension/llm/tokenizers/third-party/re2 |
43 | | - ${CMAKE_CURRENT_SOURCE_DIR}/../third-party/include/executorch/extension/llm/tokenizers/third-party/abseil-cpp |
| 59 | + ${CPP_DIR} |
| 60 | + ${INCLUDE_DIR} |
| 61 | + ${INCLUDE_DIR}/executorch/extension/llm/tokenizers/include |
| 62 | + ${INCLUDE_DIR}/executorch/extension/llm/tokenizers/third-party/json/include |
| 63 | + ${INCLUDE_DIR}/executorch/extension/llm/tokenizers/third-party/re2 |
| 64 | + ${INCLUDE_DIR}/executorch/extension/llm/tokenizers/third-party/abseil-cpp |
44 | 65 | ) |
45 | 66 |
|
46 | | -# Ensure executorch is linked with WHOLE_ARCHIVE so all static initializers are included |
47 | | -target_link_options(${CMAKE_PROJECT_NAME} PRIVATE -Wl,--whole-archive) |
48 | | -target_link_libraries(${CMAKE_PROJECT_NAME} executorch) |
49 | | -target_link_options(${CMAKE_PROJECT_NAME} PRIVATE -Wl,--no-whole-archive) |
| 67 | +# ------- ExecuTorch core (always required) ------- |
| 68 | +add_library(executorch SHARED IMPORTED) |
| 69 | +set_target_properties(executorch PROPERTIES |
| 70 | + IMPORTED_LOCATION "${LIBS_DIR}/executorch/${ANDROID_ABI}/libexecutorch.so") |
| 71 | + |
| 72 | +# ------- XNNPACK backend (optional) ------- |
| 73 | +# Backends ship as separate .so files. When enabled, CMake imports the .so and |
| 74 | +# links libreact-native-executorch.so against it; Gradle packages the .so into |
| 75 | +# the APK (via jniLibs srcDir) and the dynamic linker loads it whenever |
| 76 | +# libreact-native-executorch.so loads. Each backend .so's load-time constructor |
| 77 | +# registers itself with libexecutorch.so's backend registry. |
| 78 | +if(RNE_ENABLE_XNNPACK) |
| 79 | + add_library(xnnpack_executorch_backend SHARED IMPORTED) |
| 80 | + set_target_properties(xnnpack_executorch_backend PROPERTIES |
| 81 | + IMPORTED_LOCATION "${LIBS_DIR}/executorch/${ANDROID_ABI}/libxnnpack_executorch_backend.so") |
| 82 | +endif() |
| 83 | + |
| 84 | +# ------- Vulkan backend (optional) ------- |
| 85 | +if(RNE_ENABLE_VULKAN) |
| 86 | + add_library(vulkan_executorch_backend SHARED IMPORTED) |
| 87 | + set_target_properties(vulkan_executorch_backend PROPERTIES |
| 88 | + IMPORTED_LOCATION "${LIBS_DIR}/executorch/${ANDROID_ABI}/libvulkan_executorch_backend.so") |
| 89 | +endif() |
| 90 | + |
| 91 | +# ------- OpenCV (optional, static) ------- |
| 92 | +set(OPENCV_LINK_LIBS "") |
| 93 | +if(RNE_ENABLE_OPENCV) |
| 94 | + set(OPENCV_LINK_LIBS |
| 95 | + "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_core.a" |
| 96 | + "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_features2d.a" |
| 97 | + "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_highgui.a" |
| 98 | + "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_imgproc.a" |
| 99 | + "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_photo.a" |
| 100 | + "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_video.a" |
| 101 | + ) |
| 102 | + if(ANDROID_ABI STREQUAL "arm64-v8a") |
| 103 | + list(APPEND OPENCV_LINK_LIBS |
| 104 | + "${LIBS_DIR}/opencv-third-party/${ANDROID_ABI}/libkleidicv_hal.a" |
| 105 | + "${LIBS_DIR}/opencv-third-party/${ANDROID_ABI}/libkleidicv_thread.a" |
| 106 | + "${LIBS_DIR}/opencv-third-party/${ANDROID_ABI}/libkleidicv.a" |
| 107 | + ) |
| 108 | + endif() |
| 109 | +endif() |
50 | 110 |
|
51 | 111 | target_link_libraries(${CMAKE_PROJECT_NAME} |
52 | 112 | ReactAndroid::jsi |
53 | 113 | log |
54 | | - opencv |
| 114 | + ${OPENCV_LINK_LIBS} |
| 115 | + executorch |
| 116 | + z |
55 | 117 | ) |
| 118 | + |
| 119 | +# Linking against the backend .so (when enabled) makes Gradle bundle them and |
| 120 | +# instructs the dynamic linker to load them with libreact-native-executorch.so. |
| 121 | +if(RNE_ENABLE_XNNPACK) |
| 122 | + target_link_libraries(${CMAKE_PROJECT_NAME} xnnpack_executorch_backend) |
| 123 | +endif() |
| 124 | +if(RNE_ENABLE_VULKAN) |
| 125 | + target_link_libraries(${CMAKE_PROJECT_NAME} vulkan_executorch_backend) |
| 126 | +endif() |
0 commit comments