Building Mozilla SpiderMonkey v1.7.0 on Windows XP using MinGW

Mozilla SpiderMonkey is a stable, mature, open-source JavaScript engine, ideal for inclusion in any C/C++ software that requires an integrated scripting language.

This page exists because my primary development environment is MinGW-based, and Mozilla offers no obvious support for building the SpiderMonkey JavaScript engine in a MinGW environment. Despite hours of googling, I found unusually little information on this topic (aside from people asking the same question I was, and receiving no reply).

In the absence of a third-party tutorial, I bit the bullet and spent the time to pour over the SpiderMonkey makefiles and modify them to support building on a Windows platform via MinGW. In the end, it wasn't that difficult, but there were nonobvious pitfalls along the way. I've posted this document online with the hopes of helping others who have the same goals.

This document explains step-by-step how to compile SpiderMonkey v1.7.0 under MinGW using GNU Tools.

Contents

  1. MinGW Environment
  2. Required Files
  3. Installation
  4. Build Instructions
  5. Additional Links

MinGW Environment

It is beyond the scope of this document to describe installing and configuring MinGW from scratch. Nonetheless, if you follow these instructions for configuring MinGW/MSys/Eclipse and the wxWidgets library, everything should go quite smoothly.

From this point on, it is expected that you have a working MinGW and MSys environment, and are able to compile a simple program (as evidence of a correct configuration).

Required Files

Download all of these:

Install Files & Set Up Paths

  1. First you'll want to choose a base directory. Something like C:\SpiderMonkey or C:\Programming\Mozilla should work nicely. (The build process may not work if there are spaces in the pathname). Create that directory.

  2. Unzip the Netscape Portable Runtime ZIP files as follows:

    1. Unzip nspr-4.6.4_OPT.zip into your base directory. It should create a folder called nspr-4.6.4. Rename that folder to nspr-4.6.4_OPT.
    2. Unzip nspr-4.6.4_DBG.zip into your base directory. Rename the new nspr-4.6.4 folder to nspr-4.6.4_DBG.
    3. Unzip nspr-4.6.4_IDG.zip into your base directory. Rename the new nspr-4.6.4 folder to nspr-4.6.4_IDG.

    You no longer need the original ZIP files, so store them away or delete them as you see fit.

  3. Move preparePathsForJSBuild.bat and genProxyHeader.bat into your base directory.

  4. Unzip js-1.7.0.tar.gz into your base directory. It should create a folder called js (if you can't open a tar.gz file, use WinRAR). Technically, you're done with this GZ file and could delete it, but we'll be overwriting some files soon, and you may want to have it around in case you ever want to restore the originals.

  5. Now it's time to overwrite those nasty non-MinGW-friendly makefiles with my modified versions. I made some effort to avoid breaking them for other build targets (such as GCC on Linux or even MSVC on Windows), but I was more concerned with getting it working than being super-careful. In other words, don't use these makefiles for building on anything other than MinGW.

    Unzip js-1.7.0-MinGW-Makefiles.zip into your base directory. You must overwrite each file when prompted.

  6. Now for the GCC bug workaround. First, find out what version of GCC you're running by opening MSys and typing gcc -v. If GCC reports a version number less than 4.0.0, you'll want to overwrite base\js\src\jsapi.c with the modified version you downloaded above.

    If you don't use the replacement file and later get a bunch of compilation errors in jsapi.c saying initializer element is not constant, then this replacement file should fix those.

  7. Last but not least, we hammer away at the NSPR headers and libraries to get them into the directory structure expected by the makefiles. I wrote these batch files to create the necessary directories, to generate proxy headers that correctly reference the Debug/Release/IDG NSPR headers depending on the current make target, and to copy/rename the NSPR libraries so the Debug/Release/IDG versions can coexist in the same directory.

    Open a command prompt (Windows, not MSys) and navigate to your base directory. Then run preparePathsForJSBuild.bat "4.6.4".

  8. That's it for the setup. Your directory structure under base should look like this:

    (Not a fully-expanded view)

Build

Building should be relatively painless. Take note of the full path to your base directory, and substitute it wherever you see [base] below. Take note of the full path to your MinGW libraries directory (usually C:\Program Files\MinGW\lib) and substitute it wherever you see [mingwlib] below.

Be sure to use Linux-style path format for those directories (ie: /c/programming/mozilla instead of c:\programming\mozilla). Don't use a trailing slash. Make sure you use the short name for any paths that have spaces in them (ie: /c/progra~1/somepath instead of c:\program files\somepath).

The JS_THREADSAFE parameter may be omitted if you do not require thread-safe libraries.

Open an MSys window and navigate to your base directory. Run the following commands:

  1. cd js/src/fdlibm
  2. make -f Makefile.ref JS_THREADSAFE=1 JS_DIST=[base]/dist XLDFLAGS="-L[mingwlib]" BUILD_OPT=1
  3. make -f Makefile.ref JS_THREADSAFE=1 JS_DIST=[base]/dist XLDFLAGS="-L[mingwlib]"
  4. make -f Makefile.ref JS_THREADSAFE=1 JS_DIST=[base]/dist XLDFLAGS="-L[mingwlib]" BUILD_IDG=1
  5. cd ..
  6. make -f Makefile.ref JS_THREADSAFE=1 JS_DIST=[base]/dist XLDFLAGS="-L[mingwlib]" BUILD_OPT=1
  7. make -f Makefile.ref JS_THREADSAFE=1 JS_DIST=[base]/dist XLDFLAGS="-L[mingwlib]"
  8. make -f Makefile.ref JS_THREADSAFE=1 JS_DIST=[base]/dist XLDFLAGS="-L[mingwlib]" BUILD_IDG=1

You should now have three output directories containing all the compiled object files (*.o), archives and import-libraries (*.a), import definitions (*.def), dynamic-link libraries (*.dll), and executables (*.exe). The directories are [base]\js\src\MinGW5.1_OPT.OBJ (release version), [base]\js\src\MinGW5.1_DBG.OBJ (debug version), and [base]\js\src\MinGW5.1_DBG.OBJD (IDG version).



Written by Adam Rogers
Contact: adam [at] jargon [dot] ca
Last updated 2008-09-07