Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 451|回复: 1
打印 上一主题 下一主题

如何编译 CEF

[复制链接]

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
跳转到指定楼层
楼主
发表于 2023-11-24 15:34:19 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 Qter 于 2023-11-24 15:52 编辑

建议大家对照着 Chromium 的官方的构建文档、CEF 的BranchesAndBuilding 以及 CEF 的 MasterBuildQuickStart学习这节课,这节课的内容有一定的时效性,如果有与官方文档冲突的地方,以官方文档为准。
在编译 CEF 前,我们需要先搭建一个 Chromium 的编译环境,大家最好先完整地编译一遍 Chromium ,编译 Chromium 的电脑建议内存在 32GB 以上,磁盘空间至少预留 300GB。
配置 SOCKS5 服务器
Gitee 为我们提供了 Chromium 的源码镜像,但是 Chromium 的源码还包括许多其他依赖,这些依赖的下载我们依然需要依靠 SOCKS5 来加速。
在有 SOCKS5 服务器后,我建议大家使用 Proxifier 这个软件配置全局代理,这个软件本身是收费的,但是可以试用一个月。前面提到,Gitee 为我们提供了镜像,因此我们需要在 Proxifier 里将 Gitee 加入白名单,在 Proxifier 的 Profile 菜单里选择 Proxification Rules,如下图所示:
点击 Add,在 Target hosts 里填入 *.gitee.com,Action 选择 Direct ,然后点击 OK:
Gitee 亲测在国内可以达到超过 20MB/s 的下载速度,这可以在一定程度上缩短从零编译所需要的时间。
编译 Chromium
首先需要确保电脑上已经安装了 Visual Studio 2019 以上版本,然后从 Windows SDK and emulator archive 里单独下载 10.0.22621.755 版本的 Windows SDK,安装的时候需要勾选 Debugging Tools for Windows,如下图所示:
随后按照 Chromium 的文档下载解压 depot_tools 并将其加入环境变量中,然后打开命令提示符,确保能成功执行 gclient 命令,如下图所示:
在拉取 Chromium 代码前,我们需要先修改代码拉取的 URL,找到 depot_tools 所在目录下的 fetch_configs, 这个文件夹下存放了各个子模板的地址,比如 chromium.py 的内容如下:
将其修改为 Gitee 的镜像地址 gitee.com/mirrors/chr…
新建一个空文件夹 chromium,在该文件夹下执行命令 fetch chromium,可以看到在拉取代码的时候,gclient 会使用我们给定的镜像地址:
没有走 Gitee 的子仓库,gclient 会从谷歌的服务器下载,从 Proxifier 的监控页面里可以看到:
如果遇到拉取到一半失败的情况,可以使用 gclient sync 继续拉取没有成功的模块,而不用删掉文件夹后重新开始:
拉取的时间比较长,视情况可能需要数个小时。在编译 Chromium 前,我们需要参照 Electron 选择 Node.js 和Chromium 的版本,然后选择对应 CEF 的版本,这里我们参考的 Electron 版本是 v18.3.15,它使用的 Chromium 版本为 100.0.4896.160,对应 CEF 版本为 4896。Chromium 的代码拉取完毕后,使用 git checkout 切换到给定版本:
然后再跑一次 gclient sync:
确认 gclient sync 已执行完毕且中间没有出现错误:
按照 Chromium 的官方的构建文档 使用命令 gn gen out/Default 生成工程:
使用 autoninja -C out\Default chrome 即可开始编译 Chromium,好一点的机器一般耗时在一个小时左右,如果机器配置不高,则可能需要数个小时甚至数十个小时:
编译 CEF
在 chromium/src 目录下,使用命令 git clone bitbucket.org/chromiumemb… 手动克隆 CEF 的源码并切换到 origin/4896 这个分支:
在 chromium/src 目录下,使用 PowerShell 执行以下命令:


ini复制代码


