我有一个使用 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",
)