本页面详细介绍了 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
创建一个文件夹,其中包含要在各 build 之间共享的缓存结果。
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 标志来构建带有 Sanitizers 的项目,以相应地选择 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/
执行常规 build。
ubuntu@5a99103747c6:~/abseil-cpp$ bazel build //absl/...
通过添加 --config=asan|tsan|msan
build 标志来构建带有 Sanitizers 的项目,以相应地选择 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.10
ubuntu@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。