` Compiling V8 on the Raspberry Pi 2

Compiling V8 on the Raspberry Pi 2

For a new project I needed d8, a shell for V8 Javascript Engine on my Raspberry Pi 2 running 2015-02-16-raspbian-wheezy. After some digging around it turned out to be fairly straightforwards to build from source but in case it helps anyone else, here are the steps I took.

I started by looking at the offocial instructions for building V8 with GYP. First though, we'll need to install gcc/g++ 4.8. I had some errors trying to use the older 4.6 version that comes with this Raspbian build but 4.8 worked OK. Luckily 4.8 is already available as a package for Raspbian, so easy enough to install it and switch the links /usr/bin/gcc and /usr/bin/g++ to point to the 4.8 binaries.

sudo apt-get install gcc-4.8 g++-4.8
cd /usr/bin
sudo rm g++
sudo ln -s /usr/bin/g++-4.8 g++
sudo rm gcc
sudo ln -s /usr/bin/gcc-4.8 gcc                

Next we need to get hold of the depot tools needed to build V8 (and other software like chromium) into ~/depot_tools

cd ~
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git                

Now we can use the fetch tool to download the V8 source code and its dependencies

export PATH=~/depot_tools:$PATH
fetch v8        

This will take a while, but the build itself will take even longer - a couple of hours even when configured to run 4 tasks in parallel (-J 4). This would however not have been very practical on the original (V1) pi. Before the build I had to make a tweak to one of the build files to workaround a problem. To do the same, edit the file ~/v8/testing/gtest.gyp and remove 'host' from the toolsets list in the gtest target, afterwards the first part of the file should look like this:

# Copyright 2014 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

  'targets': [
      'target_name': 'gtest',
      'toolsets': ['target'],
      'type': 'static_library',

Now you should be able to start the build:

cd v8
make native -j 4 hardfp=on vf3p=off

Once the build completes, return the gcc and g++ links to the old 4.6 compiler.

cd /usr/bin
sudo rm g++
sudo ln -s /usr/bin/g++-4.6 g++
sudo rm gcc
sudo ln -s /usr/bin/gcc-4.6 gcc                         

Running the unit tests is recommended...

cd ~/v8/out/native

You should be able to start up the d8 shell and run Javascript code....

cd ~/v8/out/native
V8 version 4.3.0 (candidate) [console: dumb]
d8> fac = function(i) { if (i==0) return 1; else return i*fac(i-1); }
function (i) { if (i==0) return 1; else return i*fac(i-1); }
d8> fac(12)


Leave a comment

Anti-Spam Check