Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMake + Vcpkg example #575

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
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
5 changes: 5 additions & 0 deletions src/8-tooling/build_with_cmake_and_vcpkg/README.md
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 src/8-tooling/build_with_cmake_and_vcpkg/napi/CMakeLists.txt
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)
3 changes: 3 additions & 0 deletions src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.js
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 src/8-tooling/build_with_cmake_and_vcpkg/napi/package.json
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"
}
}
1 change: 1 addition & 0 deletions src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg
Submodule vcpkg added at c9e405
9 changes: 9 additions & 0 deletions src/8-tooling/build_with_cmake_and_vcpkg/napi/vcpkg.json
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"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
23 changes: 23 additions & 0 deletions src/8-tooling/build_with_cmakejs/napi/hello.c
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)
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down