$env:GN_ARGUMENTS='--ide=vs2019 --sln=cef'$env:GN_DEFINES='is_official_build=true symbol_level=1 is_component_build=false v8_enable_javascript_promise_hooks=true v8_scriptormodule_legacy_lifetime=true'python cef/tools/gclient_hook.py
确保命令已执行完毕且没有出现任何错误:
src 目录下执行 ninja -C out/Release_GN_x64 cef:
编译可能需要数小时,编译完成后在 out/Release_GN_x64 下可以找到编译好的可执行文件:
cefclient.exe 是 CEF 自带的一个示例,双击可以打开它:
小结
在这节课里,我们演示了如何从零开始编译 CEF 的代码。CEF 官方提供的编译教程是基于一个 automate-git.py 的脚本,这个脚本更适合用于 CI/CD 从零开始构建,但对于学习 CEF 的初学者来说可能不太友好。
这节课我们给大家演示了如何不借助这个脚本去编译 CEF,可以看到编译 CEF 主要有三步,第一步是按照 Chromium 的官方文档克隆代码并 checkout 到指定分支,第二步是下载 CEF 的代码并使用 CEF 提供的命令 gclient_hook 给 Chromium 的源码打 patch,最后是使用命令 ninja 编译 CEF。
如果要修改 CEF 的源代码,则必须对 CEF 的编译过程有一定了解,要不会在出现问题时会一头雾水,无从下手,找不到解决问题的思路。









回复

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
沙发
 楼主| 发表于 2023-12-23 22:07:06 | 只看该作者
https://bitbucket.org/chromiumem ... sterBuildQuickStart

WikiClone wiki
cef / MasterBuildQuickStartViewHistory

This Wiki page provides a quick-start guide for creating a Debug build of CEF/Chromium using the current master (development) branch.
Note to Editors: Changes made to this Wiki page without prior approval via the CEF Forum or Issue Tracker may be lost or reverted.



Overview
This page provides a quick-start guide for setting up a minimal development environment and building the master branch of Chromium/CEF for development purposes. For a comprehensive discussion of the available tools and configurations visit the BranchesAndBuilding Wiki page.
This guide is NOT intended for:
  • Those seeking a prebuilt binary distribution for use in third-party apps. Go here instead.
  • Those seeking to build the binary distribution in a completely automated manner. Go here instead.
Development systems can be configured using dedicated hardware or a VMware, Parallels or VirtualBox virtual machine.
The below steps can often be used to develop the most recent release branch of CEF/Chromium in addition to the master branch. Chromium build requirements change over time so review the build requirements listed on the BranchesAndBuilding Wiki page before attempting to build a release branch. Then just add --branch=XXXX to the automate-git.py command-line where "XXXX" is the branch number you wish to build.
File Structure
The same file structure will be used on all platforms. "~" can be any path that does not include spaces or special characters. We'll be building this directory structure for each platform in the following sections.
~/code/  automate/    automate-git.py   <-- CEF build script  chromium_git/    cef/              <-- CEF source checkout    chromium/      src/            <-- Chromium source checkout    update.[bat|sh]   <-- Bootstrap script for automate-git.py  depot_tools/        <-- Chromium build tools
With this file structure you can develop multiple CEF/Chromium branches side-by-side. For example, repeat the below instructions using "chromium_git1" as the directory name instead of "chromium_git".
Windows Setup
What's Required
  • Windows Build Requirements as listed on the BranchesAndBuilding Wiki page.
  • Install required Visual Studio sub-components as described here.
  • Install the exact Windows SDK version specified in the default location to avoid build issues.
  • At least 16GB of RAM (32GB+ recommended) and 150GB of free disk space (for a Debug build).
  • Approximately 4 hours with a fast internet connection (100Mbps) and fast build machine (2.4Ghz, 16 logical cores, SSD).
