Chromium 构建
为了定位 Chrome 浏览器 WebSocket 发送数据时拆分成多帧的问题,构建了一个调试版本的 Chromium 浏览器。

构建环境为 ArchLinux,Chromium 120.0.6099 版本。

gn

GN is a meta-build system that generates build files for Ninja.

https://gn.googlesource.com/gn/

gn args: (command-line tool)

  Display or configure arguments declared by the build.

GN build configurationGN build configuration
https://www.chromium.org/developers/gn-build-configuration/

chromium

❯ yay -G chromium
cd chromium

❯ vi PKGBUILD
ln -sf /usr/bin/node third_party/node/linux/node-linux-x64/bin/
ln -sf /usr/bin/java third_party/jdk/current/bin/

local _flags=(
  'is_official_build=false'
  'symbol_level=2'
  'is_component_build=false'
)
❯ makepkg -so

cd src/chromium-120.0.6099.71

❯ gn args out/Release --list

export EDITOR=vim
❯ gn args out/Release

❯ makepkg -de --nocheck

❯ ./tools/clang/scripts/generate_compdb.py -p out/Release -o compile_commands.json --target_os=linux

❯ file out/Release/chrome
out/Release/chrome: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 4.4.0, BuildID[xxHash]=cf5662d3867e49c5, with debug_info, not stripped

In a component build, many smaller shared libraries will be generated.

The Chrome Component Build
https://chromium.googlesource.com/chromium/src/+/main/docs/component_build.md

build error

❯ makepkg -de --nocheck
[37269/56273] SOLINK ./libimage_decoders.so
FAILED: libimage_decoders.so libimage_decoders.so.TOC 
"python3" "../../build/toolchain/gcc_solink_wrapper.py" --readelf="readelf" --nm="nm"  --sofile="./libimage_decoders.so" --tocfile="./libimage_decoders.so.TOC" --output="./libimage_decoders.so" -- clang++ -shared -Wl,-soname="libimage_decoders.so" -fuse-ld=lld -Wl,--fatal-warnings -Wl,--build-id -fPIC -Wl,-z,noexecstack -Wl,-z,relro -Wl,--color-diagnostics -Wl,--undefined-version -Wl,--no-call-graph-profile-sort -m64 -no-canonical-prefixes -Wl,--gdb-index -rdynamic -Wl,-z,defs -Wl,--as-needed -nostdlib++ -Wl,-rpath=\$ORIGIN -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o "./libimage_decoders.so" @"./libimage_decoders.so.rsp"  
ld.lld: error: undefined symbol: crc32
>>> referenced by png_image_reader.cc:370 (../../third_party/blink/renderer/platform/image-decoders/png/png_image_reader.cc:370)
>>>               obj/third_party/blink/renderer/platform/image-decoders/image_decoders/png_image_reader.o:(blink::CheckCrc(blink::FastSharedBufferReader const&, unsigned int, unsigned int))
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
[37290/56273] ACTION //v8:run_torque(//build/toolchain/linux/unbundle:default)
ninja: build stopped: subcommand failed.
==> ERROR: A failure occurred in build().
    Aborting...

cd src/chromium-120.0.6099.71/out/Release

"python3" "../../build/toolchain/gcc_solink_wrapper.py" --readelf="readelf" --nm="nm"  --sofile="./libimage_decoders.so" --tocfile="./libimage_decoders.so.TOC" --output="./libimage_decoders.so" -- clang++ -shared -Wl,-soname="libimage_decoders.so" -fuse-ld=lld -Wl,--fatal-warnings -Wl,--build-id -fPIC -Wl,-z,noexecstack -Wl,-z,relro -Wl,--color-diagnostics -Wl,--undefined-version -Wl,--no-call-graph-profile-sort -m64 -no-canonical-prefixes -Wl,--gdb-index -rdynamic -Wl,-z,defs -Wl,--as-needed -nostdlib++ -Wl,-rpath=\$ORIGIN -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o "./libimage_decoders.so" @"./libimage_decoders.so.rsp"
ld.lld: error: undefined symbol: crc32
>>> referenced by png_image_reader.cc:370 (../../third_party/blink/renderer/platform/image-decoders/png/png_image_reader.cc:370)
>>>               obj/third_party/blink/renderer/platform/image-decoders/image_decoders/png_image_reader.o:(blink::CheckCrc(blink::FastSharedBufferReader const&, unsigned int, unsigned int))
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

