├── .gitignore ├── local ├── local.go └── BUILD ├── with_vendor ├── bare.go ├── remote.go └── BUILD ├── vendor └── github.com │ └── laramiel │ └── BUILD ├── hello.go ├── submodule ├── src │ └── BUILD ├── BUILD ├── bare.go └── remote.go ├── bare.go ├── remote.go ├── BUILD ├── .gitmodules ├── WORKSPACE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | bazel-bazel-* 2 | bazel-bin 3 | bazel-genfiles 4 | bazel-out 5 | bazel-testlogs 6 | -------------------------------------------------------------------------------- /local/local.go: -------------------------------------------------------------------------------- 1 | package local 2 | 3 | func World() string { 4 | return "local.World!" 5 | } 6 | -------------------------------------------------------------------------------- /local/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library") 2 | 3 | go_library( 4 | name ="local", 5 | srcs = [ "local.go" ], 6 | visibility = ["//visibility:public"], 7 | ) 8 | -------------------------------------------------------------------------------- /with_vendor/bare.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/laramiel/bazel-example-golang-bare/bare" 7 | ) 8 | 9 | func main() { 10 | fmt.Println("Hello", bare.World()) 11 | } 12 | -------------------------------------------------------------------------------- /with_vendor/remote.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/laramiel/bazel-example-golang-remote/remote" 7 | ) 8 | 9 | func main() { 10 | fmt.Println("Hello", remote.World()) 11 | } 12 | -------------------------------------------------------------------------------- /vendor/github.com/laramiel/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library") 2 | 3 | go_library( 4 | name = "bare", 5 | srcs = ["bazel-example-golang-bare/bare.go"], 6 | visibility = ["//visibility:public"], 7 | ) 8 | -------------------------------------------------------------------------------- /hello.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/laramiel/bazel-example-golang/local/local" 7 | ) 8 | 9 | func main() { 10 | fmt.Println("Hello World") 11 | 12 | fmt.Println("Hello", local.World()) 13 | } 14 | -------------------------------------------------------------------------------- /submodule/src/BUILD: -------------------------------------------------------------------------------- 1 | load("@bazel_tools//tools/build_rules/go:def.bzl", "go_prefix", "go_library") 2 | 3 | go_prefix("github.com/laramiel/bazel-example-golang-bare/") 4 | 5 | go_library( 6 | name = "bare", 7 | srcs = [ "bare/bare.go" ], 8 | visibility = ["//visibility:public"], 9 | ) 10 | -------------------------------------------------------------------------------- /submodule/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_binary") 2 | 3 | package( 4 | default_visibility = ["//visibility:public"], 5 | ) 6 | 7 | go_binary( 8 | name = "bare", 9 | srcs = ["bare.go"], 10 | deps = [ 11 | "//submodule/src:bare", 12 | ], 13 | ) 14 | 15 | go_binary( 16 | name = "remote", 17 | srcs = ["remote.go"], 18 | deps = [ 19 | "//submodule/src/remote", 20 | ], 21 | ) 22 | -------------------------------------------------------------------------------- /bare.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | // HACK ALERT 7 | // The correct import is this: 8 | // "github.com/laramiel/bazel-example-golang-bare/bare" 9 | // But the bazel build rules for external golang packages like git_repository() are busted. 10 | // We hack around it by importing where those external dependencies end up: 11 | "github.com/laramiel/bazel-example-golang/external/ws_bare/bare" 12 | ) 13 | 14 | func main() { 15 | fmt.Println("Hello", bare.World()) 16 | } 17 | -------------------------------------------------------------------------------- /remote.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | // HACK ALERT 7 | // The correct import is this: 8 | // "github.com/laramiel/bazel-example-golang-remote/remote" 9 | // But the bazel build rules for external golang packages like git_repository() are busted. 10 | // We hack around it by importing where those external dependencies end up: 11 | "github.com/laramiel/bazel-example-golang/external/ws_remote/remote" 12 | ) 13 | 14 | func main() { 15 | fmt.Println("Hello ", remote.World()) 16 | } 17 | -------------------------------------------------------------------------------- /submodule/bare.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | // HACK ALERT 7 | // The correct import is this: 8 | // "github.com/laramiel/bazel-example-golang-bare/bare" 9 | // But the bazel build rules for subversion packages don't support alternate 10 | // go_prefix() commands. 11 | // We hack around it by importing where those external dependencies end up: 12 | "github.com/laramiel/bazel-example-golang/submodule/src/bare" 13 | ) 14 | 15 | func main() { 16 | fmt.Println("Hello", bare.World()) 17 | } 18 | -------------------------------------------------------------------------------- /submodule/remote.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | // HACK ALERT 7 | // The correct import is this: 8 | // "github.com/laramiel/bazel-example-golang-remote/remote" 9 | // But the bazel build rules for subversion packages don't support alternate 10 | // go_prefix() commands. 11 | // We hack around it by importing where those dependencies end up: 12 | "github.com/laramiel/bazel-example-golang/submodule/src/remote/remote" 13 | ) 14 | 15 | func main() { 16 | fmt.Println("Hello", remote.World()) 17 | } 18 | -------------------------------------------------------------------------------- /with_vendor/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_binary") 2 | 3 | package( 4 | default_visibility = ["//visibility:public"], 5 | ) 6 | 7 | # BAZEL does not work correctly with BUILD files rooted elsewhere than 8 | # the actual code directory. 9 | # 10 | #go_binary( 11 | # name ="bare", 12 | # srcs = [ "bare.go" ], 13 | # deps = [ 14 | # "//vendor/github.com/laramiel:bare", 15 | # ], 16 | #) 17 | 18 | go_binary( 19 | name = "remote", 20 | srcs = ["remote.go"], 21 | deps = [ 22 | "//vendor/github.com/laramiel/bazel-example-golang-remote:remote", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_prefix", "go_binary") 2 | 3 | package( 4 | default_visibility = ["//visibility:public"], 5 | ) 6 | 7 | go_prefix("github.com/laramiel/bazel-example-golang/") 8 | 9 | go_binary( 10 | name = "hello", 11 | srcs = ["hello.go"], 12 | deps = [ 13 | "//local", 14 | ], 15 | ) 16 | 17 | go_binary( 18 | name = "remote", 19 | srcs = ["remote.go"], 20 | deps = [ 21 | "@ws_remote//:remote", 22 | ], 23 | ) 24 | 25 | go_binary( 26 | name = "bare", 27 | srcs = ["bare.go"], 28 | deps = [ 29 | "@ws_bare//:bare", 30 | ], 31 | ) 32 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "submodule/src/bare"] 2 | path = submodule/src/bare 3 | url = git://github.com/laramiel/bazel-example-golang-bare 4 | [submodule "submodule/src/remote"] 5 | path = submodule/src/remote 6 | url = git://github.com/laramiel/bazel-example-golang-remote 7 | [submodule "vendor/github.com/laramiel/bazel-example-golang-remote"] 8 | path = vendor/github.com/laramiel/bazel-example-golang-remote 9 | url = git://github.com/laramiel/bazel-example-golang-remote 10 | [submodule "vendor/github.com/laramiel/bazel-example-golang-bare"] 11 | path = vendor/github.com/laramiel/bazel-example-golang-bare 12 | url = git://github.com/laramiel/bazel-example-golang-bare 13 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | git_repository( 2 | name = "io_bazel_rules_go", 3 | remote = "https://github.com/bazelbuild/rules_go.git", 4 | commit = "373feb6", 5 | ) 6 | 7 | load("@io_bazel_rules_go//go:def.bzl", "go_repositories") 8 | 9 | go_repositories() 10 | 11 | git_repository( 12 | name = "ws_remote", 13 | remote = "https://github.com/laramiel/bazel-example-golang-remote.git", 14 | commit = "4a9199b", 15 | ) 16 | 17 | 18 | BARE_BUILD = """ 19 | load("@io_bazel_rules_go//go:def.bzl", "go_prefix", "go_library") 20 | 21 | go_prefix("github.com/laramiel/bazel-example-golang-bare") 22 | 23 | go_library( 24 | name = "bare", 25 | srcs = [ "bare.go" ], 26 | visibility = ["//visibility:public"], 27 | ) 28 | 29 | """ 30 | 31 | new_git_repository( 32 | name = "ws_bare", 33 | remote = "https://github.com/laramiel/bazel-example-golang-bare.git", 34 | commit = "3bd848f", 35 | build_file_content = BARE_BUILD 36 | ) 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bazel-example-golang 2 | 3 | An example project demonstrating bazel build rules for go with 4 | a local binary (go_binary) and a local library (go_library). 5 | 6 | This requires bazel release > `bazel-0.20`. 7 | 8 | ``` 9 | $ git clone https://github.com/laramiel/bazel-example-golang.git 10 | $ cd bazel-example-golang 11 | $ git submodule init 12 | $ git submodule foreach git pull origin master 13 | 14 | $ bazel run :hello 15 | 16 | $ bazel run :remote 17 | $ bazel run :bare 18 | 19 | $ git submodule update --init --recursive 20 | $ bazel run //submodule:bare 21 | $ bazel run //submodule:remote 22 | 23 | $ bazel run //with_vendor:remote 24 | $ bazel run //with_vendor:bare 25 | 26 | ``` 27 | 28 | # Explanation 29 | 30 | This repository works in conjunction with other git repositories to 31 | provide a demonstration of Bazel BUILD rules for golang. 32 | 33 | * [bazel-example-golang](https://github.com/laramiel/bazel-example-golang) 34 | The main repository, demonstrating binary, local, and remote repository usage. 35 | 36 | * [bazel-example-golang-remote](https://github.com/laramiel/bazel-example-golang-remote) 37 | An example remote repository which includes Bazel rules. 38 | 39 | * [bazel-example-golang-bare](https://github.com/laramiel/bazel-example-golang-bare) 40 | An example remote repository without any Bazel rules. 41 | 42 | 43 | Each of the remote repositories is linked into `go_binary()` targets in several 44 | ways: 45 | 46 | 1. Using the WORKSPACE file to define `git_repository()` rules. 47 | This accounts for the `:remote` and `:bare` targets. 48 | 49 | 2. Using git submodules to import the repository source, and 50 | using those paths. This accounts for the `//submodule:bare` 51 | and `//submodule:remote` targets. 52 | 53 | 3. Using the VENDOR extension introduced in go1.6. 54 | 55 | --------------------------------------------------------------------------------