From 5bf8a051ba5d5c145825ff810fb040cbf303230b Mon Sep 17 00:00:00 2001 From: Leonid Pospelov Date: Wed, 8 Jan 2025 12:10:41 +0000 Subject: [PATCH 1/3] Add CMake + Vcpkg example --- .gitmodules | 3 +++ .../build_with_cmake_and_vcpkg/README.md | 5 ++++ .../napi/CMakeLists.txt | 22 ++++++++++++++++++ .../napi/hello.c | 0 .../build_with_cmake_and_vcpkg/napi/hello.js | 3 +++ .../napi/package.json | 12 ++++++++++ .../build_with_cmake_and_vcpkg/napi/vcpkg | 1 + .../napi/vcpkg.json | 9 ++++++++ .../README.md | 0 .../napi/CMakeLists.txt | 2 +- src/8-tooling/build_with_cmakejs/napi/hello.c | 23 +++++++++++++++++++ .../napi/hello.js | 0 .../napi/package.json | 4 ++-- .../node-addon-api/CMakeLists.txt | 2 +- .../node-addon-api/hello.cc | 0 .../node-addon-api/hello.js | 0 .../node-addon-api/package.json | 4 ++-- 17 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 .gitmodules create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/README.md create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/napi/CMakeLists.txt rename src/8-tooling/{build_with_cmake => build_with_cmake_and_vcpkg}/napi/hello.c (100%) create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.js create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/napi/package.json create mode 160000 src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg create mode 100644 src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg.json rename src/8-tooling/{build_with_cmake => build_with_cmakejs}/README.md (100%) rename src/8-tooling/{build_with_cmake => build_with_cmakejs}/napi/CMakeLists.txt (92%) create mode 100644 src/8-tooling/build_with_cmakejs/napi/hello.c rename src/8-tooling/{build_with_cmake => build_with_cmakejs}/napi/hello.js (100%) rename src/8-tooling/{build_with_cmake => build_with_cmakejs}/napi/package.json (66%) rename src/8-tooling/{build_with_cmake => build_with_cmakejs}/node-addon-api/CMakeLists.txt (94%) rename src/8-tooling/{build_with_cmake => build_with_cmakejs}/node-addon-api/hello.cc (100%) rename src/8-tooling/{build_with_cmake => build_with_cmakejs}/node-addon-api/hello.js (100%) rename src/8-tooling/{build_with_cmake => build_with_cmakejs}/node-addon-api/package.json (62%) diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..871f497a --- /dev/null +++ b/.gitmodules @@ -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 diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/README.md b/src/8-tooling/build_with_cmake_and_vcpkg/README.md new file mode 100644 index 00000000..76b14d69 --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/README.md @@ -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. diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/napi/CMakeLists.txt b/src/8-tooling/build_with_cmake_and_vcpkg/napi/CMakeLists.txt new file mode 100644 index 00000000..32acdb81 --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/napi/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-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) diff --git a/src/8-tooling/build_with_cmake/napi/hello.c b/src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.c similarity index 100% rename from src/8-tooling/build_with_cmake/napi/hello.c rename to src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.c 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 new file mode 100644 index 00000000..3d3c73e6 --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.js @@ -0,0 +1,3 @@ +var addon = require(process.cwd() + "/build/Release/scam_native.node"); + +console.log(addon.hello()); // 'world' diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/napi/package.json b/src/8-tooling/build_with_cmake_and_vcpkg/napi/package.json new file mode 100644 index 00000000..fc57a36d --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/napi/package.json @@ -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" + } +} diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg b/src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg new file mode 160000 index 00000000..c9e4056f --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg @@ -0,0 +1 @@ +Subproject commit c9e4056f04448359c677cff0f1f4d01401fa9270 diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg.json b/src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg.json new file mode 100644 index 00000000..7559c46a --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg.json @@ -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" + ] +} diff --git a/src/8-tooling/build_with_cmake/README.md b/src/8-tooling/build_with_cmakejs/README.md similarity index 100% rename from src/8-tooling/build_with_cmake/README.md rename to src/8-tooling/build_with_cmakejs/README.md diff --git a/src/8-tooling/build_with_cmake/napi/CMakeLists.txt b/src/8-tooling/build_with_cmakejs/napi/CMakeLists.txt similarity index 92% rename from src/8-tooling/build_with_cmake/napi/CMakeLists.txt rename to src/8-tooling/build_with_cmakejs/napi/CMakeLists.txt index f87b9bb1..5a39864a 100644 --- a/src/8-tooling/build_with_cmake/napi/CMakeLists.txt +++ b/src/8-tooling/build_with_cmakejs/napi/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.9) cmake_policy(SET CMP0042 NEW) set (CMAKE_CXX_STANDARD 11) -project (build-napi-with-cmake) +project (build-napi-with-cmakejs) include_directories(${CMAKE_JS_INC}) file(GLOB SOURCE_FILES "hello.c") add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC}) diff --git a/src/8-tooling/build_with_cmakejs/napi/hello.c b/src/8-tooling/build_with_cmakejs/napi/hello.c new file mode 100644 index 00000000..8ae1eee9 --- /dev/null +++ b/src/8-tooling/build_with_cmakejs/napi/hello.c @@ -0,0 +1,23 @@ +#include +#include + +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) diff --git a/src/8-tooling/build_with_cmake/napi/hello.js b/src/8-tooling/build_with_cmakejs/napi/hello.js similarity index 100% rename from src/8-tooling/build_with_cmake/napi/hello.js rename to src/8-tooling/build_with_cmakejs/napi/hello.js diff --git a/src/8-tooling/build_with_cmake/napi/package.json b/src/8-tooling/build_with_cmakejs/napi/package.json similarity index 66% rename from src/8-tooling/build_with_cmake/napi/package.json rename to src/8-tooling/build_with_cmakejs/napi/package.json index 6d62538e..09f66c57 100644 --- a/src/8-tooling/build_with_cmake/napi/package.json +++ b/src/8-tooling/build_with_cmakejs/napi/package.json @@ -1,7 +1,7 @@ { - "name": "build-napi-with-cmake", + "name": "build-napi-with-cmakejs", "version": "0.0.0", - "description": "Build Node-API native addon with CMake.", + "description": "Build Node-API native addon with CMake.js.", "main": "hello.js", "private": true, "dependencies": { diff --git a/src/8-tooling/build_with_cmake/node-addon-api/CMakeLists.txt b/src/8-tooling/build_with_cmakejs/node-addon-api/CMakeLists.txt similarity index 94% rename from src/8-tooling/build_with_cmake/node-addon-api/CMakeLists.txt rename to src/8-tooling/build_with_cmakejs/node-addon-api/CMakeLists.txt index 456d2312..f35ca848 100644 --- a/src/8-tooling/build_with_cmake/node-addon-api/CMakeLists.txt +++ b/src/8-tooling/build_with_cmakejs/node-addon-api/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.9) cmake_policy(SET CMP0042 NEW) set (CMAKE_CXX_STANDARD 11) -project (build-node-addon-api-with-cmake) +project (build-node-addon-api-with-cmakejs) include_directories(${CMAKE_JS_INC}) file(GLOB SOURCE_FILES "hello.cc") add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC}) diff --git a/src/8-tooling/build_with_cmake/node-addon-api/hello.cc b/src/8-tooling/build_with_cmakejs/node-addon-api/hello.cc similarity index 100% rename from src/8-tooling/build_with_cmake/node-addon-api/hello.cc rename to src/8-tooling/build_with_cmakejs/node-addon-api/hello.cc diff --git a/src/8-tooling/build_with_cmake/node-addon-api/hello.js b/src/8-tooling/build_with_cmakejs/node-addon-api/hello.js similarity index 100% rename from src/8-tooling/build_with_cmake/node-addon-api/hello.js rename to src/8-tooling/build_with_cmakejs/node-addon-api/hello.js diff --git a/src/8-tooling/build_with_cmake/node-addon-api/package.json b/src/8-tooling/build_with_cmakejs/node-addon-api/package.json similarity index 62% rename from src/8-tooling/build_with_cmake/node-addon-api/package.json rename to src/8-tooling/build_with_cmakejs/node-addon-api/package.json index ccb919b9..b03a2a7f 100644 --- a/src/8-tooling/build_with_cmake/node-addon-api/package.json +++ b/src/8-tooling/build_with_cmakejs/node-addon-api/package.json @@ -1,7 +1,7 @@ { - "name": "build-node-addon-api-with-cmake", + "name": "build-node-addon-api-with-cmakejs", "version": "0.0.0", - "description": "Build Node-API native addon with CMake and node-addon-api C++ wrapper.", + "description": "Build Node-API native addon with CMake.js and node-addon-api C++ wrapper.", "main": "hello.js", "private": true, "dependencies": { From 92999d6b1258aff85b1ca15712dc705c4039ff16 Mon Sep 17 00:00:00 2001 From: Leonid Pospelov Date: Fri, 17 Jan 2025 21:32:36 +0000 Subject: [PATCH 2/3] node-addon-api/vcpkg submodule --- .gitmodules | 3 +++ src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg | 1 + 2 files changed, 4 insertions(+) create mode 160000 src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg diff --git a/.gitmodules b/.gitmodules index 871f497a..4fcbcad4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [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 +[submodule "src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg"] + path = src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg + url = https://github.com/microsoft/vcpkg.git diff --git a/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg new file mode 160000 index 00000000..cf035d99 --- /dev/null +++ b/src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/vcpkg @@ -0,0 +1 @@ +Subproject commit cf035d9916a0a23042b41fcae7ee0386d245af08 From d9c6245c1d1d4b1b745e3cc242c2acefc89fbfde Mon Sep 17 00:00:00 2001 From: Leonid Pospelov Date: Fri, 17 Jan 2025 21:37:45 +0000 Subject: [PATCH 3/3] 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" + ] +}