本教程通过一个示例场景介绍了如何为项目配置 C++ 工具链。
学习内容
在本教程中,您将学习如何:
- 设置构建环境
- 使用
--toolchain_resolution_debug
调试工具链解析 - 配置 C++ 工具链
- 创建一个 Starlark 规则,为
cc_toolchain
提供额外的配置,以便 Bazel 可以使用clang
构建应用 - 在 Linux 机器上运行
bazel build //main:hello-world
,构建 C++ 二进制文件 - 运行
bazel build //main:hello-world --platforms=//:android_x86_64
,针对 Android 交叉编译二进制文件
准备工作
本教程假定您使用的是 Linux,并且已成功构建 C++ 应用并安装了相应的工具和库。本教程使用了 clang version 16
,您可以在自己的系统上安装它。
设置构建环境
请按照以下步骤设置构建环境:
如果您尚未下载并安装 Bazel 7.0.2 或更高版本,请执行此操作。
在根文件夹中添加一个空的
MODULE.bazel
文件。将以下
cc_binary
目标添加到main/BUILD
文件中:cc_binary( name = "hello-world", srcs = ["hello-world.cc"], )
由于 Bazel 在构建过程中会使用许多使用 C++ 编写的内部工具(例如
process-wrapper
),因此系统会为宿主平台指定现有的默认 C++ 工具链。这样,这些内部工具就可以使用本教程中创建的工具链进行构建。因此,cc_binary
目标也会使用默认工具链进行构建。使用以下命令运行构建:
bazel build //main:hello-world
构建成功,但
MODULE.bazel
中未注册任何工具链。如需进一步了解内部机制,请运行以下命令:
bazel build //main:hello-world --toolchain_resolution_debug='@bazel_tools//tools/cpp:toolchain_type' INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host, type @@bazel_tools//tools/cpp:toolchain_type -> toolchain @@bazel_tools+cc_configure_extension+local_config_cc//:cc-compiler-k8
如果未指定
--platforms
,Bazel 将使用@bazel_tools+cc_configure_extension+local_config_cc//:cc-compiler-k8
为@platforms//host
构建目标。
配置 C++ 工具链
如需配置 C++ 工具链,请重复构建应用并逐一消除每个错误,如下所述。
它还假定 clang version 9.0.1
,但不同版本的 clang 之间的详细信息应该只会略有变化。
通过以下方式添加
toolchain/BUILD
:filegroup(name = "empty") cc_toolchain( name = "linux_x86_64_toolchain", toolchain_identifier = "linux_x86_64-toolchain", toolchain_config = ":linux_x86_64_toolchain_config", all_files = ":empty", compiler_files = ":empty", dwp_files = ":empty", linker_files = ":empty", objcopy_files = ":empty", strip_files = ":empty", supports_param_files = 0, ) toolchain( name = "cc_toolchain_for_linux_x86_64", toolchain = ":linux_x86_64_toolchain", toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", exec_compatible_with = [ "@platforms//cpu:x86_64", "@platforms//os:linux", ], target_compatible_with = [ "@platforms//cpu:x86_64", "@platforms//os:linux", ], )
然后,添加适当的依赖项,并使用以下命令将工具链注册到
MODULE.bazel
bazel_dep(name = "platforms", version = "0.0.10") register_toolchains( "//toolchain:cc_toolchain_for_linux_x86_64" )
此步骤定义了
cc_toolchain
,并将其绑定到主机配置的toolchain
目标。再次运行 build。由于
toolchain
软件包尚未定义linux_x86_64_toolchain_config
目标,因此 Bazel 会抛出以下错误:ERROR: toolchain/BUILD:4:13: in toolchain_config attribute of cc_toolchain rule //toolchain:linux_x86_64_toolchain: rule '//toolchain:linux_x86_64_toolchain_config' does not exist.
在
toolchain/BUILD
文件中,按如下方式定义一个空文件组:package(default_visibility = ["//visibility:public"]) filegroup(name = "linux_x86_64_toolchain_config")
再次运行 build。Bazel 会抛出以下错误:
'//toolchain:linux_x86_64_toolchain_config' does not have mandatory providers: 'CcToolchainConfigInfo'.
CcToolchainConfigInfo
是用于配置 C++ 工具链的提供程序。如需修正此错误,请创建一个 Starlark 规则,通过创建一个包含以下内容的toolchain/cc_toolchain_config.bzl
文件向 Bazel 提供CcToolchainConfigInfo
:def _impl(ctx): return cc_common.create_cc_toolchain_config_info( ctx = ctx, toolchain_identifier = "k8-toolchain", host_system_name = "local", target_system_name = "local", target_cpu = "k8", target_libc = "unknown", compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", ) cc_toolchain_config = rule( implementation = _impl, attrs = {}, provides = [CcToolchainConfigInfo], )
cc_common.create_cc_toolchain_config_info()
创建所需的提供方CcToolchainConfigInfo
。如需使用cc_toolchain_config
规则,请将 load 语句添加到 package 语句正下方的toolchain/BUILD
中:load(":cc_toolchain_config.bzl", "cc_toolchain_config")
并将“linux_x86_64_toolchain_config”文件组替换为
cc_toolchain_config
规则的声明:cc_toolchain_config(name = "linux_x86_64_toolchain_config")
再次运行 build。Bazel 会抛出以下错误:
.../BUILD:1:1: C++ compilation of rule '//:hello-world' failed (Exit 1) src/main/tools/linux-sandbox-pid1.cc:421: "execvp(toolchain/DUMMY_GCC_TOOL, 0x11f20e0)": No such file or directory Target //:hello-world failed to build`
此时,Bazel 已经有足够的信息来尝试构建代码,但它仍然不知道要使用哪些工具来完成所需的构建操作。您将修改 Starlark 规则实现,以告知 Bazel 要使用哪些工具。为此,您需要
@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl
中的tool_path()
构造函数:# toolchain/cc_toolchain_config.bzl: # NEW load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "tool_path") def _impl(ctx): tool_paths = [ # NEW tool_path( name = "gcc", path = "/usr/bin/clang", ), tool_path( name = "ld", path = "/usr/bin/ld", ), tool_path( name = "ar", path = "/usr/bin/ar", ), tool_path( name = "cpp", path = "/bin/false", ), tool_path( name = "gcov", path = "/bin/false", ), tool_path( name = "nm", path = "/bin/false", ), tool_path( name = "objdump", path = "/bin/false", ), tool_path( name = "strip", path = "/bin/false", ), ] return cc_common.create_cc_toolchain_config_info( ctx = ctx, toolchain_identifier = "local", host_system_name = "local", target_system_name = "local", target_cpu = "k8", target_libc = "unknown", compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", tool_paths = tool_paths, # NEW )
确保
/usr/bin/clang
和/usr/bin/ld
是您的系统的正确路径。再次运行构建。Bazel 会抛出以下错误:
ERROR: main/BUILD:3:10: Compiling main/hello-world.cc failed: absolute path inclusion(s) found in rule '//main:hello-world': the source file 'main/hello-world.cc' includes the following non-builtin files with absolute paths (if these are builtin files, make sure these paths are in your toolchain): '/usr/include/c++/13/ctime' '/usr/include/x86_64-linux-gnu/c++/13/bits/c++config.h' '/usr/include/x86_64-linux-gnu/c++/13/bits/os_defines.h' ...
Bazel 需要知道在哪里搜索包含的头文件。解决此问题的方法有多种,例如使用
cc_binary
的includes
属性,但在这里,我们将使用cc_common.create_cc_toolchain_config_info
的cxx_builtin_include_directories
参数在工具链级别解决此问题。请注意,如果您使用的是其他版本的clang
,则包含路径会有所不同。这些路径也可能会因发行版本而异。将
toolchain/cc_toolchain_config.bzl
中的返回值修改为如下所示:return cc_common.create_cc_toolchain_config_info( ctx = ctx, cxx_builtin_include_directories = [ # NEW "/usr/lib/llvm-16/lib/clang/16/include", "/usr/include", ], toolchain_identifier = "local", host_system_name = "local", target_system_name = "local", target_cpu = "k8", target_libc = "unknown", compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", tool_paths = tool_paths, )
再次运行 build 命令,您会看到如下错误:
/usr/bin/ld: bazel-out/k8-fastbuild/bin/main/_objs/hello-world/hello-world.o: in function `print_localtime()': hello-world.cc:(.text+0x68): undefined reference to `std::cout'
这是因为链接器缺少 C++ 标准库,并且找不到其符号。您可以通过多种方式解决此问题,例如使用
cc_binary
的linkopts
属性。此处通过确保使用该工具链的任何目标都无需指定此标志来解决此问题。将以下代码复制到
toolchain/cc_toolchain_config.bzl
:# NEW load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") # NEW load( "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", # NEW "flag_group", # NEW "flag_set", # NEW "tool_path", ) all_link_actions = [ # NEW ACTION_NAMES.cpp_link_executable, ACTION_NAMES.cpp_link_dynamic_library, ACTION_NAMES.cpp_link_nodeps_dynamic_library, ] def _impl(ctx): tool_paths = [ tool_path( name = "gcc", path = "/usr/bin/clang", ), tool_path( name = "ld", path = "/usr/bin/ld", ), tool_path( name = "ar", path = "/bin/false", ), tool_path( name = "cpp", path = "/bin/false", ), tool_path( name = "gcov", path = "/bin/false", ), tool_path( name = "nm", path = "/bin/false", ), tool_path( name = "objdump", path = "/bin/false", ), tool_path( name = "strip", path = "/bin/false", ), ] features = [ # NEW feature( name = "default_linker_flags", enabled = True, flag_sets = [ flag_set( actions = all_link_actions, flag_groups = ([ flag_group( flags = [ "-lstdc++", ], ), ]), ), ], ), ] return cc_common.create_cc_toolchain_config_info( ctx = ctx, features = features, # NEW cxx_builtin_include_directories = [ "/usr/lib/llvm-9/lib/clang/9.0.1/include", "/usr/include", ], toolchain_identifier = "local", host_system_name = "local", target_system_name = "local", target_cpu = "k8", target_libc = "unknown", compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", tool_paths = tool_paths, ) cc_toolchain_config = rule( implementation = _impl, attrs = {}, provides = [CcToolchainConfigInfo], )
运行
bazel build //main:hello-world
,它最终应成功为主机构建二进制文件。在
toolchain/BUILD
中,复制cc_toolchain_config
、cc_toolchain
和toolchain
目标,并将目标名称中的linux_x86_64
替换为android_x86_64
。在
MODULE.bazel
中,注册 Android 的工具链register_toolchains( "//toolchain:cc_toolchain_for_linux_x86_64", "//toolchain:cc_toolchain_for_android_x86_64" )
运行
bazel build //main:hello-world --android_platforms=//toolchain:android_x86_64
以构建适用于 Android 的二进制文件。
在实践中,Linux 和 Android 应具有不同的 C++ 工具链配置。您可以针对差异修改现有的 cc_toolchain_config
,也可以为不同的平台创建单独的规则(即 CcToolchainConfigInfo
提供程序)。
查看您的工作
在本教程中,您学习了如何配置基本的 C++ 工具链,但工具链的功能比此示例更强大。
要点总结如下:
- 您需要在命令行中指定匹配的
platforms
标志,以便 Bazel 针对平台上的相同约束条件值解析到工具链。该文档包含更多与语言专用配置标志相关的信息。 - 您必须告知工具链工具所在的位置。本教程提供了一个简化版本,您可以从系统访问这些工具。如果您有兴趣了解更自给自足的方法,可以参阅外部依赖项。您的工具可能来自另一个模块,并且您必须使其文件可供
cc_toolchain
使用,并且目标依赖项是属性(例如compiler_files
)。tool_paths
也需要更改。 - 您可以创建功能来自定义应将哪些标志传递给不同的操作,无论是关联操作还是任何其他类型的操作。
深入阅读
如需了解详情,请参阅 C++ 工具链配置