19 May, 2020

Problem

You’ve installed Emscripten on Windows, but you can’t get it to work with CMake.

Solution

Install CMake

It may seem obvious, but first you should install CMake and make sure it’s on your path.

Here’s how to install CMake with the scoop package manager.

C:> scoop install cmake

Similarly, here’s how to install CMake with the chocolatey package manager.

C:> choco install -y cmake --installargs 'ADD_CMAKE_TO_PATH=User'

The --installargs 'ADD_CMAKE_TO_PATH=User parameter tells chocolatey to add the CMake binaries such as cmake and cmake-gui to your path.

If you don’t have chocolatey or scoop, or don’t want to use them, then there are other options such as downloading and installing CMake binaries.

Set your Emscripten environment

Run emsdk_env.bat from the Emscripten SDK to set the environment.

C:> D:\Tools\emsdk\emsdk_env.bat
Adding directories to PATH:
PATH += D:\Tools\emsdk
PATH += D:\Tools\emsdk\upstream\emscripten
PATH += D:\Tools\emsdk\node\12.9.1_64bit\bin
PATH += D:\Tools\emsdk\java\8.152_64bit\bin

Setting environment variables:
EMSDK = D:/Tools/emsdk
EM_CONFIG = C:\Users\Rod\.emscripten
EMSDK_NODE = D:\Tools\emsdk\node\12.9.1_64bit\bin\node.exe
EMSDK_PYTHON = D:\Tools\emsdk\python\3.7.4-pywin32_64bit\python.exe
JAVA_HOME = D:\Tools\emsdk\java\8.152_64bit

Run CMake via emcmake

Use emcmake cmake to run CMake with an environment suitable for Emscripten.

Using Ninja

If you want to use Ninja to build your projects, then run the following from your source directory:

C:> emcmake cmake . -B cmake-build-emscripten -G Ninja

This runs cmake via the emcmake tool which sets up the environment that CMake needs to use Emscripten. It uses -G Ninja to tell CMake to generate a build pipeline that uses Ninja.

If you don’t have Ninja installed, then the easiest way of getting it is to use a package manager such as chocolatey or scoop.

Here’s how to install Ninja with scoop.

C:> scoop install ninja

And here’s how to install Ninja with chocolatey.

C:> choco install ninja

Using Makefiles

If you would like to use traditional makefiles to build your projects, then run the following from your source directory:

C:> emcmake cmake . -B cmake-build-emscripten -G "CodeBlocks - MinGW Makefiles"

This runs cmake via the emcmake tool which sets up the environment that CMake needs to use Emscripten. It uses -G "CodeBlocks - MinGW Makefiles" to tell CMake to generate a build pipeline that uses makefiles.

If you don’t have a suitable make program installed (nmake is not suitable), then one of the simplest ways to get one is to add MinGW to your Emscripten SDK.

Run emsdk list to get a list of available packages, and find mingw in the output.

C:> emsdk list
...
The following precompiled tool packages are available for download:
     *     releases-upstream-048cf9424790cc525a7ea6da340820aae226f3b9-64bit     INSTALLED
     *     releases-upstream-3b8cff670e9233a6623563add831647e8689a86b-64bit     INSTALLED
           releases-fastcomp-048cf9424790cc525a7ea6da340820aae226f3b9-64bit
           releases-fastcomp-3b8cff670e9233a6623563add831647e8689a86b-64bit
           fastcomp-clang-e1.38.30-64bit
           fastcomp-clang-e1.38.31-64bit
           node-4.1.1-64bit
           node-8.9.1-64bit
     *     node-12.9.1-64bit            INSTALLED
           python-2.7.13.1-64bit
           python-3.7.4-64bit
     *     python-3.7.4-pywin32-64bit   INSTALLED
     *     java-8.152-64bit             INSTALLED
           emscripten-1.38.30
           emscripten-1.38.31
           gnu-2.5.4
           mingw-7.1.0-64bit

Install and activate mingw.

C:> emsdk install mingw-7.1.0-64bit
C:> emsdk activate mingw-7.1.0-64bit

This gets you a make program. The MinGW version of this is called mingw32-make.

Build your project

Now you should be able to build your project by running cmake --build followed by the name of the directory that CMake created earlier.

C:> cmake --build cmake-build-emscripten

If you opted to use makefiles and got an error saying that it can’t find mingw32-make, then re-run emsdk-env.bat to add the MinGW binaries to the path and try again.