Correct importpath of the generated go_library (#28)

Put library archives into the right places in the standard layout of Go
workspace by omitting "go_default_library" from their paths.
This lets linker symbols in the final binary output have right values.

It also allows us to specify -X linker flag with the importpath we
expect.
c.f. https://github.com/bazelbuild/rules_go/pull/27#issuecomment-220780509
4 files changed
tree: 607b03c15463328209113e3621e34d33e857ddb1
  1. examples/
  2. go/
  3. AUTHORS
  4. BUILD
  5. CONTRIBUTING.md
  6. CONTRIBUTORS
  7. LICENSE.txt
  8. README.md
  9. WORKSPACE
README.md

Go rules

Overview

The rules should be considered experimental. They support:

  • libraries
  • binaries
  • tests
  • vendoring
  • cgo

They currently do not support (in order of importance):

  • //+build tags
  • auto generated BUILD files.
  • C/C++ interoperation except cgo (swig etc.)
  • race detector
  • coverage
  • test sharding

Setup

  • 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.0.2",
    )
    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. It is strongly recommended that the prefix is not empty.

    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

    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

    go_binary(
        ...
        deps = ["//lib:go_default_library"]
    )
    
  • In this case, import the library as github.com/joe/project/lib.

  • For vendored libraries, you may depend on //lib/vendor/github.com/user/project:go_default_library. Vendored libraries should have BUILD files like normal libraries.

  • To declare a test,

    go_test(
        name = "mytest",
        srcs = ["file_test.go"],
        library = ":go_default_library"
    )
    

FAQ

Can I still use the 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.

Disclaimer

These rules are not supported by Google's Go team.

go_prefix

go_prefix(prefix)

go_library

go_library(name, srcs, deps, data)

cgo_library

cgo_library(name, srcs, copts, linkopts, deps, data)

NOTE

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

go_binary(name, srcs, deps, data)

go_test

go_test(name, srcs, deps, data)