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

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

Proper use of toolchain_type in Bazel?

问题

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


我的命令:

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

错误信息:

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

这是我的设置:

  1. 项目
  2. |
  3. + ccppc_tools/ -- rules.bzl BUILD
  4. + src/ -- command.c BUILD
  5. + BUILD
  6. + WORKSPACE

ccppc_tools/rules.bzl:

  1. ##################################################################### TOOLCHAIN 部分
  2. # 声明信息提供者
  3. CCPPCInfo = provider(
  4. doc = "关于如何调用ccppc编译器的信息。",
  5. fields = ["compiler_path", "system_lib", "arch_flags"],
  6. )
  7. # 用于CCPPC的规则
  8. def _ccppc_toolchain_impl(ctx):
  9. toolchain_info = platform_common.ToolchainInfo(
  10. ccppcinfo = CCPPCInfo(
  11. compiler_path = ctx.attr.compiler_path,
  12. system_lib = ctx.attr.system_lib,
  13. arch_flags = ctx.attr.arch_flags,
  14. ),
  15. )
  16. return [toolchain_info]
  17. ccppc_toolchain = rule(
  18. implementation = _ccppc_toolchain_impl,
  19. attrs = {
  20. "compiler_path": attr.string(),
  21. "system_lib": attr.label(
  22. mandatory = False,
  23. cfg = "target",
  24. ),
  25. "arch_flags": attr.string_list(),
  26. },
  27. )
  28. ##################################################################### USER RULES 部分
  29. # 用于使用CCPPC创建对象的规则
  30. def _ccppc_objects_impl(ctx):
  31. info = ctx.toolchains["//ccppc_tools:toolchain_type"].ccppcinfo
  32. command = "%s %s -c %s" % (
  33. info.compiler_path,
  34. info.system_lib,
  35. " ".join(info.arch_flags),
  36. )
  37. print("调用了CCPPC对象:")
  38. print(command)
  39. ccppc_objects = rule(
  40. implementation = _ccppc_objects_impl,
  41. attrs = {
  42. "srcs": attr.label_list(allow_files = True)
  43. },
  44. toolchains = ["//ccppc_tools:toolchain_type"],
  45. )

ccppc_tools/BUILD:

  1. load("//ccppc_tools:rules.bzl","ccppc_toolchain")
  2. toolchain_type(name = "toolchain_type")
  3. # 声明一个ccppc工具链
  4. ccppc_toolchain(
  5. name = "ccppc_windows",
  6. arch_flags = [
  7. "--arch=WINDOWS",
  8. "--DTEST",
  9. ],
  10. compiler_path = "ccppc",
  11. )
  12. # 声明一个来自ccppc_toolchain的工具链
  13. toolchain(
  14. name = "ccppc_windows_toolchain",
  15. exec_compatible_with = [
  16. "@platforms//os:windows",
  17. "@platforms//cpu:x86_64",
  18. ],
  19. target_compatible_with = [
  20. "@platforms//os:linux",
  21. "@platforms//cpu:ppc",
  22. ],
  23. toolchain = ":ccppc_windows",
  24. toolchain_type = ":toolchain_type",
  25. )

src/BUILD:

  1. load("//:ccppc_tools/rules.bzl","ccppc_objects")
  2. platform(
  3. name = "my_platform",
  4. constraint_values = [
  5. "@platforms//os:linux",
  6. ],
  7. )
  8. ccppc_objects(
  9. name = "TEST_OBJECT",
  10. srcs = ["src/command.c"]
  11. )

WORKSPACE:

  1. register_toolchains(
  2. "//ccppc_tools:ccppc_windows_toolchain",
  3. )

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

英文:

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:

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

The errors:

  1. 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.
  2. To debug, rerun with --toolchain_resolution_debug='//ccppc_tools:toolchain_type'
  3. If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
  4. ERROR: Analysis of target '//src:TEST_OBJECT' failed; build aborted:
  5. INFO: Elapsed time: 0.564s
  6. INFO: 0 processes.
  7. FAILED: Build did NOT complete successfully (2 packages loaded, 3 targets configured)

