Cmake Emscripten Example

By default, Emscripten creates a module which can be used from both Node.JS and the browser, but it has the following issues:

Build ignition libraries with emscripten (WIP). GitHub Gist: instantly share code, notes, and snippets.

  1. The module pollutes the global namespace
  2. The module is created with the name Module (in my case, I require streamingPercentiles)
  3. The module cannot be loaded by some module loaders such as require.js
Cmake Emscripten Example

I can build and run it successfully without emscripten: cmake -DCMAKEBUILDTYPE=RelWithDebInfo -G 'Unix Makefiles'. output looks good make command works Everything there is fine and I can run the output, however when I try using Emscripten like this (which has worked for me in the past). Note Checkout node runpuppeteer -help for more options to debug and reporting. The command npm install only needs to be executed once, since installs the tools dependencies; after that they are ready to use. Use PUPPETEERSKIPCHROMIUMDOWNLOAD=1 npm install -no-save puppeteer to skip automatic downloading of Chromium. You may specify own Chromium/Chrome binary through.

While the above issues can (mostly) be corrected by using –s MODULARIZE=1, it changes the semantics of the resulting JavaScript file, as the module now returns a function rather than an object. For example, code which previously read var x = new Module.Klass() would become var x = new Module().Klass(). I found this semantic change unacceptable, so I decided to abandon Emscripten’s -s MODULARIZE=1 option in favor of hand-crafting a UMD module.

I determined that the most appropriate pattern for my use case was the no dependencies pattern from UMD’s templates/returnExports.js. Applied to an Emscripten module, and using the default module name streamingPercentiles, the stanzas look like the following:

Emscripten

umdprefix.js:

umdsuffix.js:

While I might be able to use Emscripten’s --pre-js and --post-js“s options to prepend and append the above JavaScript files, these options do not guarantee in all cases that the above JavaScript files will be first and last. Therefore, I decided to prepend and append the JavaScript manually.

EmscriptenCmake

As my build system is CMake based, I needed to change change the compilation process to generate an intermediate file streamingPercentiles-unwrapped.v1.js, and then use some CMake magic to prepend and append the above JavaScript files:

With the above code, all of the original three issues are fixed without any semantic changes for users.

By default, Emscripten creates a module which can be used from both Node.JS and the browser, but it has the following issues:

  1. The module pollutes the global namespace
  2. The module is created with the name Module (in my case, I require streamingPercentiles)
  3. The module cannot be loaded by some module loaders such as require.js

While the above issues can (mostly) be corrected by using –s MODULARIZE=1, it changes the semantics of the resulting JavaScript file, as the module now returns a function rather than an object. For example, code which previously read var x = new Module.Klass() would become var x = new Module().Klass(). I found this semantic change unacceptable, so I decided to abandon Emscripten’s -s MODULARIZE=1 option in favor of hand-crafting a UMD module.

Emscripten

I determined that the most appropriate pattern for my use case was the no dependencies pattern from UMD’s templates/returnExports.js. Applied to an Emscripten module, and using the default module name streamingPercentiles, the stanzas look like the following:

umdprefix.js:

umdsuffix.js:

Cmake Emscripten Example Form

While I might be able to use Emscripten’s --pre-js and --post-js“s options to prepend and append the above JavaScript files, these options do not guarantee in all cases that the above JavaScript files will be first and last. Therefore, I decided to prepend and append the JavaScript manually.

As my build system is CMake based, I needed to change change the compilation process to generate an intermediate file streamingPercentiles-unwrapped.v1.js, and then use some CMake magic to prepend and append the above JavaScript files:

Cmake Emscripten Example Video

With the above code, all of the original three issues are fixed without any semantic changes for users.