Skip to content

Commit

Permalink
fix: find node_modules path
Browse files Browse the repository at this point in the history
  • Loading branch information
nikgraf committed Oct 1, 2023
1 parent 1e7fbb5 commit 1d12820
Show file tree
Hide file tree
Showing 2 changed files with 21 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,9 +6,9 @@ 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}/node_modules/react-native/React"
"${NODE_MODULES_DIR}/node_modules/react-native/React/Base"
"${NODE_MODULES_DIR}/node_modules/react-native/ReactCommon/jsi"
)

# set the base libsodium build directory
Expand All @@ -33,7 +33,7 @@ set_target_properties( sodium PROPERTIES IMPORTED_LOCATION ${LIBSODIUM_BUILD_DIR
# which will be built from the listed source files
add_library(libsodium
SHARED
../../../node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp
"${NODE_MODULES_DIR}/node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp"
../cpp/react-native-libsodium.cpp
../cpp/react-native-libsodium.h
cpp-adapter.cpp
Expand Down
18 changes: 17 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Libsodium_" + 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-libsodium: Failed to find node_modules path")
}

def nodeModules = findNodeModules(projectDir)

android {
ndkVersion getExtOrDefault("ndkVersion")
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
Expand All @@ -48,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 1d12820

Please sign in to comment.