From af16a229e5f0884da2b529fae39e43461dba2ae8 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sun, 13 Apr 2025 00:21:18 -0700 Subject: [PATCH] feat: update cmake-ts to 1.0.0 --- .github/workflows/CI.yml | 2 +- lib/load-addon.js | 84 +------------------------- lib/load-addon.js.map | 2 +- package.json | 102 ++------------------------------ pnpm-lock.yaml | 10 ++-- src/load-addon.ts | 125 +-------------------------------------- 6 files changed, 15 insertions(+), 310 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index a803a370..defe4803 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -152,7 +152,7 @@ jobs: - name: Build Native Windows 32 if: ${{ matrix.os == 'windows-2019' && matrix.node_arch == 'ia32' }} run: - node ./node_modules/cmake-ts/build/main.js named-configs windows-x86 + node --enable-source-maps ./node_modules/cmake-ts/build/main.js build --config win32-ia32-release - name: Use Node 20 if: ${{ matrix.native }} diff --git a/lib/load-addon.js b/lib/load-addon.js index eff7909d..d3692e37 100644 --- a/lib/load-addon.js +++ b/lib/load-addon.js @@ -3,88 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); +const loader_1 = require("cmake-ts/build/loader"); const path_1 = __importDefault(require("path")); -const fs_1 = __importDefault(require("fs")); -function errStr(error) { - return error instanceof Error - ? `${error.name}: ${error.message}\n${error.stack}` - : String(error); -} -function devWarn(message) { - if (process.env.NODE_ENV !== "production") { - console.warn(message); - } -} -function findAddon() { - let addon = undefined; - try { - const buildDir = path_1.default.resolve(__dirname, "..", "build"); - const manifest = JSON.parse(fs_1.default.readFileSync(path_1.default.resolve(buildDir, "manifest.json"), "utf-8")); - // compatible addons (abi -> addon path) - const compatibleAddons = new Map(); - const libc = detectLibc(); - const configs = Object.keys(manifest); - for (const configStr of configs) { - const config = JSON.parse(configStr); - // check if the config is compatible with the current runtime - if (config.os !== process.platform || - config.arch !== process.arch || - config.libc !== libc) { - continue; - } - const addonRelativePath = manifest[configStr]; - compatibleAddons.set(config, path_1.default.resolve(buildDir, addonRelativePath)); - } - if (compatibleAddons.size === 0) { - throw new Error(`No compatible zeromq.js addon found for ${process.platform} ${process.arch} ${libc}. The candidates were:\n${configs.join("\n")}`); - } - // sort the compatible abis in descending order - const compatibleAddonsSorted = [...compatibleAddons.entries()].sort(([c1, _p1], [c2, _p2]) => { - var _a, _b; - return ((_a = c2.abi) !== null && _a !== void 0 ? _a : 0) - ((_b = c1.abi) !== null && _b !== void 0 ? _b : 0); - }); - // try each available addon ABI - for (const [_config, addonPath] of compatibleAddonsSorted) { - try { - addon = require(addonPath); - break; - } - catch (err) { - if (fs_1.default.existsSync(addonPath)) { - devWarn(`Failed to load addon at ${addonPath}: ${errStr(err)}\nTrying others...`); - } - else { - devWarn(`No addon.node found in ${addonPath}\nTrying others...`); - } - } - } - } - catch (err) { - throw new Error(`Failed to load zeromq.js addon.node: ${errStr(err)}`); - } - if (addon === undefined) { - throw new Error("No compatible zeromq.js addon found"); - } - return addon; -} -/** - * Detect the libc used by the runtime (from cmake-ts) - */ -function detectLibc() { - if (process.platform === "linux") { - if (fs_1.default.existsSync("/etc/alpine-release")) { - return "musl"; - } - return "glibc"; - } - else if (process.platform === "darwin") { - return "libc"; - } - else if (process.platform === "win32") { - return "msvc"; - } - return "unknown"; -} -const addon = findAddon(); +const addon = (0, loader_1.loadAddon)(path_1.default.resolve(__dirname, "..", "build")); exports.default = addon; //# sourceMappingURL=load-addon.js.map \ No newline at end of file diff --git a/lib/load-addon.js.map b/lib/load-addon.js.map index 5f8d8b1e..a25c25b2 100644 --- a/lib/load-addon.js.map +++ b/lib/load-addon.js.map @@ -1 +1 @@ -{"version":3,"file":"load-addon.js","sourceRoot":"","sources":["../src/load-addon.ts"],"names":[],"mappings":";;;;;AAAA,gDAAuB;AACvB,4CAAmB;AAEnB,SAAS,MAAM,CAAC,KAAc;IAC5B,OAA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AK,CAAA;AACd,CAAC;AAuBD;;GAEG;AACH,SAAS,UAAU;IACjB,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE;QAChC,IAAI,YAAE,CAAC,UAAU,CAAC,qBAAqB,CAAC,EAAE;YACxC,OAAO,MAAM,CAAA;SACd;QACD,OAAO,OAAO,CAAA;KACf;SAAM,IAAI,OAAO,CAAC,QAAQ,KAAK,QAAQ,EAAE;QACxC,OAAO,MAAM,CAAA;KACd;SAAM,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE;QACvC,OAAO,MAAM,CAAA;KACd;IACD,OAAO,SAAS,CAAA;AAClB,CAAC;AAED,MAAM,KAAK,GAAG,SAAS,EAAE,CAAA;AACzB,kBAAe,KAAK,CAAA"} \ No newline at end of file +{"version":3,"file":"load-addon.js","sourceRoot":"","sources":["../src/load-addon.ts"],"names":[],"mappings":";;;;;AAAA,kDAA+C;AAC/C,gDAAuB;AAEvB,MAAM,KAAK,GAAG,IAAA,kBAAS,EAAC,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAA;AAC/D,kBAAe,KAAK,CAAA"} \ No newline at end of file diff --git a/package.json b/package.json index 9c5601cc..9706a777 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "homepage": "http://zeromq.github.io/zeromq.js/", "dependencies": { - "cmake-ts": "^0.6.1", + "cmake-ts": "1.0.0", "node-addon-api": "^8.3.0" }, "devDependencies": { @@ -71,7 +71,7 @@ "engines": { "node": ">= 12" }, - "packageManager": "pnpm@10.6.5", + "packageManager": "pnpm@10.8.0", "overrides": { "typescript": "~4.9.5", "node-gyp": "10.0.1" @@ -113,8 +113,8 @@ "docs.preview": "servor --browse --reload docs", "docs.dev": "run-s docs.build docs.preview", "docs.deploy": "run-s docs.build && gh-pages --dist \"./docs\"", - "build.native": "cmake-ts nativeonly", - "build.native.debug": "cross-env npm_config_zmq_enable_sanitizer_undefined=true cmake-ts dev-os-only", + "build.native": "cmake-ts build --logger=debug", + "build.native.debug": "cross-env npm_config_zmq_enable_sanitizer_undefined=true cmake-ts build --config debug", "build": "run-p build.js build.native", "build.debug": "run-s build.js build.native.debug", "test": "run-s test.unit test.examples", @@ -142,100 +142,6 @@ }, "cmake-ts": { "nodeAPI": "node-addon-api", - "configurations": [ - { - "name": "linux-x64", - "os": "linux", - "arch": "x64", - "runtime": "node", - "runtimeVersion": "12.22.12" - }, - { - "name": "linux-x64-dev", - "dev": true, - "buildType": "Debug", - "os": "linux", - "arch": "x64", - "runtime": "node", - "runtimeVersion": "12.22.12" - }, - { - "name": "darwin-arm64-dev", - "dev": true, - "buildType": "Debug", - "os": "darwin", - "arch": "arm64", - "runtime": "node", - "runtimeVersion": "12.22.12" - }, - { - "name": "windows-x64-dev", - "dev": true, - "buildType": "Debug", - "os": "win32", - "arch": "x64", - "runtime": "node", - "runtimeVersion": "12.22.12" - }, - { - "name": "windows-x64", - "os": "win32", - "arch": "x64", - "runtime": "node", - "runtimeVersion": "12.22.12" - }, - { - "name": "windows-x86", - "os": "win32", - "arch": "ia32", - "runtime": "node", - "runtimeVersion": "12.22.12" - }, - { - "name": "darwin-x64", - "os": "darwin", - "arch": "x64", - "runtime": "node", - "runtimeVersion": "12.22.12" - }, - { - "name": "darwin-arm64", - "os": "darwin", - "arch": "arm64", - "runtime": "node", - "runtimeVersion": "12.22.12" - }, - { - "name": "linux-x64-electron", - "os": "linux", - "arch": "x64", - "runtime": "electron", - "runtimeVersion": "12.22.12" - }, - { - "name": "windows-x64-electron", - "os": "win32", - "arch": "x64", - "runtime": "electron", - "runtimeVersion": "12.22.12" - }, - { - "name": "darwin-x64-electron", - "os": "darwin", - "arch": "x64", - "runtime": "electron", - "runtimeVersion": "12.22.12" - }, - { - "name": "darwin-arm64-electron", - "os": "darwin", - "arch": "arm64", - "runtime": "electron", - "runtimeVersion": "12.22.12" - } - ], - "targetDirectory": "build", - "buildType": "Release", "projectName": "addon" }, "keywords": [ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c0a0686a..8818c87a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,8 +13,8 @@ importers: .: dependencies: cmake-ts: - specifier: ^0.6.1 - version: 0.6.1 + specifier: 1.0.0 + version: 1.0.0 node-addon-api: specifier: ^8.3.0 version: 8.3.0 @@ -1044,8 +1044,8 @@ packages: clone-response@1.0.3: resolution: {integrity: sha512-ROoL94jJH2dUVML2Y/5PEDNaSHgeOdSDicUyS7izcF63G6sTc/FTjLub4b8Il9S8S0beOfYt0TaA5qvFK+w0wA==} - cmake-ts@0.6.1: - resolution: {integrity: sha512-uUn2qGhf20j8W/sQ7+UnvvqO1zNccqgbLgwRJi7S23FsjMWJqxvKK80Vc+tvLNKfpJzwH0rgoQD1l24SMnX0yg==} + cmake-ts@1.0.0: + resolution: {integrity: sha512-wg7HJHTLmoqNM7/g/D9urT2P8NEghaz1ATlr55dCZTlK19DWN9I1f8st9uV6Cn0FeOUt02Ps1CKphvfbjUPRRQ==} hasBin: true coffeescript@1.12.7: @@ -4576,7 +4576,7 @@ snapshots: dependencies: mimic-response: 1.0.1 - cmake-ts@0.6.1: {} + cmake-ts@1.0.0: {} coffeescript@1.12.7: optional: true diff --git a/src/load-addon.ts b/src/load-addon.ts index 2c9ca0be..a646ebac 100644 --- a/src/load-addon.ts +++ b/src/load-addon.ts @@ -1,126 +1,5 @@ +import {loadAddon} from "cmake-ts/build/loader" import path from "path" -import fs from "fs" -function errStr(error: unknown) { - return error instanceof Error - ? `${error.name}: ${error.message}\n${error.stack}` - : String(error) -} - -function devWarn(message: string) { - if (process.env.NODE_ENV !== "production") { - console.warn(message) - } -} - -function findAddon(): any | undefined { - let addon: undefined | any = undefined - try { - const buildDir = path.resolve(__dirname, "..", "build") - - const manifest = JSON.parse( - fs.readFileSync(path.resolve(buildDir, "manifest.json"), "utf-8"), - ) as Record - - // compatible addons (abi -> addon path) - const compatibleAddons: Map = new Map() - - const libc = detectLibc() - - const configs = Object.keys(manifest) - for (const configStr of configs) { - const config = JSON.parse(configStr) as BuildConfiguration - - // check if the config is compatible with the current runtime - if ( - config.os !== process.platform || - config.arch !== process.arch || - config.libc !== libc - ) { - continue - } - - const addonRelativePath = manifest[configStr] - compatibleAddons.set(config, path.resolve(buildDir, addonRelativePath)) - } - if (compatibleAddons.size === 0) { - throw new Error( - `No compatible zeromq.js addon found for ${process.platform} ${process.arch} ${libc}. The candidates were:\n${configs.join( - "\n", - )}`, - ) - } - - // sort the compatible abis in descending order - const compatibleAddonsSorted = [...compatibleAddons.entries()].sort( - ([c1, _p1], [c2, _p2]) => { - return (c2.abi ?? 0) - (c1.abi ?? 0) - }, - ) - - // try each available addon ABI - for (const [_config, addonPath] of compatibleAddonsSorted) { - try { - addon = require(addonPath) - break - } catch (err) { - if (fs.existsSync(addonPath)) { - devWarn( - `Failed to load addon at ${addonPath}: ${errStr(err)}\nTrying others...`, - ) - } else { - devWarn(`No addon.node found in ${addonPath}\nTrying others...`) - } - } - } - } catch (err) { - throw new Error(`Failed to load zeromq.js addon.node: ${errStr(err)}`) - } - - if (addon === undefined) { - throw new Error("No compatible zeromq.js addon found") - } - - return addon -} - -/** - * Build configuration (from cmake-ts) - */ -type BuildConfiguration = { - name: string - dev: boolean - os: typeof process.platform - arch: typeof process.arch - runtime: string - runtimeVersion: string - toolchainFile: string | null - CMakeOptions?: {name: string; value: string}[] - addonSubdirectory: string - // list of additional definitions to fixup node quirks for some specific versions - additionalDefines: string[] - /** The ABI number that is used by the runtime. */ - abi?: number - /** The libc that is used by the runtime. */ - libc?: string -} - -/** - * Detect the libc used by the runtime (from cmake-ts) - */ -function detectLibc() { - if (process.platform === "linux") { - if (fs.existsSync("/etc/alpine-release")) { - return "musl" - } - return "glibc" - } else if (process.platform === "darwin") { - return "libc" - } else if (process.platform === "win32") { - return "msvc" - } - return "unknown" -} - -const addon = findAddon() +const addon = loadAddon(path.resolve(__dirname, "..", "build")) export default addon