Z

本地构建 d8

按照 https://v8.dev/docs/source-code 拉最新代码, 推荐使用 gm 生成构建参数

1alias gm=/path/to/v8/tools/dev/gm.py
2gm

Windows 可直接在 PowerShell 中执行

1python3 /path/to/v8/tools/dev/gm.py

正常情况可以看到 gm 帮助信息

 1Convenience wrapper for compiling V8 with gn/ninja and running tests.
 2Sets up build output directories if they don't exist.
 3Produces simulator builds for non-Intel target architectures.
 4Uses Goma by default if it is detected (at output directory setup time).
 5Expects to be run from the root of a V8 checkout.
 6
 7Usage:
 8    gm.py [<arch>].[<mode>[-<suffix>]].[<target>] [testname...] [--flag]
 9
10All arguments are optional. Most combinations should work, e.g.:
11    gm.py ia32.debug x64.release x64.release-my-custom-opts d8
12    gm.py android_arm.release.check --progress=verbose
13    gm.py x64 mjsunit/foo cctest/test-bar/*
14
15For a less automated experience, pass an existing output directory (which
16must contain an existing args.gn), e.g.:
17    gm.py out/foo unittests
18
19Flags are passed unchanged to the test runner. They must start with -- and must
20not contain spaces.
21
22<arch> can be any of: ia32 x64 arm arm64 mips64el ppc ppc64 riscv32 riscv64 s390 s390x android_arm android_arm64 loong64 fuchsia_x64 fuchsia_arm64
23<mode> can be any of: release rel debug dbg optdebug opt
24<target> can be any of:
25 - d8, cctest, v8_unittests, v8_fuzzers, wasm_api_tests, wee8, mkgrokdump, generate-bytecode-expectations, inspector-test, bigint_shell, wami (build respective binary)
26 - all (build all binaries)
27 - tests (build test binaries)
28 - check (build test binaries, run most tests)
29 - checkall (build all binaries, run more tests)

根据平台选择参数

1gm x64.release d8 # Windows x64
2gm arm64.release d8 # Apple silicon

.release 模式已经包含我们所需的参数,一般情况不需要手动指定参数

 1is_component_build = false # 必要,用于 profile 的 d8 需要关闭 is_component_build
 2is_debug = false
 3target_cpu = "x64"
 4v8_enable_sandbox = true
 5use_goma = false
 6v8_enable_backtrace = true
 7v8_enable_disassembler = true # 必要,启用反汇编程序以生成机器码
 8v8_enable_object_print = true
 9v8_enable_verify_heap = true
10dcheck_always_on = false

等一会儿构建应该成功了

1Done. Made 481 targets from 135 files in 343ms
2# autoninja -C out/arm64.release d8
3ninja: Entering directory `out/arm64.release'
4[2370/2370] LINK ./d8
5Done! - V8 compilation finished successfully.

最后记得指定 D8_PATH 环境变量为构建产物目录。

如果需要更多调试信息,例如查看字节码、解释执行过程,gm 默认的构建参数是不够的,可以手动配置构建参数,例如 Apple silicon 环境下

1gn gen out/arm64.debug --args='is_component_build = false is_debug = true symbol_level = 2 target_cpu = "arm64" v8_target_cpu = "arm64" use_goma = false v8_enable_backtrace = true v8_enable_fast_mksnapshot = true v8_enable_slow_dchecks = true v8_optimized_debug = false v8_enable_disassembler = true v8_enable_trace_ignition = true v8_enable_trace_baseline_exec = true v8_enable_trace_feedback_updates=true'

Windows 使用上述命令会提示不支持 “–args” 参数

1ERROR at the command-line "--args":1:27: Undefined identifier

可以使用

1gn args .\out\x64.debug

命令创建构建参数文件,执行后 Windows 会弹出记事本,把构建参数一行一行粘贴进去即可,关闭记事本 gn 会继续创建构建目录,完成后使用 gn args 查看配置

 1gn args .\out\x64.debug\ --list --short --overrides-only # 查看指定目录的覆盖性配置
 2
 3PS C:\Users\zheng\Code\v8\v8> gn args .\out\x64.debug\ --list --short --overrides-only
 4enable_rust = false
 5is_component_build = false
 6is_debug = true
 7symbol_level = 2
 8target_cpu = "x64"
 9use_goma = false
10v8_enable_backtrace = true
11v8_enable_disassembler = true
12v8_enable_fast_mksnapshot = true
13v8_enable_slow_dchecks = true
14v8_enable_trace_baseline_exec = true
15v8_enable_trace_feedback_updates = true
16v8_enable_trace_ignition = true
17v8_optimized_debug = false
18v8_target_cpu = "x64"

确认是你刚才输入的即可。

构建参数中包含一些关键配置例如:

  • is_debug = true, debug 模式下才可以看字节码
  • v8_enable_trace_ignition = true 允许追踪 ignition 执行流程

生成构建目录后使用 ninja 构建即可

1ninja -C out/arm64.debug d8