-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove build_with_cmake from .gitignore
- Loading branch information
1 parent
0791c92
commit 142407e
Showing
9 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## Building Node-API Addons Using CMake.js | ||
|
||
### Examples | ||
|
||
The objective of these examples is to demonstrate how to build Node-API addons using [CMake.js](https://github.com/cmake-js/cmake-js#readme). | ||
|
||
These example projects assume that CMake.js has been installed globally: | ||
|
||
``` | ||
npm install -g cmake-js | ||
cmake-js --help | ||
``` | ||
|
||
Then, in each of the `napi` and `node-addon-api` directories, the following commands build and test each addon: | ||
|
||
``` | ||
npm install | ||
npm test | ||
``` | ||
|
||
Complete CMake.js documentation can be found on the [CMake.js GitHub repository](https://github.com/cmake-js/cmake-js#readme). | ||
|
||
### NAPI_VERSION | ||
|
||
When building Node-API addons, it's important to specify to the build system 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.9) | ||
cmake_policy(SET CMP0042 NEW) | ||
set (CMAKE_CXX_STANDARD 11) | ||
|
||
project (build-napi-with-cmake) | ||
include_directories(${CMAKE_JS_INC}) | ||
file(GLOB SOURCE_FILES "hello.c") | ||
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC}) | ||
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") | ||
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) | ||
|
||
# Define NAPI_VERSION | ||
add_definitions(-DNAPI_VERSION=3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <assert.h> | ||
#include <node_api.h> | ||
|
||
static napi_value Method(napi_env env, napi_callback_info info) { | ||
napi_status status; | ||
napi_value world; | ||
status = napi_create_string_utf8(env, "Hello, world!", 13, &world); | ||
assert(status == napi_ok); | ||
return world; | ||
} | ||
|
||
#define DECLARE_NAPI_METHOD(name, func) \ | ||
{ name, 0, func, 0, 0, 0, napi_default, 0 } | ||
|
||
static napi_value Init(napi_env env, napi_value exports) { | ||
napi_status status; | ||
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method); | ||
status = napi_define_properties(env, exports, 1, &desc); | ||
assert(status == napi_ok); | ||
return exports; | ||
} | ||
|
||
NAPI_MODULE(hello, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var addon = require('bindings')('build-napi-with-cmake'); | ||
|
||
console.log(addon.hello()); // 'world' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "build-napi-with-cmake", | ||
"version": "0.0.0", | ||
"description": "Build Node-API native addon with CMake.", | ||
"main": "hello.js", | ||
"private": true, | ||
"dependencies": { | ||
"bindings": "~1.2.1" | ||
}, | ||
"scripts": { | ||
"install": "cmake-js compile", | ||
"test": "node hello.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
cmake_minimum_required(VERSION 3.9) | ||
cmake_policy(SET CMP0042 NEW) | ||
set (CMAKE_CXX_STANDARD 11) | ||
|
||
project (build-node-addon-api-with-cmake) | ||
include_directories(${CMAKE_JS_INC}) | ||
file(GLOB SOURCE_FILES "hello.cc") | ||
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC}) | ||
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") | ||
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) | ||
|
||
# Include Node-API wrappers | ||
execute_process(COMMAND node -p "require('node-addon-api').include" | ||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} | ||
OUTPUT_VARIABLE NODE_ADDON_API_DIR | ||
) | ||
string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR}) | ||
|
||
target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR}) | ||
|
||
# define NAPI_VERSION | ||
add_definitions(-DNAPI_VERSION=3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <napi.h> | ||
|
||
static Napi::String Method(const Napi::CallbackInfo& info) { | ||
// Napi::Env is the opaque data structure containing the environment in which | ||
// the request is being run. We will need this env when we want to create any | ||
// new objects inside of the node.js environment | ||
Napi::Env env = info.Env(); | ||
|
||
// Create a C++ level variable | ||
std::string helloWorld = "Hello, world!"; | ||
|
||
// Return a new javascript string that we copy-construct inside of the node.js | ||
// environment | ||
return Napi::String::New(env, helloWorld); | ||
} | ||
|
||
static Napi::Object Init(Napi::Env env, Napi::Object exports) { | ||
exports.Set(Napi::String::New(env, "hello"), | ||
Napi::Function::New(env, Method)); | ||
return exports; | ||
} | ||
|
||
NODE_API_MODULE(hello, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var addon = require('bindings')('build-node-addon-api-with-cmake'); | ||
|
||
console.log(addon.hello()); // 'world' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "build-node-addon-api-with-cmake", | ||
"version": "0.0.0", | ||
"description": "Build Node-API native addon with CMake and node-addon-api C++ wrapper.", | ||
"main": "hello.js", | ||
"private": true, | ||
"dependencies": { | ||
"bindings": "~1.2.1", | ||
"node-addon-api": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"install": "cmake-js compile", | ||
"test": "node hello.js" | ||
} | ||
} |