Step-by-step Guide
All of the below commands should be run using the system "cmd.exe" and not a Cygwin shell.
1. Create the following directories.
c:\code\automatec:\code\chromium_git
WARNING: If you change the above directory names/locations make sure to (a) use only ASCII characters and (b) choose a short file path (less than 35 characters total). Otherwise, some tooling may fail later in the build process due to invalid or overly long file paths.
2. Download depot_tools.zip and extract to "c:\code\depot_tools". Do not use drag-n-drop or copy-n-paste extract from Explorer, this will not extract the hidden ".git" folder which is necessary for depot_tools to auto-update itself. You can use "Extract all..." from the context menu though. 7-zip is also a good tool for this.
3. Run "update_depot_tools.bat" to install Python and Git.
cd c:\code\depot_toolsupdate_depot_tools.bat
4. Add the "c:\code\depot_tools" folder to your system PATH. For example, on Windows 10:
  • Run the "SystemPropertiesAdvanced" command.
  • Click the "Environment Variables..." button.
  • Double-click on "ath" under "System variables" to edit the value.
5. Download the automate-git.py script to "c:\code\automate\automate-git.py".
6. Create the "c:\code\chromium_git\update.bat" script with the following contents.
set GN_DEFINES=is_component_build=trueset GN_ARGUMENTS=--ide=vs2022 --sln=cef --filters=//cef/*python3 ..\automate\automate-git.py --download-dir=c:\code\chromium_git --depot-tools-dir=c:\code\depot_tools --no-distrib --no-build
Run the "update.bat" script and wait for CEF and Chromium source code to download. CEF source code will be downloaded to "c:\code\chromium_git\cef" and Chromium source code will be downloaded to "c:\code\chromium_git\chromium\src". After download completion the CEF source code will be copied to "c:\code\chromium_git\chromium\src\cef".
cd c:\code\chromium_gitupdate.bat
7. Create the "c:\code\chromium_git\chromium\src\cef\create.bat" script with the following contents.
set GN_DEFINES=is_component_build=trueset GN_ARGUMENTS=--ide=vs2022 --sln=cef --filters=//cef/*call cef_create_projects.bat
Run the "create.bat" script to generate Ninja and Visual Studio project files.
cd c:\code\chromium_git\chromium\src\cefcreate.bat
This will generate a "c:\code\chromium_git\chromium\src\out\Debug_GN_x86\cef.sln" file that can be loaded in Visual Studio for debugging and compiling individual files. Replace “x86” with “x64” in this path to work with the 64-bit build instead of the 32-bit build. Always use Ninja to build the complete project. Repeat this step if you change the project configuration or add/remove files in the GN configuration (BUILD.gn file).
8. Create a Debug build of CEF/Chromium using Ninja. Edit the CEF source code at "c:\code\chromium_git\chromium\src\cef" and repeat this step multiple times to perform incremental builds while developing.
cd c:\code\chromium_git\chromium\srcautoninja -C out\Debug_GN_x86 cef
Replace "Debug" with "Release" to generate a Release build instead of a Debug build. Replace “x86” with “x64” to generate a 64-bit build instead of a 32-bit build.
9. Run the resulting cefclient sample application.
cd c:\code\chromium_git\chromium\srcout\Debug_GN_x86\cefclient.exe
See the Windows debugging guide for detailed debugging instructions.
Mac OS X Setup
What's Required
  • macOS Build Requirements as listed on the BranchesAndBuilding Wiki page.
  • Building on an Intel Mac is supported with all versions. Building on an Apple Silicon (ARM64) Mac is supported starting with M93 (4577 branch).
  • At least 16GB of RAM (32GB+ recommended) and 150GB of free disk space (for a Debug build).
  • Approximately 4 hours with a fast internet connection (100Mbps) and fast build machine (2.4Ghz, 16 logical cores, SSD).
