Skip to content

Commit f50fef9

Browse files
authoredSep 13, 2024
Add generated rust code (p4lang#483)
The rust code is generated using the [`neoeinstein/protoc-gen-prost`] (https://github.com/neoeinstein/protoc-gen-prost), which leverages the `prost` crate for protobuf and `tonic` crate for client/server. The rust crates used are: pbjson and pbjson-types: 0.7.0 prost: 0.13.1 tonic: 0.12.0 Currently, the generated code does not use `protoc-prost-serde` to support serde, since serde and json deserialization is not necessary for the basic use case. Signed-off-by: Campbell He <[email protected]>
1 parent c143633 commit f50fef9

15 files changed

+4874
-5
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ build
77
dist
88
*.egg-info
99
.eggs
10+
11+
# rust
12+
target/

‎CI/check_codegen.sh

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pushd "$THIS_DIR/.." >/dev/null
88

99
rm -rf go/*
1010
rm -rf py/p4
11+
rm -rf rust/src
1112
./codegen/update.sh
1213

1314
diff="$(git status --porcelain go go.mod go.sum)"
@@ -26,4 +27,14 @@ if [ ! -z "$diff" ]; then
2627
exit 1
2728
fi
2829

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+
2940
popd >/dev/null

‎README.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,11 @@ processes.
145145
approve.
146146

147147
When updating the Protobuf files in a pull request, you will also need to update
148-
the generated Go and Python files, which are hosted in this repository under
149-
[go/](go/) and [py/](py/). This can be done easily by running `./codegen/update.sh`,
150-
provided docker is installed and your user is part of the "docker" group
151-
(which means that the `docker` command can be executed without `sudo`).
148+
the generated Go, Python and Rust files, which are hosted in this repository
149+
under [go/](go/), [py/](py/) and [rust/](rust/). This can be done easily by
150+
running `./codegen/update.sh`, provided docker is installed and your user is
151+
part of the "docker" group (which means that the `docker` command can be
152+
executed without `sudo`).
152153

153154
## Use generated P4Runtime library
154155

@@ -177,6 +178,16 @@ pip3 install p4runtime
177178
pip3 install p4runtime==1.3.0
178179
```
179180

181+
### Rust
182+
183+
To include the P4Runtime Rust crate to your project, add this repository url to
184+
your `Cargo.toml` file:
185+
186+
```toml
187+
[dependencies]
188+
p4runtime = { git = "https://github.com/p4lang/p4runtime.git" }
189+
```
190+
180191
## Guidelines for using Protocol Buffers (protobuf) in backwards-compatible ways
181192

182193
P4Runtime generally follows "Live at Head" development principles - new

‎codegen/Dockerfile

+7-1
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
9+
apt-get install -y --no-install-recommends software-properties-common git curl build-essential
1010

1111
ARG GO_VERSION=1.20.5
1212

@@ -26,5 +26,11 @@ 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 stable --profile minimal; \
30+
. $HOME/.cargo/env; \
31+
cargo install protoc-gen-prost protoc-gen-prost-crate protoc-gen-prost-serde protoc-gen-tonic
32+
33+
ENV PATH="${PATH}:/root/.cargo/bin"
34+
2935
COPY . /p4runtime/
3036
WORKDIR /p4runtime/

‎codegen/compile_protos.sh

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mkdir -p "$BUILD_DIR/cpp_out"
5353
mkdir -p "$BUILD_DIR/grpc_out"
5454
mkdir -p "$BUILD_DIR/py_out"
5555
mkdir -p "$BUILD_DIR/go_out"
56+
mkdir -p "$BUILD_DIR/rust_out"
5657

5758
set -o xtrace
5859
$PROTOC $PROTOS --cpp_out "$BUILD_DIR/cpp_out" $PROTOFLAGS
@@ -65,6 +66,16 @@ $PROTOC $PROTOS --python_out "$BUILD_DIR/py_out" $PROTOFLAGS --grpc_out "$BUILD_
6566

6667
$PROTOC $PROTOS --go_out="$BUILD_DIR/go_out" --go-grpc_out="$BUILD_DIR/go_out" $PROTOFLAGS
6768

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+
6879
set +o xtrace
6980

7081
rm -rf "$tmpdir"

‎codegen/update.sh

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ 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+
2427
# Cleanup files owned by root user
2528
docker run --rm \
2629
-v "$tmpdir:/tmp/gen" \

0 commit comments

Comments
 (0)