如何在 Windows 下编译 Firefox

Firefox 68 ESR 终于发布了,正好需要 Debug 顺便记录流程。

在这里,我不从他们的代码控制获取代码编译,使用的是发布的源代码包。

预备工作

准备一台 Windows 10 x64,安装 Visual Studio 2019 并且选择「Desktop development with C++」和「Game development with C++」两大类。除了这两大项,还要选择最新且符合系统的「Windows 10 SDK」和「C++ ATL for v142 build tools (x86 and x64)」(如果要编译 ARM64 版本的,那么还要选择 ARM 的哟)。

当然,强劲的配置和足够的硬盘空间(官方推荐 40 GB+)是不可或缺的!

下载必备工具

这里下载 MozillaBuild 这个东西。

下载后解包。

Rust

是的,要安装 Rust 的。

带 llvm-config 的 LLVM

默认在 llvm.org 下载的 Windows 安装包是没有 llvm-config 的!不要被骗了!

The Windows releases don't include LLVM headers, libraries or tools (including llvm-config.exe) for using LLVM as a library. They are targeted towards users using LLVM as a tool-chain.

所以我们需要自己编译 LLVM 啦!

https://clangbuilder.net/clangbuilder/ 下载代码,然后找到 script 文件夹下的 InitializeEnv.bat,打开!它会配置好自己使用时需要的环境并且本地编译一个 ClangbuilderUI 来方便编译 LLVM。

注:下载过程中建议使用 Proxifier 搭配代理。

一切就绪后,打开 Clangbuilder。

经测试,此时使用 LLVM 9.0.0 编译 Firefox 会出问题

Branches 选择 Release,然后勾选下面的「Create Installation Package」、「Use Clean Environment (Env)」和「Build LLDB (Visual Studio 2015 or Later)」,点击 Building 就可以啦!

编译成功后会生成一个安装包,安装然后自行配置环境变量即可。

nasm

这个我没有解决,只能 Disable 了……

https://www.nasm.us/pub/nasm/releasebuilds/ 下载即可。

可能还需要的

Python 2(他们至今还没升级到 Python 3)、cmake……?

开始编译

在编译之前,我们需要弄一个配置文件——.mozconfig

因为默认编译是产出 x86 架构的,所以我们要更改一下。

打开 start-shell.bat 进入环境,然后进入解包后的源代码文件夹(cd /c/firefox-68.0 之类的)。

touch .mozconfig

接下来编辑 .mozconfig 这个文件,这里是我的配置:

# Make 64-bit build
ac_add_options --host=x86_64-pc-mingw32
ac_add_options --target=x86_64-pc-mingw32

# Disable and hide Firefox Accounts And Sync
mk_add_options MOZ_SERVICES_SYNC=0

# Disable Telemetry
mk_add_options MOZ_SERVICES_HEALTHREPORT=0
mk_add_options MOZ_DATA_REPORTING=0
mk_add_options MOZ_TELEMETRY_REPORTING=0

# Disable Wi-Fi geolocation service
ac_add_options --disable-necko-wifi

# Disable built-in extensions from "/scr/extensions/" folder
ac_add_options --disable-extensions

# Set telemetry option to "false" ("toolkit.telemetry.enabled" is locked to "true" on beta, aurora, nightly and default channels)
ac_add_options --enable-update-channel=release

# Disable Mozilla Maintenance Service (allows Firefox updates to occur in the background)
ac_add_options --disable-maintenance-service

# Disable AV1 (AOMedia Video 1 is an open, royalty-free video coding format designed for video transmissions over the Internet)
ac_add_options --disable-av1

然后保存,在终端中输入 export MOZCONFIG=/h/firefox/firefox-68.0/.mozconfig 生效。

接下来开始 ./mach boostrap

在这里我不需要 Artifact build,所以我选择不带 Artifact 的。

注:过程中建议使用 Proxifier 搭配代理。

等操作处理完后,再输入 ./mach build

待洗个澡,吃个饭,泡个茶,看个新番之后,如果出现这个就说明成功啦!

To view resource usage of the build, run |mach resource-usage|.
20:53.10 We know it took a while, but your build finally finished successfully!
To take your build for a test drive, run: |mach run|
For more information on what to do now, see https://developer.mozilla.org/docs/Developer_Guide/So_You_Just_Built_Firefox

现在可以 ./mach run 来运行,或者 ./mach package 来生成安装包啦!

生成的 exe 在 firefox-68.0\obj-x86_64-pc-mingw32\dist\bin 下,安装包在 firefox-68.0\obj-x86_64-pc-mingw32\dist\install\sea 下。注意,默认是 unoffical branding,所以狐狸是跑了的。

后日谈

如果要用 Visual Studio 来 Debug 的话,还需要 ./mach build-backend -b VisualStudio 一下。

参考资料


如果喜欢本文,欢迎点击下方的「鼓掌」按钮!

如果上面没有加载出任何东西,可以点击这里