Step-by-step Guide
In this example "~" is "/Users/marshall". Note that in some cases the absolute path must be used. Environment variables described in this section can be added to your "~/.bash_profile" file to persist them across sessions.
1. Create the following directories.
mkdir ~/codemkdir ~/code/automatemkdir ~/code/chromium_git
2. Download "~/code/depot_tools" using Git.
cd ~/codegit clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
3. Add the "~/code/depot_tools" directory to your PATH. Note the use of an absolute path here.
export PATH=/Users/marshall/code/depot_toolsPATH
4. Download the automate-git.py script to "~/code/automate/automate-git.py".
5. Create the "~/code/chromium_git/update.sh" script with the following contents.
#!/bin/bashpython3 ../automate/automate-git.py --download-dir=/Users/marshall/code/chromium_git --depot-tools-dir=/Users/marshall/code/depot_tools --no-distrib --no-build --x64-build
Replace --x64-build with --arm64-build if using an Apple Silicon Mac instead of an Intel Mac.
Give it executable permissions.
cd ~/code/chromium_gitchmod 755 update.sh
Run the "update.sh" script and wait for CEF and Chromium source code to download. CEF source code will be downloaded to "~/code/chromium_git/cef" and Chromium source code will be downloaded to "~/code/chromium_git/chromium/src". After download completion the CEF source code will be copied to "~/code/chromium_git/chromium/src/cef".
cd ~/code/chromium_git./update.sh
6. Run the "~/code/chromium_git/chromium/src/cef/cef_create_projects.sh" script to create Ninja project files. Repeat this step if you change the project configuration or add/remove files in the GN configuration (BUILD.gn file).
cd ~/code/chromium_git/chromium/src/cef./cef_create_projects.sh
Add export GN_DEFINES=is_component_build=true before running cef_create_projects.sh if using an Apple Silicon Mac instead of an Intel Mac.
7. Create a Debug build of CEF/Chromium using Ninja. Edit the CEF source code at "~/code/chromium_git/chromium/src/cef" and repeat this step multiple times to perform incremental builds while developing.
cd ~/code/chromium_git/chromium/srcautoninja -C out/Debug_GN_x64 cef
Replace "x64" with "arm64" if using an Apple Silicon Mac instead of an Intel Mac. Replace "Debug" with "Release" to generate a Release build instead of a Debug build.
8. Run the resulting cefclient, cefsimple and/or ceftests sample applications.
cd ~/code/chromium_git/chromium/srcopen out/Debug_GN_x64/cefclient.app# Or run directly in the console to see log output:./out/Debug_GN_x64/cefclient.app/Contents/MacOS/cefclient
See the Mac OS X debugging guide for detailed debugging instructions.
Linux Setup
What's Required
  • Linux Build Requirements as listed on the BranchesAndBuilding Wiki page.
  • Building with other versions or distros has not been tested and may experience issues.
  • At least 16GB of RAM (32GB+ recommended) and 120GB of free disk space (for a Debug build).
  • Approximately 4 hours with a fast internet connection (100Mbps) and fast build machine (2.4Ghz, 16 logical cores, SSD).
