Using Boost Build with Emscripten
Sunday August 11, 2013 23:45:43

A few months back, I tried to use Emscripten, an amazing tool which compiles C++ code to Javascript using LLVM and found it was incredibly easy to install and set up (this may be because I did so on Linux. At this point, I've got a nice workflow using a Ubuntu VM on my Windows box, don't expect a giant blog post on how to make this stuff work on that platform).

Boost Build is my current C++ build tool of choice, so of course I got Emscripten set up as a custom toolset and figured I'd document that simple process here.

If you haven't already, install and configure Emscripten so that you can compile simple C++ apps using it's provided tools directly on the command line. I highly recommend following this tutorial and getting to the point where you can compile hello_world.cpp into a.out.js.

Next, you need to edit your site-config.jam file, typically found in ~/site-config.jam, to contain the following:

using clang
	: emscripten
	: /home/tim/Tools/emscripten/em++
	:	<root>/home/tim/Tools/emscripten	
		<archiver>/home/tim/Tools/emscripten/emar
		<ranlib>/home/tim/Tools/emscripten/emranlib
		<linker>/home/tim/Tools/emscripten/emlink
		<cxxflags>" -std=c++11 "
		<linkflags>"-lstdc++"	
;

import type : change-generated-target-suffix ;
type.change-generated-target-suffix EXE : <toolset-clang:version>emscripten : js ;

Hopefully this is obvious, but I installed emscripten to /home/tim/Tools/emscripten- you'll need to change the above filepaths to point to wherever you git cloned emscripten.

The “using clang” section is similar to how I got the KallistiOS Dreamcast compiler to work. The tricky part was
making sure that Boost Build “exe” targets got “.js” appended to them; emscripten will compile to Javascript files only by if the output file extension is “.js”, so without that line things didn't work correctly. (related reading). Another thing to note is that the compiling things with link=static causes numerous archive (“.a”) files to be created, which the official Emscripten docs state to avoid. So for now I'm leaving that option alone when compiling Javascript “executables”.

Another thing that may or may not interest you is unit test support. Now, the Boost Test library won't work with Emscripten, as is the case with many other C++ libraries- it is targeting Javascript after all. However, all the Boost Build “unit-test” rule cares about is that some file is created which is invoked either directly as an executable or using some other tool and results in a zero exit code. This means you can use the “<testing.launcher>” property to specify that Node.JS be used to execute the tests, making it possible to unit test the compiled Javascript.

One more caveat is that in order to keep Node.JS from *always* being used to run the tests even when you're building for a different platform you'll have to define the unit-test rule multiple times, each time with a property noting the given toolset. I created a nifty Macaroni script to automatically add all such targets, but I'm not ready to show it or the game engine off, so for now here's what the generated Jamroot.jam file looks like:

# Tests
# ---------

# PrimitivesTest
unit-test MACARONI_TEST_TARGET_PrimitivesTest
    :   # Sources:
/MACARONI_PROJECT_Lp3___Lp3Project___DEV//Lp3Test
        Lp3_Core
        9342569157862752302
    :
    ;
unit-test MACARONI_TEST_TARGET_PrimitivesTest
    :   # Sources:
/MACARONI_PROJECT_Lp3___Lp3Project___DEV//Lp3Test
        Lp3_Core
        9342569157862752302
    :  <toolset-gcc:version>dreamcast
            <testing.launcher>"dctest "
    ;
unit-test MACARONI_TEST_TARGET_PrimitivesTest
    :   # Sources:
/MACARONI_PROJECT_Lp3___Lp3Project___DEV//Lp3Test
        Lp3_Core
        9342569157862752302
    :  <toolset-clang:version>emscripten
            <testing.launcher>"node "
    ;





<---2013-08-25 03:20:19 history 2013-06-15 18:07:35--->



-All material © 2007 Tim Simpson unless otherwise noted-