【问题标题】:Toolchain not downloading the tool工具链没有下载工具
【发布时间】:2019-01-31 18:41:58
【问题描述】:

您好,我正在尝试为 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)

在我的 BUILD 文件中,我调用 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,所以工具链的兼容性似乎也是错误的。

我在 repo 中使用这个工具链,所以路径会有所不同,但 here's 一个 repo 只包含 fn 内容以便于阅读。

【问题讨论】:

    标签: python bazel skylark starlark


    【解决方案1】:

    两件事:

    一,我怀疑这是您的实际问题: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 文件。

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

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

    【讨论】:

    • 您好,感谢您的详细回复。我将简化代码并尝试按照您提到的方式进行调试。不过有两个问题:1)我使用的是http_file,而不是http_archive。你是这个意思吗? 2)当您指出如何调试工具链时,您如何检查工具链的注册和解析?如果我查询 bazel query //tools/bazel_rules/fn/internal/cli:toolchain_type 我会得到适当的结果。
    • (注意:我不喜欢 SO 评论编辑器,它不小心保存了几次。对于重复通知表示歉意)。第 1 部分:哎呀,您对 http_archive 与 http_file 的看法是正确的,这是我的错误。我可能没有使用与您相同的 git 存储库,因为我没有看到 //tools/bazel_rules/fn 包。您是否以某种方式尝试将这些规则添加到 Bazel 源代码树中?我已经使用工具链分支检查了您提到的 git 存储库 (github.com/marcguilera/rules_serverless)。
    • 第 2 部分:那里有一个 //internal/tools/cli 包,它似乎有几个工具链。看起来它们已注册(通过宏),因此 Bazel 应该可以使用它们。但是,查看后宏 BUILD 文件,我发现有些混乱。从bazel query --output=build //internal/tools/cli:all,我看到:``` fn_toolchain( name = "fn_cli_linux", visibility = ["//visibility:public"], bin = "//internal/tools/cli:fn_cli_linux_bin", ) ```
    • 第 3 部分:但是,没有名为“fn_cli_linux_bin”的目标。我认为这 应该 是从 http_file 创建的外部存储库,但同样,使用深度嵌套的宏很难说清楚。下一步是测试需要工具链的构建,但要使用--toolchain_resolution_debug 标志启用工具链调试。这会产生大量信息,但会导致 Bazel 调试它正在考虑哪些工具链,哪些被丢弃(以及一些有关原因的详细信息),以及哪些被选中。
    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多