v8环境搭建

搭建v8的环境有以下两种方式:
1.官方的搭建方式可以参考:https://v8.dev/docs/build
2.使用github action将v8 source下载下来自己编译(也可以在远程编译好)
3.使用docker(docker是真的方便

官方的搭建方式

安装depot_tools

1
2
3
4
#需要代理
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
echo 'export PATH=$PATH:"$HOME/depot_tools"' >> ~/.bashrc
bash

ninja

1
2
3
4
5
6
7
8
9
10
11
12
13
#安装ninja 之前需要安装相关的配置文件re2c + libtool autoconf
git clone https://github.com/skvadrik/re2c.git re2c
cd re2c
mkdir -p m4
./autogen.sh && ./configure --prefix=/usr && make
sudo make install
re2c -v #验证结果
#现在就可以安装ninja了
git clone https://github.com/ninja-build/ninja.git && cd ninja
./configure.py --bootstrap
sudo cp ninja /usr/bin/
ninja --version
#验证结果

编译v8

1
2
3
4
5
6
7
8
9
10
11
12
cd ..
fetch v8 #这里我两天两夜没下载成功......
cd v8
gclient sync #下载依赖,这里也很容易出问题
# 设置编译参数
tools/dev/v8gen.py x64.debug #或者gn gen out/x64.release --args='v8_monolithic=true v8_enable_object_print = true v8_enable_verify_heap=true v8_enable_disassembler=true v8_enable_backtrace=true'
# v8_monolithic=true命令是为了方便迁移(只用迁移d8就可以了,v8_enable_object_print方便release使用job等命令)
ninja -C out/x64.release d8

# 编译
ninja -C out.gn/x64.debug
./out.gn/x64.debug/d8 #能调用v8就可以了

使用tools/dev/v8gen.py x64.debug 报错时可试试安装如下依赖

1
2
3
4
5
sudo apt install libpango1.0-dev
sudo apt install libcogl-pango-dev
sudo apt install libjpeg-dev
sudo apt install xz-utils
sudo apt install subversion

使用github action

项目地址:https://github.com/StarCross-Tech/v8-action
这上面的方法不久前,还可以用,现在用会出一点小问题(upload失败,不过只要稍微改一下yml文件就可以了
相关配置说明:

1
2
3
4
5
6
env:
PATCH_FLAG: false #默认下载最新的版本,
COMMIT: 0 #版本号,PATCH_FLAG为true时,修改为对应的版本号
DEPOT_UPLOAD: false #需要depot_tools就填true
SRC_UPLOAD: true #需要v8 源码就填true
BINARY_UPLOAD: false #暂时不支持

需要修改的部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- name: upload depot_tools
if: env.DEPOT_UPLOAD == 'true' && !cancelled()
run: |
curl -fsSL git.io/file-transfer | sh
./transfer cow --block 2621440 -s -p 64 --no-progress depot_tools.zip 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"

# upload v8.zip to cowtransfer
- name: upload v8_src
if: env.SRC_UPLOAD == 'true' && !cancelled()
run: |
curl -fsSL git.io/file-transfer | sh
./transfer cow --block 2621440 -s -p 64 --no-progress v8.7z.001 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"
./transfer cow --block 2621440 -s -p 64 --no-progress v8.7z.002 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"

将上面的cow 改为muse并且去掉–block 2621440(如果发生什么别的变化,对应修改就可以了),此外关于runner最好使用ubuntu18.04

本地编译v8

本地编译v8的yml参考文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#===================================================
# Description: Build v8 enginer using GitHub Actions
# Author: t1an5t
#===================================================


name: BUILD v8

on:
push:
branches: [ master ]
# watch:
# types: started

env:
PATCH_FLAG: true
COMMIT: 6dc88c191f5ecc5389dc26efa3ca0907faef3598
DEPOT_UPLOAD: true
SRC_UPLOAD: true
BINARY_UPLOAD: false

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-18.04
if: github.event.repository.owner.id == github.event.sender.id

steps:
- name: Checkout
uses: actions/checkout@master

# init ubuntu1804 environment
- name: init env
run: |
sudo apt-get update
sudo apt-get -y install pkg-config git subversion curl wget build-essential python xz-utils zip p7zip-full

# get depot_tools
- name: depot_tools
run: |
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
echo export PATH=\"\$PATH:`pwd`/depot_tools/\" >> ~/.bash_profile

# fetch v8 source code
- name: fetch v8
run: |
source ~/.bash_profile
fetch v8
cd v8

# patch source code
- name: patch v8
if: env.PATCH_FLAG == 'true' && !cancelled()
run: |
cd v8
git reset --hard $COMMIT
cd ..

- name: build v8
run: |
source ~/.bash_profile
gclient sync -D

# compress this file
- name: zip depot_tools
if: env.DEPOT_UPLOAD == 'true' && !cancelled()
run: |
zip -q -r depot_tools.zip depot_tools

# 7zip v8 src
- name: 7zip v8_src
run: |
zip -q -r v8.zip v8
7z a v8.7z ./v8.zip -v2048m

# upload depot_tools.zip to cowtransfer
- name: upload depot_tools
if: env.DEPOT_UPLOAD == 'true' && !cancelled()
run: |
curl -fsSL git.io/file-transfer | sh
./transfer muse -s -p 64 --no-progress depot_tools.zip 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"

# upload v8.zip to cowtransfer
- name: upload v8_src
if: env.SRC_UPLOAD == 'true' && !cancelled()
run: |
ls -l
curl -fsSL git.io/file-transfer | sh
./transfer muse -s -p 64 --no-progress v8.7z.001 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"
./transfer muse -s -p 64 --no-progress v8.7z.002 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"

远程编译

和本地编译的yml相比多了个get ninja以及v8编译部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#===================================================
# Description: Build v8 enginer using GitHub Actions
# Author: t1an5t
#===================================================

name: BUILD v8

on:
push:
branches: [ master ]
# watch:
# types: started

env:
PATCH_FLAG: true
COMMIT: 0
DEPOT_UPLOAD: true
SRC_UPLOAD: true
BINARY_UPLOAD: false

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-18.04
if: github.event.repository.owner.id == github.event.sender.id

steps:
- name: Checkout
uses: actions/checkout@master

# init ubuntu1804 environment
- name: init env
run: |
sudo apt-get update
sudo apt-get -y install pkg-config git subversion curl wget build-essential python xz-utils zip p7zip-full

# get depot_tools
- name: depot_tools
run: |
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
echo export PATH=\"\$PATH:`pwd`/depot_tools/\" >> ~/.bash_profile

# fetch v8 source code
- name: fetch v8
run: |
source ~/.bash_profile
fetch v8
cd v8

# patch source code
- name: patch v8
if: env.PATCH_FLAG == 'true' && !cancelled()
run: |
cd v8
git reset --hard $COMMIT
cd ..

- name: get ninja
run: |
sudo apt-get install libtool autoconf
git clone https://github.com/skvadrik/re2c.git re2c
cd re2c
mkdir -p m4
./autogen.sh && ./configure --prefix=/usr && make
sudo make install
re2c -v
cd ..
git clone https://github.com/ninja-build/ninja.git
cd ninja && ./configure.py --bootstrap && cd ..

- name: build v8
run: |
source ~/.bash_profile
gclient sync -D
cd v8
ls -l
tools/dev/v8gen.py x64.debug
ninja -C out.gn/x64.debug
cd ..

# compress this file
- name: zip depot_tools
if: env.DEPOT_UPLOAD == 'true' && !cancelled()
run: |
zip -q -r depot_tools.zip depot_tools

# 7zip v8 src
- name: 7zip v8_src
run: |
zip -q -r v8.zip v8
7z a v8.7z ./v8.zip -v2048m

# upload depot_tools.zip to cowtransfer
- name: upload depot_tools
if: env.DEPOT_UPLOAD == 'true' && !cancelled()
run: |
curl -fsSL git.io/file-transfer | sh
./transfer muse -s -p 64 --no-progress depot_tools.zip 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"

# upload v8.zip to cowtransfer
- name: upload v8_src
if: env.SRC_UPLOAD == 'true' && !cancelled()
run: |
ls -l
curl -fsSL git.io/file-transfer | sh
./transfer muse -s -p 64 --no-progress v8.7z.001 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"
./transfer muse -s -p 64 --no-progress v8.7z.002 2>&1 | tee cowtransfer.log
echo "::warning file=cowtransfer.com::$(cat cowtransfer.log | grep https)"

这里我编译的是x64.debug版本,可以根据情况调整build v8(github大概需要两个小时),
当github action结束后,对应的文件有v8.zip.001,和v8.zip.002,以及depot_tools.zip
解压命令

1
2
7zr e v8.7z.001
#然后本地会出现一个v8.zip,此时将v8解压到对应的目录就可以了

测试结果
avatar

github action + v8 + LTS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
name: BUILD and Complie v8

on:
push:
branches: [ master ]

env:
PATCH_FLAG: false #给v8打上patch,不需要则false
COMMIT: 9.3.345.16 #v8对应的版本号
SRC_UPLOAD: true
BINARY_UPLOAD: true

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-20.04
if: github.event.repository.owner.id == github.event.sender.id

steps:
- name: Checkout
uses: actions/checkout@master

# init ubuntu20.04 environment
- name: init env
run: |
sudo apt-get update
sudo apt-get -y install pkg-config git subversion curl wget build-essential xz-utils zip p7zip-full python
curl -fsSL git.io/file-transfer | sh
ls -l

# get depot_tools
- name: depot_tools
run: |
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
echo export PATH=\"\$PATH:`pwd`/depot_tools/\" >> ~/.bash_profile

# fetch v8 source code
- name: fetch v8
run: |
source ~/.bash_profile
fetch v8
cd v8
git reset --hard $COMMIT
cd ..

# patch source code
- name: patch v8
if: env.PATCH_FLAG == 'true' && !cancelled()
run: | # git clone https://github.com/Archerber/tmp_patch.git下载patch内容
cd v8
git apply ../tmp_patch/*.patch #根据diff文件修改源码 diff function
cd ..

- name: setting ninja
run: |
sudo apt-get install libtool autoconf
git clone https://github.com/skvadrik/re2c.git re2c
cd re2c
mkdir -p m4
./autogen.sh && ./configure --prefix=/usr && make
sudo make install
re2c -v
cd ..
git clone https://github.com/ninja-build/ninja.git
cd ninja && ./configure.py --bootstrap && cd ..

- name: Complie v8
run: |
source ~/.bash_profile
gclient sync -D
cd v8
ls -l
tools/dev/v8gen.py x64.debug
ninja -C out.gn/x64.debug
tools/dev/v8gen.py x64.release
gn gen out/x64.release --args='v8_monolithic=true v8_use_external_startup_data=false is_component_build=false is_debug=true target_cpu="x64" use_goma=false goma_dir="None" v8_enable_backtrace=true v8_enable_disassembler=true v8_enable_object_print=true v8_enable_verify_heap=true'
ninja -C out/x64.release d8

# 7zip d8
- name: 7zip d8
if: env.BINARY_UPLOAD == 'true' && !cancelled()
run: |
zip d8.zip v8/out/x64.release/d8
ls -l
./transfer gof v8/out/x64.release/d8
./transfer gof d8.zip

- name: save src
if: env.SRC_UPLOAD == 'true' && !cancelled()
run: |
zip -q -r v8.zip v8
./transfer gof v8.zip

使用docker集成的v8环境

项目地址:https://github.com/andreburgaud/docker-v8

调整gdb

可以将v8/tools下的gdb加载到本地gdb中,这样就可以使用job等v8官方的命令方便调试

1
echo "/path/v8/tools/gdbinit" >> ~/.gdbinit

参考链接:
1.https://mem2019.github.io/jekyll/update/2019/07/18/V8-Env-Config.html
2.https://mp.weixin.qq.com/s?__biz=Mzg5NjEyMjA5OQ==&mid=2247484916&idx=1&sn=1d07443c7e3817bd4186c616598f4889&chksm=c004a868f773217e8577b404c3032eef5e135311adeab1976c8189c1c0e7fdba3d68ae6d3f16&scene=21#wechat_redirect
3.https://kiprey.github.io/2020/11/fetch-chromium/#%E5%9B%9B%E3%80%81v8%E4%BB%A3%E7%A0%81%E4%B8%8B%E6%8B%89%E5%8F%8A%E7%BC%96%E8%AF%91
4.https://v8.dev/docs/build
5.https://github.com/andreburgaud/docker-v8