Skip to content

Commit

Permalink
Add support for react-native 0.71 to fix `CMake Error: The following …
Browse files Browse the repository at this point in the history
…variables are used in this project, but they are set to NOTFOUND. FBJNI_LIB`, ref to

mrousavy/react-native-vision-camera@b82d0e
  • Loading branch information
flyskywhy committed Aug 12, 2024
1 parent a05eed1 commit ce55e97
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 60 deletions.
114 changes: 80 additions & 34 deletions react-native-pytorch-core/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

project(PlayTorch)
cmake_minimum_required(VERSION 3.4.1)

set (CMAKE_VERBOSE_MAKEFILE ON)

# This should be matched with the Unit Test Build. Please keep them in sync. See ../cxx/test/CMakeLists.txt
set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_FLAGS "-DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID")

if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
include("${NODE_MODULES_DIR}/react-native/ReactAndroid/cmake-utils/folly-flags.cmake")
add_compile_options(${folly_FLAGS})
else()
set (CMAKE_CXX_FLAGS "-DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID")
endif()

set (PACKAGE_NAME "torchlive")
set (BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
set (RN_SO_DIR ${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/first-party/react/jni)

if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
# Consume shared libraries and headers from prefabs
find_package(fbjni REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)
else()
set (RN_SO_DIR ${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/first-party/react/jni)
endif()

# PyTorch Core shared

Expand Down Expand Up @@ -60,25 +74,43 @@ add_library(

# includes

file (GLOB LIBFBJNI_INCLUDE_DIR "${BUILD_DIR}/fbjni-*-headers.jar/")
file (GLOB PYTORCH_INCLUDE_DIRS "${BUILD_DIR}/pytorch_android_lite*.aar/headers")

target_include_directories(
${PACKAGE_NAME}
PRIVATE
"${LIBFBJNI_INCLUDE_DIR}"
"${NODE_MODULES_DIR}/react-native/React"
"${NODE_MODULES_DIR}/react-native/React/Base"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor"
"${PYTORCH_INCLUDE_DIRS}"
"../cxx/src"
"src/main/cpp"
)
if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
target_include_directories(
${PACKAGE_NAME}
PRIVATE
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"${NODE_MODULES_DIR}/react-native/ReactCommon/react/renderer/graphics/platform/cxx"
"${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor"
"${NODE_MODULES_DIR}/react-native/ReactCommon/yoga"
"${PYTORCH_INCLUDE_DIRS}"
"../cxx/src"
"src/main/cpp"
)
else()
file (GLOB LIBFBJNI_INCLUDE_DIR "${BUILD_DIR}/fbjni-*-headers.jar/")

target_include_directories(
${PACKAGE_NAME}
PRIVATE
"${LIBFBJNI_INCLUDE_DIR}"
"${NODE_MODULES_DIR}/react-native/React"
"${NODE_MODULES_DIR}/react-native/React/Base"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor"
"${PYTORCH_INCLUDE_DIRS}"
"../cxx/src"
"src/main/cpp"
)
endif()

# find libraries

Expand All @@ -90,20 +122,21 @@ find_library(
log
)

find_library(
FBJNI_LIBRARY
fbjni
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)

find_library(
REACT_NATIVE_JNI_LIB
reactnativejni
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)
if(${REACT_NATIVE_MINOR_VERSION} LESS 71)
find_library(
FBJNI_LIBRARY
fbjni
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)

find_library(
REACT_NATIVE_JNI_LIB
reactnativejni
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)
endif()

find_library(
PYTORCH_LIBRARY
Expand All @@ -112,8 +145,21 @@ find_library(
NO_CMAKE_FIND_ROOT_PATH
)

# For RN 0.66+, use the distributed libjsi.so
if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 66)
if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::folly_runtime
ReactAndroid::glog
ReactAndroid::jsi
ReactAndroid::reactnativejni
fbjni::fbjni
${PYTORCH_LIBRARY}
)
elseif(${REACT_NATIVE_MINOR_VERSION} LESS 66)
# JSI lib didn't exist on RN 0.65 and before. Simply omit it.
set (JSI_LIB "")
else()
# For RN 0.66+, use the distributed libjsi.so
find_library(
JSI_LIB
jsi
Expand Down
65 changes: 39 additions & 26 deletions react-native-pytorch-core/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ android {
// ref to https://github.com/pytorch/pytorch/issues/51020#issuecomment-1600272599
ndkVersion "21.4.7075529"

if (Integer.parseInt(appRnMinorVersion) >= 71) {
buildFeatures {
prefab true
}
}

defaultConfig {
minSdkVersion safeExtGet('PyTorchCore_minSdkVersion', 21)
targetSdkVersion safeExtGet('PyTorchCore_targetSdkVersion', 30)
Expand All @@ -73,12 +79,15 @@ android {
lintOptions {
disable 'GradleCompatible'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
if (Integer.parseInt(appRnMinorVersion) < 71) {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
externalNativeBuild {
cmake {
// version "3.22.1"
path "CMakeLists.txt"
}
}
Expand Down Expand Up @@ -120,33 +129,35 @@ dependencies {
}
}

//noinspection GradleDynamicVersion
if (Integer.parseInt(appRnMinorVersion) < 71) {
api "com.facebook.react:react-native:+" // From node_modules
} else {
//noinspection GradleDynamicVersion
implementation("com.facebook.react:react-android")
}

//noinspection GradleDynamicVersion
extractHeaders("com.facebook.fbjni:fbjni:+:headers")
//noinspection GradleDynamicVersion
extractJNI("com.facebook.fbjni:fbjni:+")

def nodeModules = "${rootDir}/../node_modules"

// For now we use the release version of the jni library
// we will look back on this once we figure out how to
// get the buildType duing configuration stage
def buildType = "release"

if (Integer.parseInt(appRnMinorVersion) < 69) {
def rnAAR = fileTree("${nodeModules}/react-native/android").matching({ it.include "**/**/*.aar" }).singleFile
extractJNI(files(rnAAR))
} else if (Integer.parseInt(appRnMinorVersion) < 71) {
// React Native 0.69
def rnAarMatcher = "**/react-native/**/*${buildType}.aar"
def rnAAR = fileTree("${nodeModules}/react-native/android").matching({ it.include rnAarMatcher }).singleFile
extractJNI(files(rnAAR))
if (Integer.parseInt(appRnMinorVersion) < 71) {
//noinspection GradleDynamicVersion
extractHeaders("com.facebook.fbjni:fbjni:+:headers")
//noinspection GradleDynamicVersion
extractJNI("com.facebook.fbjni:fbjni:+")

def nodeModules = "${rootDir}/../node_modules"

// For now we use the release version of the jni library
// we will look back on this once we figure out how to
// get the buildType duing configuration stage
def buildType = "release"

if (Integer.parseInt(appRnMinorVersion) < 69) {
def rnAAR = fileTree("${nodeModules}/react-native/android").matching({ it.include "**/**/*.aar" }).singleFile
extractJNI(files(rnAAR))
} else if (Integer.parseInt(appRnMinorVersion) < 71) {
// React Native 0.69
def rnAarMatcher = "**/react-native/**/*${buildType}.aar"
def rnAAR = fileTree("${nodeModules}/react-native/android").matching({ it.include rnAarMatcher }).singleFile
extractJNI(files(rnAAR))
}
}

extractForNativeBuild("org.pytorch:pytorch_android_lite:${pytorchLiteVersion}")
Expand Down Expand Up @@ -223,8 +234,10 @@ task extractAARForNativeBuild {
def configureCMakeTaskName = Integer.parseInt(appRnMinorVersion) < 68 ? "externalNativeBuild" : "configureCMake"
tasks.whenTaskAdded { task ->
if (task.name.contains(configureCMakeTaskName)) {
task.dependsOn(extractAARHeaders)
task.dependsOn(extractJNIFiles)
task.dependsOn(extractAARForNativeBuild)
if (Integer.parseInt(appRnMinorVersion) < 71) {
task.dependsOn(extractAARHeaders)
task.dependsOn(extractAARForNativeBuild)
}
}
}

0 comments on commit ce55e97

Please sign in to comment.