Skip to content

Commit b2fc13d

Browse files
committed
Fix the Bazel build.
Uses `gazelle` to generate the needed changes to import new dependencies. This is following https://www.tweag.io/blog/2021-09-08-rules_go-gazelle/ tutorial. At some later point, we might want to update the go toolchain, gazelle version, bazel version, etc. Also added comments so that future changes to dependencies / code that gets builds would result in easy to (auto-)generate `BUILD` file changes.
1 parent 5e6c275 commit b2fc13d

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

BUILD

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
2+
load("@bazel_gazelle//:def.bzl", "gazelle")
23

4+
# Run `bazel run //:gazelle` each time you add a new directory.
5+
# As a result, `BUILD` files will be generated for you within that directory.
6+
# gazelle:prefix github.com/slsa-framework/example-package
7+
gazelle(name = "gazelle")
8+
9+
# Run `bazel run //:gazelle-update-repos` each time `go.mod` changes (e.g., you
10+
# add a new dependency). This would result in Bazel workspace dependencies also
11+
# being brought up to date.
12+
gazelle(
13+
name = "gazelle-update-repos",
14+
args = [
15+
"-from_file=go.mod",
16+
"-to_macro=deps.bzl%go_dependencies",
17+
"-prune",
18+
],
19+
command = "update-repos",
20+
)
21+
22+
# Existing rules (you can write your own or use the ones generated by `gazelle`).
323
go_binary(
424
name = "hello",
25+
embed = [":example-package_lib"],
26+
visibility = ["//visibility:public"],
27+
)
28+
29+
go_library(
30+
name = "example-package_lib",
531
srcs = ["main.go"],
32+
importpath = "github.com/slsa-framework/example-package",
33+
visibility = ["//visibility:private"],
34+
deps = ["@com_github_pborman_uuid//:uuid"],
635
)

WORKSPACE

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
workspace(name = "com_github_slsa_framework_example_package")
2+
13
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
24

35
http_archive(
@@ -9,8 +11,21 @@ http_archive(
911
],
1012
)
1113

14+
http_archive(
15+
name = "bazel_gazelle",
16+
sha256 = "62ca106be173579c0a167deb23358fdfe71ffa1e4cfdddf5582af26520f1c66f",
17+
url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz",
18+
)
19+
1220
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
21+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
22+
load("//:deps.bzl", "go_dependencies")
23+
24+
# gazelle:repository_macro deps.bzl%go_dependencies
25+
go_dependencies()
1326

1427
go_rules_dependencies()
1528

1629
go_register_toolchains(version = "1.16")
30+
31+
gazelle_dependencies()

deps.bzl

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@bazel_gazelle//:deps.bzl", "go_repository")
2+
3+
def go_dependencies():
4+
go_repository(
5+
name = "com_github_google_uuid",
6+
importpath = "github.com/google/uuid",
7+
sum = "h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=",
8+
version = "v1.0.0",
9+
)
10+
go_repository(
11+
name = "com_github_pborman_uuid",
12+
importpath = "github.com/pborman/uuid",
13+
sum = "h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=",
14+
version = "v1.2.1",
15+
)

e2e/go/BUILD.bazel

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
2+
3+
go_library(
4+
name = "go_lib",
5+
srcs = ["main.go"],
6+
importpath = "github.com/slsa-framework/example-package/e2e/go",
7+
visibility = ["//visibility:private"],
8+
deps = ["@com_github_pborman_uuid//:uuid"],
9+
)
10+
11+
go_binary(
12+
name = "go",
13+
embed = [":go_lib"],
14+
visibility = ["//visibility:public"],
15+
)

0 commit comments

Comments
 (0)