Skip to content

Commit

Permalink
Update build to support Bazel 8/bzlmod (#192)
Browse files Browse the repository at this point in the history
* Update GNMI to support Bazel 8

* Mark gazelle overrides as dev dependencies

* Fix comment position

* Set version number in MODULE.bazel.

* Update GitHub actions to trigger properly and add go/linter actions.

* Add bazel test and remove go/staticcheck analysis for now.

* Keep bazel build cache.

* Fix syntax error with bazel cache
  • Loading branch information
bstoll authored Jan 29, 2025
1 parent d34ac37 commit d4a0a6c
Show file tree
Hide file tree
Showing 71 changed files with 4,471 additions and 4,469 deletions.
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.0.0
40 changes: 17 additions & 23 deletions .github/workflows/ci-cpp-build-gnmi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ name: "bazel build"

on:
push:
branches: [ main ]
branches: [ master ]
pull_request:
branches: [ main ]
schedule:
- cron: "0 0 * * *"

Expand All @@ -14,24 +13,19 @@ jobs:
env:
BAZEL: bazelisk-linux-amd64
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Mount bazel cache
uses: actions/cache@v2
with:
# See https://docs.bazel.build/versions/master/output_directories.html
path: "~/.cache/bazel"
# Create a new cache entry whenever Bazel files change.
# See https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows
key: bazel-${{ runner.os }}-build-${{ hashFiles('**/*.bzl', '**/*.bazel') }}
restore-keys: |
bazel-${{ runner.os }}-build-
- name: Install bazelisk
run: |
curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.8.1/$BAZEL"
chmod +x $BAZEL
sudo mv $BAZEL /usr/local/bin/bazel
- name: Build
run: bazel build //...

- uses: actions/checkout@v4
with:
submodules: recursive
- name: Mount bazel cache
uses: actions/cache@v2
with:
path: "~/.cache/bazel"
key: bazel-${{ runner.os }}-build-${{ hashFiles('**/*.bzl', '**/*.bazel') }}
restore-keys: |
bazel-${{ runner.os }}-build-
- name: Setup Bazel
uses: bazelbuild/setup-bazelisk@v3
- name: Build
run: bazel build //...
- name: Test
run: bazel test //...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
22 changes: 22 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,31 @@
#
# Supporting infrastructure for implementing and testing PINS.

load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_test")

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

exports_files(["LICENSE"])

# gazelle:go_grpc_compilers @io_bazel_rules_go//proto:go_grpc_v2, @io_bazel_rules_go//proto:go_proto
# gazelle:proto_import_prefix github.com/openconfig/gnmi
# gazelle:prefix github.com/openconfig/gnmi
gazelle(name = "gazelle")

gazelle(
name = "gazelle_update_repos",
args = [
"-from_file=go.mod",
"-to_macro=gnmi_go_deps.bzl%gnmi_go_deps",
"-prune",
],
command = "update-repos",
)

gazelle_test(
name = "gazelle_test",
workspace = "//:BUILD.bazel",
)
35 changes: 35 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module(
name = "openconfig_gnmi",
version = "0.13.0",
)

bazel_dep(name = "gazelle", version = "0.41.0", repo_name = "bazel_gazelle")
bazel_dep(name = "grpc", version = "1.69.0", repo_name = "com_github_grpc_grpc")
bazel_dep(name = "protobuf", version = "29.3", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_go", version = "0.51.0", repo_name = "io_bazel_rules_go")

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.23.4")

go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(go_deps, "com_github_cenkalti_backoff_v4", "com_github_golang_glog", "com_github_google_go_cmp", "com_github_kylelemons_godebug", "com_github_openconfig_grpctunnel", "com_github_openconfig_ygot", "com_github_protocolbuffers_txtpbfmt", "org_bitbucket_creachadair_stringset", "org_golang_google_grpc", "org_golang_google_grpc_cmd_protoc_gen_go_grpc", "org_golang_google_protobuf", "org_golang_x_crypto", "org_golang_x_net")

go_deps_dev = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
go_deps_dev.gazelle_override(
directives = [
"gazelle:proto disable", # keep
"gazelle:resolve go github.com/openconfig/gnmi/errlist @openconfig_gnmi//errlist:errlist", # keep
],
path = "github.com/openconfig/grpctunnel",
)
go_deps_dev.gazelle_override(
directives = [
"gazelle:resolve go github.com/openconfig/gnmi/proto/gnmi @openconfig_gnmi//proto/gnmi:gnmi", # keep
"gazelle:resolve go github.com/openconfig/gnmi/errlist @openconfig_gnmi//errlist:errlist", # keep
"gazelle:resolve go github.com/openconfig/gnmi/value @openconfig_gnmi//value:value", # keep
],
path = "github.com/openconfig/ygot",
)

inject_repo(go_deps, "openconfig_gnmi")
724 changes: 724 additions & 0 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(name = "gnmi")

load("//:gnmi_deps.bzl", "gnmi_deps")

gnmi_deps()

load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")

switched_rules_by_language(
name = "com_google_googleapis_imports",
cc = True,
go = True,
grpc = True,
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("//:gnmi_go_deps.bzl", "gnmi_go_deps")

# gazelle:repository_macro gnmi_go_deps.bzl%gnmi_go_deps
gnmi_go_deps()

go_rules_dependencies()

go_register_toolchains(version = "1.23.4")

gazelle_dependencies()

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

# Required by grpc
load("@build_bazel_apple_support//lib:repositories.bzl", "apple_support_dependencies")
load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies")
load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

apple_rules_dependencies(ignore_version_differences = False)

apple_support_dependencies()
45 changes: 0 additions & 45 deletions WORKSPACE.bazel

This file was deleted.

Empty file added WORKSPACE.bzlmod
Empty file.
18 changes: 18 additions & 0 deletions bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)
24 changes: 24 additions & 0 deletions bazel/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build tools

// Package bazel contains imports to keep go.mod entries for packages that are
// referenced in BUILD files, but not in Go code.
package bazel

import (
_ "google.golang.org/grpc/cmd/protoc-gen-go-grpc" // protoc-gen-go-grpc
_ "google.golang.org/protobuf/cmd/protoc-gen-go" // protoc-gen-go
)
40 changes: 40 additions & 0 deletions cache/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "cache",
srcs = ["cache.go"],
importpath = "github.com/openconfig/gnmi/cache",
visibility = ["//visibility:public"],
deps = [
"//ctree",
"//errlist",
"//latency",
"//metadata",
"//path",
"//proto/gnmi",
"//value",
"@com_github_golang_glog//:glog",
"@org_bitbucket_creachadair_stringset//:stringset",
"@org_golang_google_protobuf//encoding/prototext",
"@org_golang_google_protobuf//proto",
],
)

go_test(
name = "cache_test",
srcs = ["cache_test.go"],
embed = [":cache"],
deps = [
"//ctree",
"//errdiff",
"//latency",
"//metadata",
"//proto/gnmi",
"//value",
"@com_github_google_go_cmp//cmp",
"@com_github_google_go_cmp//cmp/cmpopts",
"@org_golang_google_protobuf//encoding/prototext",
"@org_golang_google_protobuf//proto",
"@org_golang_google_protobuf//testing/protocmp",
],
)
30 changes: 30 additions & 0 deletions cli/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "cli",
srcs = ["cli.go"],
importpath = "github.com/openconfig/gnmi/cli",
visibility = ["//visibility:public"],
deps = [
"//client",
"//ctree",
"//proto/gnmi",
"@com_github_protocolbuffers_txtpbfmt//parser",
"@org_golang_google_protobuf//encoding/prototext",
"@org_golang_google_protobuf//proto",
],
)

