Ubuntu系统下编译Chromium

操作系统:Ubuntu 22.04
硬件要求:磁盘空间(约 100 GB),内存(16 GB 或更高),CPU(越快越好)

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
sudo apt update

# 安装基本依赖
sudo apt install git python3 python3-pip curl build-essential

# 安装必要工具
sudo apt install g++-multilib clang cmake ninja-build libglib2.0-dev

# 获取depot_tools,添加PATH环境变量。depot_tools 是管理 Chromium 构建环境的重要工具
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
vi /etc/profile
export PATH="$PATH:$(pwd)/depot_tools"
source /etc/profile

# 获取Chromium 源代码(解压缩后80G左右,耗时较长)
mkdir chromium && cd chromium
fetch --nohooks chromium
gclient sync

# 用Chromium自带脚本安装必要的依赖。
cd src
./build/install-build-deps.sh

# 配置编译环境,使用gn生成编译模板。
gn gen out/Default

# 配置编译参数,下面是一个样例。
vi out/Default/args.gn
# 内容如下
is_debug = false
is_component_build = true
enable_nacl = false
v8_enable_pointer_compression = false
blink_symbol_level = 0
v8_symbol_level = 0
symbol_level = 0

# 参数解释如下
# is_debug: 是否启用调试信息,设置false可以加快编译速度
# is_component_build = true:将 Chrome 的许多部分链接到单独的共享库中,以避免最后的长链接步骤。
# enable_nacl = false:禁用 Native Client功能,加快构建速度。
# v8_enable_pointer_compression = false:禁用v8引擎的指针压缩,使Chrome的JS堆限制可以超过4GB。配置Chrome启动参数:--js-flags=--max-old-space-size=10240,解决out of memory的错误。
# blink_symbol_level = 0:删除 WebCore 符号,加快构建速度。
# v8_symbol_level = 0:删除 V8 符号,不调试 V8的情况下,设置为 0 ,加快构建速度。
# symbol_level = 0:无需符号,加快构建速度。
# target_os = "win":未尝试,应该可以编译出Windows系统用的应用程序。

# 开始编译
autoninja -C out/Default chrome

# 检查编译结果,执行chrome验证
cd out/Default/
./chrome

注意点

  1. 一定要保证【gclient sync】正确执行结束,否则可能会缺少必要的代码和设置。
    例如:FileNotFoundError: [Errno 2] No such file or directory: LASTCHANGE.committime
  2. Linux下需要执行【./build/install-build-deps.sh】安装依赖,ChataGPT的回答里面没有这个步骤,会导致错误。
    例如:FileNotFoundError: [Errno 2] No such file or directory: ‘gperf’

参考文档

Chromium编译参数参考文档:gn-build-configuration
Windows下的编译方法:Checking out and Building Chromium for Windows
Chrome历史版本下载
ChromeJS堆限制v79以后有变化的相关博客