From 47182329400e359a7a6ad5857cf1854a4f9ec720 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Fri, 9 Oct 2015 17:56:52 +0000 Subject: [PATCH] Provide an example for the Go rules, eg. bazel test examples/go/lib:lib_test bazel build examples/go/bin:bin -- MOS_MIGRATED_REVID=105070940 --- BUILD | 4 ++++ WORKSPACE | 11 ++++++++++ examples/go/bin/BUILD | 14 +++++++++++++ examples/go/bin/bin.go | 14 +++++++++++++ examples/go/lib/BUILD | 20 +++++++++++++++++++ examples/go/lib/lib.go | 6 ++++++ examples/go/lib/lib_test.go | 11 ++++++++++ .../go/vendor/github_com/user/vendored/BUILD | 12 +++++++++++ .../github_com/user/vendored/vendored.go | 6 ++++++ 9 files changed, 98 insertions(+) create mode 100644 examples/go/bin/BUILD create mode 100644 examples/go/bin/bin.go create mode 100644 examples/go/lib/BUILD create mode 100644 examples/go/lib/lib.go create mode 100644 examples/go/lib/lib_test.go create mode 100644 examples/go/vendor/github_com/user/vendored/BUILD create mode 100644 examples/go/vendor/github_com/user/vendored/vendored.go diff --git a/BUILD b/BUILD index d34269b456e91f..2e1495fca6dfea 100644 --- a/BUILD +++ b/BUILD @@ -9,3 +9,7 @@ filegroup( name = "dummy", visibility = ["//visibility:public"], ) + +load("/tools/build_rules/go/def", "go_prefix") + +go_prefix("github.com/bazelbuild/bazel") diff --git a/WORKSPACE b/WORKSPACE index fc9cb01d95a496..6c6c8d8bd58e29 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -80,3 +80,14 @@ new_http_archive( sha256 = "87494218eea2441a7a24b40f227330877dbba75c5fa9014ac6188711baed53f6", build_file = "tools/build_defs/sass/sassc.BUILD", ) + +bind(name = "go_prefix", + actual = "//:go_prefix", +) + +new_http_archive( + name= "golang-linux-amd64", + url = "https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz", + build_file = "tools/build_rules/go/toolchain/BUILD.go-toolchain", + sha256 = "2593132ca490b9ee17509d65ee2cd078441ff544899f6afb97a03d08c25524e7" +) diff --git a/examples/go/bin/BUILD b/examples/go/bin/BUILD new file mode 100644 index 00000000000000..d6676c401a628d --- /dev/null +++ b/examples/go/bin/BUILD @@ -0,0 +1,14 @@ +package( + default_visibility = ["//visibility:public"], +) + +load("/tools/build_rules/go/def", "go_binary") + +go_binary( + name = "bin", + srcs = ["bin.go"], + deps = [ + "//examples/go/lib:go_default_library", + "//examples/go/vendor/github_com/user/vendored:go_default_library", + ], +) diff --git a/examples/go/bin/bin.go b/examples/go/bin/bin.go new file mode 100644 index 00000000000000..b0c5917145c4b3 --- /dev/null +++ b/examples/go/bin/bin.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + "github_com/user/vendored" + + "github.com/bazelbuild/bazel/examples/go/lib" +) + +func main() { + fmt.Println("meaning: ", lib.Meaning()) + fmt.Println("vendored: ", vendored.Vendored()) +} diff --git a/examples/go/lib/BUILD b/examples/go/lib/BUILD new file mode 100644 index 00000000000000..ac982b114591f3 --- /dev/null +++ b/examples/go/lib/BUILD @@ -0,0 +1,20 @@ +package( + default_visibility = ["//visibility:public"], +) + +load("/tools/build_rules/go/def", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = [ + "lib.go", + ], +) + +go_test( + name = "lib_test", + srcs = [ + "lib_test.go", + ], + library = ":go_default_library", +) diff --git a/examples/go/lib/lib.go b/examples/go/lib/lib.go new file mode 100644 index 00000000000000..f97e3aefce2c10 --- /dev/null +++ b/examples/go/lib/lib.go @@ -0,0 +1,6 @@ +package lib + +// Meaning calculates the meaning of Life, the Universe and Everything. +func Meaning() int { + return 42 +} diff --git a/examples/go/lib/lib_test.go b/examples/go/lib/lib_test.go new file mode 100644 index 00000000000000..e8a9f1b707cb43 --- /dev/null +++ b/examples/go/lib/lib_test.go @@ -0,0 +1,11 @@ +package lib + +import ( + "testing" +) + +func TestMeaning(t *testing.T) { + if m := Meaning(); m != 42 { + t.Errorf("got %d, want 42", m) + } +} diff --git a/examples/go/vendor/github_com/user/vendored/BUILD b/examples/go/vendor/github_com/user/vendored/BUILD new file mode 100644 index 00000000000000..7fa0283e994eec --- /dev/null +++ b/examples/go/vendor/github_com/user/vendored/BUILD @@ -0,0 +1,12 @@ +package( + default_visibility = ["//visibility:public"], +) + +load("/tools/build_rules/go/def", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "vendored.go", + ], +) diff --git a/examples/go/vendor/github_com/user/vendored/vendored.go b/examples/go/vendor/github_com/user/vendored/vendored.go new file mode 100644 index 00000000000000..1e9ee6e7857202 --- /dev/null +++ b/examples/go/vendor/github_com/user/vendored/vendored.go @@ -0,0 +1,6 @@ +package vendored + +// Vendored returns a string. +func Vendored() string { + return "I was vendored" +}