工具链未下载工具

时间:2019-01-31 18:41:58

标签: 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)

在我的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,因此工具链兼容性似乎也错了。

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

1 个答案:

答案 0 :(得分:3)

两件事:

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

>

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

对于调试工具链,我分两个步骤:首先,确认工具存储库存在并且可以直接访问,然后检查工具链的注册和解析。

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

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

我很乐意根据需要帮助回答其他问题。