本页面详细介绍了 Bazel 容器的内容、如何使用 Bazel 容器内的 Bazel 构建 abseil-cpp项目,以及如何使用 Bazel 容器(带目录装载)直接从宿主机构建此项目。
使用目录装载从宿主机构建 Abseil 项目
借助本部分中的说明,您可以使用 Bazel 容器(源代码在主机环境中检出)进行构建。对于您执行的每个 build 命令,系统都会启动一个容器。构建结果会缓存在主机环境中,以便在各构建之间重复使用。
将项目克隆到宿主机中的某个目录。
git clone --depth 1 --branch 20220623.1 https://github.com/abseil/abseil-cpp.git /src/workspace创建一个文件夹,其中包含要在各构建之间共享的缓存结果。
mkdir -p /tmp/build_output/使用 Bazel 容器构建项目,并在宿主机中的输出文件夹中提供构建输出。
docker run \
-e USER="$(id -u)" \
-u="$(id -u)" \
-v /src/workspace:/src/workspace \
-v /tmp/build_output:/tmp/build_output \
-w /src/workspace \
gcr.io/bazel-public/bazel:latest \
--output_user_root=/tmp/build_output \
build //absl/...通过添加 --config=asan|tsan|msan build
标志来构建带有 Sanitizer 的项目,以相应地选择 AddressSanitizer (asan)、ThreadSanitizer (tsan) 或
MemorySanitizer (msan)。
docker run \
-e USER="$(id -u)" \
-u="$(id -u)" \
-v /src/workspace:/src/workspace \
-v /tmp/build_output:/tmp/build_output \
-w /src/workspace \
gcr.io/bazel-public/bazel:latest \
--output_user_root=/tmp/build_output \
build --config={asan | tsan | msan} -- //absl/... -//absl/types:variant_test从容器内构建 Abseil 项目
借助本部分中的说明,您可以使用 Bazel 容器(源代码在容器内)进行构建。通过在开发工作流开始时启动容器并在容器内的工作区中进行更改,构建结果将被缓存。
在 Bazel 容器中启动 shell:
docker run --interactive --entrypoint=/bin/bash gcr.io/bazel-public/bazel:latest每个容器 ID 都是唯一的。在下面的说明中,容器为 5a99103747c6。
克隆项目。
ubuntu@5a99103747c6:~$ git clone --depth 1 --branch 20220623.1 https://github.com/abseil/abseil-cpp.git && cd abseil-cpp/执行常规构建。
ubuntu@5a99103747c6:~/abseil-cpp$ bazel build //absl/...通过添加 --config=asan|tsan|msan
build 标志来构建带有 Sanitizer 的项目,以相应地选择 AddressSanitizer (asan)、ThreadSanitizer (tsan) 或
MemorySanitizer (msan)。
ubuntu@5a99103747c6:~/abseil-cpp$ bazel build --config={asan | tsan | msan} -- //absl/... -//absl/types:variant_test探索 Bazel 容器
如果尚未执行此操作,请在 Bazel 容器内启动交互式 shell。
docker run -it --entrypoint=/bin/bash gcr.io/bazel-public/bazel:latest
ubuntu@5a99103747c6:~$探索容器内容。
ubuntu@5a99103747c6:~$ gcc --version gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.ubuntu@5a99103747c6:~$ java -version openjdk version "1.8.0_362" OpenJDK Runtime Environment (build 1.8.0_362-8u372-ga~us1-0ubuntu1~20.04-b09) OpenJDK 64-Bit Server VM (build 25.362-b09, mixed mode)ubuntu@5a99103747c6:~$ python -V Python 3.8.10ubuntu@5a99103747c6:~$ bazel version WARNING: Invoking Bazel in batch mode since it is not invoked from within a workspace (below a directory having a WORKSPACE file). Extracting Bazel installation... Build label: 6.2.1 Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar Build time: Fri Jun 2 16:59:58 2023 (1685725198) Build timestamp: 1685725198 Build timestamp as int: 1685725198
探索 Bazel Dockerfile
如果您想检查 Bazel Docker 映像的构建方式,可以在 bazelbuild/continuous-integration/bazel/oci 中找到其 Dockerfile。