在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:bazelbuild/rules_go开源软件地址:https://github.com/bazelbuild/rules_go开源编程语言:Go 53.1%开源软件介绍:BazelGo rules forMailing list: bazel-go-discuss Slack: #go on Bazel Slack, #bazel on Go Slack Announcements
ContentsDocumentation
Quick linksOverviewThe rules are in the beta stage of development. They support:
They currently do not support or have limited support for:
The Go rules are tested and supported on the following host platforms:
Users have reported success on several other platforms, but the rules are only tested on those listed above. Note: Since version v0.30.0, rules_go requires Bazel ≥ 4.2.1 to work. The SetupSystem setupTo build Go code with Bazel, you will need:
You normally won't need a Go toolchain installed. Bazel will download one. See Using rules_go on Windows for Windows-specific setup instructions. Several additional tools need to be installed and configured. Initial project setupCreate a file at the top of your repository named load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "685052b498b6ddfe562ca7a97736741d87916fe536623afb7da2824c0211c369",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.33.0/rules_go-v0.33.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.33.0/rules_go-v0.33.0.zip",
],
)
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
go_rules_dependencies()
go_register_toolchains(version = "1.18.3") You can use rules_go at Add a file named load("@io_bazel_rules_go//go:def.bzl", "go_binary")
go_binary(
name = "hello",
srcs = ["hello.go"],
) You can build this target with Generating build filesIf your project can be built with Add the
Add the code below to the load("@bazel_gazelle//:def.bzl", "gazelle")
# gazelle:prefix github.com/example/project
gazelle(name = "gazelle") This declares a bazel run //:gazelle This will generate a Writing build files by handIf your project doesn't follow In each directory that contains Go code, create a file named load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") For each library, add a go_library rule like the one below. Source files are
listed in the go_library(
name = "foo_library",
srcs = [
"a.go",
"b.go",
],
importpath = "github.com/example/project/foo",
deps = [
"//tools",
"@org_golang_x_utils//stuff",
],
visibility = ["//visibility:public"],
) For tests, add a go_test rule like the one below. The library being tested
should be listed in an go_test(
name = "foo_test",
srcs = [
"a_test.go",
"b_test.go",
],
embed = [":foo_lib"],
deps = [
"//testtools",
"@org_golang_x_utils//morestuff",
],
) For binaries, add a go_binary rule like the one below. go_binary(
name = "foo",
srcs = ["main.go"],
) Adding external repositoriesFor each Go repository, add a go_repository rule to load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Download the Go rules.
http_archive(
name = "io_bazel_rules_go",
sha256 = "ab21448cef298740765f33a7f5acee0607203e4ea321219f2a4c85a6e0fb0a27",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/archive/refs/tags/v0.32.0.zip",
"https://github.com/bazelbuild/rules_go/archive/refs/tags/v0.32.0.zip",
],
)
# Download Gazelle.
http_archive(
name = "bazel_gazelle",
sha256 = "5982e5463f171da99e3bdaeff8c0f48283a7a5f396ec5282910b9e8a49c0dd7e",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.25.0/bazel-gazelle-v0.25.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.25.0/bazel-gazelle-v0.25.0.tar.gz",
],
)
# Load macros and repository rules.
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
# Declare Go direct dependencies.
go_repository(
name = "org_golang_x_net",
importpath = "golang.org/x/net",
sum = "h1:zK/HqS5bZxDptfPJNq8v7vJfXtkU7r9TLIoSr1bXaP4=",
version = "v0.0.0-20200813134508-3edf25e44fcc",
)
# Declare indirect dependencies and register toolchains.
go_rules_dependencies()
go_register_toolchains(version = "1.18.2")
gazelle_dependencies() protobuf and gRPCTo generate code from protocol buffers, you'll need to add a dependency on
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_google_protobuf",
sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113",
strip_prefix = "protobuf-3.14.0",
urls = [
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
"https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
],
)
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps() You'll need a C/C++ toolchain registered for the execution platform (the platform where Bazel runs actions) to build protoc. The proto_library rule is provided by the gRPC dependencies are not declared by default (there are too many). You can
declare them in WORKSPACE using go_repository. You may want to use
gazelle update-repos to import them from See Proto dependencies, gRPC dependencies for more information. See also Avoiding conflicts. Once all dependencies have been registered, you can declare proto_library and go_proto_library rules to generate and compile Go code from .proto files. load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
deps = ["//bar:bar_proto"],
visibility = ["//visibility:public"],
)
go_proto_library(
name = "foo_go_proto",
importpath = "github.com/example/protos/foo_proto",
protos = [":foo_proto"],
visibility = ["//visibility:public"],
) A Note that recent versions of rules_go support both APIv1
( FAQGo
Protocol buffers Dependencies and testing
Can I still use the go command?Yes, but not directly. rules_go invokes the Go compiler and linker directly, based on the targets
described with go_binary and other rules. Bazel and rules_go together
fill the same role as the That said, it's usually still a good idea to follow conventions required by
the Does this work with Go modules?Yes, but not directly. Bazel ignores You can download a Go module at a specific version as an external repository using go_repository, a workspace rule provided by gazelle. This will also generate build files using gazelle. You can import go_repository rules from a What's up with the go_default_library name?This was used to keep import paths consistent in libraries that can be built
with In order to compile and link correctly, rules_go must know the Go import path
(the string by which a package can be imported) for each library. This is now
set explicitly with the Since How do I cross-compile?You can cross-compile by setting the $ bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //cmd By default, cgo is disabled when cross-compiling. To cross-compile with cgo,
add a $ bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64_cgo //cmd Platform-specific sources with build tags or filename suffixes are filtered
automatically at compile time. You can selectively include platform-specific
dependencies with 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": [],
}),
) To build a specific go_binary or go_test target for a target platform,
set the How do I access testdata?Bazel executes tests in a sandbox, which means tests don't automatically have
access to files. You must include test files using the go_test(
name = "foo_test",
srcs = ["foo_test.go"],
data = glob(["testdata/**"]),
importpath = "github.com/example/project/foo",
) By default, tests are run in the directory of the build file that defined them.
Note that this follows the Go testing convention, not the Bazel convention
followed by other languages, which run in the repository root. This means
that you can access test files using relative paths. You can change the test
directory using the Gazelle will automatically add a Note that on Windows, data files are not directly available to tests, since test data files rely on symbolic links, and by default, Windows doesn't let unprivileged users create symbolic links. You can use the github.com/bazelbuild/rules_go/go/tools/bazel library to access data files. How do I access go_binary executables from go_test?The location where To depend on an executable in a go_binary(
name = "cmd",
srcs = ["cmd.go"],
)
go_test(
name = "cmd_test",
srcs = ["cmd_test.go"],
args = ["$(location :cmd)"],
data = [":cmd"],
) See //tests/core/cross for a full example of a test that accesses a binary. Alternatively, you can set the go_binary(
name = "cmd",
srcs = ["cmd.go"],
out = "cmd",
)
go_test(
name = "cmd_test",
srcs = ["cmd_test.go"],
data = [":cmd"],
) How do I avoid conflicts with protocol buffers?See Avoiding conflicts in the proto documentation. Can I use a vendored gRPC with go_proto_library?This is not supported. When using go_proto_library with the
If you're using Gazelle with proto rule generation enabled, imports of
If you specifically need to use a vendored gRPC package, it's best to avoid
using How do I use different versions of dependencies?See Overriding dependencies for instructions on overriding repositories declared in go_rules_dependencies. |