CEF编译打包(支持MP4播放,x64版本)
标签(空格分隔): CEF C++ Windows vs2022
文章目录
引用博客地址:https://www.cnblogs.com/zgliubo/p/16423247.html
背景
之前项目cef是32位,渲染网页3d模型内存被限制,内存在1G多一直上不去,使用官方的64位cef编译QCefView后,可以播放3D,内存也突破了1G多到了3~4G,但是mp4播放不了,原因是cef官方默认编译好的cef二进制不支持mp4播放,需要自己下载源码编译支持.
一、准备工作
因为需要下载cef和depot_tools等源码,源码网站又被墙,所以需要科学上网
这里推荐使用cloudflare和ZeroTrust的组合,这样不用设置代理,方便很多,也不用担心把网络环境搞乱了
二、编译环境准备
- 较新的版本CEF建议使用vs2022编译,安装时注意安装windows kits,我这里win10就选择windows10版本
- cef的编译脚本还需要ninja
- 过程中需要执行python脚本,我安装的python3, 有些地方如果提示"python.bat"找不到,可以改成"python3.bat"试试
- git,这个应该都有吧,版本尽量高一些
- cmake,这个也需要,版本也尽量高一些
三、具体编译流程
大概流程
步骤:
1、init.bat
2、down_cef.bat
3、down_chromium.bat
4、down_depot_tools.bat
5、mygclient.bat
6、mygclient-s2.bat
7、mygclient-s3.bat
8、generate.bat
创建工作目录
md C:\Code\cef\
md C:\Code\chromium\src\
md C:\Code\depot_tools\
init.bat
执行以下init.bat脚本初始化:
init.bat
:: 禁止depot_tools自动更新
setx DEPOT_TOOLS_UPDATE "0"
:: 使用本地安装的 Visual Studio 版本
setx DEPOT_TOOLS_WIN_TOOLCHAIN "0"
:: 如果前面已经设置了这些环境变量则可以跳过这3条命令
:: GN 构建工具
set CEF_USE_GN=1
:: 使用 VS2022 创建项目
set GN_ARGUMENTS=--ide=vs2022 --sln=cef --filters=//cef/*
:: 使用官方构建并添加ffmpeg音视频解码
set GN_DEFINES=is_official_build=true proprietary_codecs=true ffmpeg_branding=Chrome chrome_pgo_phase=0
set GYP_DEFINES=buildtype=Official
set GYP_GENERATORS=ninja,msvs-ninja
set GYP_MSVS_VERSION=2022
set CEF_ARCHIVE_FORMAT=tar.bz2
set VS_CRT_ROOT=E:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\crt
set WIN_CUSTOM_TOOLCHAIN=1
:: 设置为none可能最后打包会有问题,先设置为none,生成工程后,打包前再设置也可以
set CEF_VCVARS=none
set GYP_MSVS_OVERRIDE_PATH=E:\Program Files\Microsoft Visual Studio\2022\Community
set MSVC_VERSION=14.39.33519
set SDK_ROOT=E:\Windows Kits\10
set WINDOWSSDKDIR=E:\Windows Kits\10
set SDK_VERSION=10.0.19041.0
:: 将depot_tools路径("D:\cef\depot_tools\")添加到用户环境变量Path
for /f "tokens=3,*" %i in ('reg query HKCU\Environment /v Path') do setx Path "d:\cef\depot_tools\;%i%j"
:: 谨记:打包需要编译cef_sandbox.lib --client参数不需要,但是没有camke文件 标准版还需要编译Debug 这里我编译的--minimal版本
down_cef.bat
down_cef.bat(下载CEF)
cd D:\cef
:: clone源码并切换至5672分支
git clone https://bitbucket.org/chromiumembedded/cef.git -b 5672
down_chromium.bat
下载chromium:
cd D:\cef\chromium
:: clone指定tag(113.0.5672.129)的源码, (cef 5672分支 对应chromiun tag: 113.0.5672.129)
:: "--depth 1": 只下载当前版本不需要历史提交记录, 否则耗时严重且浪费储存空间或者意外中断
git clone https://chromium.googlesource.com/chromium/src.git -b 113.0.5672.129 --depth 1
down_depot_tools.bat
下载depot_tools:
cd D:\cef
:: clone depot_tools工具
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
cd D:\cef\depot_tools
:: 临时允许更新
set DEPOT_TOOLS_UPDATE=1
:: 执行update_depot_tools.bat下载内部工具
update_depot_tools.bat
:: 禁止更新
set DEPOT_TOOLS_UPDATE=0
:: checkout, (cef 5672分支 对应depot_tools commit id: 2075f733e8)
checkout 2075f733e8
cef,chromium和depot_tools对应关系
cef与chromium和depot_tools对应版本关系在 "cef\CHROMIUM_BUILD_COMPATIBILITY.txt"
myclient.bat
cd D:\cef\chromium
:: 通过命令创建.gclient文件
:: @echo "gclient config"
:: gclient config https://chromium.googlesource.com/chromium/src.git --unmanaged
@echo "gclient sync"
gclient sync --nohooks --no-history
: 如果前面已经设置了此环境变量, 可以不用执行
set DEPOT_TOOLS_WIN_TOOLCHAIN=0
@echo "gclient runhooks"
gclient runhooks
myclient-s2.bat
cd D:\cef\chromium
@echo "gclient sync"
gclient sync --nohooks --no-history
myclient-s3.bat
cd D:\cef\chromium
@echo "gclient sync"
gclient sync --nohooks --no-history
:: 注意:最好不要使用这种方式拷贝文件夹,可能会忽略掉.git文件夹,后面会导致代码分支版本等信息找不到,诸如"check git failed"之类的问题
:: xcopy "D:\cef\cef" "D:\cef\chromium\src\cef\" /e
generate.bat
cd D:\cef\chromium\src\cef
:: 如果前面已经设置了这些环境变量则可以跳过这3条命令
set CEF_USE_GN=1
set GN_ARGUMENTS=--ide=vs2022 --sln=cef --filters=//cef/*
set GN_DEFINES=is_official_build=true proprietary_codecs=true ffmpeg_branding=Chrome chrome_pgo_phase=0
set GYP_DEFINES=buildtype=Official
set GYP_GENERATORS=ninja,msvs-ninja
set GYP_MSVS_VERSION=2022
set CEF_ARCHIVE_FORMAT=tar.bz2
set VS_CRT_ROOT=E:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\crt
set WIN_CUSTOM_TOOLCHAIN=1
:: 设置为none可能最后打包会有问题
::set CEF_VCVARS=none
set GYP_MSVS_OVERRIDE_PATH=E:\Program Files\Microsoft Visual Studio\2022\Community
set MSVC_VERSION=14.39.33519
set SDK_ROOT=E:\Windows Kits\10
set WINDOWSSDKDIR=E:\Windows Kits\10
set SDK_VERSION=10.0.19041.0
@REM 编译64位调用VS的脚本有问题,环境变量还是去找的32位路径,下面改手动设置路径
@REM call "E:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
set INCLUDE=%SDK_ROOT%\Include\%SDK_VERSION%\um;%SDK_ROOT%\Include\%SDK_VERSION%\ucrt;%SDK_ROOT%\Include\%SDK_VERSION%\shared;%GYP_MSVS_OVERRIDE_PATH%\VC\Tools\MSVC\%MSVC_VERSION%\include;%GYP_MSVS_OVERRIDE_PATH%\VC\Tools\MSVC\%MSVC_VERSION%\atlmfc\include;%INCLUDE%
set LIB=%SDK_ROOT%\Lib\%SDK_VERSION%\um\x64;%SDK_ROOT%\10\Lib\%SDK_VERSION%\ucrt\x64;%GYP_MSVS_OVERRIDE_PATH%\VC\Tools\MSVC\%MSVC_VERSION%\lib\x64;%GYP_MSVS_OVERRIDE_PATH%\VC\Tools\MSVC\%MSVC_VERSION%\atlmfc\lib\x64;%LIB%
set PATH=%SDK_ROOT%\bin\%SDK_VERSION%\x64;%GYP_MSVS_OVERRIDE_PATH%\VC\Tools\MSVC\%MSVC_VERSION%\bin\HostX64\x64;%GYP_MSVS_OVERRIDE_PATH%\VC\Tools\MSVC\%MSVC_VERSION%\bin\HostX64\x64;%GYP_MSVS_OVERRIDE_PATH%\VC\Redist\MSVC\14.38.33135\x64\Microsoft.VC143.CRT;%PATH%
:: 构建项目
call cef_create_projects.bat
编译CEF
cd chromium_git/chromium/src
ninja -C out/Release_GN_x64 cef
-
CEF编译报错:
ValueError: path is on mount '\\tab_group_types.mojom-webui.js', start on mount 'F:
-
解决方案:
修改F:\code\chromium_git\chromium\src\mojo\public\tools\bindings\generators\mojom_ts_generator.py
def _GetWebUiModulePath(module):
path. Otherwise, returned paths always end in a '/' and begin with either
chrome://resources/
or a '/'."""
path = module.metadata.get('webui_module_path')- if path == '':
- path = '/'
if path is None or path == '/':
return path
if _IsAbsoluteChromeResourcesPath(path):
编译cef_sandbox.lib
原以为这个可以不编译,但是这个不编译打包会出问题:
ninja -C out\Release_GN_x86_sandbox cef_sandbox
四、打包CEF
pack.bat
cd D:\cef\chromium\src\cef\tools
set CEF_VCVARS=none
set CEF_ARCHIVE_FORMAT=tar.bz2
:: --minimal
:: 这里编译minimal版本,因为标准版本需要Debug,我这里没有编译
:: --no-docs不需要文档,安装了Doxygen也报错找不到,直接就去掉吧
:: --x64-build 打包X64版本
:: --client 有些文章使用的这个参数,这个打包参数只会打包Release文件夹,很多库和目录都没有,其他库联合cef编译有可能会报错
make_distrib.bat --ninja-build --no-docs --x64-build --no-symbols --minimal
打包可能的报错:
msvs_en.bat执行失败:
-
VC路径没有设置好,修改如下:
@echo off
:: Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
:: reserved. Use of this source code is governed by a BSD-style license
:: that can be found in the LICENSE file.:: Set up the environment for use with MSVS tools and then execute whatever
:: was specified on the command-line.set RC=
:: Support !! syntax for delayed variable expansion.
setlocal enabledelayedexpansion:: Require that platform is passed as the first argument.
if "%1" == "win32" (
set vcvarsbat=vcvars32.bat
) else if "%1" == "win64" (
set vcvarsbat=vcvars64.bat
) else if "%1" == "winarm64" (
set vcvarsbat=vcvarsamd64_arm64.bat
) else (
echo ERROR: Please specify a target platform: win32, win64 or winarm64
set ERRORLEVEL=1
goto end
):: Check if vcvars is already provided via the environment.
:: 在这里添加CEF_VCVARS变量,设置vs路径
set CEF_VCVARS=E:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat
set vcvars="%CEF_VCVARS%"
if %vcvars% == "none" goto found_vcvars
if exist %vcvars% goto found_vcvars:: Search for the default VS installation path.
for %%x in (2022) do (
for %%y in ("%PROGRAMFILES%" "%PROGRAMFILES(X86)%") do (
for %%z in (Professional Enterprise Community BuildTools) do (
::set vcvars="%%~y\Microsoft Visual Studio%%x%%z\VC\Auxiliary\Build%vcvarsbat%"
set vcvars="%CEF_VCVARS%"
if exist !vcvars! goto found_vcvars
)
)
)echo ERROR: Failed to find vcvars
set ERRORLEVEL=1
goto end:found_vcvars
echo vcvars:
echo %vcvars%if not %vcvars% == "none" (
:: Set this variable to keep VS2017 < 15.5 from changing the current working directory.
set "VSCMD_START_DIR=%CD%"
call %vcvars%
)echo PATH:
echo %PATH%:: Remove the first argument and execute the command.
for /f "tokens=1,* delims= " %%a in ("%*") do set ALL_BUT_FIRST=%%b
echo command:
echo %ALL_BUT_FIRST%
%ALL_BUT_FIRST%:end
endlocal & set RC=%ERRORLEVEL%
goto omega:returncode
exit /B %RC%:omega
call :returncode %RC%
如果还报错msvs_env.bat执行失败,可能是cef_sandbox.lib没有编译,参考前面的步骤编译后再打包即可,在目录chromium_git\chromium\src\cef\binary_distrib下就可以找到.tar.bz2的包了