Skip to content
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ Configuration is done entirely via `package.json`. You can specify multiple buil
"name": "win-x64", // name for named-configs mode
"os": "win32", // win32, linux and darwin are supported
"arch": "x64", // x64, x86 should work
"generatorToUse": "Visual Studio 15 2017", // optional CMake generator override for this configuration
"generatorFlags": ["-A", "x64"], // optional extra flags passed to the generator
"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
Expand All @@ -177,6 +179,8 @@ Configuration is done entirely via `package.json`. You can specify multiple buil
} // more build configurations ...
],
"targetDirectory": "build", // where to build your project
"generatorToUse": "Ninja", // optional default CMake generator for all configurations
"generatorFlags": ["-A", "ARM64"], // optional default generator flags for all configurations
"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
Expand Down
74 changes: 47 additions & 27 deletions package.json
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pnpm add which@3.0.1 decided it needed to reformat the entire file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which then made linting fail 🤦

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I can fix that

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cmake-ts",
"version": "1.0.2",
"version": "1.0.3",
"description": "cmake-js rewrite in typescript to support advanced build configurations",
"main": "build/lib.js",
"module": "build/lib.mjs",
Expand Down Expand Up @@ -66,66 +66,86 @@
"test": "cross-env NODE_OPTIONS=--enable-source-maps pnpx vitest --watch false",
"coverage": "cross-env NODE_OPTIONS=--enable-source-maps pnpx vitest --coverage --watch false"
},
"files": ["build/**/*", "src/**/*", "./*.mts", "./tsconfig.json"],
"files": [
"build/**/*",
"src/**/*",
"./*.mts",
"./tsconfig.json"
],
"repository": {
"type": "git",
"url": "git+https://github.com/EmbeddedEnterprises/cmake-ts.git"
},
"keywords": ["cmake", "nan", "node", "native", "addon", "build", "cmake-js"],
"keywords": [
"cmake",
"nan",
"node",
"native",
"addon",
"build",
"cmake-js"
],
"author": "Amin Yahyaabadi <aminyahyaabadi74@gmail.com>",
"contributors": ["Amin Yahyaabadi <aminyahyaabadi74@gmail.com>", "Martin Koppehel <martin@mko.dev>"],
"contributors": [
"Amin Yahyaabadi <aminyahyaabadi74@gmail.com>",
"Martin Koppehel <martin@mko.dev>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/EmbeddedEnterprises/cmake-ts/issues"
},
"homepage": "https://github.com/EmbeddedEnterprises/cmake-ts#readme",
"devDependencies": {
"@babel/core": "7.26.10",
"@biomejs/biome": "1.9.4",
"@types/babel__core": "7.20.5",
"@types/escape-quotes": "1.0.0",
"@types/fs-extra": "11.0.4",
"@types/memoizee": "0.4.7",
"@types/node": "22.14.0",
"@types/resolve": "1.20.6",
"@types/semver": "7.7.0",
"@types/tar": "6.1.13",
"@types/url-join": "4.0.3",
"@types/which": "3.0.4",
"@types/escape-quotes": "1.0.0",
"@upleveled/babel-plugin-remove-node-prefix": "1.0.5",
"@types/memoizee": "0.4.7",
"turbo": "2.5.0",
"@vitest/coverage-v8": "3.1.1",
"@vitest/ui": "3.1.1",
"ci-info": "4.2.0",
"commander": "^13.1.0",
"cross-env": "7.0.3",
"escape-quotes": "^1.0.2",
"eslint": "^8",
"eslint-config-atomic": "1.22.1",
"npm-run-all2": "7.0.2",
"rollup-plugin-visualizer": "5.14.0",
"shx": "0.4.0",
"typescript": "5.8.3",
"vite": "6.2.7",
"vitest": "3.1.1",
"vite-plugin-babel": "1.3.0",
"@vitest/coverage-v8": "3.1.1",
"@vitest/ui": "3.1.1",
"@biomejs/biome": "1.9.4",
"prettier": "3.5.3",
"prettier-config-atomic": "4.0.0",
"execa": "9.5.2",
"ci-info": "4.2.0",
"fast-glob": "3.3.3",
"fs-extra": "^10",
"memoizee": "0.4.17",
"msvc-dev-cmd": "github:aminya/msvc-dev-cmd#c01f519bd995460228ed3dec4df51df92dc290fd",
"node-downloader-helper": "^2.1.9",
"npm-run-all2": "7.0.2",
"prettier": "3.5.3",
"prettier-config-atomic": "4.0.0",
"resolve": "^1.22.10",
"rollup-plugin-visualizer": "5.14.0",
"semver": "^7.7.1",
"shx": "0.4.0",
"tar": "^6",
"turbo": "2.5.0",
"typescript": "5.8.3",
"url-join": "^4.0.1",
"which": "^2",
"node-downloader-helper": "^2.1.9",
"escape-quotes": "^1.0.2",
"commander": "^13.1.0",
"msvc-dev-cmd": "github:aminya/msvc-dev-cmd#c01f519bd995460228ed3dec4df51df92dc290fd",
"memoizee": "0.4.17"
"vite": "6.2.7",
"vite-plugin-babel": "1.3.0",
"vitest": "3.1.1",
"which": "^2"
},
"packageManager": "pnpm@10.8.0",
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/package.json",
"pnpm": {
"onlyBuiltDependencies": ["@biomejs/biome", "core-js", "esbuild"]
"onlyBuiltDependencies": [
"@biomejs/biome",
"core-js",
"esbuild"
]
}
}
6 changes: 3 additions & 3 deletions src/argumentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ export function getCMakeArchitecture(arch: NodeJS.Architecture, os: NodeJS.Platf

const cmakeArchMap: Record<string, Record<string, string>> = {
win32: {
arm64: "arm64",
x64: "AMD64",
ia32: "X86",
arm64: "ARM64",
x64: "x64",
ia32: "Win32",
Comment on lines -160 to +162
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to change this? I tested it when I created this to make sure it works.

},
darwin: {
arm64: "arm64",
Expand Down
8 changes: 6 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readJson } from "fs-extra"
import which from "which"
import type { BuildCommandOptions, BuildConfiguration, BuildConfigurations, Options } from "./config-types.d"
import { getCmakeGenerator } from "./generator.js"
import { logger } from "./lib.js"
import { whichNoThrow } from "./whichNoThrow.js"

export async function parseBuildConfigs(
opts: Options,
Expand Down Expand Up @@ -97,11 +97,15 @@ export async function getBuildConfig(

config.additionalDefines ??= globalConfig.additionalDefines ?? []

config.cmakeToUse ??= globalConfig.cmakeToUse ?? (await which("cmake", { nothrow: true })) ?? "cmake"
config.cmakeToUse ??= globalConfig.cmakeToUse ?? (await whichNoThrow("cmake", { nothrow: true })) ?? "cmake"

const { generator, generatorFlags, binary } = await getCmakeGenerator(config.cmakeToUse, config.os, config.arch)
// If this build configuration specifies a generator, use that.
// If not, use the globally defined generator.
// If that isn't found, use the detected generator from getCmakeGenerator.
config.generatorToUse ??= globalConfig.generatorToUse ?? generator
config.generatorFlags ??= globalConfig.generatorFlags ?? generatorFlags
logger.debug(`Using generator: ${ config.generatorToUse} ${config.generatorFlags} for ${config.os} ${config.arch}`)
config.generatorBinary ??= globalConfig.generatorBinary ?? binary

return config as BuildConfiguration
Expand Down
10 changes: 5 additions & 5 deletions src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import memoizee from "memoizee"
import which from "which"
import { getCMakeArchitecture } from "./argumentBuilder.js"
import { execCapture } from "./utils/exec.js"
import { logger } from "./utils/logger.js"
import { whichNoThrow } from "./whichNoThrow.js"

export const getCmakeGenerator = memoizee(
async (
Expand All @@ -15,9 +15,9 @@ export const getCmakeGenerator = memoizee(
binary?: string
}> => {
// use ninja if available
const ninja = await which("ninja", { nothrow: true })
const ninja = await whichNoThrow("ninja");
if (ninja !== null) {
logger.debug(`Using generator: Ninja for ${os} ${arch}`)
logger.debug(`Found generator: Ninja for ${os} ${arch}`)
return {
generator: "Ninja",
binary: ninja,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const getCmakeGenerator = memoizee(
const generator = useArchSwitch ? parsedGenerator : `${parsedGenerator}${archString}`
const generatorFlags = useArchSwitch ? ["-A", getCMakeArchitecture(arch, os)] : undefined

logger.debug(`Using generator: ${generator} ${generatorFlags} for ${os} ${arch}`)
logger.debug(`Found generator: ${generator} ${generatorFlags} for ${os} ${arch}`)
return {
generator,
generatorFlags,
Expand All @@ -68,7 +68,7 @@ export const getCmakeGenerator = memoizee(
}

// use native generator
logger.debug(`Using generator: native for ${os} ${arch}`)
logger.debug(`Found generator: native for ${os} ${arch}`)
return {
generator: "native",
binary: undefined,
Expand Down
15 changes: 15 additions & 0 deletions src/whichNoThrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import which from "which";

/**
* Wraps `which` to return `null` instead of throwing an error if the command is not found,
* matching the v3 behaviour if `nothrow: true` option is used.
*
* `@types/which@2.0.2` still show this `nothrow` parameter, even though it's not respected in v2.
*/
export const whichNoThrow = async (cmd: string, options?: which.Options): Promise<Awaited<ReturnType<typeof which>> | null> => {
try {
return await which(cmd, options);
} catch {
return null;
}
};
Loading