electron 源码下载与编译构建 compile

推荐用electron自己的下载工具下载编译代码,非常方便:https://www.cnblogs.com/bigben0123/p/13098017.html

下面是自己手动执行各个步骤,或者遇到问题原理方便查看的:

windows代码下载:

1, 下载depot_tools 参见:https://www.cnblogs.com/bigben0123/p/13065252.html

2, 直接解压(不要打开后解压,会解不出来隐藏目录文件),然后设置环境变量:

GYP_MSVS_VERSION=2019 指定使用的vs版本。

GIT_CACHE_PATH设置的路径一定不要用双引号。否则python报错:‘“c:/ 路径不对。
set path=C:\Users\Administrator\Downloads\depot_tools;%PATH%
set DEPOT_TOOLS_WIN_TOOLCHAIN=0set GIT_CACHE_PATH=C:\Users\Administrator\Downloads\git_cache
set GCLIENT_PY3=0
cd C:\Users\Administrator\Downloads

3 执行:

gclient

会下载最新工具 python,git等。

3,

 gclient sync --with_branch_heads --with_tags  ( --no-history --nohooks 不下载历史版本,不能做版本切换。不下载外部依赖。)

Traceback (most recent call last):
  File "src/electron/script/apply_all_patches.py", line 36, in <module>
    main()
  File "src/electron/script/apply_all_patches.py", line 32, in main
    apply_patches(json.load(config_json))
  File "src/electron/script/apply_all_patches.py", line 18, in apply_patches
    committer_name="Electron Scripts", committer_email="scripts@electron")
  File "C:\zhibin\src\electron\script\lib\git.py", line 97, in import_patches
    newvalue='HEAD'
  File "C:\\src\electron\script\lib\git.py", line 121, in update_ref
    return subprocess.check_call(args)
  File "C:\\depot_tools\bootstrap-3_8_0_chromium_8_bin\python\bin\Lib\subprocess.py", line 187, in check_call
    retcode = call(*popenargs, **kwargs)
  File "C:\zhibin\depot_tools\bootstrap-3_8_0_chromium_8_bin\python\bin\Lib\subprocess.py", line 172, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\depot_tools\bootstrap-3_8_0_chromium_8_bin\python\bin\Lib\subprocess.py", line 396, in __init__
    errread, errwrite)
  File "C:\\depot_tools\bootstrap-3_8_0_chromium_8_bin\python\bin\Lib\subprocess.py", line 654, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Error: Command 'vpython.bat src/electron/script/apply_all_patches.py src/electron/patches/config.json' returned non-zero exit status 1 in C

需要装nodejs

 

5, gclient runhooks 下载外部依赖

 

6,编译报错:

6.1 对于electron编译,看了里面路径都对。

