Saturday, October 2, 2010

How to build mod_v8 in Ubuntu

mod_v8 is a simple Apache module which enable Server Side JavaScript(SSJS) support in Apache Server. It uses Google V8 engine for the JavaScript execution. What this module basically does is, it adds a Handler for a particular file extension(.v8) and then, those files are treated as SSJS files which then get executed via V8 engine.

While I was building this amazing mod_v8 module, I had to do small modification to the process. So I though it might be worth to have a post with the steps I followed.

1. Checkout V8 development branch
svn checkout http://v8.googlecode.com/svn/branches/bleeding_edge/ v8

2. Build V8 using scons with the library=shared option. Optionally you might need to add arch option too. You can find more info about it at Building V8 in Ubuntu 64 bit
scons library=shared arch=x64

3. Assuming your v8 checkout is at /home/wageesha/v8, building process will create following obj, libv8.so etc.. directories and files within v8 directory. You need to copy this libv8.so into /usr/lib.
cp ~/wageesha/v8/libv8.so /usr/lib

4. Checkout mod_v8 code
svn checkout https://svn.i-want-a-pony.com/repos/mod_v8/trunk/ mod_v8

5. Build mod_v8 using scons. Depending on your scons version, you might need to change
opts = Options('build.py') into opts = Variables('build.py')
and
opts.Add(PathOption(....)) into opts.Add(PathVariable(....))

in the SConstruct. Further, if you haven't copied libv8.so into the /usr/lib directory as in step 3, build process will fail saying
/usr/bin/ld: cannot find -lv8

6. Then open /etc/apache2/apache2.conf and add following lines, with the correct path to libmod_v8.so
LoadModule v8_module /home/wageesha/mod_v8/trunk/libmod_v8.so

<IfModule mod_v8.cpp>
#Adds the v8 handler
AddHandler v8-script .v8
</IfModule>

7. Copy /home/wageesha/mod_v8/trunk/test.v8 into /var/www folder.

8. Restart apache and access test.v8
http://localhost/test.v8

which will then print
Hello World!

in the browser.

Friday, October 1, 2010

Building V8 in Ubuntu 64 bit

If you are trying to build Google V8 in a Ubuntu 64bit machine you might get
/usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h: No such file or directory
error when you ran scons with default arguments. When default arguments are used, it tries to guess the OS. In my case it failed to detect the correct architecture (i.e. x64). So you can specify arch argument when you run the scons to manually set the architecture. i.e.
scons arch=x64
arch argument accepts any of arm, ia32, x64 or mips