toolchain_type在Bazel中的正确使用方式是什么?

huangapple go评论77阅读模式
英文:

Proper use of toolchain_type in Bazel?

问题

我需要在Bazel中实现一个自定义的工具链来使用自定义编译器 ccppc
我正在遵循 这里,但是似乎Bazel在处理 //ccppc_tools:toolchain_type 时出现了问题。


我的命令:

bazel build --platforms=//src:my_platform //src:TEST_OBJECT

错误信息:

错误: C:/tools/dev/test_partition/src/BUILD:11:14: 在解析目标 //src:TEST_OBJECT 的工具链时出错:未找到与类型 //ccppc_tools:toolchain_type 匹配的工具链。
要进行调试,请使用 --toolchain_resolution_debug='//ccppc_tools:toolchain_type'
如果平台或工具链对您来说是一个新概念,我们建议阅读 https://bazel.build/concepts/platforms-intro。
错误: 对目标 '//src:TEST_OBJECT' 的分析失败;构建中止:
信息: 耗时: 0.564 秒
信息: 0 个进程。
构建失败:构建未成功完成(已加载 2 个软件包,已配置 3 个目标)

这是我的设置:

项目
  | 
  + ccppc_tools/ -- rules.bzl BUILD
  + src/ -- command.c BUILD
  + BUILD
  + WORKSPACE

ccppc_tools/rules.bzl:

##################################################################### TOOLCHAIN 部分

# 声明信息提供者
CCPPCInfo = provider(
    doc = "关于如何调用ccppc编译器的信息。",
    fields = ["compiler_path", "system_lib", "arch_flags"],
)

# 用于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 部分

# 用于使用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("调用了CCPPC对象:")
    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")

# 声明一个ccppc工具链
ccppc_toolchain(
    name = "ccppc_windows",
    arch_flags = [
        "--arch=WINDOWS",
        "--DTEST",
    ],
    compiler_path = "ccppc",
)

# 声明一个来自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",
)

我尝试阅读项目和文档,看看我做错了什么,但是找不到任何信息。

英文:

I need to implement a custom toolchain in Bazel to use a custom compiler ccppc.
I'm following this, but it seems like Bazel is having trouble with //ccppc_tools:toolchain_type.


My command:

bazel build --platforms=//src:my_platform //src:TEST_OBJECT

The errors:

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'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
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)

Here is my setup:

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",
)

I tried reading through project and documentations see what I'm doing wrong but couldn't find anything

答案1

得分: 0

你需要将 @platforms//cpu:ppc 添加到 //src:my_platformconstraint_values 中。

可能还有其他原因阻止了选择该工具链,请参考我的评论,使用 --toolchain_resolution_debug='//ccppc_tools:toolchain_type' 添加输出以进一步进行调试。

英文:

You need to add @platforms//cpu:ppc to the constraint_values of //src:my_platform.

There may be other things preventing that toolchain from being selected, see my comment about adding output with --toolchain_resolution_debug='//ccppc_tools:toolchain_type' to include to debug further.

huangapple
  • 本文由 发表于 2023年6月1日 15:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定