[8/34363] CXX obj/base/util/memory_pressure/memory_pressure/system_memory_pressure_evaluator_win.obj
FAILED: obj/base/util/memory_pressure/memory_pressure/system_memory_pressure_evaluator_win.obj
\\electron\external_binaries\sccache.exe ..\..\third_party\llvm-build\Release+Asserts\bin\clang-cl.exe /nologo /showIncludes:user "-imsvcC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\ATLMFC\include" "-imsvcC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include" "-imsvcC:\Program Files (x86)\Windows Kits\NETFXSDK\4.7\include\um" "-imsvcC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-imsvcC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-imsvcC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-imsvcC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-imsvcC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" -DDCHECK_ALWAYS_ON=1 -DUSE_AURA=1 "-DCR_CLANG_REVISION=\"n354867-0e41d647-2\"" -D_HAS_NODISCARD -D_LIBCPP_ABI_UNSTABLE -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_ENABLE_NODISCARD -D_LIBCPP_DEBUG=0 -D_LIBCPP_NO_AUTO_LINK -D__STD_C -D_CRT_RAND_S -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_DEPRECATE -D_ATL_NO_OPENGL -D_WINDOWS -DCERT_CHAIN_PARA_HAS_EXTRA_FIELDS -DPSAPI_VERSION=2 -DWIN32 -D_SECURE_ATL -DWINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP -DWIN32_LEAN_AND_MEAN -DNOMINMAX -D_UNICODE -DUNICODE -DNTDDI_VERSION=NTDDI_WIN10_RS2 -D_WIN32_WINNT=0x0A00 -DWINVER=0x0A00 -D_DEBUG -DDYNAMIC_ANNOTATIONS_ENABLED=1 -I../.. -Igen -I../../third_party/boringssl/src/include -fcolor-diagnostics -fmerge-all-constants -fcrash-diagnostics-dir=../../tools/clang/crashreports -Xclang -mllvm -Xclang -instcombine-lower-dbg-declare=0 -fcomplete-member-pointers /Gy /FS /bigobj /utf-8 /Zc:twoPhase /Zc:sizedDealloc- /X -fmsc-version=1916 /guard:cf,nochecks -m64 /Brepro -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -Xclang -fdebug-compilation-dir -Xclang . -no-canonical-prefixes /W4 -Wimplicit-fallthrough -Wunreachable-code -Wthread-safety -Wextra-semi /WX /wd4091 /wd4127 /wd4251 /wd4275 /wd4312 /wd4324 /wd4351 /wd4355 /wd4503 /wd4589 /wd4611 /wd4100 /wd4121 /wd4244 /wd4505 /wd4510 /wd4512 /wd4610 /wd4838 /wd4995 /wd4996 /wd4456 /wd4457 /wd4458 /wd4459 /wd4200 /wd4201 /wd4204 /wd4221 /wd4245 /wd4267 /wd4305 /wd4389 /wd4702 /wd4701 /wd4703 /wd4661 /wd4706 /wd4715 -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-undefined-var-template -Wno-nonportable-include-path -Wno-ignored-pragma-optimize -Wno-implicit-int-float-conversion -Wno-final-dtor-non-final-class -Wno-builtin-assume-aligned-alignment -Wno-deprecated-copy -Wno-non-c-typedef-for-linkage -Wmax-tokens /Od /Ob0 /GF -gline-tables-only -ftrivial-auto-var-init=pattern /MTd -Xclang -add-plugin -Xclang find-bad-constructs -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wno-undefined-bool-conversion -Wno-tautological-undefined-compare /TP /wd4577 /GR- -I../../buildtools/third_party/libc++/trunk/include /c ../../base/util/memory_pressure/system_memory_pressure_evaluator_win.cc /Foobj/base/util/memory_pressure/memory_pressure/system_memory_pressure_evaluator_win.obj /Fd"obj/base/util/memory_pressure/memory_pressure_cc.pdb"
CreateProcess failed: The system cannot find the file specified.
ninja: build stopped: subcommand failed.

这是由于sscache没有设置对。在out/Testing/args.gn

比如:cc_wrapper="D:/dev/electron-master/electron/src/electron/external_binaries/sccache.exe"

gn gen out/vsproject --ide=vs2019  --args="import(\"//electron/build/args/debug.gn\")   cc_wrapper = \"D:\dev\electron7\src\electron\external_binaries\sccache\""   --sln=electron7 --filters=//content/*;//electron/*;//third_party/blink/*  --no-deps

 

6.2 jumbo生成错误

