diff --git a/README.md b/README.md index cee138b..cd0466e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/package.json b/package.json index 2f115bb..fe7936d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -66,14 +66,30 @@ "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 ", - "contributors": ["Amin Yahyaabadi ", "Martin Koppehel "], + "contributors": [ + "Amin Yahyaabadi ", + "Martin Koppehel " + ], "license": "MIT", "bugs": { "url": "https://github.com/EmbeddedEnterprises/cmake-ts/issues" @@ -81,51 +97,55 @@ "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" + ] } } diff --git a/src/argumentBuilder.ts b/src/argumentBuilder.ts index 1d5beff..e29e21a 100644 --- a/src/argumentBuilder.ts +++ b/src/argumentBuilder.ts @@ -157,9 +157,9 @@ export function getCMakeArchitecture(arch: NodeJS.Architecture, os: NodeJS.Platf const cmakeArchMap: Record> = { win32: { - arm64: "arm64", - x64: "AMD64", - ia32: "X86", + arm64: "ARM64", + x64: "x64", + ia32: "Win32", }, darwin: { arm64: "arm64", diff --git a/src/config.ts b/src/config.ts index f14cd62..2faee49 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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, @@ -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 diff --git a/src/generator.ts b/src/generator.ts index e6a6477..cb2e650 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -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 ( @@ -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, @@ -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, @@ -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, diff --git a/src/whichNoThrow.ts b/src/whichNoThrow.ts new file mode 100644 index 0000000..3644f25 --- /dev/null +++ b/src/whichNoThrow.ts @@ -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> | null> => { + try { + return await which(cmd, options); + } catch { + return null; + } +};