Windows上检出并编译Chromium

Checking out and Building Chromium for Windows

其它平台的向导,请移步get the code页面。

Google员工

如果你是Google员工,请参考go/building-chrome-win

系统需求

  • 一台英特尔机器,内存至少8GB。建议配备16GB以上的内存。
  • 至少100GB以上以NTFS格式化的硬盘空间。不支持FAT32格式化的硬盘,因为有一些Git包文件大小超过4GB。
  • 合适的 Visual Studio 版本,下面会详述。
  • Windows 7 或以上系统。

构建Windows编译环境

Visual Studio

从2017年9月起(R503915),编译Chromium需要Visual Studio 2017 update 3.2 15063(Creators Update)的Windows SDK或更新版本的环境。你必须安装“Desktop development with C++” 组件及它下面的“MFC and ATL support” 组件。可以将以下命令行参数提供给Visual Studio的安装包来完成:

--add Microsoft.VisualStudio.Workload.NativeDesktop
    --add Microsoft.VisualStudio.Component.VC.ATLMFC --includeRecommended

你必须安装Windows 10 SDK,10.0.15063或更新的版本。10.0.15063 SDK开始有一些错误,但10.0.15063.468版本运行稳定。大部分Visual Studio都会安装它。
如果Windows 10 SDK是通过Visual Studio包安装的话,Debugging Tools可以通过以下步骤安装:控制面板→程序和功能→找到“Windows Software Development Kit”→右键“更改”→选择“Change”→选中“Debugging Tools For Windows”→Change。或者,你可以下载单独的SDK包来安装Debugging Tools。

安装depot_tools

下载depot_tools并解压。

Warning: 不要从资源管理器中直接拖放或复制粘贴,这样压缩包里的“.git”隐藏文件夹将不会被提取出来,此文件夹是保证depot_tools自动更新的。你可以从右键菜单中选择“提取所有文件…”。

将depot_tools的路径添加到PATH环境变量中(必须放在其它Python环境路径的前面)。假设你将压缩包解压到了C:\src\depot_tools
打开:
控制面板→系统和安全→系统→高级系统设置
如果你有管理员访问权限,修改系统环境变量PATH并将C:\src\depot_tools放到最前面(或者至少放到其它存在Python和Git环境路径的前面)。
如果你没有管理员权限,你可以将C:\src\depot_tools放到用户环境变量PATH的最前面,如果系统环境变量PATH中有其它的Python环境路径,你就倒霉了。
还有,以同样的方式,将DEPOT_TOOLS_WIN_TOOLCHAIN添加到系统环境变量中,值设为0。它会告诉depot_tools使用本地安装的Visual Studio版本(默认情况下,depot_tools将使用google-internal版本)。
打开cmd shell,输入命令gclient(不带参数)。第一次运行时,gclient会安装指定位数(与系统匹配)的软件来处理Chromium代码,这些软件包括 msysgit 和 python。

  • 如果你在一个 non-cmd shell中运行gclient(比如cygwin,PowerShell),它可能看似正确运行了,但是msysgit,python等其它工具可能没有正确安装。
  • 如果第一次运行gclient的时候出现奇怪的关于文件系统的错误,,你可能需要参考disable Windows Indexing

等gclient运行结束,新开一个cmd命令行窗口,输入where python回车,并确定depot_tools中的python.bat出现在所有python.exe副本的前面。如果不能保证,这将导致使用gn时会构建过多-参考crbug.com/611087

获取代码

首先,配置Git:

$ git config --global user.name "My Name"
$ git config --global user.email "[email protected]"
$ git config --global core.autocrlf false
$ git config --global core.filemode false
$ git config --global branch.autosetuprebase always

创建一个chromium目录用来检出代码,并切换到其中(你可以任意命名,只要你开心,也可以将放在任意路径,只需保证全路径中没空格字符):

$ mkdir chromium && cd chromium

运行depot_tools中的fetch工具来检出代码及其依赖项:

$ fetch chromium

如果你不需要完整的仓库历史,你可以通过添加--no-history参数,这会节省一大笔时间。
在一个高速的网络环境中预计会花30分钟,网速低的情况下可能需要几个小时。
fetch完成后,会在当前目录下创建一个.gclient的文件和一个src的文件夹。在接下来的说明中,假设你已经切换到src目录中:

$ cd src

Optional: You can also install API keys if you want your build to talk to some Google services, but this is not necessary for most development and testing purposes.

