Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-walls-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-native-node-api": patch
---

Moved weak-node-api into a separate "weak-node-api" package.
7 changes: 7 additions & 0 deletions .changeset/quick-poets-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"gyp-to-cmake": minor
"cmake-rn": minor
"react-native-node-api": minor
---

Use `find_package` instead of `include` to locate "weak-node-api"
5 changes: 5 additions & 0 deletions .changeset/spotty-beers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-native-node-api": minor
---

No longer exporting weakNodeApiPath, import from "weak-node-api" instead
5 changes: 5 additions & 0 deletions .changeset/tired-words-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"weak-node-api": patch
---

Initial release!
18 changes: 8 additions & 10 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ jobs:
- run: rustup target add x86_64-linux-android
- run: npm ci
- run: npm run build
# Bootstrap host package to get weak-node-api and ferric-example to get types
# Bootstrap weak-node-api and ferric-example to get types
# TODO: Solve this by adding an option to ferric to build only types or by committing the types into the repo as a fixture for an "init" command
- run: npm run bootstrap --workspace react-native-node-api
- run: npm run bootstrap --workspace weak-node-api
- run: npm run bootstrap --workspace @react-native-node-api/ferric-example
- run: npm run lint
env:
Expand Down Expand Up @@ -184,9 +184,8 @@ jobs:
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
- name: Build weak-node-api for all architectures
run: npm run build-weak-node-api:android
working-directory: packages/host
- name: Build weak-node-api for all Android architectures
run: npm run build-weak-node-api:android --workspace weak-node-api
- name: Build ferric-example for all architectures
run: npm run build -- --android
working-directory: packages/ferric-example
Expand Down Expand Up @@ -239,11 +238,10 @@ jobs:
- run: rustup toolchain install nightly --component rust-src
- run: npm ci
- run: npm run build
# Build weak-node-api for all Apple architectures
- run: |
npm run prepare-weak-node-api
npm run build-weak-node-api:apple
working-directory: packages/host
- name: Build weak-node-api for all Apple architectures
run: |
npm run prepare-weak-node-api --workspace weak-node-api
npm run build-weak-node-api:apple --workspace weak-node-api
# Build Ferric example for all Apple architectures
- run: npx ferric --apple
working-directory: packages/ferric-example
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ dist/

# Treading the MacOS app as ephemeral
apps/macos-test-app

