Skip to content

Latest commit

 

History

History
87 lines (55 loc) · 3.5 KB

cmake-js.md

File metadata and controls

87 lines (55 loc) · 3.5 KB

CMake.js

CMake.js is a build tool that allow native addon developers to compile their C or C++ code into executable form. It works like node-gyp but instead of Google's gyp tool it is based on the CMake build system.

Quick Start

Install CMake

CMake.js requires that CMake be installed. Installers for a variety of platforms can be found on the CMake website.

Install CMake.js

For developers, CMake.js is typically installed as a global package:

npm install -g cmake-js
cmake-js --help

For users of your native addon, CMake.js should be configured as a dependency in your package.json as described in the CMake.js documentation.

CMakeLists.txt

Your project will require a CMakeLists.txt file. The CMake.js README file shows what's necessary.

NAPI_VERSION

When building Node-API addons, it's crucial to specify the Node-API version your code is designed to work with. With CMake.js, this information is specified in the CMakeLists.txt file:

add_definitions(-DNAPI_VERSION=3)

Since Node-API is ABI-stable, your Node-API addon will work, without recompilation, with the Node-API version you specify in NAPI_VERSION and all subsequent Node-API versions.

In the absence of a need for features available only in a specific Node-API version, version 3 is a good choice as it is the version of Node-API that was active when Node-API left experimental status.

NAPI_EXPERIMENTAL

The following line in the CMakeLists.txt file will enable Node-API experimental features if your code requires them:

add_definitions(-DNAPI_EXPERIMENTAL)

Exception Handling

To enable C++ exception handling (for more info see: Setup), define the corresponding preprocessor directives depending on which exception handling behavior is desired.

To enable C++ exception handling with Napi::Error objects only:

add_definitions(-DNAPI_EXPERIMENTAL)

To enable C++ exception handling for all exceptions thrown:

add_definitions(-DNODE_ADDON_API_CPP_EXCEPTIONS)
add_definitions(-DNODE_ADDON_API_CPP_EXCEPTIONS_ALL)

node-addon-api

If your Node-API native add-on uses the optional node-addon-api C++ wrapper, the CMakeLists.txt file requires additional configuration information as described on the CMake.js README file.

Example

A working example of an Node-API native addon built using CMake.js can be found on the node-addon-examples repository.

CMake Reference

Sometimes finding the right settings is not easy so to accomplish at most complicated task please refer to: