forked from bazelbuild/examples
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resurrect Go rules, in a style compatible with the open-source 'go' t…
…ool.
- Loading branch information
Showing
15 changed files
with
611 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package def | ||
|
||
func PI() float64 { return 3.141596 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package lib | ||
|
||
func M() int { return 42 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package grumble | ||
|
||
func Record() string { return "broken" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.