Skip to content

Commit

Permalink
fix: find correct node_modules path
Browse files Browse the repository at this point in the history
  • Loading branch information
nikgraf committed Oct 2, 2023
1 parent 09e9935 commit 8b89907
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 4 additions & 4 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ set (CMAKE_CXX_STANDARD 11)
# add directories to "include" search paths
include_directories(
../cpp
../node_modules/react-native/React
../node_modules/react-native/React/Base
../node_modules/react-native/ReactCommon/jsi
"${NODE_MODULES_DIR}/react-native/React"
"${NODE_MODULES_DIR}/react-native/React/Base"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
)

# here we define a library target called "opaque"
# which will be built from the listed source files
add_library(opaque
SHARED
../node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp"
../cpp/react-native-opaque.cpp
../cpp/react-native-opaque.h
../cpp/opaque-rust.h
Expand Down
20 changes: 19 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.nio.file.Paths

buildscript {
repositories {
google()
Expand Down Expand Up @@ -30,6 +32,21 @@ def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Opaque_" + name]).toInteger()
}

static def findNodeModules(baseDir) {
def basePath = baseDir.toPath().normalize()
while (basePath) {
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
return nodeModulesPath.toString()
}
basePath = basePath.getParent()
}
throw new GradleException("react-native-opaque: Failed to find node_modules path")
}

def nodeModules = findNodeModules(projectDir)

android {
ndkVersion getExtOrDefault("ndkVersion")
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
Expand All @@ -46,7 +63,8 @@ android {
// exceptions (and other STL types) across shared object boundaries work
// correctly.
// see https://developer.android.com/ndk/guides/cpp-support#selecting_a_c_runtime
arguments "-DANDROID_STL=c++_shared"
arguments "-DANDROID_STL=c++_shared",
"-DNODE_MODULES_DIR=${nodeModules}"
}
}
}
Expand Down

0 comments on commit 8b89907

Please sign in to comment.