crc32 符号需要链接 zlib,可以在出错的命令行上手工添加 -lz 选项进行验证。

查看依赖:

❯ gn desc out/Release //third_party/blink/renderer/platform

❯ gn desc out/Release //third_party/blink/renderer/platform/image-decoders:image_decoders

libs
  dl
  pthread
  rt

增加 zlib 依赖如下:

❯ vi third_party/blink/renderer/platform/image-decoders/BUILD.gn
component("image_decoders") {
  libs = [ "z" ]
}

❯ gn desc out/Release //third_party/blink/renderer/platform/image-decoders:image_decoders

libs
  z
  dl
  pthread
  rt

重新执行 makepkg -de --nocheck 即可(会进行增量构建)。

How to import third party library in GN?
https://stackoverflow.com/questions/60823786/how-to-import-third-party-library-in-gn

How to include a shared library in chromium’s gn file?
https://stackoverflow.com/questions/49102921/how-to-include-a-shared-library-in-chromiums-gn-file

depot_tools

depot_tools 的新版本貌似存在很多问题,如果不使用 CLion 的构建功能的话,可以完全不需要用到:

❯ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
cd depot_tools
❯ ./cipd ensure -ensure-file cipd_manifest.txt -root .cipd_bin -log-level debug

❯ sudo vi /etc/profile.d/depot.sh
append_path "$HOME/working/cxx/depot_tools"
export PATH

depot_tools 这种隐式下载文件的方式真的很恶心,特别是国内这种网络环境。

CLion

d26dc2f3d4bdbe5d096bdb59cf72334a.png

67d64bd809c3d7e1c1cf1a3d66bc0fe5.png

如果 depot_tools 没法用,可以不使用 CLion 的构建功能(手工构建即可):

38e292ad7f40ff1d5e0d61a6722f4ef6.png

❯ vi ~/.gdbinit
source ~/working/arch/chromium/src/chromium-120.0.6099.71/tools/gdb/gdbinit

❯ gdb out/Release/chrome
(gdb) set args --incognito --disable-extensions --disable-plugins --single-process

List of Chromium Command Line Switches
https://peter.sh/experiments/chromium-command-line-switches/

logging

--v

// Gives the default maximal active V-logging level; 0 is the default.
// Normally positive values are used for V-logging levels.
const char kV[] = "v";

--vmodule

// Gives the per-module maximal V-logging levels to override the value
// given by --v.  E.g. "my_module=2,foo*=3" would change the logging
// level for all code in source files "my_module.*" and "foo*.*"
// ("-inl" suffixes are also disregarded for this matching).
//
// Any pattern containing a forward or backward slash will be tested
// against the whole pathname and not just the module.  E.g.,
// "*/foo/bar/*=2" would change the logging level for all code in
// source files under a "foo/bar" directory.
const char kVModule[] = "vmodule";
❯ ./chrome --v=3
❯ ./chrome --vmodule=websocket=3

❯ ./chrome --incognito --disable-extensions --disable-plugins --single-process --vmodule='websocket*=10'

❯ ./chrome --incognito --disable-extensions --disable-plugins --single-process --vmodule='*/websocket*/*=10,websocket*=10,web_socket*=10'

注意选项后的 = 必不可少。

main

// chrome/app/chrome_exe_main_aura.cc
main

// chrome/app/chrome_main.cc
ChromeMain

content::ContentMain

// content/app/content_main.cc

ContentMain
  RunContentProcess

// content/app/content_main_runner_impl.cc

ContentMainRunnerImpl::Run
  RunBrowser

appendix

CLion Dev
https://chromium.googlesource.com/chromium/src/+/master/docs/clion.md

Checking out and building Chromium on Linux
https://mudongliang.github.io/2017/08/10/install-chromium-on-debian-stretch.html

Building ungoogled-chromium
https://github.com/ungoogled-software/ungoogled-chromium/blob/master/docs/building.md


最后修改于 2023-12-12