Building v8 on Arch Linux

My original goal was to script some parts of a Go application using javascript. Knowing chromium components were portable, and that Nodejs was built with v8, I wanted to try it as an experimental platform. This article relates my (little) journey through building the v8 engine on an up-to-date arch linux. In this post, I assume that you already have a working build system (with clang, etc). See the base-devel group for more information.

Update 1: After building v8, I ran into some issues to build the v8 samples myself, so I added the relevant steps that had to be done to get them to link.

Get the depot_tools

The first step is to get chromium's depot_tools, as this package gives you all the needed tools and commands to get and build the code. Actually, it's pretty simple to get them, just run through the following commands, from the folder of your choice :

git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH=`pwd`/depot_tools:"$PATH"

You might want to add the export line at the end of your .bashrc (and replace the pwd with an absolute path) in order to persist the path modification.

Fetch the v8 source

To get the v8 source code, choose the folder you want to work in, and run

fetch v8
cd v8

After some time, you'll end up with a ready-to-use™ file tree, ready to be built. It might weight over 1 gigabyte. Now, to build v8, the first thing to do is to generate the build files, and then launch the build :

tools/dev/v8gen.py x64.release
ninja -C out.gn/x64.release
Static build

As mentionned by @stricker in the comments, you may want to edit the default build arguments in order to have a static build :

gn args out.gn/x64.release

And then add/edit the following lines :

is_component_build = false
v8_static_library = true

This is the default workflow to build v8. If you want to get more information about it, or even customize it, please refer to this document on the v8 wiki. But I wouldn't write this post if it was that easy. If you launch this, you'll actually end-up with some errors early, telling you that libtinfo.so.6 doesn't exist. And that's a problem because you can't just pacman it.

Finding nemo libtinfo.so.6

As libtinfo.so.6 doesn't exist in the standard packages, we have to find another way to get it. Hopefully for us, it's available on AUR (Arch User Repository). We'll need two AUR packages actually:

  1. libtinfo AUR link
  2. libtinfo5 AUR link

If you know how to build AUR packages just skip to the next section.

Now, if you visit the link of those packages, you'll see that there's a git repository linked to each of them, and that inside these repositories, there's a PKGBUILD file. This file is a description of the process needed to build the package, and is used by makepkg. Here's the process to follow:

git clone <the aur repo>
cd <were you cloned>
makepkg -Acs

The makepkg will automatically use the PKGBUILD file in the current folder.

The -A option ignores the target Arch architecture. The -c option cleans up the directory after makepkg is done, and -s installs the needed dependencies.
stackexchange

Once done, you'll end-up with an archive next to your PKGBUILD file. You can install it using pacman -U <archive file>. And then you're done ! :)

Going back to v8

Once you've installed libtinfo and libtinfo5, you'll have to try again the ninja command that we used before. Normally everything should be fine and build smoothly. Enjoy :D

Building the samples

After building v8, I wanted to check that my v8 build was actually working. Following the Getting Started with Embedding, I tried to run the provided command after building the static libraries:

g++ -I. -Iinclude samples/hello-world.cc -o hello-world -Wl,--start-group out.gn/x64.release/obj/{libv8_{base,libbase,external_snapshot,libplatform,libsampler},third_party/icu/libicu{uc,i18n},src/inspector/libinspector}.a -Wl,--end-group -lrt -ldl -pthread -std=c++0x

Which gave the following output:

As you can see, all the (really long, 2793 lines) output is made of errors. But all of them are undefined references that refers to the std:: namespace, which indicates a standard library problem. Adding -lstdc++ doesn't change anything, so it must be the libc++ library that causes the problem. Again, it's an AUR package that you have to install, using the same process that we used to build libtinfo. Once built and installed, just slightly change the build command to link against our brand new library:

g++ -I. -Iinclude samples/hello-world.cc -o hello-world -Wl,--start-group out.gn/x64.release/obj/{libv8_{base,libbase,external_snapshot,libplatform,libsampler},third_party/icu/libicu{uc,i18n},src/inspector/libinspector}.a -Wl,--end-group -lrt -ldl -pthread -std=c++0x -lc++

And that should be enough to get it to build. Just don't forget to copy the natives_blob.bin and snapshot_blob.bin files from the out.gn/x64.release folder to the same folder as the final executable.


If you have any questions, or something is incorrect, tell me in the comments.
Header picture