下载编译Chromium

Checking out and Building Chromium


depot_tools

  1. Windows: https://storage.googleapis.com/chrome-infra/depot_tools.zip
  2. Linux:
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

环境变量

# windows
# 首先向环境变量PATH添加 `C:\src\depot_tools`,最好在Python路径之前
$ set DEPOT_TOOLS_UPDATE=0
# 这告诉depot_tools使用本地安装的Visual Studio版本(默认情况下,depot_tools将尝试使用google内部版本)
$ set DEPOT_TOOLS_WIN_TOOLCHAIN=0
$ set vs2017_install="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise"
$ set vs2019_install="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
$ set vs2022_install=C:\Program Files\Microsoft Visual Studio\2022\Professional
$ set vs2022_install=C:\Program Files\Microsoft Visual Studio\2022\Community

# vs2015的设置
$ set GYP_MSVS_VERSION=2015
$ set GYP_MSVS_OVERRIDE_PATH=C:\Program Files (x86)\Microsoft Visual Studio 14.0

# linux
# depot tool path必须使用全路径,不要使用~/depot_tools
$ export PATH="$PATH:/path/to/depot_tools"
$ export DEPOT_TOOLS_UPDATE=0

Windows环境首次运行gclient的时候有时会出现下面情况

C:\Users\YANGLEI>gclient
系统找不到文件 D:\Documents\GitHub\googlesource\depot_tools\python3_bin_reldir.txt。
[E2022-07-23T13:45:49.800597+08:00 9328 0 annotate.go:273] goroutine 1:
#0 go.chromium.org/luci/vpython/venv/config.go:304 - venv.(*Config).resolvePythonInterpreter()
  reason: none of [D:\Documents\GitHub\googlesource\depot_tools\\\python3.exe] matched specification 3.8.0

#1 go.chromium.org/luci/vpython/venv/config.go:148 - venv.(*Config).resolveRuntime()
  reason: failed to resolve system Python interpreter

#2 go.chromium.org/luci/vpython/venv/venv.go:142 - venv.With()
  reason: failed to resolve python runtime

#3 go.chromium.org/luci/vpython/run.go:60 - vpython.Run()
#4 go.chromium.org/luci/vpython/application/application.go:322 - application.(*application).mainImpl()
#5 go.chromium.org/luci/vpython/application/application.go:410 - application.(*Config).Main.func1()
#6 go.chromium.org/luci/vpython/application/support.go:46 - application.run()
#7 go.chromium.org/luci/vpython/application/application.go:409 - application.(*Config).Main()
#8 infra/tools/vpython/main.go:120 - vpython.mainImpl()
#9 infra/tools/vpython/main.go:134 - vpython.Main()
#10 vpython/main.go:10 - main.main()
#11 runtime/proc.go:255 - runtime.main()
#12 runtime/asm_amd64.s:1581 - runtime.goexit()

找了很久也没有定位到原因。有时可能是环境变量DEPOT_TOOLS_UPDATE=0的问题,它阻止了depot_tools工具在Windows上正确引导。 所以如果是第一次启动gclient,不要设置环境变量DEPOT_TOOLS_UPDATE=0

设置代理

# windows
$ set http_proxy=127.0.0.1:10809
$ set https_proxy=127.0.0.1:10809
# linux
$ export http_proxy=127.0.0.1:10809
$ export https_proxy=127.0.0.1:10809
# Git proxy
$ git config --global http.proxy "http://127.0.0.1:10809"
$ git config --global https.proxy "https://127.0.0.1:10809"

gclient

$ gclient
Updating depot_tools...
Usage: gclient.py <command> [options]

Meta checkout dependency manager for Git.

