@aminya/cmake-ts

中文文档
0.3.0-aminya.7发布于 2024/10/23MITTypeScriptCommonJS近 30 天下载 2万

cmake-js rewrite in typescript to support advanced build configurations

原始使用文档
文档目录3 节
  1. Configuration
  2. Workflow
  3. Cross Compilation

cmake-ts

A CMake-based build system for native NodeJS and Electron addons.

This project is loosely inspired by cmake-js but attempts to fix several design flaws.

It is intended to prebuild addons for different versions of NodeJS and Electron and ship a binary version.

Configuration

Configuration is done entirely via package.json. You can specify multiple build configurations under the cmake-ts key:

"cmake-ts": {
  "nodeAPI": "node-addon-api" // Specify the node API package such as `node-addon-api`, `nan`, or the path to a directory that has the nodeAPI header. Default is `node-addon-api`, a warning is emitted if nan is used
  "configurations": [
    {
      "name": "win-x64", // name for named-configs mode
      "os": "win32", // win32, linux and darwin are supported
      "arch": "x64", // x64, x86 should work
      "runtime": "electron", // node or electron
      "runtimeVersion": "4.0.1", // Version of the runtime which it is built
      "toolchainFile": "/windows.cmake", // CMake Toolchain file to use for crosscompiling
      "CMakeOptions": [ //Same syntax as for the globalCMakeOptions
        {
          "name": "MY_CMAKE_OPTION",
          "value": "my_value",
        }
      ],
      "addonSubdirectory": "avx2-generic" // if you build addons for multiple architectures in high performance scenarios, you can put the addon inside another subdirectory
    }, // more build configurations...
    {
      "dev": true, // whether this configuration is eligible to be used in a dev test build
      "os": "linux", // win32, linux and darwin are supported
      "arch": "x64", // x64, x86 should work
      "runtime": "node", // node or electron
      "runtimeVersion": "10.3.0", // Version of the runtime which it is built
    } // more build configurations ...
  ],
  "targetDirectory": "build", // where to build your project
  "buildType": "Release", // Debug or Release build, most likely set it to Release
  "projectName": "addon" // The name of your CMake project.
  "globalCMakeOptions": [{ // this might be omitted of no further options should be passed to CMake
    "name": "CMAKE_CXX_FLAGS",
    "value": "-Og"
  }, {
    "name": "CMAKE_CXX_FLAGS",
    "value": "-I$ROOT$/include", // $ROOT$ will be replaced by the package.json directory
  }, {
      "name": "CMAKE_EXPORT_COMPILE_COMMANDS",
      "value": "1"
  }]
}

Workflow

While it is desirable to perform a full build (all configurations) within a CI environment, long build times hinder local package development. Therefore cmake-ts knows multiple build modes:

  • TODO nativeonly -> Builds the native code only for the runtime cmake-ts is currently running on, ignoring all previously specified configurations. This is useful if you'd like to run some unit tests against the compiled code. When running cmake-ts nativeonly, cmake-ts will determine the runtime, ABI, and platform from the environment, and build only the configuration required to run on this platform.
    • Example using the configuration above
    • You run cmake-ts nativeonly on NodeJS 11.7 on MacOS, cmake-ts will ignore all specified configurations above and build the native addon for NodeJS 11.7 on MacOS
  • TODO osonly -> Builds the native code for all configurations which match the current operating system. This is useful for those developing for example an electron addon and want to test their code in electron. In such a case, you would specify electron and NodeJS runtimes for several platforms in your configuration and you can use cmake-ts osonly to build a local package you can install in your application.
    • Example using the configuration above
    • You run cmake-ts osonly on NodeJS 11.7 on Linux, cmake-ts will ignore all configurations above where os != linux and build the native addon for all remaining configurations, in this case it will build for NodeJS 10.3 on Linux.
  • TODO HINT: For both osonly and nativeonly, the specified CMake Toolchain files are ignored since I assume you got your toolchain set up correctly for your own operating system.
  • None / Omitted: Builds all configs
  • dev-os-only builds the first config that has dev == true and os matches the current OS
  • named-configs arg1 arg2 ... builds all configs for which name is one of the args

Cross Compilation

This module supports cross-compilation from Linux to macOS and Windows, given a correct toolchain setup. There is a docker container that has a cross-toolchain based on CLang 7 setup for Windows and macOS which might be used in a CI.

Docker Image

版本与发布记录

@aminya/cmake-ts 共发布 7 个版本,首次发布于 2024/10/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.0-aminya.7latest2024/10/232 年前
0.3.0-aminya.62024/10/232 年前
0.3.0-aminya.52024/10/232 年前
0.3.0-aminya.42024/10/232 年前
0.3.0-aminya.32024/10/232 年前
0.3.0-aminya.22024/10/232 年前

关于 @aminya/cmake-ts 的常见问题

@aminya/cmake-ts 是什么?

cmake-js rewrite in typescript to support advanced build configurations

如何安装 @aminya/cmake-ts?

在项目目录运行 npm install @aminya/cmake-ts,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 cmake-ts,可以用 npx cmake-ts 直接运行。

@aminya/cmake-ts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aminya/cmake-ts 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aminya/cmake-ts 可以商用吗?

@aminya/cmake-ts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aminya/cmake-ts 的下载量有多少?

根据 npm 官方下载统计,@aminya/cmake-ts 在 2026/10/06 – 2026/11/04 这 30 天内被下载 19,617 次,日均约 654 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aminya/cmake-ts 还在维护吗?

最新版本 0.3.0-aminya.7 发布于 2024/10/23(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/10/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。