Skip to content

Commit

Permalink
Add support for Bzlmod (fixes #515).
Browse files Browse the repository at this point in the history
Specifically, this allows P4Runtime users to use Bzlmod.
I am not yet enabling Blzmod for P4Runtime itself. I plan to do so in a follow up PR.

Signed-off-by: Steffen Smolka <[email protected]>
  • Loading branch information
smolkaj committed Dec 18, 2024
1 parent 1b44d49 commit 16720ef
Show file tree
Hide file tree
Showing 12 changed files with 826 additions and 1 deletion.
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 instead of WORKSPACE.
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",
)
727 changes: 727 additions & 0 deletions bazel/example/using-bzlmod/MODULE.bazel.lock

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
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();
}
34 changes: 34 additions & 0 deletions proto/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module(
name = "p4runtime",
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 16720ef

Please sign in to comment.