# Cache used by the rust analyzer
target/rust-analyzer/
3 changes: 2 additions & 1 deletion apps/test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"react": "19.1.0",
"react-native": "0.81.4",
"react-native-node-api": "*",
"react-native-test-app": "^4.4.7"
"react-native-test-app": "^4.4.7",
"weak-node-api": "*"
}
}
40 changes: 27 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"workspaces": [
"packages/cli-utils",
"packages/cmake-file-api",
"packages/weak-node-api",
"packages/cmake-rn",
"packages/ferric",
"packages/gyp-to-cmake",
Expand Down Expand Up @@ -66,7 +67,7 @@
"prettier": "^3.6.2",
"react-native": "0.81.4",
"read-pkg": "^9.0.1",
"tsx": "^4.20.5",
"tsx": "^4.20.6",
"typescript": "^5.8.0",
"typescript-eslint": "^8.38.0"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cmake-rn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Android's dynamic linker imposes restrictions on the access to global symbols (s

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.

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.
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.

```cmake
cmake_minimum_required(VERSION 3.15...3.31)
project(tests-buffers)

# Defines the "weak-node-api" target
include(${WEAK_NODE_API_CONFIG})
find_package(weak-node-api REQUIRED CONFIG)

add_library(addon SHARED addon.c)
target_link_libraries(addon PRIVATE weak-node-api)
Expand Down
26 changes: 14 additions & 12 deletions packages/cmake-rn/src/weak-node-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import {
isAndroidTriplet,
isAppleTriplet,
SupportedTriplet,
weakNodeApiPath,
} from "react-native-node-api";

import {
applePrebuildPath,
androidPrebuildPath,
weakNodeApiCmakePath,
} from "weak-node-api";

import { ANDROID_ARCHITECTURES } from "./platforms/android.js";
import { getNodeAddonHeadersPath, getNodeApiHeadersPath } from "./headers.js";

Expand All @@ -20,19 +25,14 @@ export function getWeakNodeApiPath(
triplet: SupportedTriplet | "apple",
): string {
if (triplet === "apple" || isAppleTriplet(triplet)) {
const xcframeworkPath = path.join(
weakNodeApiPath,
"weak-node-api.xcframework",
);
assert(
fs.existsSync(xcframeworkPath),
`Expected an XCFramework at ${xcframeworkPath}`,
fs.existsSync(applePrebuildPath),
`Expected an XCFramework at ${applePrebuildPath}`,
);
return xcframeworkPath;
return applePrebuildPath;
} else if (isAndroidTriplet(triplet)) {
const libraryPath = path.join(
weakNodeApiPath,
"weak-node-api.android.node",
androidPrebuildPath,
ANDROID_ARCHITECTURES[triplet],
"libweak-node-api.so",
);
Expand All @@ -58,8 +58,10 @@ export function getWeakNodeApiVariables(
triplet: SupportedTriplet | "apple",
): Record<string, string> {
return {
// Expose an includable CMake config file declaring the weak-node-api target
WEAK_NODE_API_CONFIG: path.join(weakNodeApiPath, "weak-node-api.cmake"),
// Enable use of `find_package(weak-node-api REQUIRED CONFIG)`
"weak-node-api_DIR": path.dirname(weakNodeApiCmakePath),
// Enable use of `include(${WEAK_NODE_API_CONFIG})`
WEAK_NODE_API_CONFIG: weakNodeApiCmakePath,
WEAK_NODE_API_INC: getNodeApiIncludePaths().join(";"),
WEAK_NODE_API_LIB: getWeakNodeApiPath(triplet),
};
Expand Down
13 changes: 4 additions & 9 deletions packages/ferric/src/cargo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
UsageError,
spawn,
} from "@react-native-node-api/cli-utils";
import { weakNodeApiPath } from "react-native-node-api";
import { applePrebuildPath, androidPrebuildPath } from "weak-node-api";

import {
AndroidTargetName,
Expand Down Expand Up @@ -169,25 +169,20 @@ export function getTargetAndroidPlatform(target: AndroidTargetName) {
}

export function getWeakNodeApiFrameworkPath(target: AppleTargetName) {
const xcframeworkPath = joinPathAndAssertExistence(
weakNodeApiPath,
"weak-node-api.xcframework",
);
const result = APPLE_XCFRAMEWORK_SLICES_PER_TARGET[target].find((slice) => {
const candidatePath = path.join(xcframeworkPath, slice);
const candidatePath = path.join(applePrebuildPath, slice);
return fs.existsSync(candidatePath);
});
assert(
result,
`No matching slice found in weak-node-api.xcframework for target ${target}`,
);
return joinPathAndAssertExistence(xcframeworkPath, result);
return joinPathAndAssertExistence(applePrebuildPath, result);
}

export function getWeakNodeApiAndroidLibraryPath(target: AndroidTargetName) {
return joinPathAndAssertExistence(
weakNodeApiPath,
"weak-node-api.android.node",
androidPrebuildPath,
ANDROID_ARCH_PR_TARGET[target],
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gyp-to-cmake/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function bindingGypToCmakeLists({
];

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

for (const target of gyp.targets) {
Expand Down
7 changes: 0 additions & 7 deletions packages/host/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,5 @@ include/
android/.cxx/
android/build/

# Everything in weak-node-api is generated, except for the configurations
# Generated and built bia `npm run build-weak-node-api-injector`
/weak-node-api/build/
/weak-node-api/*.xcframework
/weak-node-api/*.android.node
/weak-node-api/weak_node_api.cpp
/weak-node-api/weak_node_api.hpp
# Generated via `npm run generate-weak-node-api-injector`
/cpp/WeakNodeApiInjector.cpp
7 changes: 1 addition & 6 deletions packages/host/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ set(CMAKE_CXX_STANDARD 20)

find_package(ReactAndroid REQUIRED CONFIG)
find_package(hermes-engine REQUIRED CONFIG)

add_library(weak-node-api INTERFACE)
target_include_directories(weak-node-api INTERFACE
../weak-node-api
../weak-node-api/include
)
find_package(weak-node-api REQUIRED CONFIG)

add_library(node-api-host SHARED
src/main/cpp/OnLoad.cpp
Expand Down
Loading
Loading