Skip to content

Commit b07067e

Browse files
committed
Use find_package instead of include to locate "weak-node-api"
1 parent d9a600f commit b07067e

File tree

9 files changed

+66
-28
lines changed

9 files changed

+66
-28
lines changed

.changeset/quick-poets-greet.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"gyp-to-cmake": minor
3+
"cmake-rn": minor
4+
"react-native-node-api": minor
5+
---
6+
7+
Use `find_package` instead of `include` to locate "weak-node-api"

packages/cmake-rn/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ Android's dynamic linker imposes restrictions on the access to global symbols (s
1010

1111
The implementation of Node-API is split between Hermes and our host package and to avoid addons having to explicitly link against either, we've introduced a `weak-node-api` library (published in `react-native-node-api` package). This library exposes only Node-API and will have its implementation injected by the host.
1212

13-
To link against `weak-node-api` just include the CMake config exposed through `WEAK_NODE_API_CONFIG` and add `weak-node-api` to the `target_link_libraries` of the addon's library target.
13+
To link against `weak-node-api` just use `find_package` to import the `weak-node-api` target and add it to the `target_link_libraries` of the addon's library target.
1414

1515
```cmake
1616
cmake_minimum_required(VERSION 3.15...3.31)
1717
project(tests-buffers)
1818
1919
# Defines the "weak-node-api" target
20-
include(${WEAK_NODE_API_CONFIG})
20+
find_package(weak-node-api REQUIRED CONFIG)
2121
2222
add_library(addon SHARED addon.c)
2323
target_link_libraries(addon PRIVATE weak-node-api)

packages/cmake-rn/src/weak-node-api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ export function getWeakNodeApiVariables(
5858
triplet: SupportedTriplet | "apple",
5959
): Record<string, string> {
6060
return {
61-
// Expose an includable CMake config file declaring the weak-node-api target
61+
// Enable use of `find_package(weak-node-api REQUIRED CONFIG)`
62+
"weak-node-api_DIR": path.dirname(weakNodeApiCmakePath),
63+
// Enable use of `include(${WEAK_NODE_API_CONFIG})`
6264
WEAK_NODE_API_CONFIG: weakNodeApiCmakePath,
6365
WEAK_NODE_API_INC: getNodeApiIncludePaths().join(";"),
6466
WEAK_NODE_API_LIB: getWeakNodeApiPath(triplet),

packages/gyp-to-cmake/src/transformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function bindingGypToCmakeLists({
8585
];
8686

8787
if (weakNodeApi) {
88-
lines.push(`include(\${WEAK_NODE_API_CONFIG})`, "");
88+
lines.push(`find_package(weak-node-api REQUIRED CONFIG)`, "");
8989
}
9090

9191
for (const target of gyp.targets) {

packages/host/android/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ set(CMAKE_CXX_STANDARD 20)
55

66
find_package(ReactAndroid REQUIRED CONFIG)
77
find_package(hermes-engine REQUIRED CONFIG)
8-
9-
add_library(weak-node-api INTERFACE)
10-
target_include_directories(weak-node-api INTERFACE
11-
../../weak-node-api/generated
12-
../../weak-node-api/include
13-
)
8+
find_package(weak-node-api REQUIRED CONFIG)
149

1510
add_library(node-api-host SHARED
1611
src/main/cpp/OnLoad.cpp

packages/host/android/build.gradle

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ if (!System.getenv("REACT_NATIVE_OVERRIDE_HERMES_DIR")) {
1414
].join('\n'))
1515
}
1616

17+
def findWeakNodeApiDir() {
18+
def searchDir = rootDir.toPath()
19+
do {
20+
def p = searchDir.resolve("node_modules/weak-node-api")
21+
if (p.toFile().exists()) {
22+
return p.toRealPath().toString()
23+
}
24+
} while (searchDir = searchDir.getParent())
25+
throw new GradleException("Could not find `weak-node-api`");
26+
}
27+
1728
buildscript {
1829
ext.getExtOrDefault = {name ->
1930
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['NodeApiModules_' + name]
@@ -86,7 +97,7 @@ android {
8697
cmake {
8798
targets "node-api-host"
8899
cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
89-
arguments "-DANDROID_STL=c++_shared"
100+
arguments "-DANDROID_STL=c++_shared", "-Dweak-node-api_DIR=${findWeakNodeApiDir()}"
90101
abiFilters (*reactNativeArchitectures())
91102

92103
buildTypes {

packages/weak-node-api/src/weak-node-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ export const androidPrebuildPath = path.resolve(
2222

2323
export const weakNodeApiCmakePath = path.resolve(
2424
weakNodeApiPath,
25-
"weak-node-api.cmake",
25+
"weak-node-api-config.cmake",
2626
);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
# Get the current file directory
3+
get_filename_component(WEAK_NODE_API_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY)
4+
5+
if(NOT DEFINED WEAK_NODE_API_LIB)
6+
# Auto-detect library path for Android NDK builds
7+
if(ANDROID)
8+
# Define the library path pattern for Android
9+
set(WEAK_NODE_API_LIB_PATH "weak-node-api.android.node/${ANDROID_ABI}/libweak-node-api.so")
10+
11+
# Try Debug first, then Release using the packaged Android node structure
12+
set(WEAK_NODE_API_LIB_DEBUG "${WEAK_NODE_API_CMAKE_DIR}/build/Debug/${WEAK_NODE_API_LIB_PATH}")
13+
set(WEAK_NODE_API_LIB_RELEASE "${WEAK_NODE_API_CMAKE_DIR}/build/Release/${WEAK_NODE_API_LIB_PATH}")
14+
15+
if(EXISTS "${WEAK_NODE_API_LIB_DEBUG}")
16+
set(WEAK_NODE_API_LIB "${WEAK_NODE_API_LIB_DEBUG}")
17+
message(STATUS "Using Debug weak-node-api library: ${WEAK_NODE_API_LIB}")
18+
elseif(EXISTS "${WEAK_NODE_API_LIB_RELEASE}")
19+
set(WEAK_NODE_API_LIB "${WEAK_NODE_API_LIB_RELEASE}")
20+
message(STATUS "Using Release weak-node-api library: ${WEAK_NODE_API_LIB}")
21+
else()
22+
message(FATAL_ERROR "Could not find weak-node-api library for Android ABI ${ANDROID_ABI}. Expected at:\n ${WEAK_NODE_API_LIB_DEBUG}\n ${WEAK_NODE_API_LIB_RELEASE}")
23+
endif()
24+
else()
25+
message(FATAL_ERROR "WEAK_NODE_API_LIB is not set")
26+
endif()
27+
endif()
28+
29+
if(NOT DEFINED WEAK_NODE_API_INC)
30+
set(WEAK_NODE_API_INC "${WEAK_NODE_API_CMAKE_DIR}/include;${WEAK_NODE_API_CMAKE_DIR}/generated")
31+
message(STATUS "Using weak-node-api include directories: ${WEAK_NODE_API_INC}")
32+
endif()
33+
34+
add_library(weak-node-api SHARED IMPORTED)
35+
36+
set_target_properties(weak-node-api PROPERTIES
37+
IMPORTED_LOCATION "${WEAK_NODE_API_LIB}"
38+
INTERFACE_INCLUDE_DIRECTORIES "${WEAK_NODE_API_INC}"
39+
)

packages/weak-node-api/weak-node-api.cmake

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)