CMake.js 是 node-gyp 的一个优秀构建工具替代方案。CMake.js 基于必须安装的 CMake 工具。
🌐 Pros
- 使用 CMake 工具,该工具在开源社区中被广泛采用。
- 非常适合已经基于 CMake 的现有 C/C++ 库。
🌐 Cons
- 在 Node 社区中并未被广泛采用。
🌐 Installation
CMake.js 需要已安装 CMake。可以在 CMake 网站 上获取安装程序。
🌐 CMake.js requires that CMake is already installed. Installers are available on the CMake website.
macOS 开发者可能会发现使用 Homebrew 安装 CMake 更加方便。安装 Homebrew 后,可以使用
brew install cmake命令安装 CMake。
你可以使用以下命令来验证你的 CMake 安装:
🌐 You can verify your CMake installation with the command:
cmake --version作为 Node 原生模块的开发者,你可能会觉得将 CMake.js 安装为全局命令行工具很方便:
🌐 As a Node native module developer, you may find it convenient to install CMake.js as a global command line tool:
npm install cmake-js -g你可以使用以下命令验证你的 CMake.js 安装:
🌐 You can verify your CMake.js installation with the command:
cmake-js --version你的 package.json 文件需要有几个条目,以便你的本地模块可以与 CMake.js 一起工作。
🌐 Your package.json file needs to have a couple of entries for your native module to work with CMake.js.
由于你的本地模块需要在安装时使用 CMake.js 编译,因此你的 package.json 文件的 scripts 属性需要一个 install 条目来实现这一点:
🌐 Since your native module needs to be compiled using CMake.js on installation, the scripts property of your package.json file needs an install entry to make this happen:
"scripts": {
"install": "cmake-js compile"
}你的本地模块的用户不太可能将 CMake.js 安装为全局命令行工具。因此,你的项目需要声明对 CMake.js 的开发依赖。这可以通过输入以下命令来完成:
🌐 It is unlikely that the users of your native module will have CMake.js installed as a global command line tool. Therefore, your project needs to declare a development dependency on CMake.js. This can be accomplished by entering this command:
npm install cmake-js --save-dev另一种方法是手动将开发依赖添加到你的 package.json 文件中:
🌐 An alternative is to manually add the development dependency to your package.json file:
"devDependencies": {
"cmake-js": "^6.0.0"
}这种方法的一个例子可以在这里找到。
🌐 An example of this approach is available here.
基于 CMake.js 构建的本地模块有一个 CMakeLists.txt,用于描述模块的构建方式。该文件的用途与使用 node-gyp 的项目中的 binding.gyp 相同。
🌐 Native modules built on CMake.js have a CMakeLists.txt that describes how the module is to be built. The file serves the same purpose as the binding.gyp for projects that use node-gyp.
除了任何 CMake 构建所需的条目外,构建原生模块时还需要额外的条目。
🌐 In addition to the entries required for any CMake build, additional entries are required when building native modules.
以下是使用 CMake.js 构建的所有本地模块所需的行:
🌐 Here are the lines required for all native modules built using CMake.js:
project(node-api-cmake-build-example)
include_directories(${CMAKE_JS_INC})
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})在基于 Node-API 构建原生模块时,声明你的模块设计用于运行的最低 Node-API 版本非常重要。对于 CMake.js,这可以通过在 CMakeLists.txt 文件中添加如下行来实现:
🌐 When building a native module based on Node-API, it is important to declare the minimum Node-API version against which your module is designed to work. For CMake.js, this is accomplished by adding a line like this to the CMakeLists.txt file:
# define NAPI_VERSION
add_definitions(-DNAPI_VERSION=3)在没有其他要求的情况下,Node-API 版本 3 是一个不错的选择,因为这是 Node-API 在脱离实验阶段时的活跃版本。
基于 node-addon-api 的 Node-API 模块需要额外的配置值。
🌐 Additional configuration values are required for Node-API modules based on node-addon-api.
node-addon-api 需要 C++11。在 CMakeLists.txt 文件顶部的这些配置行指定了此要求:
cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0042 NEW)
set (CMAKE_CXX_STANDARD 11)基于 node-addon-api 的模块包括 Node 本身之外的其他头文件。这些行指示 CMake.js 在哪里可以找到这些文件:
🌐 Modules based on node-addon-api include additional header files that are not part of Node itself. These lines instruct CMake.js where to find these files:
# 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(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})这种方法的一个例子可以在这里找到。
🌐 An example of this approach is available here.