Skip to main content

Autolinking

Autolinking finds the native code of the packages an app depends on, and builds it into the app. It runs on every run-desktop and init-desktop, and writes its output to desktop/autolink/, which is generated and git-ignored.

A package takes part by declaring platforms.desktop in its react-native.config.js:

react-native.config.js
module.exports = {
dependency: {
platforms: {
desktop: {
crate: 'rn-gpui-awesome-storage',
},
},
},
};

What autolinking does​

  • Adds each Rust crate to the app's desktop/Cargo.toml as a dependency, and calls its rn_gpui_register function at startup.
  • Compiles each cmake project and cppProvider into the app.
  • Runs React Native Codegen over the specifications of every package with a codegenConfig, and compiles the generated C++ into the app.
  • Generates the Rust and C++ spec glue for modules, and the typed glue for host components.
  • Registers Fabric component descriptors and host components.
  • Writes a [patch.crates-io] table into the app's desktop/Cargo.toml, which points core's crates at the copy the app installed and pins GPUI to core's revision. It is regenerated on every run, so leave it alone.
  • Records the installed JavaScript engines.

platforms.desktop​

KeyTypeDescription
cratestringThe name of a Cargo crate in the package's desktop/ folder (or the package root). It must have an rn_gpui_register function.
pathstringThe folder of the crate, when it is not desktop/ or the package root.
cmakestringA folder with a CMakeLists.txt that adds C++ sources to the app.
cppProviderstringThe C++ function that creates a C++ TurboModule for each specification of the package.
cppSourcesstring[]The C++ sources of the module, if they should not be found from cmake.
specsstringThe folder of the specifications, when codegenConfig.jsSrcsDir is not set. Defaults to src.
hostComponentsobject[]The host components the package provides: {name, measured?, nativeSurface?, container?}.
componentDescriptorsstring[]C++ identifiers of Fabric component descriptors to register.
componentHeadersstring[]The headers that declare those descriptors. Required with componentDescriptors.
componentSourcesstring[]Extra C++ sources for the components.
componentIncludeDirsstring[]Extra include folders for the components.
componentSourcePackagestringThe package that componentSources and componentIncludeDirs are relative to. Defaults to the package itself.
componentCodegenPackagestringThe package whose component specifications Codegen runs over. For adapters.
sourceOverridesobjectFiles of installed JavaScript packages that Metro replaces for desktop.
jsEngineobjectDeclares the package as a JavaScript engine.

A hostComponents entry cannot be both measured and a container.

project.desktop​

The app's own react-native.config.js carries build options under project.desktop:

react-native.config.js
module.exports = {
project: {
desktop: {
prebuilt: 'auto', // 'auto' | 'always' | 'never'
},
},
};

prebuilt chooses whether React Native's C++ runtime is downloaded or built from source. See CLI.

Errors​

Autolinking checks what it reads. A missing or duplicate component descriptor, an invalid crate name, a jsEngine entry that points at a file that does not exist, and a hostComponents entry that cannot be both measured and a container are all reported while linking, with the name of the package, and not later in the build.