-
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
6 changed files
with
70 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
var addon = require(process.cwd() + "/build/Release/scam_native.node"); | ||
var addon = require(process.cwd() + "/build/Release/build-napi-with-cmake-and-vcpkg.node"); | ||
|
||
console.log(addon.hello()); // 'world' |
22 changes: 22 additions & 0 deletions
22
src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/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-node-addon-api-with-cmake-and-vcpkg) | ||
|
||
find_package(unofficial-node-addon-api 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-addon-api::node-addon-api) | ||
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src) |
23 changes: 23 additions & 0 deletions
23
src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.cc
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) |
3 changes: 3 additions & 0 deletions
3
src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/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,3 @@ | ||
var addon = require(process.cwd() + "/build/Release/build-node-addon-api-with-cmake-and-vcpkg.node"); | ||
|
||
console.log(addon.hello()); // 'world' |
12 changes: 12 additions & 0 deletions
12
src/8-tooling/build_with_cmake_and_vcpkg/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "build-node-addon-api-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" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg.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,9 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", | ||
"name": "build-node-addon-api-with-cmake-and-vcpk", | ||
"homepage": "https://github.com/nodejs/node-addon-examples", | ||
"description": "Build Node-API native addon with CMake abd Vcpkg.", | ||
"dependencies": [ | ||
"node-addon-api" | ||
] | ||
} |