Skip to content

feat: update cmake-ts to 1.0.0 #723

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

Merged
merged 1 commit into from
Apr 13, 2025
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
84 changes: 2 additions & 82 deletions lib/load-addon.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/load-addon.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 4 additions & 98 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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": [
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 2 additions & 123 deletions src/load-addon.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>

// compatible addons (abi -> addon path)
const compatibleAddons: Map<BuildConfiguration, string> = 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
Loading