编译篇
研究Chrome ,首先得把它编译出来,这对于后续的代码分析和阅读有很大的帮助,想想自己编译出一个 Chrome 浏览器来使用,那是一件很炫的事情。
编译环境准备
Chrome的编译和 WebKit 相比起来,难度上来说,简直是一元二次方程求解和偏微分方程求解的对比(我到现在还没有完整的把 WebKit 编译出来,鄙视一下自己)。虽然 Chrome 也是从 WebKit 演进过来,差不多也就是把 WebKit 的 JS 引擎替换成了 V8 。但是不得不承认 Google 把 WebKit 的编译难度降低了几个数量级。
言归正传,Chrome 官方网站上公布的是基于 Visual Studio 2005 进行编译,网上也有兄弟基于 Visual Studio 2008 编译成功过,但我手头上没有 Visual Stdio 2008 ,所以无从得知。本文也是以 Visual Studio2005 环境为例。我曾经在自己家里的电脑上用 Visual C++ 2005 Express 版本编译过,没有成功。在 XP Professional 和 Vista Home 两个操作系统上均编译成功。
在下载Chrome 代码前,需要安装下面几个软件:
1. 安装Visual Studio 2005.
2. 安装 Visual Studio 2005 Service Pack 1 .
3. 安装热补丁 Hotfix 947315 .
4. 如果操作系统是Vista ,还需要安装 Visual Studio 2005 Service Pack 1 Update for Windows Vista 。
5. 安装 Windows 2008 SDK 。按照网上说法,如果是Visual Studio 2008 ,就不需要安装这个了。
6. 配置Windows 2008 SDK 。在开始 -> 程序 -> Microsoft Windows SDK v6.1 > Visual Studio Registration > Windows SDK Configuration Tool .。选择 make current 按钮,幸运的话,应该能一次成功。如果不能成功, Chrome 官方网站上有一个 手动配置 的帮助,大家可以参考。
下载 代码
Google为 Chrome 提供了一个一个部署工具 depot_tools ,包括下载代码、同步代码、上传代码等功能。这个工具采用Python 编写的,其中还包含了一些 Javascript 脚本。 Depot_tools 中包含了一个 gclient 工具,是我们需要关注的重点。
下载代码有几 种 方式:
1. Chrome官网上提供了一个 源代码包 ,可以直接下载下来。不过这个包并不是最新的包。我采用的是这种方法进行下载的,相对来说比较快。
2. 采用SVN 客户端工具进行下载,比如 TortoiseSVN 客户端工具, SVN 服务器地址是 http://src.chromium.org/svn/trunk/src 。
3. 采用google 提供的 depot_tools 工具。
l 下载和安装 depot_tools 。
l 把depot_tools 的安装目录设置到系统目录(系统 Path 环境变量 ) 中。
l 创建一个存放Chrome 代码的目录,比如 d:\chrome 。目录不要包含空格。
l 在命令行下,先将当前目录切换到chrome 代码的目录,例如上面的 (d:\chrome) 。
l 运行 gclient config http://src.chromium.org/svn/trunk/src 命令。Gclient 将会先下载 svn 工具和 python 工具,然后调用 svn 进行代码同步。
注意: gclient 中下载svn 和 python 采用的是 javascript 实现。如果在需要设置 proxy 的环境中,则需要需要修改一下脚本。
1. 打开X:\ depot _tools\bootstrap\win\get_file.js文件。其中 X 是你的安装盘符。
2. 将Line9-Line22 行之间的代码
try {
xml_http = new ActiveXObject("MSXML2.ServerXMLHTTP");
} catch (e) {
WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() +
": Cannot create Active-X object (" + e.description) + ").";
WScript.Quit(1);
}
try {
xml_http.open("GET", url, false);
} catch (e) {
WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() +
": invalid URL.");
WScript.Quit(1);
}
修改成
try {
xml_http = new ActiveXObject("MSXML2. ServerXMLHTTP.5.0 ");
} catch (e) {
WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() +
": Cannot create Active-X object (" + e.description) + ").";
WScript.Quit(1);
}
try {
xml_http.setProxy(2, proxyIP:Port);
xml_http.open("GET", url, false);
xml_http. setProxyCredentials(username,pwd);
} catch (e) {
WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() +
": invalid URL.");
WScript.Quit(1);
}
编译 代码
如果你是下载的源代码包,则需要先解压,这个代码包是双重压缩。估计要把所有代码解压下来,半个小时左右,我在我的本本上是花了将近半个小时时间才解压出来,解压下来所有代码的大小是3 个多 G 。
从网上搜索了很久相关Chrome 编译相关的材料,大家都反馈在 src\chrome 目录下有 chrome.sln 文件,直接打开这个 sln 就可以利用 Visual Studio 2005 进行编译了。但是我翻遍所有代码却找不到这个文件,让我郁闷了很久,开始怀疑是我下的代码版本问题,在线查看了 Chrome 的 SVN 目录,发现最新版本也没有这个文件了。检查一下网上的那些文章基本上都是 2008 年的文章,开始怀疑是否是 chrome 做了改变,但是从 chrome 的官网上:
Building Chromium
1 Open the chrome/chrome.sln solution file in Visual Studio and build the solution. This can take from 25 minutes to 1 hour.
2 If you just want the Chromium browser, and none of the tests, you can speed up your build by right-clicking the chrome project in the solution explorer and selecting Build . You may want to make sure this project is the Startup project (which will display as bold) by right-clicking it and selecting Set as Startup Project . This will make Chromium (as opposed to some random test) build and run when you press F5 .
来看,似乎没有更新。最后在线翻阅了chrome 的开发 group 论坛,才知道 Chrome 确实作了修改,原来代码中的那些 .sln 、 .vcproj 文件全部抛弃了, google 自己开发了一个脚本工具 GYP 工具,这个工具也是采用python 编写的。 GYP 采用了自定义的一套规则,用于生成各种工程文件。我们可以看一下下面这个gyp 文件。
{
'includes': [
'../../build/common.gypi',
],
'targets': [
{
'target_name': 'memory_watcher',
'type': 'shared_library',
'msvs_guid': '3BD81303-4E14-4559-AA69-B30C3BAB08DD',
'dependencies': [
'../../base/base.gyp:base',
],
'defines': [
'BUILD_MEMORY_WATCHER',
],
'include_dirs': [
'../..',
],
'sources': [
'call_stack.cc',
'call_stack.h',
'dllmain.cc',
'hotkey.h',
'ia32_modrm_map.cc',
'ia32_opcode_map.cc',
'memory_hook.cc',
'memory_hook.h',
'memory_watcher.cc',
'memory_watcher.h',
'mini_disassembler.cc',
&nbs