envoy源代码分析(二)--用bazel编译
bazel的官方文档和手册:https://bazel.build/docs/user-manual?hl=zh-cn#bazel-run-options
最好看英文:https://bazel.build/versions/7.6.0/reference/command-line-reference#build-options
安装
在ubuntu下
sudo apt install apt-transport-https curl gnupg curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-archive-keyring.gpg sudo mv bazel-archive-keyring.gpg /usr/share/keyrings echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt update && sudo apt install bazel
ref:https://bazel.build/versions/6.0.0/install/ubuntu?hl=zh-cn
C++编译
新手指引:https://bazel.build/start/cpp
关键文件
MODULE.bazel 放在工程根目录
BUILD 一个或多个,放在合适的位置

命令
bazel build //main:hello-world
//main是指定BUILD文件所在的目录位置,冒号后边是BUILD文件里配置的target name。
编译结果:./bazel-bin/main/hello-world
来个复杂的BUILD

实践
在openeular2403编译envoy1.34.0,使用8核16G虚拟机。
使用操作系统自带的12.3.1
根据官方文档,每核需要2G内存。

第一次编译的时候,如果bazel版本信息不对,会提示下面的错误信息
cd "/usr/bin" && curl -fLO https://releases.bazel.build/7.6.0/release/bazel-7.6.0-linux-x86_64 && chmod +x bazel-7.6.0-linux-x86_64
照着执行,就有了对应版本的bazel。
或:envoy官方更推荐安装bazelish
sudo wget -O /usr/local/bin/bazel https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-$([ $(uname -m) = "aarch64" ] && echo "arm64" || echo "amd64") sudo chmod +x /usr/local/bin/bazel
sudo yum install git gcc gcc-c++ libstdc++-static
另外,还不能root,要建立一个非root账户进行编译。
编译命令
bazel build envoy --copt=-Wno-error=missing-requires --copt=-Wno-changes-meaning --copt=-Wno-dangling-pointer --copt=-Wno-error=dangling-pointer --verbose_failures --jobs=6
编译debug版本
bazel build envoy -c dbg --subcommands --strip=never --spawn_strategy=local --define=absoulte_paths=enabled --sandbox_debug

CLEAN
bazel clean // 只清理本工程。不清楚第三方依赖, bazel clean --expunge //全部清除,包括第三方依赖
LLVM
gcc 用gdb有点问题,见:gdb envoy找不到源代码的原因
另外,官方文档里也推荐优先clang。下面我们试一下
dnf install clang lld lldb
我的版本是17.0

编译命令
--config=clang
可以写着命令行里,也可以追加到.bazelrc中,如下
#time bazel build envoy -c dbg --subcommands --copt=-Wno-error=missing-requires --copt=-Wno-changes-meaning --copt=-Wno-dangling-pointer --copt=-Wno-error=dangling-pointer
time bazel build envoy -c dbg --subcommands --config=clang
浙公网安备 33010602011771号