From d9c6245c1d1d4b1b745e3cc242c2acefc89fbfde Mon Sep 17 00:00:00 2001 From: Leonid Pospelov Date: Fri, 17 Jan 2025 21:37:45 +0000 Subject: [PATCH] Add C++ example. TODO: test --- .../build_with_cmake_and_vcpkg/napi/hello.js | 2 +- .../node-addon-api/CMakeLists.txt | 22 ++++++++++++++++++ .../node-addon-api/hello.cc | 23 +++++++++++++++++++ .../node-addon-api/hello.js | 3 +++ .../node-addon-api/package.json | 12 ++++++++++ .../node-addon-api/vcpkg.json | 9 ++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/CMakeLists.txt create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.cc create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.js create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/package.json create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg.json diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.js b/src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.js index 3d3c73e6..6097e2e1 100644 --- a/src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.js +++ b/src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.js @@ -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' diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/CMakeLists.txt b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/CMakeLists.txt new file mode 100644 index 00000000..b8a5153a --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/CMakeLists.txt @@ -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) diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.cc b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.cc new file mode 100644 index 00000000..1c422da3 --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.cc @@ -0,0 +1,23 @@ +#include + +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) diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.js b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.js new file mode 100644 index 00000000..c8abc95b --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.js @@ -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' diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/package.json b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/package.json new file mode 100644 index 00000000..83e9ac3f --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/package.json @@ -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" + } +} diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg.json b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg.json new file mode 100644 index 00000000..76963fa2 --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg.json @@ -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" + ] +}