Skip to content

Instantly share code, notes, and snippets.

@Jiab77
Last active January 11, 2017 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jiab77/e59841227d1f9c7d2877862ced673ec6 to your computer and use it in GitHub Desktop.
Save Jiab77/e59841227d1f9c7d2877862ced673ec6 to your computer and use it in GitHub Desktop.
v8js compilation for Raspberry Pi 3 Model B

v8js compilation for Raspberry Pi 3 Model B

The reason of this work is that I'm currently working on a web project which require latest web technology. One of these technologies is PHP/v8js.

Dependencies

sudo apt install build-essential chrpath re2c clang-dev git git-doc

Requierements

Instructions

Check your available memory and swap space

zramctl && echo -e "\nCompression stats:" && zramstat && echo -e "\nMemory usage:" && free -mlht

If you have less than 1Gb as total memory, you should increase it by modifying the file /etc/systemd/zram.conf and set the factor value to 50 for example then restart the mkzram service.

Depending on your embedded memory size, you may have around of 1.4Gb after that.

To restart the mkzram service, just execute this command: sudo systemctl restart mkzram

Thanks to the zRam kernel module which is included in the Raspberry Pi kernel and my small project systemd-zram

Add gclient to the path

export PATH=`pwd`/depot_tools:$PATH

ninja may also be needed, in this case, have a look here and there

To compile it, simply do: ./configure.py --bootstrap

Then add it to the path:

export PATH=`pwd`/ninja:$PATH

And... Let's Go ! ⏳

gclient
fetch v8
cd v8

You must have installed git and depot_tools first.

This process will be long with almost no output so be patient

Select your version (you may skip this step if you want to use the latest)

git checkout 4.9.385.28 // for example

Syncing changes / update to selected version

gclient sync

!! Very important step !! πŸ‘ˆ

Make sure to remove host from compilation toolsets in the file v8/testing/gtest.gyp

Use Ctrl+\ then search for "'host', " (without '"') and replace (leave empty)

Then Ctrl+X and confirm to save the file

The long way... v8 compilation on the Raspberry Pi 3 !! πŸ™

# Build (with internal snapshots)
export GYPFLAGS="-Dv8_use_external_startup_data=0"

# Force gyp to use system-wide ld.gold
export GYPFLAGS="${GYPFLAGS} -Dlinux_use_bundled_gold=0"

# Compile V8 (compilation succeed with -j1 | 1.4 Gb RAM / Failed with -j[2-4])
make native hardfp=on vf3p=off library=shared snapshot=on werror=no -j1

# Install to /opt/v8
sudo mkdir -pv /opt/v8/{lib,include}
sudo cp -v out/native/lib.target/lib*.so /opt/v8/lib/
sudo cp -Rv include/* /opt/v8/include

# Fix libv8.so's RUNPATH header
sudo chrpath -r '$ORIGIN' /opt/v8/lib/libv8.so

Now, if compilation succeed without any issues, you must finger cross for the next step...

Compilation success... or not ! πŸ˜…

To verify if v8 is compiled properly, you should execute the unittests and hello-world scripts

# Go to the output directory
cd path-to-v8/out/native

# Run unit tests
./unittests

# If unit tests are passed, then say hello to the world :stuck_out_tongue_winking_eye:
./hello-world

# You should see as result :
Hello, World!

PHP/v8js compilation

git clone https://github.com/phpv8/v8js.git
cd v8js
phpize
./configure --with-v8js=/opt/v8
make -j $(nproc)
make test

If during the make test process, you see that all tests are failing, it means that your v8js code won't run πŸ˜“

If all tests are passed successfully, you may go to the next step and install the library then update your PHP config files

sudo make install

# Now updating the PHP / Apache2 config file
sudo nano /etc/php/7.0/apache2/php.ini

# Add these lines at the end
[v8js]
extension=v8js.so

# Repeat the process for the PHP / CLI config file
sudo nano /etc/php/7.0/cli/php.ini

# To finish, restart your webserver (Apache 2 in my case)
sudo systemctl restart apache2

# Verify the status
sudo systemctl status apache2

Now... Some testing ! 😁

Go ahead on PHP v8js examples and see if that runs as shown.

If the above example is working, you should see in your browser :

Hello World! int(13)

Otherwise, you may get something like that in your logs :

==== C stack trace ===============================



#
# Fatal error in ../src/snapshot/natives-external.cc, line 122
# Check failed: holder_.
#

==== C stack trace ===============================



#
# Fatal error in ../src/snapshot/natives-external.cc, line 122
# Check failed: holder_.
#
[Tue Jan 10 23:49:17.151018 2017] [core:notice] [pid 26162] AH00051: child pid 26169 exit signal Illegal instruction (4), possible coredump in /etc/apache2
[Tue Jan 10 23:49:17.151691 2017] [core:notice] [pid 26162] AH00051: child pid 26175 exit signal Illegal instruction (4), possible coredump in /etc/apache2

==== C stack trace ===============================



#
# Fatal error in ../src/snapshot/natives-external.cc, line 122
# Check failed: holder_.
#
[Tue Jan 10 23:49:19.161500 2017] [core:notice] [pid 26162] AH00051: child pid 26171 exit signal Illegal instruction (4), possible coredump in /etc/apache2

==== C stack trace ===============================



#
# Fatal error in ../src/snapshot/natives-external.cc, line 122
# Check failed: holder_.
#
[Tue Jan 10 23:49:24.167417 2017] [core:notice] [pid 26162] AH00051: child pid 26177 exit signal Illegal instruction (4), possible coredump in /etc/apache2

==== C stack trace ===============================



#
# Fatal error in ../src/snapshot/natives-external.cc, line 122
# Check failed: holder_.
#
[Tue Jan 10 23:49:55.203367 2017] [core:notice] [pid 26162] AH00051: child pid 26179 exit signal Illegal instruction (4), possible coredump in /etc/apache2

==== C stack trace ===============================



#
# Fatal error in ../src/snapshot/natives-external.cc, line 122
# Check failed: holder_.
#

No need to say that the compilation has failed somewhere...

I'm still trying actually to make this process succeed... but there is still some fight right now !

And the winner... Me 😎 The compilation took all night long to be done but now it works !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment