1

您好我正在尝试为 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 中使用这个工具链,所以路径会有所不同,但这里是一个 repo,只包含 fn 的东西以便于阅读。

4

1 回答 1

3

两件事情:

一,我怀疑这是你的实际问题:https ://github.com/bazelbuild/bazel/issues/6828 问题 的核心是,toolchain_type 目标是在外部存储库中,它总是需要被引用完全限定名称,从不使用本地限定名称。

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

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

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

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

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

于 2019-01-31T20:22:19.430 回答