LoginSignup
2

More than 5 years have passed since last update.

Google V8 JavaScript Engine - サンプルコードのビルド

Posted at

Google V8 JavaScript Engine のビルドしてみたのでメモ

https://v8.dev/docs/embed の内容のそのままといえばそのままである。

MacOS環境でV8のビルド・サンプロコードのビルド・実行をしているので、プラットフォームによっては若干差分がある。

Use Python 2.7

3系だと途中でつまずくので要注意. なんらかの手段で2.7系に切り替えておく
https://qiita.com/LittleWat/items/166f976cccf30bc63e62

Install depot_tools

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

Get the V8 source code, including all branches and dependencies:

$ mkdir ~/v8
$ cd ~/v8
$ fetch v8
$ cd v8

# 適当に最新のtagを利用する
$ git checkout refs/tags/7.5.10

Create a build configuration using the helper script:

$ tools/dev/v8gen.py x64.release.sample
$ gn args out.gn/x64.release.sample
=========== ひとまず下記のデフォルトの状態でOK
  1 is_component_build = false
  2 is_debug = false
  3 target_cpu = "x64"
  4 use_custom_libcxx = false
  5 v8_monolithic = true
  6 v8_use_external_startup_data = false

v8_monolithic: がビルド実行によって出力されるスタティックライブラリとして出力される

Build static library

$ ninja -C out.gn/x64.release.sample v8_monolith

# ライブラリが「obj」配下に出来ていればとりあえずOK
$ ls -lh ./out.gn/x64.release.sample/obj/ | grep \\.a$                                                                                                                [~/v8/v8]
-rw-r--r--    1 xxx  yyy   2.3M  libv8_libbase.a
-rw-r--r--    1 xxx  yyy   2.7M  libv8_libplatform.a
-rw-r--r--    1 xxx  yyy   1.9G  libv8_monolith.a

マシンのCPU性能によると思うが、かなり時間がかかるため、要注意.

Build Sample Code

「./samples/hello-world.cc」
というV8エンジンを使ってHello, World!の出力を行うサンプルコードが存在するため、これを利用する。

$ g++ -I. -Iinclude samples/hello-world.cc -o hello_world -lv8_monolith -Lout.gn/x64.release.sample/obj/ -pthread -std=c++0x

# サンプルコードの実行
$ ./hello_world
Hello, World!
3 + 4 = 7

とにかくサンプルコードを動かすだけのビルドは、時間がかかるだけでかなり簡単にできる。
中身を追ってみたり、wrapperなライブラリ作って他言語のランタイムからJavaScript実行させたりはまたあとで

参考URL

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2