1

我有一个使用 Gazelle 设置的(纯)Go 项目。它有一个主二进制文件,它cmd/main/BUILD由 Gazelle 生成,如下所示:

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
    name = "main_lib",
    srcs = ["main.go"],
    importpath = "github.com/me/myrepo/....",
    visibility = ["//visibility:private"],
    deps = [
        "//pkg/api",
        "//pkg/archive",
        ...
    ],
)

go_binary(
    name = "main",
    embed = [":main_lib"],
    visibility = ["//visibility:public"],
)

我可以为我的主机系统构建二进制文件,或者通过传递--platforms=@io_bazel_rules_go//go/toolchain:linux_arm64命令行为我们的部署目标构建二进制文件。但是现在,我希望能够为两个平台创建一个包含构建的 tar,而无需使用不同的 CLI 参数调用 Bazel。

正如这里所推荐的(“您可以通过 //command_line_option:platforms 上的 Bazel 配置转换等效地依赖 go_binary 或 go_test 规则”),我正在尝试使用转换进行设置。

我的transitions.bzl文件:

# build opts for development machine
def _host_transition_impl(settings, attr):
    _ignore = (settings, attr)
    return {
        "//command_line_option:platforms": "@io_bazel_rules_go//go/toolchain:linux_amd64",
        "//command_line_option:compilation_mode": "fastbuild",
    }

# build opts for deployment target
def _target_transition_impl(settings, attr):
    _ignore = (settings, attr)
    return {
        "//command_line_option:platforms": "@io_bazel_rules_go//go/toolchain:linux_arm64",
        "//command_line_option:compilation_mode": "opt",
    }

host_transition = transition(
    implementation = _host_transition_impl,
    inputs = [],
    outputs = ["//command_line_option:platforms", "//command_line_option:compilation_mode"],
)

target_transition = transition(
    implementation = _target_transition_impl,
    inputs = [],
    outputs = ["//command_line_option:platforms", "//command_line_option:compilation_mode"],
)

def _impl(ctx):
    return []

host_transitioning_rule = rule(
    implementation = _impl,
    cfg = host_transition,
    attrs = {
        "_allowlist_function_transition": attr.label(
            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
        ),
    },
)

target_transitioning_rule = rule(
    implementation = _impl,
    cfg = target_transition,
    attrs = {
        "_allowlist_function_transition": attr.label(
            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
        ),
    },
)

但是现在,我不知道如何将转换附加到 Gazelle 生成的 go_binary 规则。我的根构建文件:

load("@bazel_gazelle//:def.bzl", "gazelle")

load(":transitions.bzl", "host_transitioning_rule", "target_transitioning_rule")

load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")

# gazelle:prefix github.com/me/mypath...
gazelle(name = "gazelle")

gazelle(
    name = "gazelle-update-repos",
    args = [
        "-from_file=go.mod",
        "-to_macro=deps.bzl%go_dependencies",
        "-prune",
        "-build_file_proto_mode=disable_global",
    ],
    command = "update-repos",
)

host_transitioning_rule(
    name = "mainHost",
    # TODO: how to attach this to //cmd/main:main
)

target_transitioning_rule(
    name = "mainTarget",
    # TODO: how to attach this to //cmd/main:main
)

pkg_tar(
    name = "release",
    srcs = [
        ":mainHost",
        ":mainTarget",
    ],
    package_dir = "lib",
)

4

1 回答 1

0

只需从其他规则转发相关提供者即可。FilesToRunProviderDefaultInfo通常是最重要的。您正在有效地重新创建别名,并将您的过渡附加到传出边缘。像这样的东西:

def _impl(ctx):
    providers = []
    if DefaultInfo in ctx.attr.dep:
        providers.append(ctx.attr.dep[DefaultInfo])
    if FilesToRunProvider in ctx.attr.dep:
        providers.append(ctx.attr.dep[FilesToRunProvider])
    return providers

host_transitioning_rule = rule(
    implementation = _impl,
    attrs = {
        "_allowlist_function_transition": attr.label(
            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
        ),
        "dep": attr.label(cfg = host_transition),
    },
)

然后像这样使用它:

host_transitioning_rule(
    name = "mainHost",
    dep = "//cmd/main:main",
)

您尝试将过渡附加到传入边缘。这会改变规则本身的配置。在这种情况下并没有真正的区别,因为该规则只有一个依赖项,但是如果您想将主机配置中的一个依赖项和目标配置中的一个依赖项(例如)结合起来,那么您将需要使用传出边缘转换在单独的属性上。

您可以遍历所有提供者,例如examples/blob/main/rules/attributes/printer.bzl,然后将它们全部转发,而不是寻找特定的提供者。不过,对于某些提供商来说,这可能会有点棘手,所以我会避免它。例如,如果你只是在交叉编译时盲目地转发GoLibrary,你最终会尝试链接为不同架构编译的库,这是行不通的。

于 2022-02-18T20:21:37.423 回答