前言
本文记录一次在 Windows 环境下,从已有 Chromium checkout 中单独编译 V8 10.8.168.25 的完整过程。
本次目标产物是 V8 单体静态库:
text
v8_monolith.lib
如果需要 V8 命令行工具,也可以额外编译:
text
d8.exe
环境说明
本文使用的本地目录如下:
text
C:\chromium119
已有 Chromium 目录:
text
C:\chromium119\src
已有 depot_tools:
text
C:\chromium119\depot_tools
本次新建的 V8 独立目录:
text
C:\chromium119\v8-10.8.168.25
当前机器已安装的 Windows SDK:
text
10.0.22621.0
10.0.26100.0
1. 确认本地是否有 V8 版本 tag
先在已有 Chromium checkout 的 V8 仓库中确认 tag 是否存在:
powershell
git -C C:\chromium119\src\v8 tag --list "10.8.168.25"
如果输出:
text
10.8.168.25
说明本地已经有这个 tag。
查看版本提交信息:
powershell
git -C C:\chromium119\src\v8 show -s --format="%H %ci %s" 10.8.168.25
本次对应提交为:
text
18a6255a7b7c82e94089589396db33b2f66f83a0 2022-12-20 14:51:43 +0000 Version 10.8.168.25
2. 创建独立 V8 worktree
不要直接切换 C:\chromium119\src\v8,因为它属于 Chromium 119 checkout,当前版本是 V8 11.9.169.6,直接切换会影响原来的 Chromium 构建目录。
推荐创建独立 worktree:
powershell
git -C C:\chromium119\src\v8 worktree add C:\chromium119\v8-10.8.168.25 10.8.168.25
确认版本:
powershell
git -C C:\chromium119\v8-10.8.168.25 describe --tags --always
输出应为:
text
10.8.168.25
3. 为独立 V8 目录添加 .gclient
V8 独立构建需要 build、buildtools、third_party/icu 等依赖。新建:
text
C:\chromium119\v8-10.8.168.25\.gclient
内容如下:
python
solutions = [
{
"name": ".",
"url": "https://chromium.googlesource.com/v8/v8.git",
"deps_file": "DEPS",
"managed": False,
"custom_deps": {},
"custom_vars": {},
},
]
cache_dir = "C:\\chromium119\\git_cache"
这里复用已有的 Chromium git cache,避免重复下载大量 Git 对象。
4. 同步依赖
先配置 depot_tools 到 PATH,并关闭 depot_tools 自动更新:
powershell
cd C:\chromium119\v8-10.8.168.25
$env:DEPOT_TOOLS_UPDATE='0'
$env:PATH='C:\chromium119\depot_tools;' + $env:PATH
同步依赖:
powershell
gclient sync --no-history --nohooks
然后运行 hooks:
powershell
gclient runhooks
hooks 会完成这些工作:
- 下载或校验 Clang
- 生成
build/util/LASTCHANGE - 下载部分测试包
- 安装 vpython 环境
5. 处理 Windows SDK 版本问题
V8 10.8.168.25 的旧版构建脚本中硬编码了 Windows SDK:
text
10.0.20348.0
如果机器上没有这个 SDK,gn gen 会报错:
text
Exception: Path "C:\Program Files (x86)\Windows Kits\10\\include\10.0.20348.0\\um" from environment variable "include" does not exist.
本机实际安装的是:
text
10.0.22621.0
10.0.26100.0
可以查看:
powershell
Get-ChildItem 'C:\Program Files (x86)\Windows Kits\10\Include'
Get-ChildItem 'C:\Program Files (x86)\Windows Kits\10\Lib'
因此只在独立 V8 worktree 中修改:
text
C:\chromium119\v8-10.8.168.25\build\toolchain\win\setup_toolchain.py
将:
python
args.append('10.0.20348.0')
改为:
python
args.append('10.0.22621.0')
注意:这个修改只影响 C:\chromium119\v8-10.8.168.25,不影响原 Chromium 119 目录。
6. 配置 GN 参数
创建输出目录:
powershell
New-Item -ItemType Directory -Force C:\chromium119\v8-10.8.168.25\out\x64.release | Out-Null
创建:
text
C:\chromium119\v8-10.8.168.25\out\x64.release\args.gn
内容如下:
gn
is_debug = false
target_cpu = "x64"
is_component_build = false
v8_monolithic = true
v8_use_external_startup_data = false
v8_enable_i18n_support = false
参数说明:
is_debug = false:Release 构建target_cpu = "x64":构建 x64 目标is_component_build = false:关闭组件构建v8_monolithic = true:生成单体静态库v8_monolith.libv8_use_external_startup_data = false:不额外生成外部 snapshot blobv8_enable_i18n_support = false:关闭 ICU 国际化支持,减少依赖
生成 Ninja 文件:
powershell
gn gen out\x64.release
成功时会看到类似输出:
text
Done. Made 178 targets from 97 files
7. 编译 v8_monolith.lib
执行:
powershell
autoninja -C out\x64.release v8_monolith
成功后产物在:
text
C:\chromium119\v8-10.8.168.25\out\x64.release\obj\v8_monolith.lib
本次生成的文件大小:
text
892,289,528 bytes
8. 整理可分发目录
为了方便其他项目使用,可以整理一个 package 目录:
powershell
$pkg='C:\chromium119\v8-10.8.168.25\package\x64.release'
New-Item -ItemType Directory -Force $pkg | Out-Null
Copy-Item -Recurse -Force C:\chromium119\v8-10.8.168.25\include $pkg\include
Copy-Item -Force C:\chromium119\v8-10.8.168.25\out\x64.release\obj\v8_monolith.lib $pkg\v8_monolith.lib
Copy-Item -Force C:\chromium119\v8-10.8.168.25\out\x64.release\args.gn $pkg\args.gn
Copy-Item -Force C:\chromium119\v8-10.8.168.25\out\x64.release\v8_build_config.json $pkg\v8_build_config.json
最终目录:
text
C:\chromium119\v8-10.8.168.25\package\x64.release
包含:
text
include
v8_monolith.lib
args.gn
v8_build_config.json
9. 如果需要 d8.exe
本文默认生成的是静态库,不会生成 exe。
如果需要 V8 命令行工具 d8.exe,额外执行:
powershell
cd C:\chromium119\v8-10.8.168.25
$env:DEPOT_TOOLS_UPDATE='0'
$env:PATH='C:\chromium119\depot_tools;' + $env:PATH
autoninja -C out\x64.release d8
生成位置:
text
C:\chromium119\v8-10.8.168.25\out\x64.release\d8.exe
如果想把 d8.exe 也放到 package 目录:
powershell
Copy-Item -Force C:\chromium119\v8-10.8.168.25\out\x64.release\d8.exe C:\chromium119\v8-10.8.168.25\package\x64.release\d8.exe
10. 常见问题
gclient 尝试更新 depot_tools 失败
如果看到:
text
Failed to update depot_tools.
可以关闭 depot_tools 自动更新:
powershell
$env:DEPOT_TOOLS_UPDATE='0'
缺少 LASTCHANGE 或 Clang 版本不匹配
如果 gn gen 报:
text
Did you run "gclient sync"?
FileNotFoundError: build\util\LASTCHANGE.committime
说明 hooks 没跑完整,执行:
powershell
gclient runhooks
Windows SDK 10.0.20348.0 不存在
先查看本机 SDK:
powershell
Get-ChildItem 'C:\Program Files (x86)\Windows Kits\10\Include'
然后修改独立 V8 目录中的:
text
build\toolchain\win\setup_toolchain.py
将硬编码 SDK 版本改为本机实际存在的版本。
11. 本次构建结果
本次最终产物:
text
C:\chromium119\v8-10.8.168.25\package\x64.release\v8_monolith.lib
构建配置:
json
{
"current_cpu": "x64",
"is_component_build": false,
"is_debug": false,
"target_cpu": "x64",
"v8_enable_i18n_support": false,
"v8_enable_pointer_compression": true,
"v8_enable_sandbox": true,
"v8_enable_webassembly": true,
"v8_target_cpu": "x64"
}
总结:
- V8 版本:
10.8.168.25 - 构建目标:
v8_monolith - 构建模式:x64 Release
- 主要产物:
v8_monolith.lib - 可选 exe:
d8.exe