From da2e3441e1774112ecf44905ba8ef57d1b33c3e0 Mon Sep 17 00:00:00 2001 From: Steffen Smolka Date: Wed, 18 Dec 2024 10:02:21 -0800 Subject: [PATCH] Add support for Bzlmod, Bazel 7/8, Ubuntu 24.04 (fixes #515, fixes #516, fixes #514). (#520) Signed-off-by: Steffen Smolka --- .github/workflows/ci-build-proto.yml | 19 ++++------ .gitignore | 7 +++- bazel/example/.bazelrc | 4 -- bazel/example/.bazelversion | 1 - bazel/example/using-bzlmod/.bazelrc | 7 ++++ bazel/example/{ => using-bzlmod}/BUILD.bazel | 0 bazel/example/using-bzlmod/MODULE.bazel | 24 ++++++++++++ .../{ => using-bzlmod}/hello_p4runtime.cc | 0 bazel/example/using-workspace/.bazelrc | 7 ++++ bazel/example/using-workspace/.bazelversion | 1 + bazel/example/using-workspace/BUILD.bazel | 8 ++++ .../{ => using-workspace}/WORKSPACE.bazel | 2 +- .../using-workspace/hello_p4runtime.cc | 21 ++++++++++ proto/.bazelrc | 9 +++-- proto/.bazelversion | 1 - proto/MODULE.bazel | 38 +++++++++++++++++++ proto/WORKSPACE.bzlmod | 5 +++ 17 files changed, 131 insertions(+), 23 deletions(-) delete mode 100644 bazel/example/.bazelrc delete mode 100644 bazel/example/.bazelversion create mode 100644 bazel/example/using-bzlmod/.bazelrc rename bazel/example/{ => using-bzlmod}/BUILD.bazel (100%) create mode 100644 bazel/example/using-bzlmod/MODULE.bazel rename bazel/example/{ => using-bzlmod}/hello_p4runtime.cc (100%) create mode 100644 bazel/example/using-workspace/.bazelrc create mode 100644 bazel/example/using-workspace/.bazelversion create mode 100644 bazel/example/using-workspace/BUILD.bazel rename bazel/example/{ => using-workspace}/WORKSPACE.bazel (98%) create mode 100644 bazel/example/using-workspace/hello_p4runtime.cc delete mode 100644 proto/.bazelversion create mode 100644 proto/MODULE.bazel create mode 100644 proto/WORKSPACE.bzlmod diff --git a/.github/workflows/ci-build-proto.yml b/.github/workflows/ci-build-proto.yml index 4ab094c7..82736686 100644 --- a/.github/workflows/ci-build-proto.yml +++ b/.github/workflows/ci-build-proto.yml @@ -2,11 +2,11 @@ name: "build protobufs" on: push: - branches: + branches: - main - '*-dev' pull_request: - branches: + branches: - main - '*-dev' schedule: @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@v2 with: submodules: recursive - + - name: Mount bazel cache uses: actions/cache@v2 with: @@ -33,14 +33,11 @@ jobs: restore-keys: | bazel-${{ runner.os }}-build- - - name: Install bazelisk - run: | - curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.15.0/$BAZEL" - chmod +x $BAZEL - sudo mv $BAZEL /usr/local/bin/bazel - - 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 //... diff --git a/.gitignore b/.gitignore index 8af80faf..c2889b78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,15 @@ # Emacs *~ docs/v1/build/ -bazel-* .DS_Store build dist *.egg-info .eggs -# rust +# Rust target/ + +# Bazel +bazel-* +*.lock diff --git a/bazel/example/.bazelrc b/bazel/example/.bazelrc deleted file mode 100644 index be255a80..00000000 --- a/bazel/example/.bazelrc +++ /dev/null @@ -1,4 +0,0 @@ -# bazelrc file - -# C++14 required for recent gRPC versions -build --cxxopt='-std=c++14' diff --git a/bazel/example/.bazelversion b/bazel/example/.bazelversion deleted file mode 100644 index f22d756d..00000000 --- a/bazel/example/.bazelversion +++ /dev/null @@ -1 +0,0 @@ -6.5.0 diff --git a/bazel/example/using-bzlmod/.bazelrc b/bazel/example/using-bzlmod/.bazelrc new file mode 100644 index 00000000..decbefb7 --- /dev/null +++ b/bazel/example/using-bzlmod/.bazelrc @@ -0,0 +1,7 @@ +# Use Bzlmod (`MODULE.bazel`) instead of `WORKSPACE.bazel`. +common --enable_bzlmod +common --noenable_workspace + +# Use C++14 (required for recent gRPC versions). +build --cxxopt=-std=c++14 +build --host_cxxopt=-std=c++14 diff --git a/bazel/example/BUILD.bazel b/bazel/example/using-bzlmod/BUILD.bazel similarity index 100% rename from bazel/example/BUILD.bazel rename to bazel/example/using-bzlmod/BUILD.bazel diff --git a/bazel/example/using-bzlmod/MODULE.bazel b/bazel/example/using-bzlmod/MODULE.bazel new file mode 100644 index 00000000..d6099a81 --- /dev/null +++ b/bazel/example/using-bzlmod/MODULE.bazel @@ -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 = "", +# ) + +bazel_dep( + name = "protobuf", + version = "29.1", + repo_name = "com_google_protobuf", +) diff --git a/bazel/example/hello_p4runtime.cc b/bazel/example/using-bzlmod/hello_p4runtime.cc similarity index 100% rename from bazel/example/hello_p4runtime.cc rename to bazel/example/using-bzlmod/hello_p4runtime.cc diff --git a/bazel/example/using-workspace/.bazelrc b/bazel/example/using-workspace/.bazelrc new file mode 100644 index 00000000..0f20b823 --- /dev/null +++ b/bazel/example/using-workspace/.bazelrc @@ -0,0 +1,7 @@ +# Use `WORKSPACE.bazel` instead of bzlmod (`MODULE.bazel`). +common --enable_workspace +common --noenable_bzlmod + +# Use C++14 (required for recent gRPC versions). +build --cxxopt=-std=c++14 +build --host_cxxopt=-std=c++14 diff --git a/bazel/example/using-workspace/.bazelversion b/bazel/example/using-workspace/.bazelversion new file mode 100644 index 00000000..815da58b --- /dev/null +++ b/bazel/example/using-workspace/.bazelversion @@ -0,0 +1 @@ +7.4.1 diff --git a/bazel/example/using-workspace/BUILD.bazel b/bazel/example/using-workspace/BUILD.bazel new file mode 100644 index 00000000..5c8067b6 --- /dev/null +++ b/bazel/example/using-workspace/BUILD.bazel @@ -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", + ] +) diff --git a/bazel/example/WORKSPACE.bazel b/bazel/example/using-workspace/WORKSPACE.bazel similarity index 98% rename from bazel/example/WORKSPACE.bazel rename to bazel/example/using-workspace/WORKSPACE.bazel index 074d6ed2..35a8ae1b 100644 --- a/bazel/example/WORKSPACE.bazel +++ b/bazel/example/using-workspace/WORKSPACE.bazel @@ -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", diff --git a/bazel/example/using-workspace/hello_p4runtime.cc b/bazel/example/using-workspace/hello_p4runtime.cc new file mode 100644 index 00000000..779c2f3d --- /dev/null +++ b/bazel/example/using-workspace/hello_p4runtime.cc @@ -0,0 +1,21 @@ +#include + +#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(); +} diff --git a/proto/.bazelrc b/proto/.bazelrc index be255a80..decbefb7 100644 --- a/proto/.bazelrc +++ b/proto/.bazelrc @@ -1,4 +1,7 @@ -# bazelrc file +# 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' +# Use C++14 (required for recent gRPC versions). +build --cxxopt=-std=c++14 +build --host_cxxopt=-std=c++14 diff --git a/proto/.bazelversion b/proto/.bazelversion deleted file mode 100644 index f22d756d..00000000 --- a/proto/.bazelversion +++ /dev/null @@ -1 +0,0 @@ -6.5.0 diff --git a/proto/MODULE.bazel b/proto/MODULE.bazel new file mode 100644 index 00000000..9f50aa9f --- /dev/null +++ b/proto/MODULE.bazel @@ -0,0 +1,38 @@ +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", +) +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", +) + +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") diff --git a/proto/WORKSPACE.bzlmod b/proto/WORKSPACE.bzlmod new file mode 100644 index 00000000..203f54c6 --- /dev/null +++ b/proto/WORKSPACE.bzlmod @@ -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.