clang++: error: unable to execute command: Killed
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 10.0.0 (https://github.com/llvm/llvm-project/ 8455294f2ac13d587b13d728038a9bffa7185f2b)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: ../../third_party/llvm-build/Release+Asserts/bin
clang++: note: diagnostic msg: PLEASE submit a bug report to https://crbug.com and run tools/clang/scripts/process_crashreports.py (only works inside Google) which will upload a report and include the crash backtrace, preprocessed source, and associated run script.
clang++: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: ../../tools/clang/crashreports/browser_jumbo_14-d89ff1.cpp
clang++: note: diagnostic msg: ../../tools/clang/crashreports/browser_jumbo_14-d89ff1.sh
clang++: note: diagnostic msg: 

********************
[16718/19561] CXX obj/content/browser/browser/browser_jumbo_2.o
ninja: build stopped: subcommand failed.

swap内存不够:参考https://www.cnblogs.com/bigben0123/p/13066136.html

electron 调试 渲染进程单进程模式

 

electron.exe --process-per-site  .

 启动调试

devenv /debugexe out\Debug\chrome.exe <options to Chromium can go here>

 

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.5.4
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86_x64'

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>
devenv /debugexe D:\dev\electron7\src\out\Testing\electron.exe  --process-per-site    --disable-gpu --disable-gpu-compositing   --enable-logging --v=1 D:\dev\nodejsdemo

 将离线git 仓库连接上远程

$ cd src/electron
$ git remote remove origin
$ git remote add origin https://github.com/electron/electron
$ git checkout master
$ git branch --set-upstream-to=origin/master
$ cd -
$ cd src/electron
$ git remote remove origin
$ git remote add origin https://github.com/electron/electron
$ git fetch 如果远程已有分支,可以获取到
$ git branch --set-upstream-to=origin/master
$ cd -

 git checkout 78.0.3904.130  检出的是tag,无法提交修改

git checkout -b branchElectron7 78.0.3904.130  为tag建立了本地分支

 git push origin branchElectron7:branchElectron7  将本地分支关联到远程分支。远程分支没有会被创建。

 

electron编译c++文件

 

所有的c++文件都存放在:
D:\dev\electron7\src\electron\filenames.gni

通过 D:\dev\electron7\src\electron\BUILD.gn 引用:

 


 

构建步骤 (Windows)

遵循下面的步骤, 在 Windows 平台上构建 Electron。

基本要求

  • Windows 10 / Server 2012 R2 或更高版本
  • Visual Studio 2017 15.7.2 或更高版本 - 免费下载 VS 2019 社区版
    • 请参阅Chromium构建文档,以了解有哪些Visual Studio 组件需要安装等详细信息。
    • 如果您的 Visual Studio 安装在非默认目录中, 您需要 设置几个环境变量来将工具链指向您的安装路径。
      • vs2019_install = DRIVE:\path\to\Microsoft Visual Studio\2019\Community, replacing 2019 and Community with your installed versions and replacing DRIVE: with the drive that Visual Studio is on. Often, this will be C:.
      • WINDOWSSDKDIR = DRIVE:\path\to\Windows Kits\10, replacing DRIVE: with the drive that Windows Kits is on. Often, this will be C:.
  • Python 2.7.10 或更高版本
    • 与下面 depot_tools 的安装说明不同,你必须安装 2.7.10 以上版本的 Python(支持 TLS 1.2)。 为此,应确保 PATH 中 Python 的安装目录在 depot_tools 之前。 目前 depot_tools 仍捆绑 Python 2.7.6,这将导致 gclient 命令失效(见 crbug.com/868864)。
    • Python for Windows (pywin32) 扩展对于构建流程也是必需的。
  • Node.js
  • Git
  • Debugging Tools for Windows of Windows SDK 10.0.15063.468 if you plan on creating a full distribution since symstore.exe is used for creating a symbol store from .pdb files.
    • 不同版本的SDK可以同时安装 安装 SDK,打开 Visual Studio 安装程序,选择 更改单个组件,向下滚动并选择适当的 要安装的 Windows SDK 组件。 另一个选择是查看 windows SDK 和仿真器存档 并分别下载 SDK 的独立版本。
    • 还必须安装 SDK 调试工具。 如果已安装了 Windows 10 SDK 通过 Visual Studio 安装程序,然后可以用以下方式安装它们: 控制面板程序程序和功能→选择“Windows 软件开发工具包”→ 更改更改→选中“Windows 调试工具”→更改。 或者,您可以下载独立的 SDK 安装程序,并且使用它安装调试工具。

如果您当前没有安装 Windows, dev.microsoftedge.com 上有时间限制的 Windows 版本,你可以用来构建 Electron。

构建 Electron 完全由命令行脚本完成,无法通过 Visual Studio 完成。 您可以使用任何编辑器开发 Electron,但将来将会使用 Visual Studio 构建支持。

Note: Even though Visual Studio is not used for building, it's still required because we need the build toolchains it provides.

32 位构建

为了构建 32bit 版本,您需要通过 target_cpu = “x86" 作为 GN 参数。 可以使用不同的 GN 输出目录(例如, out/Release-x86) 和不同的参数,在 64 位目标旁边构建 32 位目标。

$ gn gen out/Release-x86 --args="import(\"//electron/build/args/release.gn\") target_cpu=\"x86\""

其他构建步骤完全一样。

Visual Studio 项目

要生成 Visual Studio 项目,可以传递 --ide=vs2017 参数 给 gn gen

$ gn gen out/Testing --ide=vs2017

Command xxxx not found

如果你遇到了一个错误,类似 Command xxxx not found, 可以尝试使用 VS2015 Command Prompt 控制台来执行构建脚本.

Fatal internal compiler error: C1001

确保你已经安装了 Visual Studio 的最新安装包.

LNK1181: cannot open input file 'kernel32.lib'

重新安装 32位的 Node.js.

Error: ENOENT, stat 'C:\Users\USERNAME\AppData\Roaming\npm'

创建那个目录 应该可以解决问题:

$ mkdir ~\AppData\Roaming\npm

node-gyp is not recognized as an internal or external command

如果你使用 Git Bash 来构建,或许会遇到这个错误,可以使用 PowerShell 或 VS2015 Command Prompt 来代替.

无法在“…”处创建目录:文件名太长

node.js 有一些 极长的路径名,默认情况下,windows 上的 git 不能正确处理长路径名(即使 windows 支持它们)。 这应该可以修复它:

$ git config --system core.longpaths true

错误:使用未声明的标识符“DefaultDelegateCheckMode”

This can happen during build, when Debugging Tools for Windows has been installed with Windows Driver Kit. Uninstall Windows Driver Kit and install Debugging Tools with steps described above.

导入错误:没有名为 win32file 的模块

确保已使用 pip install pywin32 安装了 pywin32

构建脚本挂起, 直到某个按键按下才有响应

这个bug 是 Windows 命令提示符的一个"功能" It happens when clicking inside the prompt window with QuickEdit enabled and is intended to allow selecting and copying output text easily. 由于每次意外点击都会暂停构建过程,您可能需要在命令的属性中禁用此 功能。


 

构建Electron的常见问题(Mac)

 

 

背景

起因是产品的需求,需要更换Electron为底层平台,但因为会有不少定制化的功能要实现,必须自己实现此类内容,所以也就导致必须自己编译Electron的源代码。

整个构建过程,看Electron的构建文档就行了:https://github.com/electron/i18n/blob/master/content/zh-CN/docs/development/build-instructions-gn.md

不过在构建Electron的过程中,出现了很多问题。

一、拉取不了chromium代码。

这个问题大家都懂的,需要翻墙才拉得了代码。这里需要注意了,整个构建代码大约有20几G,得留意工具的套餐有没有这么多流量。

另外,一般来讲我们是用终端命令行拉代码,然而终端的请求是不会经过翻墙工具的,所以需要设代理。

例如我的小飞机是这么设的:

export http_proxy=http://127.0.0.1:1087;
export https_proxy=http://127.0.0.1:1087;

二、拉了好久都没拉完。

一开始我是在宿舍拉代码的,但用了一天一夜,都还没有拉完,而终端的日志也让人奔溃。

[7:52:44] Still working on:
[7:52:44]   src

后来感觉是因为宿舍的网速太慢,于是到公司去拉,约4个半小时就拉好!

[4:20:04] Still working on:
[4:20:04]   src/v8
Syncing projects: 100% (95/95), done.

所以,网速很重要!!!

三、拉不下来,报错了。

在公司拉代码时,第一次遇到报错,拉取失败。

[1:30:24] error: RPC failed; curl 18 transfer closed with outstanding read data remaining
[1:30:24] fatal: The remote end hung up unexpectedly
[1:30:24] fatal: early EOF
[1:30:24] fatal: index-pack failed

查询资料说是git的 http.postBuffer 这个参数太小,会导致大文件拉不下来。

于是设置了:

git config --global http.postBuffer 999M

这么设置后,就正常拉完了!!!也就是第二次成功了。

四、还是拉不下来。

磁盘空间不足,也会导致代码拉取失败的!

整体代码拉下来,大概是30几G,另外编译代码生成的中间代码也会有30几G,所以要保证当前磁盘空间至少有70多G(一般128G的Mac就不用想编译这东西了)。 

五、编译出错。

__main__.SdkError: 'Install Xcode, launch it, accept the license agreement, and run `sudo xcode-select -s /path/to/Xcode.app` to continue.'

这个错误,其实它说得很清楚,按提示处理就行。

sudo xcode-select -s /Applications/Xcode.app

总结

在整个构建过程中,问题主要是拉取代码和编译两方面。拉取代码保证网速、稍大的磁盘容量和Git http.postBuffer参数基本就没什么问题,而编译过程中的问题,按错误日志处理也就可以解决!

 

mac 上

1.获取源码

Copy
mkdir electron && cd electron
gclient config --name "src/electron" --unmanaged https://github.com/electron/electron
gclient sync --with_branch_heads --with_tags
# This will take a while, go get a coffee.

在获取源码时,需要外网访问权限,最重要的是需要网络稳定。下载源码时关键的一步是对git头的下载,大概有23G左右。在git头下载期间vpn重连导致下载速度变成很慢,这时可以重新执行命令下载了,如果过一段时间速度回升则可以继续下载,vpn断联大概率会导致之前代理下载的文件删除,需要重新下载。

如果git头已经下载完毕,这时候vpn断联可以通过删除除git头之外的文件,通过git先拉取一部分代码,因为删除文件会导致本地有未提交的git修改,可以通过以下命令强制覆盖本地修改。

Copy
git fetch --all                  # 拉取所有更新,不同步
git reset --hard origin/master   # 本地代码同步线上最新版本(会覆盖本地所有与远程仓库上同名的文件)
git pull                         # 再更新一次

git同步下来的代码并不完全是我们需要的代码,还需要通过gclient进行二次同步,不过再此之前我们可以先将electron的源码切换到我们想要编译的版本,例如v7.1.5,这样我们在执行gclient时可以直接同步electron-v7.1.5对应的源码。如果我们一切顺利代码一次性同步完后,在我们切换完electron版本后也要再执行以下的命令进行同步。

Copy
gclient sync --with_branch_heads --with_tags

如果electron的版本和gclient同步下来的chrome等的代码版本不对应,会导致一些错误发生,例如electron-v7.1.5编译时会使用jambo进行加速,但是在新的chrome中这个编译选项是禁止,即使我们禁用了jambo编译选项,但是又会导致其它的一些问题。

2.编译源码

Copy
cd src
export CHROMIUM_BUILDTOOLS_PATH=`pwd`/buildtools
# this next line is needed only if building with sccache
export GN_EXTRA_ARGS="${GN_EXTRA_ARGS} cc_wrapper=\"${PWD}/electron/external_binaries/sccache\""
gn gen out/Release --args="import(\"//electron/build/args/release.gn\") $GN_EXTRA_ARGS" --ide=xcode   # (mac)

sccache会帮助我们在删除编译文件后再次编译时进行加速 上面的命令会生成工程文件,下面的命令接着编译出目标文件

Copy
ninja -C out/Release electron
ninja -C out/Release electron:electron_dist_zip     # 编译出electron的同时将其打包成zip包

 

官方文档地址:

https://www.electronjs.org/docs/development/build-instructions-gn

https://www.electronjs.org/docs/development/build-instructions-linux

构建步骤 (Linux)

遵循下面的准则在 Linux 上创建 Electron 。

基本要求

  • 至少 25GB 硬盘空间 和 8GB 内存.

  • Python 2.7.x. 一些发行版如 CentOS 6.x 仍然使用 Python 2.6.x ,所以或许需要 check 你的 Python 版本,使用 python -V.

    请确保您的系统和Python的版本至少支持TLS 1.2。 您可以运行下面这个脚本来测试:

    $ npx @electron/check-python-tls
    

    如果脚本反映你的设置使用过时的安全协议,请用系统的软件包管理器更新Python在2.7.x中的最高版本。 或者,您也可以访问ttps://www.python.org/downloads/,以获取更加详细的指导信息。

  • Node.js. 有多种方法安装 Node.js。 您可以从 Nodejs.org 下载源代码并进行编译。 也可以作为一个标准的用户在 home 目录下安装 node. 或者尝试使用 NodeSource 仓库

  • 下载nodejs:
https://github.com/nodejs/help/wiki/Installation#how-to-install-nodejs-via-binary-archive-on-linux

How to install Node.js via binary archive on Linux?
Unzip the binary archive to any directory you wanna install Node, I use /usr/local/lib/nodejs
 VERSION=v10.15.0
 DISTRO=linux-x64
 sudo mkdir -p /usr/local/lib/nodejs
 sudo tar -xJvf node-$VERSION-$DISTRO.tar.xz -C /usr/local/lib/nodejs 
Set the environment variable ~/.profile, add below to the end
# Nodejs
VERSION=v10.15.0
DISTRO=linux-x64
export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH
Refresh profile
. ~/.profile
Test installation using
$ node -v

$ npm version

$ npx -v

the normal output is:

➜  node -v
v10.15.1
➜  npm version
{ npm: '6.4.1',
 ares: '1.15.0',
 cldr: '33.1',
 http_parser: '2.8.0',
 icu: '62.1',
 modules: '64',
 napi: '3',
 nghttp2: '1.34.0',
 node: '10.15.1',
 openssl: '1.1.0j',
 tz: '2018e',
 unicode: '11.0',
 uv: '1.23.2',
 v8: '6.8.275.32-node.12',
 zlib: '1.2.11' }

 

  • clang 3.4 或更高版本。

  • Development headers of GTK 3 and libnotify.

在 Ubuntu, 安装下面的库:

$ sudo apt-get install build-essential clang libdbus-1-dev libgtk-3-dev \
                       libnotify-dev libgnome-keyring-dev \
                       libasound2-dev libcap-dev libcups2-dev libxtst-dev \
                       libxss1 libnss3-dev gcc-multilib g++-multilib curl \
                       gperf bison python-dbusmock openjdk-8-jre

在 RHEL / CentOS, 安装下面的库:

$ sudo yum install clang dbus-devel gtk3-devel libnotify-devel \
                   libgnome-keyring-devel xorg-x11-server-utils libcap-devel \
                   cups-devel libXtst-devel alsa-lib-devel libXrandr-devel \
                   nss-devel python-dbusmock openjdk-8-jre

在 Fedora, 安装下面的库:

$ sudo dnf install clang dbus-devel gtk3-devel libnotify-devel \
                   libgnome-keyring-devel xorg-x11-server-utils libcap-devel \
                   cups-devel libXtst-devel alsa-lib-devel libXrandr-devel \
                   nss-devel python-dbusmock openjdk-8-jre

Other distributions may offer similar packages for installation via package managers such as pacman. Or one can compile from source code.

交叉编译

如果想创建一个 arm target ,应当还要下载下面的依赖:

$ sudo apt-get install libc6-dev-armhf-cross linux-libc-dev-armhf-cross \
                       g++-arm-linux-gnueabihf

类似地,对于 arm64 平台,请安装下列内容:

$ sudo apt-get install libc6-dev-arm64-cross linux-libc-dev-arm64-cross \
                       g++-aarch64-linux-gnu

若要为 arm 或 ia32 平台的目标设备交叉编译,您应当为 target_cpu 添加gn gen 参数:

$ gn gen out/Testing --args='import(...) target_cpu="arm"'

加载共享库时出现错误: libtinfo.so.5

Prebuilt clang will try to link to libtinfo.so.5. Depending on the host architecture, symlink to appropriate libncurses:

$ sudo ln -s /usr/lib/libncurses.so.5 /usr/lib/libtinfo.so.5

高级提示

The default building configuration is targeted for major desktop Linux distributions. To build for a specific distribution or device, the following information may help you.

使用系统提供的 clang 替换下载的 clang 二进制文件

默认情况下, Electron 是由 Chromium 项目提供的预生成的 clang 二进制文件构建的。 如果出于某些原因你想用你系统已安装的 clang来构建,你可以在GN的参数中指定clang_base_path

例如如果你的 clang安装在 /usr/local/bin/clang下:

$ gn gen out/Testing --args='import("//electron/build/args/testing.gn") clang_base_path = "/usr/local/bin"'

使用 clang 之外的其它编译器

Electron 不支持除 clang之外的其他编译器构建

 

——————————————————————————————————————————————————

Build Instructions

请遵循以下指南来构建Electron。

平台要求

各个平台所对应的构建要求如下:

前置知识

此外,你还需要安装depot_tools,这是一个用于获取Chromium,及其相关依赖工具。

另外,如果使用Windows系统, 你需要设置环境变量DEPOT_TOOLS_WIN_TOOLCHAIN=0。 依次打开 Control Panel → System and Security → System → Advanced system settings ,然后添加系统变量 DEPOT_TOOLS_WIN_TOOLCHAIN ,并设置默认值为 0. 这将促使depot_tools 使用本地已安装的Visual Studio(默认状态下,depot_tools将会下载一个只有谷歌内部员工有权限使用的内部版本)。

Setting up the git cache

If you plan on checking out Electron more than once (for example, to have multiple parallel directories checked out to different branches), using the git cache will speed up subsequent calls to gclient. To do this, set a GIT_CACHE_PATH environment variable:

$ export GIT_CACHE_PATH="${HOME}/.git_cache"
$ mkdir -p "${GIT_CACHE_PATH}"
# This will use about 16G.

Getting the code

$ mkdir electron && cd electron
$ gclient config --name "src/electron" --unmanaged https://github.com/electron/electron
$ gclient sync --with_branch_heads --with_tags
# This will take a while, go get a coffee.

Instead of https://github.com/electron/electron, you can use your own fork here (something like https://github.com/<username>/electron).

A note on pulling/pushing

If you intend to git pull or git push from the official electron repository in the future, you now need to update the respective folder's origin URLs.

$ cd src/electron
$ git remote remove origin
$ git remote add origin https://github.com/electron/electron
$ git checkout master
$ git branch --set-upstream-to=origin/master
$ cd -

📝 gclient works by checking a file called DEPS inside the src/electron folder for dependencies (like Chromium or Node.js). Running gclient sync -f ensures that all dependencies required to build Electron match that file.

So, in order to pull, you'd run the following commands:

$ cd src/electron
$ git pull
$ gclient sync -f

构建

$ cd src
$ export CHROMIUM_BUILDTOOLS_PATH=`pwd`/buildtools
# this next line is needed only if building with sccache
$ export GN_EXTRA_ARGS="${GN_EXTRA_ARGS} cc_wrapper=\"${PWD}/electron/external_binaries/sccache\""
$ gn gen out/Testing --args="import(\"//electron/build/args/testing.gn\") $GN_EXTRA_ARGS"

Or on Windows (without the optional argument):

$ cd src
$ set CHROMIUM_BUILDTOOLS_PATH=%cd%\buildtools
$ gn gen out/Testing --args="import(\"//electron/build/args/testing.gn\")"

This will generate a build directory out/Testing under src/ with the testing build configuration. You can replace Testing with another name, but it should be a subdirectory of out. Also you shouldn't have to run gn gen again—if you want to change the build arguments, you can run gn args out/Testing to bring up an editor.

To see the list of available build configuration options, run gn args out/Testing --list.

For generating Testing build config of Electron:

$ gn gen out/Testing --args="import(\"//electron/build/args/testing.gn\") $GN_EXTRA_ARGS"

For generating Release (aka "non-component" or "static") build config of Electron:

$ gn gen out/Release --args="import(\"//electron/build/args/release.gn\") $GN_EXTRA_ARGS"

To build, run ninja with the electron target: Nota Bene: This will also take a while and probably heat up your lap.

For the testing configuration:

$ ninja -C out/Testing electron

For the release configuration:

$ ninja -C out/Release electron

这个过程会构建 'libchromiumcontent' 里的所有内容,(如 chromium中的content,及其依赖(包括Webkit 和 V8))。因此,这个构建过程会比较费时。

你可以使用sccache命令来提高后面的构建过程。 Add the GN arg cc_wrapper = "sccache" by running gn args out/Testing to bring up an editor and adding a line to the end of the file.

The built executable will be under ./out/Testing:

$ ./out/Testing/Electron.app/Contents/MacOS/Electron
# or, on Windows
$ ./out/Testing/electron.exe
# or, on Linux
$ ./out/Testing/electron

打包

On linux, first strip the debugging and symbol information:

electron/script/strip-binaries.py -d out/Release

To package the electron build as a distributable zip file:

ninja -C out/Release electron:electron_dist_zip

交叉编译

To compile for a platform that isn't the same as the one you're building on, set the target_cpu and target_os GN arguments. For example, to compile an x86 target from an x64 host, specify target_cpu = "x86" in gn args.

$ gn gen out/Testing-x86 --args='... target_cpu = "x86"'

Not all combinations of source and target CPU/OS are supported by Chromium.

HostTarget状态
Windows x64 Windows arm64 实验功能
Windows x64 Windows x86 Automatically tested
Linux x64 Linux x86 Automatically tested

If you test other combinations and find them to work, please update this document :)

