Skip to content

Commit

Permalink
Add generated rust code
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
duskmoon314 committed Aug 18, 2024
1 parent 62a9bd6 commit 594405b
Show file tree
Hide file tree
Showing 15 changed files with 4,808 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ build
dist
*.egg-info
.eggs

# rust
target/
9 changes: 9 additions & 0 deletions CI/check_codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pushd "$THIS_DIR/.." >/dev/null

rm -rf go/*
rm -rf py/p4
rm -rf rust/src
./codegen/update.sh

diff="$(git status --porcelain go go.mod go.sum)"
Expand All @@ -26,4 +27,12 @@ if [ ! -z "$diff" ]; then
exit 1
fi

diff="$(git status --porcelain rust)"

if [ ! -z "$diff" ]; then
echo "The generated Rust files are not up-to-date"
echo "You can regenerate them with './codegen/update.sh' and commit the changes"
exit 1
fi

popd >/dev/null
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ processes.
approve.

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

## Use generated P4Runtime library

Expand Down Expand Up @@ -177,6 +178,16 @@ pip3 install p4runtime
pip3 install p4runtime==1.3.0
```

### Rust

To include the P4Runtime Rust crate to your project, add this repository url to
your `Cargo.toml` file:

```toml
[dependencies]
p4runtime = { git = "https://github.com/p4lang/p4runtime.git" }
```

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

P4Runtime generally follows "Live at Head" development principles - new
Expand Down
8 changes: 7 additions & 1 deletion codegen/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LABEL description="Dockerfile used for CI testing of p4lang/p4runtime"
ARG DEBIAN_FRONTEND=noninteractive

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

ARG GO_VERSION=1.20.5

Expand All @@ -26,5 +26,11 @@ ENV PATH="${PATH}:/usr/local/go/bin:/root/go/bin"
RUN go install google.golang.org/protobuf/cmd/[email protected]
RUN go install google.golang.org/grpc/cmd/[email protected]

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal; \
. $HOME/.cargo/env; \
cargo install protoc-gen-prost protoc-gen-prost-crate protoc-gen-prost-serde protoc-gen-tonic

ENV PATH="${PATH}:/root/.cargo/bin"

COPY . /p4runtime/
WORKDIR /p4runtime/
11 changes: 11 additions & 0 deletions codegen/compile_protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mkdir -p "$BUILD_DIR/cpp_out"
mkdir -p "$BUILD_DIR/grpc_out"
mkdir -p "$BUILD_DIR/py_out"
mkdir -p "$BUILD_DIR/go_out"
mkdir -p "$BUILD_DIR/rust_out"

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

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

$PROTOC $PROTOS $PROTOFLAGS \
--prost_out="$BUILD_DIR/rust_out/src" \
--prost_opt=compile_well_known_types \
--prost_opt=extern_path=.google.protobuf=::pbjson_types \
--tonic_out="$BUILD_DIR/rust_out/src" \
--tonic_opt=compile_well_known_types \
--tonic_opt=extern_path=.google.protobuf=::pbjson_types \
--prost-crate_out="$BUILD_DIR/rust_out" \
--prost-crate_opt="gen_crate=rust/Cargo.toml"

set +o xtrace

rm -rf "$tmpdir"
Expand Down
3 changes: 3 additions & 0 deletions codegen/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ cp -r "$tmpdir"/go_out/github.com/p4lang/p4runtime/go/* go/
cp -r "$tmpdir"/py_out/p4 py/
find py/p4 -type d -exec touch {}/__init__.py \;

# Rust
cp -r "$tmpdir"/rust_out/* rust/

# Cleanup files owned by root user
docker run --rm \
-v "$tmpdir:/tmp/gen" \
Expand Down
Loading

0 comments on commit 594405b

Please sign in to comment.