Skip to main content

使用使用 GN 构建 Electron

· 5 min read

Electron 现在使用 GN 进行自我构建。以下是原因说明。

¥Electron now uses GN to build itself. Here's a discussion of why.


GYP 和 GN

¥GYP and GN

2013 年 Electron 首次发布时,Chromium 的构建配置是用 GYP("生成你的项目" 的缩写)编写的。

¥When Electron was first released in 2013, Chromium's build configuration was written with GYP, short for "Generate Your Projects".

2014 年,Chromium 项目引入了一个名为 GN(“Generate 忍者” 的缩写)的新构建配置工具。Chromium 的构建文件迁移到了 GN,GYP 也从源代码中移除。

¥In 2014, the Chromium project introduced a new build configuration tool called GN (short for "Generate Ninja") Chromium's build files were migrated to GN and GYP was removed from the source code.

Electron 一直以来都将主要的 Electron 代码libchromiumcontent 分开,libchromiumcontent 是 Electron 中封装 Chromium 'content' 子模块的部分。Electron 继续使用 GYP,而作为 Chromium 子集的 libchromiumcontent 也随着 Chromium 的迁移而切换到了 GN。

¥Electron has historically kept a separation between the main Electron code and libchromiumcontent, the part of Electron that wraps Chromium's 'content' submodule. Electron has carried on using GYP, while libchromiumcontent -- as a subset of Chromium -- switched to GN when Chromium did.

就像无法完全啮合的齿轮一样,使用这两种构建系统之间存在摩擦。维护兼容性很容易出错,因为编译器标志和 #defines 需要在 Chromium、Node、V8 和 Electron 之间保持同步。

¥Like gears that don't quite mesh, there was friction between using the two build systems. Maintaining compatibility was error-prone, from compiler flags and #defines that needed to be meticulously kept in sync between Chromium, Node, V8, and Electron.

为了解决这个问题,Electron 团队一直致力于将所有内容迁移到 GN。今天,用于从 Electron 中移除最后 GYP 代码的 commit 已发布到 master 分支。

¥To address this, the Electron team has been working on moving everything to GN. Today, the commit to remove the last of the GYP code from Electron was landed in master.

这对你意味着什么

¥What this means for you

如果你正在为 Electron 本身做贡献,那么从 master 或 4.0.0 版本检出并构建 Electron 的过程与 3.0.0 及更早版本截然不同。请参阅 GN 构建说明 获取详情。

¥If you're contributing to Electron itself, the process of checking out and building Electron from master or 4.0.0 is very different than it was in 3.0.0 and earlier. See the GN build instructions for details.

如果你正在使用 Electron 开发应用,你可能会注意到新版 Electron 4.0.0-nightly 中有一些细微的变化;但更有可能的是,Electron 在构建系统中的更改对你来说是完全透明的。

¥If you're developing an app with Electron, there are a few minor changes you might notice in the new Electron 4.0.0-nightly; but more than likely, Electron's change in build system will be totally transparent to you.

这对 Electron 意味着什么

¥What this means for Electron

GN 比 GYP 更先进,其文件更易于阅读和维护。此外,我们希望使用单一构建配置系统能够减少将 Electron 升级到新版本 Chromium 所需的工作量。

¥GN is faster than GYP and its files are more readable and maintainable. Moreover, we hope that using a single build configuration system will reduce the work required to upgrade Electron to new versions of Chromium.

  • 它已经极大地帮助了 Electron 4.0.0 的开发,因为 Chromium 67 取消了对 MSVC 的支持,并在 Windows 上切换到使用 Clang 构建。GN 版本直接从 Chromium 继承了所有编译器命令,因此我们免费获得了 Windows 上的 Clang 版本!

    ¥It's already helped development on Electron 4.0.0 substantially because Chromium 67 removed support for MSVC and switched to building with Clang on Windows. With the GN build, we inherit all the compiler commands from Chromium directly, so we got the Clang build on Windows for free!

  • 它还使 Electron 更容易在 Electron、Chromium 和 Node 之间统一构建使用 BoringSSL - 这在 之前存在问题 中是可以做到的。

    ¥It's also made it easier for Electron to use BoringSSL in a unified build across Electron, Chromium, and Node -- something that was problematic before.