See the GN reference for allowable values of target_os and target_cpu.

Windows on Arm (experimental)

To cross-compile for Windows on Arm, follow Chromium's guide to get the necessary dependencies, SDK and libraries, then build with ELECTRON_BUILDING_WOA=1 in your environment before running gclient sync.

set ELECTRON_BUILDING_WOA=1
gclient sync -f --with_branch_heads --with_tags

Or (if using PowerShell):

$env:ELECTRON_BUILDING_WOA=1
gclient sync -f --with_branch_heads --with_tags

Next, run gn gen as above with target_cpu="arm64".

测试

To run the tests, you'll first need to build the test modules against the same version of Node.js that was built as part of the build process. To generate build headers for the modules to compile against, run the following under src/ directory.

$ ninja -C out/Testing third_party/electron_node:headers

You can now run the tests.

可以通过增加其它标记来调试程序,例如:

$ npm run test -- \
  --enable-logging -g 'BrowserWindow module'

Sharing the git cache between multiple machines

It is possible to share the gclient git cache with other machines by exporting it as SMB share on linux, but only one process/machine can be using the cache at a time. The locks created by git-cache script will try to prevent this, but it may not work perfectly in a network.

On Windows, SMBv2 has a directory cache that will cause problems with the git cache script, so it is necessary to disable it by setting the registry key

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Lanmanworkstation\Parameters\DirectoryCacheLifetime

to 0. More information: stackoverflow.com/a/9935126

This can be set quickly in powershell (ran as administrator):

New-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\Lanmanworkstation\Parameters" -Name DirectoryCacheLifetime -Value 0 -PropertyType DWORD -Force

Stale locks in the git cache

If gclient sync is interrupted while using the git cache, it will leave the cache locked. 如需移除锁,请将--ignore_locks参数传递至 gclient sync

I'm being asked for a username/password for chromium-internal.googlesource.com

If you see a prompt for Username for 'https://chrome-internal.googlesource.com': when running gclient sync on Windows, it's probably because the DEPOT_TOOLS_WIN_TOOLCHAIN environment variable is not set to 0. Open Control Panel → System and Security → System → Advanced system settings and add a system variable DEPOT_TOOLS_WIN_TOOLCHAIN with value 0. 这将促使depot_tools 使用本地已安装的Visual Studio(默认状态下,depot_tools将会下载一个只有谷歌内部员工有权限使用的内部版本)。

 

posted @ 2020-05-26 18:06  Bigben  阅读(1075)  评论(0编辑  收藏