-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
84 additions
and
6 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,3 @@ | ||
[submodule "src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg"] | ||
path = src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg | ||
url = https://github.com/microsoft/vcpkg.git |
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,5 @@ | ||
## Building Node-API Addons Using CMake and Vcpkg | ||
|
||
### Examples | ||
|
||
The objective of these examples is to demonstrate how to build Node-API addons using CMake and Vcpkg. |
22 changes: 22 additions & 0 deletions
22
src/8-tooling/build_with_cmake_and_vcpkg/napi/CMakeLists.txt
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 @@ | ||
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake") | ||
|
||
# File from vcpkg submodule. This indicates inability to find this file or checkout submodules. | ||
if(NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}") | ||
set(msg "${CMAKE_TOOLCHAIN_FILE} doesn't exist. It seems that vcpkg submodule is not initialized.") | ||
set(msg "${msg}\nUse commands below to initialize:") | ||
set(msg "${msg}\n git submodule init") | ||
set(msg "${msg}\n git submodule update") | ||
message(FATAL_ERROR "${msg}") | ||
endif() | ||
|
||
cmake_minimum_required(VERSION 3.19) | ||
project(build-napi-with-cmake-and-vcpkg) | ||
|
||
find_package(unofficial-node-api-headers REQUIRED) | ||
|
||
set(sources hello.c) | ||
add_library(${PROJECT_NAME} SHARED ${sources}) | ||
|
||
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") | ||
target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::node-api-headers::node-api-headers) | ||
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src) |
File renamed without changes.
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(process.cwd() + "/build/Release/scam_native.node"); | ||
|
||
console.log(addon.hello()); // 'world' |
12 changes: 12 additions & 0 deletions
12
src/8-tooling/build_with_cmake_and_vcpkg/napi/package.json
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,12 @@ | ||
{ | ||
"name": "build-napi-with-cmake-and-vcpkg", | ||
"version": "0.0.0", | ||
"description": "Build Node-API native addon with CMake abd Vcpkg.", | ||
"main": "hello.js", | ||
"private": true, | ||
"dependencies": {}, | ||
"scripts": { | ||
"install": "git submodule update --init && mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release", | ||
"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,9 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", | ||
"name": "build-napi-with-cmake-and-vcpkg", | ||
"homepage": "https://github.com/nodejs/node-addon-examples", | ||
"description": "Build Node-API native addon with CMake abd Vcpkg.", | ||
"dependencies": [ | ||
"node-api-headers" | ||
] | ||
} |
File renamed without changes.
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
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) |
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ooling/build_with_cmake/napi/package.json → ...ling/build_with_cmakejs/napi/package.json
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
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
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ld_with_cmake/node-addon-api/package.json → ..._with_cmakejs/node-addon-api/package.json
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