Skip to content

Commit fb3fe8a

Browse files
committed
Redesign the Rust crates.
See rust/README.md for more details. Signed-off-by: Steffen Smolka <steffen.smolka@gmail.com>
1 parent f5187a2 commit fb3fe8a

File tree

24 files changed

+1173
-4206
lines changed

24 files changed

+1173
-4206
lines changed

.github/workflows/ci-build-proto.yml renamed to .github/workflows/bazel-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: "build protobufs"
1+
name: "Bazel build of protobufs"
22

33
on:
44
push:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Ensure google/rpc/status.proto is synced with upstream.
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
check-google-rpc-status-synced:
8+
runs-on: [ubuntu-latest]
9+
steps:
10+
- uses: actions/checkout@v5
11+
- run: ./proto/update_google_rpc_status.sh
12+
- run: |
13+
DIFF=$(git diff proto/google/rpc/status.proto)
14+
if [ -n "$DIFF" ]; then
15+
echo "$DIFF"
16+
echo ""
17+
echo "ERROR: proto/google/rpc/status.proto is not synced with upstream."
18+
echo "Please run ./proto/update_google_rpc_status.sh to fix it."
19+
exit 1
20+
fi
21+
echo "SUCCESS: proto/google/rpc/status.proto is synced with upstream."

.github/workflows/rust-build.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: "Rust build"
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: [ubuntu-latest]
11+
steps:
12+
- uses: actions/checkout@v5
13+
- run: cd rust && cargo build
14+
- run: cd rust && cargo test

CI/check_codegen.sh

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pushd "$THIS_DIR/.." >/dev/null
88

