Skip to content

Refactor JNI targets to allow selective build #11005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions extension/android/jni/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@fbsource//tools/build_defs/android:fb_android_cxx_library.bzl", "fb_andro
load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs.bzl", "third_party_dep")
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
load("@fbsource//xplat/executorch/codegen:codegen.bzl", "executorch_generated_lib")
load(":build_defs.bzl", "ET_JNI_COMPILER_FLAGS")

oncall("executorch")

Expand All @@ -28,16 +29,12 @@ non_fbcode_target(_kind = executorch_generated_lib,
non_fbcode_target(_kind = fb_android_cxx_library,
name = "executorch_jni",
srcs = ["jni_layer.cpp", "log.cpp"],
headers = ["jni_layer_constants.h"],
allow_jni_merging = False,
compiler_flags = [
"-frtti",
"-fexceptions",
"-Wno-unused-variable",
],
compiler_flags = ET_JNI_COMPILER_FLAGS,
soname = "libexecutorch.$(ext)",
visibility = ["PUBLIC"],
deps = [
":jni_headers",
":log_provider_static",
"//fbandroid/libraries/fbjni:fbjni",
"//fbandroid/native/fb:fb",
Expand All @@ -53,16 +50,12 @@ non_fbcode_target(_kind = fb_android_cxx_library,
non_fbcode_target(_kind = fb_android_cxx_library,
name = "executorch_jni_full",
srcs = ["jni_layer.cpp", "log.cpp"],
headers = ["jni_layer_constants.h"],
allow_jni_merging = False,
compiler_flags = [
"-frtti",
"-fexceptions",
"-Wno-unused-variable",
],
compiler_flags = ET_JNI_COMPILER_FLAGS,
soname = "libexecutorch.$(ext)",
visibility = ["PUBLIC"],
deps = [
":jni_headers",
":log_provider_static",
":generated_op_lib_optimized_static",
"//fbandroid/libraries/fbjni:fbjni",
Expand All @@ -82,17 +75,14 @@ non_fbcode_target(_kind = fb_android_cxx_library,
"jni_layer.cpp",
"jni_layer_llama.cpp",
],
headers = ["jni_layer_constants.h"],
allow_jni_merging = False,
compiler_flags = [
"-frtti",
"-fexceptions",
compiler_flags = ET_JNI_COMPILER_FLAGS + [
"-DEXECUTORCH_BUILD_LLAMA_JNI",
"-Wno-format",
],
soname = "libexecutorch.$(ext)",
visibility = ["PUBLIC"],
deps = [
":jni_headers",
":log_provider_static",
"//fbandroid/libraries/fbjni:fbjni",
"//fbandroid/native/fb:fb",
Expand All @@ -112,13 +102,20 @@ non_fbcode_target(_kind = runtime.cxx_library,
name = "log_provider",
srcs = ["log.cpp"],
exported_headers = ["log.h"],
compiler_flags = [
"-frtti",
"-fexceptions",
"-Wno-unused-variable",
],
compiler_flags = ET_JNI_COMPILER_FLAGS,
deps = [
"//executorch/runtime/core:core",
],
visibility = ["@EXECUTORCH_CLIENTS"],
)

runtime.export_file(
name = "jni_layer.cpp",
)

runtime.cxx_library(
name = "jni_headers",
exported_headers = [
"jni_layer_constants.h",
]
)
5 changes: 5 additions & 0 deletions extension/android/jni/build_defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ET_JNI_COMPILER_FLAGS = [
"-frtti",
"-fexceptions",
"-Wno-unused-variable",
]
19 changes: 9 additions & 10 deletions extension/android/jni/jni_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/extension/android/jni/jni_layer_constants.h>
#include <executorch/extension/android/jni/log.h>
#include <executorch/extension/module/module.h>
#include <executorch/extension/runner_util/inputs.h>
#include <executorch/extension/tensor/tensor.h>
#include <executorch/runtime/core/portable_type/tensor_impl.h>
#include <executorch/runtime/platform/log.h>
#include <executorch/runtime/platform/platform.h>
#include <executorch/runtime/platform/runtime.h>
#include <cassert>
#include <chrono>
#include <iostream>
Expand All @@ -15,16 +24,6 @@
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "jni_layer_constants.h"

#include <executorch/extension/android/jni/log.h>
#include <executorch/extension/module/module.h>
#include <executorch/extension/runner_util/inputs.h>
#include <executorch/extension/tensor/tensor.h>
#include <executorch/runtime/core/portable_type/tensor_impl.h>
#include <executorch/runtime/platform/log.h>
#include <executorch/runtime/platform/platform.h>
#include <executorch/runtime/platform/runtime.h>

#ifdef ET_USE_THREADPOOL
#include <cpuinfo.h>
Expand Down
28 changes: 28 additions & 0 deletions extension/android/jni/selective_jni.buck.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@fbcode_macros//build_defs:build_file_migration.bzl", "non_fbcode_target")
load("@fbsource//tools/build_defs/android:fb_android_cxx_library.bzl", "fb_android_cxx_library")
load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs.bzl", "third_party_dep")
load("@fbsource//xplat/executorch/extension/android/jni:build_defs.bzl", "ET_JNI_COMPILER_FLAGS")

def selective_jni_target(name, deps, srcs = [], soname = "libexecutorch.$(ext)"):
non_fbcode_target(_kind = fb_android_cxx_library,
name = name,
srcs = [
"//xplat/executorch/extension/android/jni:jni_layer.cpp",
] + srcs,
allow_jni_merging = False,
compiler_flags = ET_JNI_COMPILER_FLAGS,
soname = soname,
visibility = ["PUBLIC"],
deps = [
"//fbandroid/libraries/fbjni:fbjni",
"//fbandroid/native/fb:fb",
"//third-party/glog:glog",
"//xplat/executorch/extension/android/jni:jni_headers",
"//xplat/executorch/extension/android/jni:log_provider_static",
"//xplat/executorch/extension/module:module_static",
"//xplat/executorch/extension/runner_util:inputs_static",
"//xplat/executorch/extension/tensor:tensor_static",
"//xplat/executorch/extension/threadpool:threadpool_static",
third_party_dep("cpuinfo"),
] + deps,
)
Loading