Bazel自定义ccppc工具链报错:无法匹配//ccppc_tools:toolchain_type
Bazel自定义工具链无法识别//ccppc_tools:toolchain_type的问题解决
问题场景
在Bazel中实现自定义工具链以使用ccppc编译器,遵循官方文档配置后,执行构建命令时Bazel无法识别//ccppc_tools:toolchain_type,报错找不到匹配的工具链。
执行命令
bazel build --platforms=//src:my_platform //src:TEST_OBJECT
报错信息
ERROR: C:/tools/dev/test_partition/src/BUILD:11:14: While resolving toolchains for target //src:TEST_OBJECT: No matching toolchains found for types //ccppc_tools:toolchain_type. To debug, rerun with --toolchain_resolution_debug='//ccppc_tools:toolchain_type' ERROR: Analysis of target '//src:TEST_OBJECT' failed; build aborted: INFO: Elapsed time: 0.564s INFO: 0 processes. FAILED: Build did NOT complete successfully (2 packages loaded, 3 targets configured)
项目结构
Project | + ccppc_tools/ -- rules.bzl BUILD + src/ -- command.c BUILD + BUILD + WORKSPACE
相关配置文件
ccppc_tools/rules.bzl
##################################################################### TOOLCHAIN PART # Declare the information provider CCPPCInfo = provider( doc = "Information about how to invoke the ccppc compiler.", # In the real world, compiler_path and system_lib might hold File objects, # but for simplicity they are strings for this example. arch_flags is a list # of strings. fields = ["compiler_path", "system_lib", "arch_flags"], ) # Rules for the CCPPC def _ccppc_toolchain_impl(ctx): toolchain_info = platform_common.ToolchainInfo( ccppcinfo = CCPPCInfo( compiler_path = ctx.attr.compiler_path, system_lib = ctx.attr.system_lib, arch_flags = ctx.attr.arch_flags, ), ) return [toolchain_info] ccppc_toolchain = rule( implementation = _ccppc_toolchain_impl, attrs = { "compiler_path": attr.string(), "system_lib": attr.label( mandatory = False, cfg = "target", ), "arch_flags": attr.string_list(), }, ) ##################################################################### USER RULES PART # Rule for creating objects with CCPPC def _ccppc_objects_impl(ctx): info = ctx.toolchains["//ccppc_tools:toolchain_type"].ccppcinfo command = "%s %s -c %s" % ( info.compiler_path, info.system_lib, " ".join(info.arch_flags), ) print("CALLED CCPPC OBJECTS: ") print(command) ccppc_objects = rule( implementation = _ccppc_objects_impl, attrs = { "srcs": attr.label_list(allow_files = True) }, toolchains = ["//ccppc_tools:toolchain_type"], )
ccppc_tools/BUILD
load("//ccppc_tools:rules.bzl","ccppc_toolchain") toolchain_type(name = "toolchain_type") # Declare a ccppc toolchain ccppc_toolchain( name = "ccppc_windows", arch_flags = [ "--arch=WINDOWS", "--DTEST", ], compiler_path = "ccppc", ) # Declare a toolchain from the ccppc_toolchain toolchain( name = "ccppc_windows_toolchain", exec_compatible_with = [ "@platforms//os:windows", "@platforms//cpu:x86_64", ], target_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:ppc", ], toolchain = ":ccppc_windows", toolchain_type = ":toolchain_type", )
src/BUILD
load("//:ccppc_tools/rules.bzl","ccppc_objects") platform( name = "my_platform", constraint_values = [ "@platforms//os:linux", ], ) ccppc_objects( name = "TEST_OBJECT", srcs = ["src/command.c"] )
WORKSPACE
register_toolchains( "//ccppc_tools:ccppc_windows_toolchain", )
解决方案
1. 修复平台约束不匹配问题
自定义平台//src:my_platform仅指定了操作系统约束,缺少工具链要求的CPU约束,导致Bazel无法匹配到工具链。修改src/BUILD中的platform定义,添加CPU约束:
platform( name = "my_platform", constraint_values = [ "@platforms//os:linux", "@platforms//cpu:ppc", # 新增该约束,与工具链target_compatible_with中的CPU匹配 ], )
2. 处理system_lib属性空值问题
当前ccppc_windows规则未赋值system_lib属性,在_ccppc_objects_impl中直接拼接会导致空字符串,可通过两种方式修复:
- 方式一:在
ccppc_toolchain规则中给system_lib设置默认空值
修改ccppc_tools/rules.bzl中ccppc_toolchain的attrs:"system_lib": attr.label( mandatory = False, cfg = "target", default = "", # 添加默认值 ), - 方式二:在实现函数中判断空值
修改_ccppc_objects_impl:def _ccppc_objects_impl(ctx): info = ctx.toolchains["//ccppc_tools:toolchain_type"].ccppcinfo system_lib_arg = info.system_lib if info.system_lib else "" command = "%s %s -c %s" % ( info.compiler_path, system_lib_arg, " ".join(info.arch_flags), ) print("CALLED CCPPC OBJECTS: ") print(command)
3. 验证工具链解析过程(可选)
执行调试命令查看工具链匹配的详细日志,确认约束是否正确匹配:
bazel build --platforms=//src:my_platform //src:TEST_OBJECT --toolchain_resolution_debug='//ccppc_tools:toolchain_type'
内容的提问来源于stack exchange,提问作者mtribiere
相关产品推荐
相关产品推荐

