Skip to content

Commit

Permalink
Resurrect Go rules, in a style compatible with the open-source 'go' t…
Browse files Browse the repository at this point in the history
…ool.
  • Loading branch information
hanwen committed Sep 24, 2015
1 parent 621a58e commit 8cb9da8
Show file tree
Hide file tree
Showing 15 changed files with 611 additions and 0 deletions.
7 changes: 7 additions & 0 deletions go/def/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("/tools/go/build_rules/go_rules", "go_binary", "go_prefix", "go_library")

go_library(
name = "go_default_library",
srcs = [ "lib.go" ],
visibility = ["//visibility:public" ],
)
3 changes: 3 additions & 0 deletions go/def/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package def

func PI() float64 { return 3.141596 }
23 changes: 23 additions & 0 deletions go/pkg/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

load("/tools/go/build_rules/go_rules", "go_binary", "go_prefix", "go_library", "go_test")
go_prefix("github.com/bazelbuild/examples")

go_binary(
name = "hello",
srcs = [ "hello.go" ],
deps = [ ":lib",
"//go/def:go_default_library",
"//go/pkg/vendor/golang_org/bradfitz/grumble:go_default_library"
],
)

# non-default library, eg for protobufs.
go_library(
name = "lib",
srcs = [ "lib.go" ])

go_test(
name = "lib_test",
srcs = [ "lib_test.go" ],
library = ":lib",
)
12 changes: 12 additions & 0 deletions go/pkg/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"fmt"
"github.com/bazelbuild/examples/go/pkg/lib"
"github.com/bazelbuild/examples/go/def"
"golang_org/bradfitz/grumble"
)

func main() {
fmt.Println("hello", lib.M(), def.PI(), grumble.Record());
}
3 changes: 3 additions & 0 deletions go/pkg/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package lib

func M() int { return 42 }
12 changes: 12 additions & 0 deletions go/pkg/lib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lib

import (
"testing"

)

func TestM(t *testing.T) {
if M() != 42 {
t.Errorf("got %d want 42", M())
}
}
6 changes: 6 additions & 0 deletions go/pkg/vendor/golang_org/bradfitz/grumble/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("/tools/go/build_rules/go_rules", "go_binary", "go_prefix", "go_library")

go_library(
name = "go_default_library",
srcs = [ "grumble.go" ],
visibility = [ "//visibility:public" ])
3 changes: 3 additions & 0 deletions go/pkg/vendor/golang_org/bradfitz/grumble/grumble.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package grumble

func Record() string { return "broken" }
9 changes: 9 additions & 0 deletions tools/go/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package(default_visibility = ["//visibility:public"])

load("/tools/go/build_rules/go_rules", "go_binary")

go_binary(
name = "generate_test_main",
srcs = ["generate_test_main.go"],
)

81 changes: 81 additions & 0 deletions tools/go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Go rules
--------

The rules should be considered experimental. They support:

* libraries
* binaries
* tests
* vendoring

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

* //+build tags
* multiple target configurations
* C/C++ interoperation (cgo, swig etc.)
* coverage
* race detector


Setup
-----

* Decide on the name of your package, eg. "github.com/joe/project"

* Copy tools/go/toolchain/WORKSPACE.go-toolchain to WORKSPACE

* Add a BUILD file to your the top of your workspace, containing

load("/tools/go/build_rules/go_rules", "go_prefix")
go_prefix("github.com/joe/project")

* For a library "github.com/joe/project/lib", create lib/BUILD, containing

load("/tools/go/build_rules/go_rules", "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. Just make sure your workspace lives under

$GOROOT/src/github.com/joe/project/

and it should work.

# Do I have to specify dependencies twice?

Yes, once in the BUILD file, once in the source file. Bazel does not
examine file contents and is language agnostic, so it cannot infer the
dependencies.

We will shortly provide a tool that can generate and update BUILD
files by analyszing source code; stay tuned.

Disclaimer
----------

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

0 comments on commit 8cb9da8

Please sign in to comment.