Commands are:
  config   creates a .gclient file in the current directory
  diff     displays local diff for every dependencies
  fetch    fetches upstream commits for all modules
  flatten  flattens the solutions into a single DEPS file
  getdep   gets revision information and variable values from a DEPS file
  grep     greps through git repos managed by gclient
  help     prints list of commands or help for a specific command
  metrics  reports, and optionally modifies, the status of metric collection
  pack     generates a patch which can be applied at the root of the tree
  recurse  operates [command args ...] on all the dependencies
  revert   reverts all modifications in every dependencies
  revinfo  outputs revision info mapping for the client and its dependencies
  root     outputs the solution root (or current dir if there isn't one)
  runhooks runs hooks for files that have been modified in the local working copy
  setdep   modifies dependency revisions and variable values in a DEPS file
  status   shows modification status for every dependencies
  sync     checkout/update all modules
  validate validates the .gclient and DEPS syntax
  verify   verifies the DEPS file deps are only from allowed_hosts

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -j JOBS, --jobs=JOBS  Specify how many SCM commands can run in parallel;
                        defaults to 8 on this machine
  -v, --verbose         Produces additional output for diagnostics. Can be
                        used up to three times for more logging info.
  --gclientfile=CONFIG_FILENAME
                        Specify an alternate .gclient file
  --spec=SPEC           create a gclient file containing the provided string.
                        Due to Cygwin/Python brokenness, it can't contain any
                        newlines.
  --no-nag-max          Ignored for backwards compatibility.
  • --nohooks 不执行 DEPS 中的 hooks
  • --no-history 不想要完整的历史记录,可以少下载一半的量
  • --jobs 16 if you wish to use parallelism

可以修改.gclient文件来不下载某些依赖库,例如以下三个文件夹是编译后生成的文件,不下载不影响编译和调试。

solutions = [
  {
    "name": "src",
    "url": "https://gitee.com/jerry-se/chromium_deps.git",
    "managed": False,
    "custom_deps": {
      "src/chrome/tools/test/reference_build/chrome_mac": None,
      "src/chrome/tools/test/reference_build/chrome_win": None,
      "src/chrome/tools/test/reference_build/chrome_linux": None,
    },
    "custom_vars": {},
  },
]
target_os = ["win"]

Git 配置

Windows环境需要

$ git config --global user.name "My Name"
$ git config --global user.email "my-name@chromium.org"
$ git config --global core.autocrlf false
$ git config --global core.filemode false
$ git config --global branch.autosetuprebase always

Get the code

$ mkdir chromium && cd chromium
$ fetch --nohooks chromium

如果代码下载过程中因报错等中断了,使用下面的命令继续:

$ gclient sync --nohooks

签出代码后,如果使用的是Ubuntu系统,请运行:

$ ./build/install-build-deps.sh

此脚本会安装一些构建依赖项。

切换Release分支

# Inside chromium/src/
# 确保你能获得你要签出的所有release tag信息
$ git fetch --tags
# FETCH_HEAD想要的分支,my_stable_branch本地分支的名称
$ git checkout -b my_stable_branch FETCH_HEAD
$ git checkout -b 100.0.4896.127 100.0.4896.127
$ git checkout -b 103.0.5060.134 103.0.5060.134
# Now synchronize the dependencies to the current branch
$ gclient sync --with_branch_heads --with_tags

回到"trunk"

# Make sure you are in 'src'.
$ git checkout -f origin/main
$ gclient sync
$ gclient --runhooks

可能还会遇到一个代理的报错:

Failed download_from_google_storage --no_resume --platform=win32 --no_auth --bucket chromium-gn -s src/buildtools/linux32/gn.sha1 
NOTICE: You have PROXY values set in your environment, but gsutil in depot_tools does not (yet) obey them. 
Also, --no_auth prevents the normal BOTO_CONFIG environment variable from being used. 
To use a proxy in this situation, please supply those settings in a .boto file pointed to by the NO_AUTH_BOTO_CONFIG environment var.

办法就是找个文件夹创建一个文件boto.cfg或者.boto,在文件中输入以下内容:

[Boto]
proxy = http://http.proxy.com
proxy_port = 12345

然后设置环境变量

set NO_AUTH_BOTO_CONFIG=D:\boto.cfg
export NO_AUTH_BOTO_CONFIG=D:\.boto

参考链接:

构建

$ gn gen out/Default
# Build arguments go here.
# See "gn args <out_dir> --list" for available build arguments.
is_debug = false
is_component_build = true
enable_nacl = false
target_cpu = "x86"
blink_symbol_level = 0
v8_symbol_level = 0
symbol_level = 0
use_goma = false

Windows 系统中构建和编译代码需要安装 C++ 和 "MFC/ATL support",在 Visual Studio Installer 中选择即可。还需要安装 Windows SDK,例如版本10.0.20348.0,并且安装时选中 Debugging Tools for Windows,即:

"C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\dbghelp.dll"

Windows SDK 可以在 Visual Studio 安装器中安装,还可以下载单独的安装程序,请注意系统和版本。

Windows SDK

Windows SDK 历史版本

编译

autoninja 是一个包装器,它可以自动为 ninja 的参数提供最佳值。

可以通过运行命令 gn ls out/Default 获取所有的构建目标列表。要编译其中一个,请将不带"//"的GN标签传递给Ninja(因此对于 //chrome/test:unit_tests 请使用命令 ninja -C out/Default chrome/test:unit_tests)。

$ autoninja -C out/Default chrome
$ autoninja -C out/Default base
$ autoninja -C out/Default base_unittests
$ autoninja -C out/Default unit_tests

更新检出的代码

$ git rebase-update
$ gclient sync -D

第一条命令更新主要的 chromium 源存储库,并将您的任何本地分支重新定位在 origin/main 上。如何不想使用这个脚步,可以只使用 git pull 或其他常见的 Git 命令来更新 repo。

第二条命令将子存储库同步到适当的版本,删除不再需要的,并根据需求重新运行 hooks。

使用 Visual Studio IDE

如果想使用 Visual Studio IDE 来开发 Chromium,请在构建生成输出目录时使用 --ide 参数运行命令 gn gen

$ gn gen --ide=vs out\Default
$ devenv out\Default\all.sln

GN 将在构建目录中生成文件 all.sln 。它将在内部使用 Ninja 进行编译,同时仍然允许大多数 IDE 功能工作。

生成的解决方案将包含数千个项目,并且加载速度非常慢。使用 --filters 参数来限制仅为您感兴趣的代码生成项目文件。虽然这也会限制在项目资源管理器中显示的文件,但调试仍然有效,可以在手动打开的文件中设置断点。让您在 IDE 中编译和运行 Chrome 但不会显示任何源文件的最小解决方案是:

$ gn gen --ide=vs --filters=//chrome --no-deps out\Default

可以有选择地将关心的其他目录添加到过滤器中,如下所示: --filters=//chrome;//third_party/WebKit/*;//gpu/*

还有其他选项可用于控制解决方案的生成方式,请在当前文档下运行 gn help gen 命令。

快速获取代码

直接下载官方代码压缩包,可以指定版本。例如: https://commondatastorage.googleapis.com/chromium-browser-official/chromium-70.0.3538.67.tar.xz

下载解压后在上一层文件夹手动创建.gclient等文件,然后执行命令gclient runhooks即可。

如何快速获取Chromium源码和编译