Skip to content

TUTORIAL 03 Using CMake.js based native modules with nw.js

Jay Sistar edited this page Jun 5, 2017 · 9 revisions

About

If you're in making an NW.js application, then you know about its building process which involves nw-gyp. Well that's an ugly hack, isn't it?

The good news is if you create your native module based on CMake.js then your module will be compatible with nw.js out-of-the-box.

The module

Ok, let's rock! We take the native module called 'cmake-js-tut-01-module' implemented during the first tutorial and calculate the PI by using it in an nw.js 0.12.1 based application, like a boss.

The package.json

Create a directory for your application. Add a package.json file with the follwoing content:

{
  "name": "cmake-js-tut-03-nwjs-app",
  "description": "nw.js application demonstrating CMake.js out-of-the-box nw.js compatibility",
  "version": "1.0.0",
  "main": "index.html",
  "dependencies": {
    "cmake-js-tut-01-module": "unbornchikken/cmake-js-tut-01-module"
  },
  "cmake-js": {
    "runtime": "nw",
    "runtimeVersion": "0.12.1"
  }
}

The mentioned module hasn't been published to npmjs.com, so you have to reference it from Github.

CMake.js build system have to know somehow that you're using it's modules in an nw.js application. That's why cmake-js key is needed.

Available settings:

  • runtime: application's target runtime, possible values are:
    • node: Node.js
    • iojs: io.js
    • nw: NW.js
  • runtimeVersion: version of the application's target runtime, for example: 0.12.1
  • arch: architecutre of appication's target runtime (eg: x64, ia32, arm). Notice: on non-Windows systems the C++ toolset's architecture's gonna be used despite of 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.

So, the actual runtime is nw, runtimeVersion is 0.12.1 (or anything that you've downloaded at the time of reading). You can skip the arch setting for Windows if you download nw.js for exactly the same architecture that node/io.js is using on your system.

The application

We're just using an index.html with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>CMake.js with nw.js</title>
</head>
<body>
<h1>nw.js application demonstrating CMake.js <span style="white-space: nowrap">out-of-the-box</span> nw.js compatibility</h1>
<p>This nw.js application uses
    <a href="https://github.com/unbornchikken/cmake-js/wiki/TUTORIAL-01-Creating-a-native-module-by-using-CMake.js-and-NAN" target="_blank">CMake.js demo module (cmake-js-tut-01-module) from the first tutorial</a>.
    There is no <a href="https://github.com/nwjs/nw.js/wiki/Using-Node-modules#3rd-party-modules-with-cc-addons" target="_blank">nw-gyp like magic</a> required,
    <strong>all CMake.js native modules are compatible with nw.js <span style="white-space: nowrap">out-of-the-box</span></strong>. <a>See the third CMake.js tutorial for details.</a></p>
<p>We're calculating PI synchronously to see if
    <a href="https://github.com/unbornchikken/cmake-js-tut-01-module" target="_blank">cmake-js-tut-01-module</a>
    works as intended:</p>
<p>PI:
    <script>
        var addon = require("cmake-js-tut-01-module");
        var calculations = 10000000;
        var pi = addon.calculateSync(calculations);
        document.write(pi);
    </script>
</p>.
</body>
</html>

The script references the PI calculator native addon, and write it's synchronous result to the document.

The magic

Ok, now enter npm install. Just a few second, and the native module gets installed. There is nothing else to do, just enter:

nw .

Then you get:

Download

The example application is on Github.

git clone https://github.com/unbornchikken/cmake-js-tut-03-nwjs-app.git