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 仓库
-
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"‘
——————————————————————————————————————————————————
Electron 文档
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 likehttps://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.
Host | Target | 状态 |
---|---|---|
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: *.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
将会下载一个只有谷歌内部员工有权限使用的内部版本)。