配置编译环境

Chromium使用Ninja作为它的编译工具,并配套使用一个叫GN的工具来生成.ninja文件。你可以用不同的配置参数创建任意数量的编译目录。创建编译目录:

$ gn gen out/Default

使用 Visual Studio IDE

如果你想要使用Visual Studio IDE,需要在生成输出目录时在gn gen后面添加--ide参数(就像get the code中描述的那样):

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

GN会在编译目录中生成一个all.sln的文件。它在内部使用Ninja来编译,但仍可以使用IDE的大部分功能(没有原生的Visual Studio编译模式)。如果你再次运行gen,你还是需要提供这个参数,但是通常编译的时候,GN会自动更新编译和IDE文件。
生成的解决方案会包含几千个项目,并且加载起来会很慢。使用--filters参数来限制生成项目文件,选择只生成你感兴趣的代码,尽管在管理器中看不到这些文件。有一个最小的方案让你在IDE中编译,运行Chrome且不显示任何源代码:

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

还有其它选项可以控制如何生成解决方案,可以运行gn help gen参考。

快速编译指南

  • 减少文件系统的开销。排除反病毒软件和索引软件对编译目录的操作。
  • 将编译相关目录存储在一个快速的磁盘上(最好是SSD)。
  • 更多核的CPU会更好(20+不算多),更多的内存(64GB不算多)。

有几个gn参数的设置可以提高编译速度。创建输出目录时(gn args out/Default),你可以在弹出的文本编辑器中输入这些,也可以直接在gn gen 命令行中输入(gn gen out/Default --args="is_component_build = true is_debug = true")。
一些有用的设置提供参考:

  • use_jumbo_build = true - 一些实验性的Jumbo/unity编译。
  • is_component_build = true - 使用最多,更小Dlls,链接时自增。
  • enable_nacl = false - 禁用Native Client,本地编译通常不需要。
  • target_cpu = "x86" - x86编译比x64编译稍微快一些并且支持链接时自增。设置此项时如果没有设置enable_nacl = false,编译时间可能会更糟。
  • remove_webcore_debug_symbols = true - 关闭blink代码层的调试功能来减少编译时间,如果你不打算调试blink,此项设置正合适。

此外,Google员工应该考虑使用goma,一个分布式的编译系统。内部有goma详细的信息,相关的gn参数为:

  • use_goma = true
  • symbol_level = 2 - 默认goma编译时将symbol_level由2改为1,目的是禁用source-level调试。此项将它还原。这实际上会增加编译时间,但可以使goma可用。
  • is_win_fastlink = true - 启用goma并且symbol_level为2时,必须设置此项。

Note that debugging of is_win_fastlink built binaries is unreliable prior to VS 2017 Update 3 and may crash Visual Studio.

To get any benefit from goma it is important to pass a large -j value to ninja. A good default is 10*numCores to 20*numCores. If you run autoninja.bat then it will pass an appropriate -j value to ninja for goma or not, automatically.

When invoking ninja specify ‘chrome’ as the target to avoid building all test binaries as well.

Still, builds will take many hours on many machines.

编译Chromium

编译Chromium(the “chrome” target),使用Ninja:

$ ninja -C out\Default chrome

在命令行运行gn ls out/Default,你可以得到一个所有编译目标的列表信息。需要单独编译某一个时,先去掉GN标签中前面的”//”,然后将剩下的标签传给Ninja(比如 //chrome/test:unit_tests,使用`ninja -C out/Default chrome/test:unit_tests“)。

运行Chromium

一旦编译成功,你可以这样运行:

$ out\Default\chrome.exe

(”.exe”的后缀名是可选的)。

运行tests目标

你可以用同样的方式运行tests。你也可以使用--gtest_filter参数限制运行某些tests,比如:

$ out\Default\unit_tests.exe --gtest_filter="PushClientTest.*"

你可以在GitHub page找到更多关于GoogleTest的测试。

更新你的检出

为了更新一个已经存在的检出,你可以运行

$ git rebase-update
$ gclient sync

The first command updates the primary Chromium source repository and rebases any of your local branches on top of tip-of-tree (aka the Git branch origin/master). If you don’t want to use this script, you can also just use git pull or other common Git commands to update the repo.
The second command syncs the subrepositories to the appropriate versions and re-runs the hooks as needed.


2017-11-07

猜你喜欢

转载自blog.csdn.net/Vincent95/article/details/78469807