From 131992cb1f7693da1d2df2ddbe018ff6afc18f1e Mon Sep 17 00:00:00 2001 From: Nik Graf Date: Sun, 1 Oct 2023 07:25:09 +0200 Subject: [PATCH] fix: find node_modules path --- android/CMakeLists.txt | 8 ++++---- android/build.gradle | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index dd4e4f4..f934106 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -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 @@ -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 diff --git a/android/build.gradle b/android/build.gradle index 3dbc737..49282df 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,3 +1,5 @@ +import java.nio.file.Paths + buildscript { repositories { google() @@ -30,6 +32,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") @@ -48,7 +65,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}" } } }