go_test(
name = "cli_test",
srcs = ["cli_test.go"],
embed = [":cli"],
deps = [
"//client",
"//client/gnmi",
"//testing/fake/gnmi",
"//testing/fake/proto",
"//testing/fake/testing/grpc/config",
"@org_golang_google_grpc//:grpc",
],
)
8 changes: 4 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func displayOnceResults(ctx context.Context, query client.Query, cfg *Config) er
return fmt.Errorf("client had error while displaying results:\n\t%v", err)
}
displayPeer(c, cfg)
displayWalk(query.Target, c, cfg)
displayWalk(c, cfg)
return nil
}

Expand Down Expand Up @@ -298,7 +298,7 @@ func displayPollingResults(ctx context.Context, query client.Query, cfg *Config)
displayPeer(c, cfg)
header = true
}
displayWalk(query.Target, c, cfg)
displayWalk(c, cfg)
if !cfg.countExhausted {
time.Sleep(cfg.PollingInterval)
}
Expand Down Expand Up @@ -331,7 +331,7 @@ func displayStreamingResults(ctx context.Context, query client.Query, cfg *Confi
case client.Delete:
display(v.Path, v.TS, v.Val)
case client.Sync:
displayWalk(query.Target, c, cfg)
displayWalk(c, cfg)
complete = true
case client.Error:
cfg.Display([]byte(fmt.Sprintf("Error: %v", v)))
Expand All @@ -344,7 +344,7 @@ func displayStreamingResults(ctx context.Context, query client.Query, cfg *Confi
return c.Subscribe(ctx, query, cfg.ClientTypes...)
}

func displayWalk(target string, c *client.CacheClient, cfg *Config) {
func displayWalk(c *client.CacheClient, cfg *Config) {
b := make(pathmap)
c.WalkSorted(func(path []string, _ *ctree.Leaf, value interface{}) error {
switch v := value.(type) {
Expand Down
Loading

0 comments on commit d4a0a6c

Please sign in to comment.