Building and compiling V8 on Mac OS X

Now that Google Chrome is released, the project page and repository of the V8 JavaScript engine is revealed and available for public hacking. This guide shows how to get the source of V8 and build a V8 JavaScript shell on Mac OS X Leopard.

Mac OS X fulfills all the V8 build requirements except for SCons, which is a build system written in Python. Think make, but better. That's the SCons project goal. MacPorts (and by extension, Porticus) only has 0.98.4_0, not the 1.0.0 that V8 requires, so downloading the latest stable tarball would do. After untarring the tarball, how to install SCons is not immediately obvious. According to README.txt, this is what you should do (and what I did): open a terminal, cd into the untarred directory, then

koeji:kourge$ sudo python install

sudo is needed because files are copied to various system locations only writable to root. After the installation, use which to check if everything went well:

koeji:kourge$ which scons

You should get similar output.

After installing SCons, the first step is to get the source. Thus a simple svn co should suffice:

koeji:kourge$ svn checkout v8-read-only

After checking out and cding to the v8-read-only directory (or whatever directory you specified), simply run the following to build V8 as a library:

koeji:kourge$ scons

A file named libv8.a should appear after pages and streams of text float across the screen (like any compiling should). This library is not consumable (as a shell) yet, and thankfully, a shell is already included in the samples/ directory in source code form. All you need to do is compile it:

koeji:kourge$ g++ -Iinclude samples/ -o v8 libv8.a -lpthread

This will generate an executable called v8 in the current directory:

koeji:kourge$ ./v8
V8 version 0.2.5

The shell supports basic functions like print, load, quit, and version. Passing it a list of filenames as arguments will load and run the scripts. If you add the option --shell along the list of filenames, it will also show the prompt and not immediately exit after loading and running the scripts.

Hack away!

An easier way to build V8

An easier way to build V8 with shell is running "scons sample=shell".

Thanks for making that note;

Thanks for making that note; I believe the sample flag was added a short time frame after I wrote this post.
By using sample=shell, the name of the compiled executable would be named "shell", not "v8".