2018-09-09 04:15:32 +03:00
# Build Instructions
2018-05-18 23:04:46 +03:00
2021-04-19 18:50:00 +03:00
Follow the guidelines below for building **Electron itself** , for the purposes of creating custom Electron binaries. For bundling and distributing your app code with the prebuilt Electron binaries, see the [application distribution][application-distribution] guide.
[application-distribution]: ../tutorial/application-distribution.md
2018-05-18 23:04:46 +03:00
2018-08-21 22:40:06 +03:00
## Platform prerequisites
2018-05-18 23:04:46 +03:00
2018-07-25 17:45:52 +03:00
Check the build prerequisites for your platform before proceeding
2020-11-06 01:12:43 +03:00
* [macOS ](build-instructions-macos.md#prerequisites )
* [Linux ](build-instructions-linux.md#prerequisites )
* [Windows ](build-instructions-windows.md#prerequisites )
2018-07-25 17:45:52 +03:00
2020-06-08 18:59:19 +03:00
## Build Tools
[Electron's Build Tools ](https://github.com/electron/build-tools ) automate much of the setup for compiling Electron from source with different configurations and build targets. If you wish to set up the environment manually, the instructions are listed below.
2021-10-27 04:18:38 +03:00
Electron uses [GN ](https://gn.googlesource.com/gn ) for project generation and
[ninja ](https://ninja-build.org/ ) for building. Project configurations can
be found in the `.gn` and `.gni` files.
## GN Files
The following `gn` files contain the main rules for building Electron:
* `BUILD.gn` defines how Electron itself is built and
includes the default configurations for linking with Chromium.
* `build/args/{testing,release,all}.gn` contain the default build arguments for
building Electron.
2018-08-21 22:40:06 +03:00
## GN prerequisites
2018-07-25 17:45:52 +03:00
You'll need to install [`depot_tools`][depot-tools], the toolset
used for fetching Chromium and its dependencies.
2018-08-11 20:20:19 +03:00
Also, on Windows, you'll need to set the environment variable
`DEPOT_TOOLS_WIN_TOOLCHAIN=0` . To do so, open `Control Panel` → `System and
Security` → `System` → `Advanced system settings` and add a system variable
`DEPOT_TOOLS_WIN_TOOLCHAIN` with value `0` . This tells `depot_tools` to use
your locally installed version of Visual Studio (by default, `depot_tools` will
try to download a Google-internal version that only Googlers have access to).
2018-05-18 23:04:46 +03:00
2020-11-02 12:58:14 +03:00
[depot-tools]: https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html#_setting_up
2018-05-18 23:04:46 +03:00
2020-04-28 19:17:50 +03:00
### Setting up the git cache
2018-05-18 23:04:46 +03:00
2020-04-28 19:17:50 +03:00
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:
2018-08-17 19:20:20 +03:00
```sh
2018-08-21 22:40:06 +03:00
$ export GIT_CACHE_PATH="${HOME}/.git_cache"
$ mkdir -p "${GIT_CACHE_PATH}"
# This will use about 16G.
2018-08-17 19:20:20 +03:00
```
2018-09-10 22:31:41 +03:00
2018-08-21 22:40:06 +03:00
## Getting the code
2018-08-17 19:20:20 +03:00
2018-05-18 23:04:46 +03:00
```sh
2020-04-28 19:17:50 +03:00
$ mkdir electron & & cd electron
2019-04-23 12:11:35 +03:00
$ gclient config --name "src/electron" --unmanaged https://github.com/electron/electron
2018-05-18 23:04:46 +03:00
$ gclient sync --with_branch_heads --with_tags
# This will take a while, go get a coffee.
```
2018-10-22 23:40:39 +03:00
> Instead of `https://github.com/electron/electron`, you can use your own fork
> here (something like `https://github.com/<username>/electron`).
2020-11-06 01:11:38 +03:00
### A note on pulling/pushing
2018-10-10 07:48:37 +03:00
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.
```sh
$ cd src/electron
$ git remote remove origin
$ git remote add origin https://github.com/electron/electron
2021-09-27 18:35:56 +03:00
$ git checkout main
$ git branch --set-upstream-to=origin/main
2018-10-10 07:48:37 +03:00
$ cd -
```
2019-03-20 23:12:47 +03:00
:memo: `gclient` works by checking a file called `DEPS` inside the
2018-10-10 07:48:37 +03:00
`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:
2020-11-06 01:12:43 +03:00
2018-10-10 07:48:37 +03:00
```sh
$ cd src/electron
$ git pull
$ gclient sync -f
```
2018-05-18 23:04:46 +03:00
## Building
2022-01-31 14:36:47 +03:00
**Set the environment variable for chromium build tools**
On Linux & MacOS
2018-05-18 23:04:46 +03:00
```sh
$ cd src
$ export CHROMIUM_BUILDTOOLS_PATH=`pwd`/buildtools
```
2022-01-31 14:36:47 +03:00
On Windows:
2020-11-06 01:12:43 +03:00
2018-10-22 23:40:39 +03:00
```sh
$ cd src
$ set CHROMIUM_BUILDTOOLS_PATH=%cd%\buildtools
```
2022-01-31 14:36:47 +03:00
**To generate Testing build config of Electron:**
2018-07-25 17:45:52 +03:00
```sh
2022-01-31 14:36:47 +03:00
$ gn gen out/Testing --args="import(\"//electron/build/args/testing.gn\")"
2018-07-25 17:45:52 +03:00
```
2022-01-31 14:36:47 +03:00
**To generate Release build config of Electron:**
2018-07-25 17:45:52 +03:00
```sh
2022-01-31 14:36:47 +03:00
$ gn gen out/Release --args="import(\"//electron/build/args/release.gn\")"
2018-07-25 17:45:52 +03:00
```
2018-05-18 23:04:46 +03:00
2022-01-31 14:36:47 +03:00
**Note:** This will generate a `out/Testing` or `out/Release` build directory under `src/` with the testing or release build depending upon the configuration passed above. You can replace `Testing|Release` with another names, 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` .
2018-09-13 23:46:49 +03:00
**To build, run `ninja` with the `electron` target:**
2022-01-28 23:40:20 +03:00
Note: This will also take a while and probably heat up your lap.
2018-05-18 23:04:46 +03:00
2019-11-07 22:51:44 +03:00
For the testing configuration:
2020-11-06 01:12:43 +03:00
2018-05-18 23:04:46 +03:00
```sh
2019-11-07 22:51:44 +03:00
$ ninja -C out/Testing electron
2018-09-24 19:12:16 +03:00
```
For the release configuration:
2020-11-06 01:12:43 +03:00
2018-09-24 19:12:16 +03:00
```sh
$ ninja -C out/Release electron
2018-05-18 23:04:46 +03:00
```
This will build all of what was previously 'libchromiumcontent' (i.e. the
`content/` directory of `chromium` and its dependencies, incl. WebKit and V8),
so it will take a while.
2019-11-07 22:51:44 +03:00
The built executable will be under `./out/Testing` :
2018-05-18 23:04:46 +03:00
```sh
2019-11-07 22:51:44 +03:00
$ ./out/Testing/Electron.app/Contents/MacOS/Electron
2018-08-11 20:20:19 +03:00
# or, on Windows
2019-11-07 22:51:44 +03:00
$ ./out/Testing/electron.exe
2018-05-18 23:04:46 +03:00
# or, on Linux
2019-11-07 22:51:44 +03:00
$ ./out/Testing/electron
2018-05-18 23:04:46 +03:00
```
2019-03-09 02:24:00 +03:00
### Packaging
On linux, first strip the debugging and symbol information:
2020-11-06 01:12:43 +03:00
2019-03-09 02:24:00 +03:00
```sh
2022-01-31 14:36:47 +03:00
$ electron/script/strip-binaries.py -d out/Release
2019-03-09 02:24:00 +03:00
```
To package the electron build as a distributable zip file:
2020-11-06 01:12:43 +03:00
2019-03-09 02:24:00 +03:00
```sh
2022-01-31 14:36:47 +03:00
$ ninja -C out/Release electron:electron_dist_zip
2019-03-09 02:24:00 +03:00
```
2018-07-20 02:57:54 +03:00
### Cross-compiling
To compile for a platform that isn't the same as the one you're building on,
2018-08-11 20:20:19 +03:00
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` .
2018-07-20 02:57:54 +03:00
```sh
2019-11-07 22:51:44 +03:00
$ gn gen out/Testing-x86 --args='... target_cpu = "x86"'
2018-07-20 02:57:54 +03:00
```
Not all combinations of source and target CPU/OS are supported by Chromium.
2019-06-04 00:29:25 +03:00
2020-11-10 00:52:06 +03:00
| Host | Target | Status |
|-------------|---------------|----------------------|
| Windows x64 | Windows arm64 | Experimental |
| Windows x64 | Windows x86 | Automatically tested |
| Linux x64 | Linux x86 | Automatically tested |
2019-06-04 00:29:25 +03:00
If you test other combinations and find them to work, please update this document :)
2018-08-11 20:20:19 +03:00
See the GN reference for allowable values of [`target_os`][target_os values]
2019-06-04 00:29:25 +03:00
and [`target_cpu`][target_cpu values].
2018-08-11 20:20:19 +03:00
2022-03-17 15:45:55 +03:00
[target_os values]: https://gn.googlesource.com/gn/+/main/docs/reference.md#built_in-predefined-variables-target_os_the-desired-operating-system-for-the-build-possible-values
[target_cpu values]: https://gn.googlesource.com/gn/+/main/docs/reference.md#built_in-predefined-variables-target_cpu_the-desired-cpu-architecture-for-the-build-possible-values
2018-07-20 02:57:54 +03:00
2019-06-04 00:29:25 +03:00
#### Windows on Arm (experimental)
2020-11-06 01:12:43 +03:00
2022-03-17 15:45:55 +03:00
To cross-compile for Windows on Arm, [follow Chromium's guide ](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/windows_build_instructions.md#Visual-Studio ) to get the necessary dependencies, SDK and libraries, then build with `ELECTRON_BUILDING_WOA=1` in your environment before running `gclient sync` .
2019-06-04 00:29:25 +03:00
```bat
set ELECTRON_BUILDING_WOA=1
gclient sync -f --with_branch_heads --with_tags
```
Or (if using PowerShell):
2020-11-06 01:12:43 +03:00
2019-06-04 00:29:25 +03:00
```powershell
$env:ELECTRON_BUILDING_WOA=1
gclient sync -f --with_branch_heads --with_tags
```
Next, run `gn gen` as above with `target_cpu="arm64"` .
2018-05-18 23:04:46 +03:00
## Tests
To run the tests, you'll first need to build the test modules against the
2018-07-25 17:45:52 +03:00
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.
2018-05-18 23:04:46 +03:00
```sh
2019-11-07 22:51:44 +03:00
$ ninja -C out/Testing third_party/electron_node:headers
2018-05-18 23:04:46 +03:00
```
2019-10-04 17:11:51 +03:00
You can now [run the tests ](testing.md#unit-tests ).
2018-05-18 23:04:46 +03:00
If you're debugging something, it can be helpful to pass some extra flags to
the Electron binary:
```sh
2019-11-07 22:51:44 +03:00
$ npm run test -- \
2019-10-31 02:38:21 +03:00
--enable-logging -g 'BrowserWindow module'
2018-05-18 23:04:46 +03:00
```
2018-09-12 05:06:55 +03:00
## 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
```sh
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Lanmanworkstation\Parameters\DirectoryCacheLifetime
```
to 0. More information: https://stackoverflow.com/a/9935126
2019-07-15 11:57:33 +03:00
This can be set quickly in powershell (ran as administrator):
```powershell
New-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\Lanmanworkstation\Parameters" -Name DirectoryCacheLifetime -Value 0 -PropertyType DWORD -Force
```
2018-09-12 05:06:55 +03:00
## Troubleshooting
2020-10-13 20:23:38 +03:00
### gclient sync complains about rebase
If `gclient sync` is interrupted the git tree may be left in a bad state, leading to a cryptic message when running `gclient sync` in the future:
```plaintext
2> Conflict while rebasing this branch.
2> Fix the conflict and run gclient again.
2> See man git-rebase for details.
```
If there are no git conflicts or rebases in `src/electron` , you may need to abort a `git am` in `src` :
```sh
$ cd ../
$ git am --abort
$ cd electron
$ gclient sync -f
```
2018-11-19 22:53:28 +03:00
### I'm being asked for a username/password for chromium-internal.googlesource.com
2020-11-06 01:12:43 +03:00
2018-11-19 22:53:28 +03:00
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` . This tells `depot_tools` to use
your locally installed version of Visual Studio (by default, `depot_tools` will
try to download a Google-internal version that only Googlers have access to).