本教程使用一个示例场景来说明如何配置 C++ 工具链。
学习内容
在本教程中,您将学习如何:
- 设置构建环境
- 使用
--toolchain_resolution_debug
调试工具链解析 - 配置 C++ 工具链
- 创建一条 Starlark 规则,为
cc_toolchain
,以便 Bazel 可以使用clang
构建应用 - 通过在
bazel build //main:hello-world
Linux 机器 - 通过运行
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 会为其构建目标@platforms//host
正在使用@bazel_tools+cc_configure_extension+local_config_cc//:cc-compiler-k8
。
配置 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
目标 主机配置。再次运行构建。由于
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")
再次运行构建。Bazel 会抛出以下错误:
'//toolchain:linux_x86_64_toolchain_config' does not have mandatory providers: 'CcToolchainConfigInfo'.
CcToolchainConfigInfo
是用于配置 C++ 组件的提供程序 工具链。要修正此错误,请创建一个 Starlark 规则, 对 Bazel 进行CcToolchainConfigInfo
操作toolchain/cc_toolchain_config.bzl
文件,其中包含以下内容: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
规则,请添加加载项 语句添加到toolchain/BUILD
语句(位于 package 语句正下方):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")
再次运行构建。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,什么 工具。为此,您需要使用
tool_path()
构造函数@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl
:# 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
属性, 但在这里,使用cxx_builtin_include_directories
cc_common.create_cc_toolchain_config_info
的参数。请注意 使用的是其他版本的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, )
再次运行构建命令,您将看到如下错误:
/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++ 工具链 配置