首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Bazel一次编译多个平台目标?

如何使用Bazel一次编译多个平台目标?
EN

Stack Overflow用户
提问于 2020-06-15 09:41:53
回答 1查看 986关注 0票数 2

编译单一平台:

代码语言:javascript
运行
复制
# BUILD
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
go_binary(
      name = 'example',
      srcs = glob(["src/*.go"]),
)

它可以正常编译,但当我想编译多个平台时,只能这样:

代码语言:javascript
运行
复制
bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //:example
bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_386 //:example
bazel build --platforms=@io_bazel_rules_go//go/toolchain:windows_amd64 //:example
bazel build --platforms=@io_bazel_rules_go//go/toolchain:darwin_amd64 //:example

我想一次编译多个平台目标,所以我尝试这样做:

代码语言:javascript
运行
复制
# BUILD
load("//:matrix.bzl", "build_all_platform")


build_all_platform(
    name = 'example',
    pkg = glob(["src/*.go"]),
)

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

SUPPORTED_MATRIX = [
  ("windows", "amd64"),
  ("darwin", "amd64"),
  ("linux", "amd64"),
  ("linux", "386"),
]

def _build(ctx):
    for goos, goarch in SUPPORTED_MATRIX:
        target_name = 'proxy-download-' + goos + '-' + goarch
        if goos == 'windows':
            target_name += '.exe'

        go_binary(
            name = target_name,
            srcs = ctx.attr.pkg,
            pure = "auto",
            goos = goos,
            goarch = goarch,
        )

build_all_platform = rule(
    _build,
     attrs = {
        'pkg': attr.string_list(),
      },
      executable = True,
)

但遇到一个错误,我认为这可能是rules_go的原因。

代码语言:javascript
运行
复制
Traceback (most recent call last):
    File "/source/proxy-download/BUILD", line 4
        build_all_platform(name = 'proxy-download')
    File "/source/proxy-download/matrix.bzl", line 16, in _build
        go_binary(name = target_name, <4 more arguments>)
    File "/private/var/tmp/_bazel_/071099b99a462d431baf96a3ef76cd28/external/io_bazel_rules_go/go/private/rules/wrappers.bzl", line 50, in go_binary
        go_transition_wrapper(go_binary, <3 more arguments>)
    File "/private/var/tmp/_bazel_/071099b99a462d431baf96a3ef76cd28/external/io_bazel_rules_go/go/private/rules/transition.bzl", line 60, in go_transition_wrapper
        transition_kind(name = name, <1 more arguments>)
'rule' can only be called during the loading phase

尝试同时通过this issue中提到的多个平台

代码语言:javascript
运行
复制
bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_386,@io_bazel_rules_go//go/toolchain:linux_amd64 //:example

> WARNING: --platforms only supports a single target platform: using the first option @io_bazel_rules_go//go/toolchain:linux_386

作为参考,this code通过使用build命令行来实现此功能

代码语言:javascript
运行
复制
options = [
    "go",
    "build",
    "-o", output_file.path,
    "-compiler", "gc",
    "-gcflags", '"all=-trimpath=${GOPATH}/src"',
    "-asmflags", '"all=-trimpath=${GOPATH}/src"',
    "-ldflags", "'%s'" % ld_flags,
    "-tags", "'%s'" % ctx.attr.gotags,
    pkg,
  ]
EN

回答 1

Stack Overflow用户

发布于 2020-06-15 13:23:35

您可能想看看Bazel的user defined transitions,它可以使用不同的构建配置构建依赖项。请注意,这是一个实验性的特性,并不是所有的构建参数都是可配置的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62380155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档