Skip to content

Latest commit

 

History

History

README.md

cmake-rn

A wrapper around Cmake making it easier to produce prebuilt binaries targeting iOS and Android matching the prebuilt binary specification.

Serves the same purpose as cmake-js does for the Node.js community and could potentially be upstreamed into cmake-js eventually.

Linking against Node-API

Android's dynamic linker imposes restrictions on the access to global symbols (such as the Node-API free functions): A dynamic library must explicitly declare any dependency bringing symbols it needs as DT_NEEDED.

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 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_minimum_required(VERSION 3.15...3.31)
project(tests-buffers)

# Defines the "weak-node-api" target
find_package(weak-node-api REQUIRED CONFIG)

add_library(addon SHARED addon.c)
target_link_libraries(addon PRIVATE weak-node-api)
target_compile_features(addon PRIVATE cxx_std_20)

if(APPLE)
  # Build frameworks when building for Apple (optional)
  set_target_properties(addon PROPERTIES
    FRAMEWORK TRUE
    MACOSX_FRAMEWORK_IDENTIFIER async_test.addon
    MACOSX_FRAMEWORK_SHORT_VERSION_STRING 1.0
    MACOSX_FRAMEWORK_BUNDLE_VERSION 1.0
    XCODE_ATTRIBUTE_SKIP_INSTALL NO
   )
else()
  set_target_properties(addon PROPERTIES
    PREFIX ""
    SUFFIX .node
   )
endif()

This is different from how cmake-js "injects" the Node-API for linking (via ${CMAKE_JS_INC}, ${CMAKE_JS_SRC} and ${CMAKE_JS_LIB}). To allow for interoperability between these tools, we inject these when you pass --cmake-js to cmake-rn.