Step-by-step Guide
In this example "~" is "/home/marshall". Note that in some cases the absolute path must be used. Environment variables described in this section can be added to your "~/.profile" or "~/.bashrc" file to persist them across sessions.
1. Create the following directories.
mkdir ~/codemkdir ~/code/automatemkdir ~/code/chromium_git
2. Download and run "~/code/install-build-deps.py" to install build dependencies. Answer Y (yes) to all of the questions.
cd ~/codesudo apt-get install curl file lsb-release procps python3 python3-pipcurl 'https://chromium.googlesource.com/chromium/src/+/main/build/install-build-deps.py?format=TEXT' | base64 -d > install-build-deps.pysudo python3 ./install-build-deps.py --no-arm --no-chromeos-fonts --no-naclpython3 -m pip install dataclasses importlib_metadata
3. Download "~/code/depot_tools" using Git.
cd ~/codegit clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
4. Add the "~/code/depot_tools" directory to your PATH. Note the use of an absolute path here.
export PATH=/home/marshall/code/depot_toolsPATH
5. Download the "~/automate/automate-git.py" script.
cd ~/code/automatewget https://bitbucket.org/chromiumem ... ate/automate-git.py
6. Create the "~/code/chromium_git/update.sh" script with the following contents.
#!/bin/bashpython3 ../automate/automate-git.py --download-dir=/home/marshall/code/chromium_git --depot-tools-dir=/home/marshall/code/depot_tools --no-distrib --no-build
Give it executable permissions.
cd ~/code/chromium_gitchmod 755 update.sh
Run the "update.sh" script and wait for CEF and Chromium source code to download. CEF source code will be downloaded to "~/code/chromium_git/cef" and Chromium source code will be downloaded to "~/code/chromium_git/chromium/src". After download completion the CEF source code will be copied to "~/code/chromium_git/chromium/src/cef".
cd ~/code/chromium_git./update.sh
7. Configure GN_DEFINES for your desired build environment.
Chromium provides sysroot images for consistent builds across Linux distros. The necessary files will have downloaded automatically as part of step 6 above. Usage of Chromium's sysroot is recommended if you don't want to deal with potential build breakages due to incompatibilities with the package or kernel versions that you've installed locally. To use the sysroot image configure the following GN_DEFINES:
export GN_DEFINES="use_sysroot=true use_allocator=none symbol_level=1 is_cfi=false use_thin_lto=false"
It is also possible to build using locally installed packages instead of the provided sysroot. Choosing this option may require additional debugging effort on your part to work through any build errors that result. On Ubuntu 18.04 the following GN_DEFINES have been tested to work reliably:
export GN_DEFINES="use_sysroot=false use_allocator=none symbol_level=1 is_cfi=false use_thin_lto=false use_vaapi=false"
Note that the "cefclient" target cannot be built directly when using the sysroot image. You can work around this limitation by creating a binary distribution after completing step 9 below, and then building the cefclient target using that binary distribution.
You can also create an AddressSanitizer build for enhanced debugging capabilities. Just add is_asan=true dcheck_always_on=true to the GN_DEFINES listed above and build the out/Release_GN_x64 directory in step 9 below. Run with the asan_symbolize.py script as described in the AddressSanitizer link to get symbolized output.
The various other listed GN arguments are based on recommendations from the AutomateBuildSetup Wiki page. You can search for them by name in the Chromium source code to find more details.
8. Run the "~/code/chromium_git/chromium/src/cef/cef_create_projects.sh" script to create Ninja project files. Repeat this step if you change the project configuration or add/remove files in the GN configuration (BUILD.gn file).
cd ~/code/chromium_git/chromium/src/cef./cef_create_projects.sh
9. Create a Debug build of CEF/Chromium using Ninja. Edit the CEF source code at "~/code/chromium_git/chromium/src/cef" and repeat this step multiple times to perform incremental builds while developing. Note the additional "chrome_sandbox" target may be required by step 10. The "cefclient" target will only build successfully if you set use_sysroot=false in step 7, so remove that target if necessary.
cd ~/code/chromium_git/chromium/srcautoninja -C out/Debug_GN_x64 cefclient cefsimple ceftests chrome_sandbox
Replace "Debug" with "Release" to generate a Release build instead of a Debug build.
10. Set up the Linux SUID sandbox if you are using an older kernel (< 3.8). See here for more background on Linux sandboxing technology.
# This environment variable should be set at all times.export CHROME_DEVEL_SANDBOX=/usr/local/sbin/chrome-devel-sandbox# This command only needs to be run a single time.cd ~/code/chromium_git/chromium/srcsudo BUILDTYPE=Debug_GN_x64 ./build/update-linux-sandbox.sh
11. Run the cefclient, cefsimple and/or ceftests sample applications. Note that the cefclient application will only have built successfully if you set use_sysroot=false in step 7.
cd ~/code/chromium_git/chromium/src./out/Debug_GN_x64/cefclient
See the Linux debugging guide for detailed debugging instructions.
Next Steps
  • If you're seeking a good code editor on Linux check out the Eclipse and Emacs tutorials.
  • Review the Tutorial and GeneralUsage Wiki pages for details on CEF implementation and usage.
  • Review the Chromium debugging guide for Windows, Mac OS X or Linux.
  • When you’re ready to contribute your changes back to the CEF project see the ContributingWithGit Wiki page for instructions on creating a pull request.
Updated 2023-11-15




回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-4-30 23:01 , Processed in 0.066832 second(s), 21 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表