python - 工具链未下载工具

标签 python bazel skylark starlark

您好,我正在尝试为 Fn 项目设置一个工具链。该方法是为 GitHub 中可用的每个二进制文件设置一个工具链,然后理论上在规则中使用它。

我有一个通用包,其中包含可用的二进制文件:

default_version = "0.5.44"

os_list = [
    "linux",
    "mac",
    "windows"
]

def get_bin_name(os):
    return "fn_cli_%s_bin" % os

下载部分如下所示:

load(":common.bzl", "get_bin_name", "os_list", "default_version")

_url = "https://github.com/fnproject/cli/releases/download/{version}/{file}"
_os_to_file = {
    "linux" : "fn_linux",
    "mac" : "fn_mac",
    "windows" : "fn.exe",
}

def _fn_binary(os):
    name = get_bin_name(os)
    file = _os_to_file.get(os)
    url = _url.format(
        file = file,
        version = default_version
    )
    native.http_file(
        name = name,
        urls = [url],
        executable = True
    )

def fn_binaries():
    """
    Installs the hermetic binary for Fn.
    """
    for os in os_list:
        _fn_binary(os)

然后我像这样设置工具链:

load(":common.bzl", "get_bin_name", "os_list")

_toolchain_type = "toolchain_type"

FnInfo = provider(
    doc = "Information about the Fn Framework CLI.",
    fields = {
        "bin" : "The Fn Framework binary."
    }
)

def _fn_cli_toolchain(ctx):
  toolchain_info = platform_common.ToolchainInfo(
      fn_info = FnInfo(
          bin = ctx.attr.bin
      )
  )
  return [toolchain_info]

fn_toolchain = rule(
    implementation = _fn_cli_toolchain,
    attrs = {
        "bin" : attr.label(mandatory = True)
    }
)

def _add_toolchain(os):
    toolchain_name = "fn_cli_%s" % os
    native_toolchain_name = "fn_cli_%s_toolchain" % os
    bin_name = get_bin_name(os)
    compatibility = ["@bazel_tools//platforms:%s" % os]

    fn_toolchain(
        name = toolchain_name,
        bin = ":%s" % bin_name,
        visibility = ["//visibility:public"]
    )

    native.toolchain(
        name = native_toolchain_name,
        toolchain = ":%s" % toolchain_name,
        toolchain_type = ":%s" % _toolchain_type,
        target_compatible_with = compatibility
    )

def setup_toolchains():
    """
    Macro te set up the toolchains for the different platforms
    """
    native.toolchain_type(name = _toolchain_type)

    for os in os_list:
      _add_toolchain(os)

def fn_register():
    """
    Registers the Fn toolchains.
    """
    path = "//tools/bazel_rules/fn/internal/cli:fn_cli_%s_toolchain"

    for os in os_list:
      native.register_toolchains(path % os)

在我的构建文件中,我调用 setup_toolchains:

load(":toolchain.bzl", "setup_toolchains")
setup_toolchains()

通过这个设置,我有一个如下所示的规则:

_toolchain = "//tools/bazel_rules/fn/cli:toolchain_type"

def _fn(ctx):
  print("HEY")
  bin = ctx.toolchains[_toolchain].fn_info.bin
  print(bin)

# TEST RULE
fn = rule(
    implementation = _fn,
    toolchains = [_toolchain]
)

工作空间:

workspace(name = "basicwindow")

load("//tools/bazel_rules/fn:defs.bzl", "fn_binaries", "fn_register")
fn_binaries()
fn_register()

当我使用 bazel query //tools/bazel_rules/fn/internal/cli:fn_cli_linux_bin 查询不同的二进制文件时他们在那里,但正在打电话bazel build //...导致错误,提示:

ERROR: /Users/marcguilera/Code/Marc/basicwindow/tools/bazel_rules/fn/internal/cli/BUILD.bazel:2:1: in bin attribute of fn_toolchain rule //tools/bazel_rules/fn/internal/cli:fn_cli_windows: rule '//tools/bazel_rules/fn/internal/cli:fn_cli_windows_bin' does not exist. Since this rule was created by the macro 'setup_toolchains', the error might have been caused by the macro implementation in /Users/marcguilera/Code/Marc/basicwindow/tools/bazel_rules/fn/internal/cli/toolchain.bzl:35:15
ERROR: Analysis of target '//tools/bazel_rules/fn/internal/cli:fn_cli_windows' failed; build aborted: Analysis of target '//tools/bazel_rules/fn/internal/cli:fn_cli_windows' failed; build aborted
INFO: Elapsed time: 0.079s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

我尝试按照文档中的工具链教程进行操作,但无法使其工作。另一个有趣的事情是,我实际上使用的是 mac,所以工具链兼容性似乎也是错误的。

我在存储库中使用此工具链,因此路径有所不同,但 here's为了便于阅读,仅包含 fn 内容的存储库。

最佳答案

有两件事:

一,我怀疑这是您的实际问题:https://github.com/bazelbuild/bazel/issues/6828 问题的核心在于,如果 toolchain_type 目标位于外部存储库中,那么它总是需要通过完全限定名称来引用,而不是通过本地限定名称来引用。

第二个是更基本的:这里有很多 Starlark 宏正在生成其他目标,而且很难阅读。实际上,删除许多宏会简单得多,例如 _fn_binary、fn_binaries 和 _add_toolchains。只需让 setup_toolchains 直接创建所需的 native.toolchain 目标,并使用一个存储库宏调用 http_archive 三次来声明三组不同的二进制文件。这将使代码更容易阅读,从而更容易调试。

对于调试工具链,我遵循两个步骤:首先,验证工具存储库是否存在并且可以直接访问,然后检查工具链注册和解析。

深入几个级别后,您似乎正在调用 http_archive,将新存储库命名为 @linux,并下载特定的二进制文件。这不是 http_archive 的工作方式:它期望获取一个 zip 文件(或 tar.gz 文件),解压该文件,并在其中找到一个 WORKSPACE 和至少一个 BUILD 文件。

我的建议:简化宏,明确定义外部存储库,然后探索使用工具链解析来选择正确的存储库。

我很乐意根据需要帮助回答更多问题。

关于python - 工具链未下载工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467249/

相关文章:

Bazel:输出目录的genrule

python - 来自多个模块的数据库连接

python - 替换为 Python 中多个子字符串中的提取物

python - wxPython 和高速公路 websockets

c++ - Googletest 永远不会通过 bazel 测试失败(应该失败),但可以与 cmake 和 clion 一起使用

node.js - 构建依赖于另一个规则的 bazel 规则

python - 如何拆分具有相同分隔符的列

java - Bazel 使用 Jackson 注解构建 ser/Deser 并生成 Java 类

bazel - 如何将 Starlark 脚本正确加载到另一个脚本中?

bazel - 使用相同生成的输出文件实例化 Bazel 宏两次