Skip to content
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

Auto-detect Visual Studio 2019 platform #201

Merged
merged 3 commits into from Feb 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -128,6 +128,7 @@ Options:
available (Posix) [boolean]
-G, --generator use specified generator [string]
-t, --toolset use specified toolset [string]
-A, --platform use specified platform name [string]
-T, --target only build the specified target [string]
-C, --prefer-clang use Clang compiler instead of default CMake compiler,
if available (Posix) [boolean]
Expand Down Expand Up @@ -281,6 +282,8 @@ This will print during configure:

### Runtimes

If any of the `runtime`, `runtimeVersion`, or `arch` configuration parameters is not explicitly configured, sensible defaults will be auto-detected based on the JavaScript environment where CMake.js runs within.

You can configure runtimes for compiling target for all depending CMake.js modules in an application. Define a `cmake-js` key in the application's root `package.json` file, eg.:

```json
Expand All @@ -304,15 +307,15 @@ Available settings:
- `nw`: nw.js
- `electron`: Electron
- **runtimeVersion**: version of the application's target runtime, for example: `0.12.1`
- **arch**: architecture of application's target runtime (eg: `x64`, `ia32`, `arm`). *Notice: on non-Windows systems the C++ toolset's architecture's gonna be used despite this setting. If you don't specify this on Windows, then architecture of the main node/io.js runtime is gonna be used, so you have to choose a matching nw.js runtime.*
- **arch**: architecture of application's target runtime (eg: `x64`, `ia32`, `arm64`, `arm`). *Notice: on non-Windows systems the C++ toolset's architecture's gonna be used despite this setting. If you don't specify this on Windows, then architecture of the main node/io.js runtime is gonna be used, so you have to choose a matching nw.js runtime.*

#### Runtime options in CMakeLists.txt

The actual node runtime parameters are detectable in CMakeLists.txt files, the following variables are set:

- **NODE_RUNTIME**: `"node"`, `"nw"`, `"electron"`
- **NODE_RUNTIMEVERSION**: for example: `"0.12.1"`
- **NODE_ARCH**: `"x64"`, `"ia32"`, `"arm"`
- **NODE_ARCH**: `"x64"`, `"ia32"`, `"arm64"`, `"arm"`

#### NW.js

Expand Down Expand Up @@ -406,7 +409,6 @@ and add it to the include directories of your *CMake* project file
`CMakeLists.txt`:

```cmake

# Include N-API wrappers
execute_process(COMMAND node -p "require('node-addon-api').include"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Expand Down
7 changes: 7 additions & 0 deletions bin/cmake-js
Expand Up @@ -102,6 +102,12 @@ var yargs = require("yargs")
describe: "use specified toolset",
type: "string"
},
A: {
alias: "platform",
demand: false,
describe: "use specified platform name",
type: "string"
},
T: {
alias: "target",
demand: false,
Expand Down Expand Up @@ -193,6 +199,7 @@ var options = {
cmakePath: argv.c || null,
generator: argv.G,
toolset: argv.t,
platform: argv.A,
target: argv.T,
preferMake: argv.m,
preferXcode: argv.x,
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
@@ -1,3 +1,8 @@
unreleased
==========

- Add support for "-A/--platform" option to make target platform selectable for Visual Studio 2019 generator: https://github.com/cmake-js/cmake-js/pull/201

v6.0.0 - 30/09/19
=================

Expand Down
3 changes: 3 additions & 0 deletions lib/cMake.js
Expand Up @@ -190,6 +190,9 @@ CMake.prototype.getConfigureCommand = async function () {
if (this.toolset.generator) {
command += " -G\"" + this.toolset.generator + "\"";
}
if (this.toolset.platform) {
command += " -A\"" + this.toolset.platform + "\"";
}
if (this.toolset.toolset) {
command += " -T\"" + this.toolset.toolset + "\"";
}
Expand Down
24 changes: 24 additions & 0 deletions lib/toolset.js
Expand Up @@ -15,6 +15,7 @@ function Toolset(options) {
this.targetOptions = new TargetOptions(this.options);
this.generator = options.generator;
this.toolset = options.toolset;
this.platform = options.platform;
this.target = options.target;
this.cCompilerPath = options.cCompilerPath;
this.cppCompilerPath = options.cppCompilerPath;
Expand Down Expand Up @@ -170,6 +171,29 @@ Toolset.prototype.initializeWin = async function (install) {
}
this.linkerFlags.push("/SAFESEH:NO");
}

// The CMake Visual Studio Generator does not support the Win64 or ARM suffix on
// the generator name. Instead the generator platform must be set explicitly via
// the platform parameter
if (!this.platform && this.generator.startsWith("Visual Studio 16")) {
switch(this.targetOptions.arch) {
case "ia32":
this.platform = "Win32";
break;
case "x64":
this.platform = "x64";
break;
case "arm":
this.platform = "ARM";
break;
case "arm64":
this.platform = "ARM64";
break;
default:
this.log.warn("TOOL", "Unknown NodeJS architecture: " + this.targetOptions.arch);
break;
}
}
}
else {
throw new Error("There is no Visual C++ compiler installed. Install Visual C++ Build Toolset or Visual Studio.");
Expand Down