Wener Site

Brazel Go

约 3 分钟阅读

Brazel Go

  • 默认开启 trimpath
  • 暂不支持 coverage
bash
# cross compile no cgo
bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //cmd
# cross compile with cgo
bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64_cgo //cmd
python
workspace(name = "my_project")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
    ],
)

http_archive(
    name = "bazel_gazelle",
    sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

#
go_rules_dependencies()
go_register_toolchains(version = "1.17.2")
gazelle_dependencies()
BUILD
load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix wener.me/wode/core
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",
)
bash
bazel run //:gazelle

bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=deps.bzl%go_dependencies
bazel run //:gazelle-update-repos
bash
go get github.com/bazelbuild/bazel-gazelle/cmd/gazelle
gazelle -go_prefix github.com/example/project

cross compile

python
go_library(
    name = "foo",
    srcs = [
        "foo_linux.go",
        "foo_windows.go",
    ],
    deps = select({
        "@io_bazel_rules_go//go/platform:linux_amd64": [
            "//bar_linux",
        ],
        "@io_bazel_rules_go//go/platform:windows_amd64": [
            "//bar_windows",
        ],
        "//conditions:default": [],
    }),
)

go_repository does not support file path replacements

rules_go

python
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")

go_rules_dependencies()

go_register_toolchains(version = "1.15.5")
# 使用 host 的 go 版本 - 编译不一定可重现
# 但更快,少了下载安装过程
go_register_toolchains(version = "host")
bash
# 交叉编译
bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //cmd
# with CGO
bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64_cgo //cmd
  • 也可以在 go_binary 设置 goos 和 goarch

gazelle

bash
# 命令行生成
gazelle -go_prefix go-micro.dev/v4 -proto disable

gazelle update

go_sdk

bash
# Host
go_host_sdk(name="go_sdk")
# Local
go_local_sdk(
    name = "go_sdk",
    path = "~/sdk/go1.18beta1",
)
go_register_toolchains()

# Download
go_register_toolchains(version = "1.17.6")
# Host
go_register_toolchains(version = "host")

found but does not contain package

is not visible from target

缓存相关问题

go: cannot find GOROOT directory: /usr/local/go

/usr/lib/go

关联信息

反向链接和本文引用的外部资料。

References

GitHub

9 条

另有 1 个未显示 references。

其他外链

1 条

另有 1 个 references 未显示。

最近更新commit a16a504Edit

On this page