Skip to content

Commit

Permalink
Add support for Bzlmod (fixes #515).
Browse files Browse the repository at this point in the history
Signed-off-by: Steffen Smolka <[email protected]>
  • Loading branch information
smolkaj committed Dec 18, 2024
1 parent 1b44d49 commit 47b1c5d
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 13 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/ci-build-proto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: "build protobufs"

on:
push:
branches:
branches:
- main
- '*-dev'
pull_request:
branches:
branches:
- main
- '*-dev'
schedule:
Expand All @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v2
with:
submodules: recursive

- name: Mount bazel cache
uses: actions/cache@v2
with:
Expand All @@ -42,5 +42,8 @@ jobs:
- name: Build proto/
run: cd proto && bazel build //... && bazel test //...

- name: Build bazel/example/
run: cd bazel/example/ && bazel build //...
- name: Build bazel/example/using-workspace/
run: cd bazel/example/using-workspace && bazel build //...

- name: Build bazel/example/using-bzlmod/
run: cd bazel/example/using-bzlmod && bazel build //...
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Emacs
*~
docs/v1/build/
bazel-*
.DS_Store
build
dist
*.egg-info
.eggs

# rust
# Rust
target/

# Bazel
bazel-*
*.lock
6 changes: 6 additions & 0 deletions bazel/example/using-bzlmod/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Use Bzlmod (`MODULE.bazel`) instead of `WORKSPACE.bazel`.
common --enable_bzlmod
common --noenable_workspace

# C++14 required for recent gRPC versions
build --cxxopt='-std=c++14'
File renamed without changes.
24 changes: 24 additions & 0 deletions bazel/example/using-bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
bazel_dep(
name = "p4runtime",
repo_name = "com_github_p4lang_p4runtime",
)

# In your own project, you will likely want to use `http_archive` instead
# of `local_repository` to load p4runtime.
local_path_override(
module_name = "p4runtime",
path = "../../../proto",
)

# git_override(
# module_name = "p4runtime",
# strip_prefix = "p4runtime-1.4.1/proto",
# urls = ["https://github.com/p4lang/p4runtime/archive/v1.4.1.tar.gz"],
# # sha256 = "<insert hash value here>",
# )

bazel_dep(
name = "protobuf",
version = "29.1",
repo_name = "com_google_protobuf",
)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Flip when we add Bzlmod support (#518).
# Use `WORKSPACE.bazel` instead of bzlmod (`MODULE.bazel`).
common --enable_workspace
common --noenable_bzlmod

Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions bazel/example/using-workspace/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cc_binary(
name = "hello_p4runtime",
srcs = ["hello_p4runtime.cc"],
deps = [
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_google_protobuf//:protobuf",
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# of `local_repository` to load p4runtime.
local_repository(
name = "com_github_p4lang_p4runtime",
path = "../../proto",
path = "../../../proto",
)
# http_archive(
# name = "com_github_p4lang_p4runtime",
Expand Down
21 changes: 21 additions & 0 deletions bazel/example/using-workspace/hello_p4runtime.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

#include "google/protobuf/text_format.h"
#include "p4/config/v1/p4info.pb.h"

using ::google::protobuf::TextFormat;
using ::p4::config::v1::P4Info;

int main() {
P4Info p4info;
TextFormat::ParseFromString(R"PROTO(
tables {
preamble {
id: 10
name: "Hello, P4Runtime!"
}
}
)PROTO", &p4info);
p4info.mutable_tables()->at(0).mutable_preamble()->set_id(42);
std::cout << p4info.DebugString();
}
6 changes: 3 additions & 3 deletions proto/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flip when we add Bzlmod support (#518).
common --enable_workspace
common --noenable_bzlmod
# Use Bzlmod.
common --enable_bzlmod
common --noenable_workspace

# C++14 required for recent gRPC versions
build --cxxopt='-std=c++14'
1 change: 0 additions & 1 deletion proto/.bazelversion

This file was deleted.

35 changes: 35 additions & 0 deletions proto/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module(
name = "p4runtime",
bazel_compatibility = [">=7.4.1"],
repo_name = "com_github_p4lang_p4runtime",
)

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(
name = "googleapis",
version = "0.0.0-20240819-fe8ba054a",
repo_name = "com_google_googleapis",
)

switched_rules = use_extension("@com_google_googleapis//:extensions.bzl", "switched_rules")
switched_rules.use_languages(
cc = True,
go = True,
grpc = True,
python = True,
)
use_repo(switched_rules, "com_google_googleapis_imports")

bazel_dep(name = "grpc", version = "1.68.0", repo_name = "com_github_grpc_grpc")
bazel_dep(
name = "protobuf",
version = "29.1",
repo_name = "com_google_protobuf",
)
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_proto", version = "7.0.2")
bazel_dep(
name = "rules_go",
version = "0.50.1",
repo_name = "io_bazel_rules_go",
)
5 changes: 5 additions & 0 deletions proto/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# https://bazel.build/external/migration#workspace.bzlmod
#
# This file is intentionally empty. When bzlmod is enabled and this
# file exists, the contents of WORKSPACE.bazel is ignored. This prevents
# bzlmod builds from unintentionally depending on the WORKSPACE.bazel file.

0 comments on commit 47b1c5d

Please sign in to comment.