99
rm -rf go/*
1010
rm -rf py/p4
11-
rm -rf rust/src
1211
./codegen/update.sh
1312

1413
diff="$(git status --porcelain go go.mod go.sum)"
@@ -27,14 +26,4 @@ if [ ! -z "$diff" ]; then
2726
exit 1
2827
fi
2928

30-
diff="$(git status --porcelain rust)"
31-
32-
if [ ! -z "$diff" ]; then
33-
echo "The generated Rust files are not up-to-date"
34-
echo "DIFF:"
35-
echo "$diff"
36-
echo "You can regenerate them with './codegen/update.sh' and commit the changes"
37-
exit 1
38-
fi
39-
4029
popd >/dev/null

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,7 @@ pip3 install p4runtime==1.3.0
179179

180180
### Rust
181181

182-
To include the P4Runtime Rust crate to your project, add this repository url to
183-
your `Cargo.toml` file:
184-
185-
```toml
186-
[dependencies]
187-
p4runtime = { git = "https://github.com/p4lang/p4runtime.git" }
188-
```
182+
See the [rust/README.md](rust/README.md).
189183

190184
## Guidelines for using Protocol Buffers (protobuf) in backwards-compatible ways
191185

codegen/Dockerfile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LABEL description="Dockerfile used for CI testing of p4lang/p4runtime"
66
ARG DEBIAN_FRONTEND=noninteractive
77

88
RUN apt-get update && \
9-
apt-get install -y --no-install-recommends software-properties-common git curl build-essential
9+
apt-get install -y --no-install-recommends software-properties-common git curl
1010

1111
ARG GO_VERSION=1.20.5
1212

@@ -26,11 +26,5 @@ ENV PATH="${PATH}:/usr/local/go/bin:/root/go/bin"
2626
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.31
2727
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3
2828

29-
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.81.0 --profile minimal; \
30-
. $HOME/.cargo/env; \
31-
cargo install --locked protoc-gen-prost@0.4.0 protoc-gen-prost-crate@0.4.1 protoc-gen-tonic@0.4.1
32-
33-
ENV PATH="${PATH}:/root/.cargo/bin"
34-
3529
COPY . /p4runtime/
3630
WORKDIR /p4runtime/

codegen/compile_protos.sh

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,18 @@ set -e
3333
THIS_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
3434
PROTO_DIR="$THIS_DIR/../proto"
3535

36-
tmpdir="$(mktemp -d)"
37-
pushd "$tmpdir" > /dev/null
38-
git clone --depth 1 https://github.com/googleapis/googleapis.git
39-
popd > /dev/null
40-
GOOGLE_PROTO_DIR="$tmpdir/googleapis"
41-
4236
PROTOS="\
4337
$PROTO_DIR/p4/v1/p4data.proto \
4438
$PROTO_DIR/p4/v1/p4runtime.proto \
4539
$PROTO_DIR/p4/config/v1/p4info.proto \
46-
$PROTO_DIR/p4/config/v1/p4types.proto \
47-
$GOOGLE_PROTO_DIR/google/rpc/status.proto \
48-
$GOOGLE_PROTO_DIR/google/rpc/code.proto"
40+
$PROTO_DIR/p4/config/v1/p4types.proto"
4941

50-
PROTOFLAGS="-I$GOOGLE_PROTO_DIR -I$PROTO_DIR"
42+
PROTOFLAGS="-I$PROTO_DIR"
5143

5244
mkdir -p "$BUILD_DIR/cpp_out"
5345
mkdir -p "$BUILD_DIR/grpc_out"
5446
mkdir -p "$BUILD_DIR/py_out"
5547
mkdir -p "$BUILD_DIR/go_out"
56-
mkdir -p "$BUILD_DIR/rust_out"
5748

5849
set -o xtrace
5950
$PROTOC $PROTOS --cpp_out "$BUILD_DIR/cpp_out" $PROTOFLAGS
@@ -66,18 +57,4 @@ $PROTOC $PROTOS --python_out "$BUILD_DIR/py_out" $PROTOFLAGS --grpc_out "$BUILD_
6657

6758
$PROTOC $PROTOS --go_out="$BUILD_DIR/go_out" --go-grpc_out="$BUILD_DIR/go_out" $PROTOFLAGS
6859

69-
$PROTOC $PROTOS $PROTOFLAGS \
70-
--prost_out="$BUILD_DIR/rust_out/src" \
71-
--prost_opt=compile_well_known_types \
72-
--prost_opt=extern_path=.google.protobuf=::pbjson_types \
73-
--tonic_out="$BUILD_DIR/rust_out/src" \
74-
--tonic_opt=compile_well_known_types \
75-
--tonic_opt=extern_path=.google.protobuf=::pbjson_types \
76-
--prost-crate_out="$BUILD_DIR/rust_out" \
77-
--prost-crate_opt="gen_crate=rust/Cargo.toml"
78-
79-
set +o xtrace
80-
81-
rm -rf "$tmpdir"
82-
8360
echo "SUCCESS"

codegen/update.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ cp -r "$tmpdir"/go_out/github.com/p4lang/p4runtime/go/* go/
2121
cp -r "$tmpdir"/py_out/p4 py/
2222
find py/p4 -type d -exec touch {}/__init__.py \;
2323

24-
# Rust
25-
cp -r "$tmpdir"/rust_out/* rust/
26-
2724
# Cleanup files owned by root user
2825
docker run --rm \
2926
-v "$tmpdir:/tmp/gen" \

proto/google/rpc/status.proto

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
package google.rpc;
18+
19+
import "google/protobuf/any.proto";
20+
21+
option cc_enable_arenas = true;
22+
option go_package = "google.golang.org/genproto/googleapis/rpc/status;status";
23+
option java_multiple_files = true;
24+
option java_outer_classname = "StatusProto";
25+
option java_package = "com.google.rpc";
26+
option objc_class_prefix = "RPC";
27+
28+
// The `Status` type defines a logical error model that is suitable for
29+
// different programming environments, including REST APIs and RPC APIs. It is
30+
// used by [gRPC](https://github.com/grpc). Each `Status` message contains
31+
// three pieces of data: error code, error message, and error details.
32+
//
33+
// You can find out more about this error model and how to work with it in the
34+
// [API Design Guide](https://cloud.google.com/apis/design/errors).
35+
message Status {
36+
// The status code, which should be an enum value of
37+
// [google.rpc.Code][google.rpc.Code].
38+
int32 code = 1;
39+
40+
// A developer-facing error message, which should be in English. Any
41+
// user-facing error message should be localized and sent in the
42+
// [google.rpc.Status.details][google.rpc.Status.details] field, or localized
43+
// by the client.
44+
string message = 2;
45+
46+
// A list of messages that carry the error details. There is a common set of
47+
// message types for APIs to use.
48+
repeated google.protobuf.Any details = 3;
49+
}

proto/update_google_rpc_status.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Updates proto/google/rpc/status.proto with the latest version from the
4+
# official upstream repository.
5+
#
6+
# The file is very stable and hasn't changed in many years, but we enforce
7+
# freshness of this copy using a GitHub Actions workflow just in case.
8+
9+
set -e
10+
11+
STATUS_PROTO_URL="https://raw.githubusercontent.com/googleapis/googleapis/refs/heads/master/google/rpc/status.proto"
12+
THIS_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
13+
14+
wget "$STATUS_PROTO_URL" -O "$THIS_DIR/google/rpc/status.proto"

0 commit comments

Comments
 (0)