Here is my setup:

  1. Project
  2. |
  3. + ccppc_tools/ -- rules.bzl BUILD
  4. + src/ -- command.c BUILD
  5. + BUILD
  6. + WORKSPACE

ccppc_tools/rules.bzl:

  1. ##################################################################### TOOLCHAIN PART
  2. # Declare the information provider
  3. CCPPCInfo = provider(
  4. doc = "Information about how to invoke the ccppc compiler.",
  5. # In the real world, compiler_path and system_lib might hold File objects,
  6. # but for simplicity they are strings for this example. arch_flags is a list
  7. # of strings.
  8. fields = ["compiler_path", "system_lib", "arch_flags"],
  9. )
  10. # Rules for the CCPPC
  11. def _ccppc_toolchain_impl(ctx):
  12. toolchain_info = platform_common.ToolchainInfo(
  13. ccppcinfo = CCPPCInfo(
  14. compiler_path = ctx.attr.compiler_path,
  15. system_lib = ctx.attr.system_lib,
  16. arch_flags = ctx.attr.arch_flags,
  17. ),
  18. )
  19. return [toolchain_info]
  20. ccppc_toolchain = rule(
  21. implementation = _ccppc_toolchain_impl,
  22. attrs = {
  23. "compiler_path": attr.string(),
  24. "system_lib": attr.label(
  25. mandatory = False,
  26. cfg = "target",
  27. ),
  28. "arch_flags": attr.string_list(),
  29. },
  30. )
  31. ##################################################################### USER RULES PART
  32. # Rule for creating objects with CCPPC
  33. def _ccppc_objects_impl(ctx):
  34. info = ctx.toolchains["//ccppc_tools:toolchain_type"].ccppcinfo
  35. command = "%s %s -c %s" % (
  36. info.compiler_path,
  37. info.system_lib,
  38. " ".join(info.arch_flags),
  39. )
  40. print("CALLED CCPPC OBJECTS: ")
  41. print(command)
  42. ccppc_objects = rule(
  43. implementation = _ccppc_objects_impl,
  44. attrs = {
  45. "srcs": attr.label_list(allow_files = True)
  46. },
  47. toolchains = ["//ccppc_tools:toolchain_type"],
  48. )

ccppc_tools/BUILD:

  1. load("//ccppc_tools:rules.bzl","ccppc_toolchain")
  2. toolchain_type(name = "toolchain_type")
  3. # Declare a ccppc toolchain
  4. ccppc_toolchain(
  5. name = "ccppc_windows",
  6. arch_flags = [
  7. "--arch=WINDOWS",
  8. "--DTEST",
  9. ],
  10. compiler_path = "ccppc",
  11. )
  12. # Declare a toolchain from the ccppc_toolchain
  13. toolchain(
  14. name = "ccppc_windows_toolchain",
  15. exec_compatible_with = [
  16. "@platforms//os:windows",
  17. "@platforms//cpu:x86_64",
  18. ],
  19. target_compatible_with = [
  20. "@platforms//os:linux",
  21. "@platforms//cpu:ppc",
  22. ],
  23. toolchain = ":ccppc_windows",
  24. toolchain_type = ":toolchain_type",
  25. )

src/BUILD:

  1. load("//:ccppc_tools/rules.bzl","ccppc_objects")
  2. platform(
  3. name = "my_platform",
  4. constraint_values = [
  5. "@platforms//os:linux",
  6. ],
  7. )
  8. ccppc_objects(
  9. name = "TEST_OBJECT",
  10. srcs = ["src/command.c"]
  11. )

WORKSPACE:

  1. register_toolchains(
  2. "//ccppc_tools:ccppc_windows_toolchain",
  3. )

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:

确定