commit | 0843ec20abde815c4e80145bb1cfac2fdf1bad1d | [log] [tgz] |
---|---|---|
author | Jay Conrod <jayconrod@gmail.com> | Fri Apr 07 13:31:43 2017 -0400 |
committer | GitHub <noreply@github.com> | Fri Apr 07 13:31:43 2017 -0400 |
tree | 476cc0f45bfe031533d14dc190b679c734a586fc | |
parent | 76c63b5cd0d47c1f2b47ab4953db96c574af1c1d [diff] |
Cherrypicks for 0.3.4 release (#366) Cherrypicked the following changes: * #319 go_repository: add importpath and vcs flags. This allows us to have separate import paths and remote URLs for repositories. * #362 Fetch buildifier from new location. This allows the buildifier zip to be extracted with the new name.
Bazel ≥0.3.1 | linux-x86_64 | ubuntu_15.10-x86_64 | darwin-x86_64 |
---|---|---|---|
The rules should be considered experimental. They support:
They currently do not support (in order of importance):
//+build
comments - see here))Decide on the name of your package, eg. github.com/joe/project
Add the following to your WORKSPACE file:
git_repository( name = "io_bazel_rules_go", remote = "https://github.com/bazelbuild/rules_go.git", tag = "0.3.1", ) load("@io_bazel_rules_go//go:def.bzl", "go_repositories") go_repositories()
Add a BUILD
file to the top of your workspace, declaring the name of your workspace using go_prefix
. This prefix is used for Go‘s “import” statements to refer to packages within your own project, so it’s important to choose a prefix that might match the location that another user might choose to put your code into.
load("@io_bazel_rules_go//go:def.bzl", "go_prefix") go_prefix("github.com/joe/project")
For a library github.com/joe/project/lib
, create lib/BUILD
, containing a single library with the special name “go_default_library.” Using this name tells Bazel to set up the files so it can be imported in .go files as (in this example) github.com/joe/project/lib
.
load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", srcs = ["file.go"] )
Inside your project, you can use this library by declaring a dependency on the full Bazel name (including :go_default_library
), and in the .go files, import it as shown above.
go_binary( ... deps = ["//lib:go_default_library"] )
To declare a test,
go_test( name = "mytest", srcs = ["file_test.go"], library = ":go_default_library" )
For instructions on how to depend on external libraries, see Vendoring.md.
go
tool?Yes, this setup was deliberately chosen to be compatible with the go
tool. Make sure your workspace appears under
$GOROOT/src/github.com/joe/project/
eg.
mkdir -p $GOROOT/src/github.com/joe/ ln -s my/bazel/workspace $GOROOT/src/github.com/joe/project
and it should work.
These rules are not supported by Google's Go team.
go_repositories()
Instantiates external dependencies to Go toolchain in a WORKSPACE. All the other workspace rules and build rules assume that this rule is placed in the WORKSPACE.
go_repository(name, importpath, remote, vcs, commit, tag)
Fetches a remote repository of a Go project, expecting it contains BUILD
files. It is an analogy to git_repository
but it recognizes importpath redirection of Go.
Either importpath
or remote
may be specified. To bypass importpath redirection, specify both remote
and vcs
.
new_go_repository(name, importpath, remote, vcs, commit, tag)
Fetches a remote repository of a Go project and automatically generates BUILD
files in it. It is an analogy to new_git_repository
but it recognizes importpath redirection of Go.
go_prefix(prefix)
go_library(name, srcs, deps, data)
cgo_library(name, srcs, copts, clinkopts, cdeps, deps, data)
srcs
cannot contain pure-Go files, which do not have import "C"
. So you need to define another go_library
when you build a go package with both cgo-enabled and pure-Go sources.
cgo_library( name = "cgo_enabled", srcs = ["cgo-enabled.go", "foo.cc", "bar.S", "baz.a"], ) go_library( name = "go_default_library", srcs = ["pure-go.go"], library = ":cgo_enabled", )
go_binary(name, srcs, deps, data)
go_test(name, srcs, deps, data)
go_proto_library(name, srcs, deps, has_services)