环境准备
安装npm:brew install npm
安装cnpm:npm install -g cnpm --registry=https://registry.npm.taobao.org
安装Electron:npm install electron -g
安装node版本管理模块n:sudo npm install n -g
安装稳定版:sudo n stable
安装node指定版本:sudo n 16.17.0
安装cmake:brew install cmake
安装cmake-js:npm install cmake-js -g
项目简介
CMake.js 是一个开源项目,它是CMake的封装,为编写跨平台的Node.js模块提供了更简单的构建方式。使用CMake-JS,开发人员可以更容易地构建和部署Node.js模块,同时还可以受益于CMake的功能和跨平台的特性。它是一个功能强大、易于使用的工具,适用于任何需要使用C++编写的Node.js模块项目。
构建Node模块我们需要编写
package.json
{
"name": "build-node-addon-api-with-cmake",
"version": "0.0.0",
"description": "Build Node-API native addon with CMake and node-addon-api C++ wrapper.",
"main": "hello.js",
"private": true,
"dependencies": {
"bindings": "~1.2.1",
"node-addon-api": "^1.0.0"
},
"scripts": {
"install": "cmake-js -G Xcode -a 'arm64 x86_64'",
"test": "node hello.js"
}
}
cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0042 NEW)
set (CMAKE_CXX_STANDARD 11)
SET(CMAKE_INSTALL_PREFIX "")
set (CMAKE_OSX_DEPLOYMENT_TARGET 11.0)
project (build-node)
message(========${CMAKE_SOURCE_DIR}===$(SRCROOT))
include_directories(${CMAKE_JS_INC}
${CMAKE_SOURCE_DIR}/macDemoSDK.framework/Headers
)
LINK_DIRECTORIES(
)
file(GLOB SOURCE_FILES "hello.cc")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}
${CMAKE_SOURCE_DIR}/macDemoSDK.framework
)
# Include Node-API wrappers
execute_process(COMMAND node -p "require('node-addon-api').include"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NODE_ADDON_API_DIR
)
string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})
# define NAPI_VERSION
add_definitions(-DNAPI_VERSION=4)
hello.cc 内部实现调用macSDK的方法demo::helloWord();
#include <napi.h>
#include "ZYBMacHeader.h"
#include "helloWord.hpp"
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();
demo::getDiskFreeSpace();
demo::helloWord();
// 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) 通过require方法引用构建的`build-node`模块,可以调用`hello()`方法
var addon = require('./build-node');
console.log(addon.hello()); // 'world'
macDemoSDK.framework通过macDemoSDK构建,放在同级目录后,在CmakeList文件中引用。
include_directories(${CMAKE_JS_INC}
${CMAKE_SOURCE_DIR}/macDemoSDK.framework/Headers
)
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}
${CMAKE_SOURCE_DIR}/macDemoSDK.framework
)
change_node_project.sh 是给Xcode项目中的RunPathSearch
和FrameWork_Search_Path
分别添加@loader_path
和$(SRCROOT)
,给build-node.node
动态库添加相对路径,只需要把macDemoSDK.framework
放在同一目录就可以正确连接成功。
#! /bin/bash
cd "$(dirname "$0")"
source_code_dir=$(cd "$(dirname "$0")";pwd)
echo "src:${source_code_dir}"
cd build
cd build-node.xcodeproj
sed -i '' "s|LD_RUNPATH_SEARCH_PATHS = (\"|LD_RUNPATH_SEARCH_PATHS = (\"@loader_path |g" project.pbxproj
sed -i '' "s|FRAMEWORK_SEARCH_PATHS = (\"|FRAMEWORK_SEARCH_PATHS = (\"\$(SRCROOT) |g" project.pbxproj
sed -i '' "s|MACOSX_DEPLOYMENT_TARGET = 12.6|MACOSX_DEPLOYMENT_TARGET = 11.0|g" project.pbxproj
Build
下载Demo
cd nodeSimpleDemo
npm install
sh ./change_node_project.sh
cd build
xcodebuild -configuration "Release" -target "build-node" clean build
cd ..
cp -R "${PWD}/build/Release/Release/" "${PWD}/"
测试
npm test
> build-node-addon-api-with-cmake@0.0.0 test
> node hello.js
helloWord C++Hello, world!
备注:如果npm执行过程慢可以把上述命令中的npm换成cnpm