It is important to note that this article uses the CLion IDE and macOS

Other ides and systems can be adapted along these lines

preface

There are some problems with the electron official build documentation. When you follow the documentation completely, you will find it impossible to debug. When LLDB is used, variable information and context under the current frame cannot be seen. It looks like this:

build

In order to quickly skip over the unimportant information, let’s start with the problem on the website.

The official construction documents are as follows:

  1. Build Steps (macOS)
  2. Build Instructions (macOS)

Please make sure your MacOS SDK is correct before you prepare for the operation. For details, see “Setting up macOS SDK” below.

There is an error in the command “Precautions for Pulling and Pushing”. The correct command should be:

CD SRC/electron git remote remove origin git remote add origin https://github.com/electron/electron # from here is not the same as the git checkout main git fetch && git pull --rebase origin main git branch --set-upstream-to=origin/mainCopy the code

The gclient sync -f command is used to sync the gclient sync -f command. The gclient sync -f command is used to sync the gclient -f command. The gclient sync -f command is used to sync the gclient -f command.

Here comes the main point, which is the core of this article

After completing the above command when you run, you also need to modify: build/config/compiler compiler. The gni file.

Put the inside of this

forbid_non_component_debug_builds = build_with_chromium
Copy the code

To:

forbid_non_component_debug_builds = false
Copy the code

If this step is missing, you will get an error when using gn gen:

ERROR at //build/config/compiler/compiler.gni:302:3: Assertion failed.assert(symbol_level ! =2|| current_toolchain ! = default_toolchain || ^----- Can't do non-component debug builds at symbol_level=2
See //BUILD.gn:12:1: whence it was imported.
import("//build/config/compiler/compiler.gni")
Copy the code

When building gn Gen, use the following command to replace the command on the official website:

gn gen out/Testing --args="import("//electron/build/args/testing.gn") is_debug=true symbol_level=2 $GN_EXTRA_ARGS"
Copy the code

If you want to use ccache, use:

gn gen out/Testing --args="import("//electron/build/args/testing.gn") cc_wrapper="ccache" is_debug=true symbol_level=2 $GN_EXTRA_ARGS"
Copy the code

Then it’s time to build:

ninja -C out/Testing electron
Copy the code

Debugging (CLion)

If you want to debug with CLion, make sure you have successfully executed the ninja -c out/Testing electron command “before opening CLion, create a cmakelists. TXT file in the root directory (SRC equivalent) as follows:

cmake_minimum_required(VERSION 3.20)
project(electron)

set(CMAKE_CXX_STANDARD 14)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/electron)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/out/Testing/gen)

add_executable(electron_exec ${CMAKE_CURRENT_SOURCE_DIR}/src/electron/shell/app/electron_main.cc)
Copy the code

The purpose of this file is to ensure that CLion can correctly recognize the code and perform functions such as code completion prompts, otherwise the IDE’s code prompts will be completely disabled.

Then use CLion to open the project. Note the root directory of the project, as shown below:

Once opened, you may need to wait dozens of minutes for CLion to set up/flush the cache.

Open: Setting -> Build, Execution, Deployment -> Custom Build Targets -> + -> Set Build.

The final figure looks like this:

Then set: Run/Debug Configurations, as shown in the figure:

If you want to open your own app using self-compiled Electron, simply add your app’s address to Program Arguments

Where: CHROMIUM_LLDBINIT_SOURCED=1 also needs to be added, otherwise cannot debug: Chormium source

Once setup is complete, there is one more area that needs to be set, or you can’t debug:

Create a ~/.lldbinit file and write:

script sys.path[:0] = ['/Users/black-hole/Code/Github/electron/src/tools/lldb']
script import lldbinit
Copy the code

Remember to replace the path above with your own.

This step is also mentioned in the website, but if according to the official website said: the command script import ~ / electron/SRC/tools/LLDB/lldbinit py to set, will not be able to use, for unknown reasons.

New ~/.lldbinit writing method, I refer to Chromium writing method

Breakpoint debugging can then be used normally, as shown in the following figure:

The problem

Set the macOS SDK

Currently, as mentioned in the Electron official documentation, it is best to use macOSX11.0.sdk.

You can download the MacOSX11.0.sdk file in Macosx-SDks to a directory: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/

Can.

Download dugite failure solution

Sometimes an error occurs when you use the: gclient sync -f command to synchronize:

error /Users/black-hole/Code/Github/electron/src/electron/node_modules/dugite: Command failed.
Exit code: 1
Command: node ./script/download-git.js
Arguments:
Directory: /Users/black-hole/Code/Github/electron/src/electron/node_modules/dugite
Output:
Downloading Git from: https://github.com/desktop/dugite-native/releases/download/v2.29.3-2/dugite-native-v2.29.3-3d467be-macOS-x64.tar.gz
Error raised while downloading https://github.com/desktop/dugite-native/releases/download/v2.29.3-2/dugite-native-v2.29.3-3d467be-macOS-x64.tar.gz GotError [RequestError]: Client network socket disconnected before secure TLS connection was established
Copy the code

The reason for this is that dugite does not recognize the agent using your computer when it downloads the binary file. You can use a browser to download it and start an HTTP service using Python -m SimpleHTTPServer, as follows:

Then edit the SRC/electron/node_modules/dugite/script/embedded – git. Json file, changed to:

The location of the modification is not the same for different systems, according to your system model to modify the corresponding good.

Then execute:

cd src/electron/node_modules/dugite
node ./script/download-git.js
Copy the code

After all the execution is complete, run gclient sync -f again to see that the execution is successful.