Skip to content

feat(grpc): Add protobuf codegen #2320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ jobs:
with:
components: clippy
- uses: taiki-e/install-action@protoc
- name: Install Bazel
uses: bazel-contrib/[email protected]
with:
# Avoid downloading Bazel every time.
bazelisk-cache: true
# Store build cache per workflow.
disk-cache: ${{ github.workflow }}
# Share repository cache between workflows.
repository-cache: true
module-root: ./compiler
- name: Build and protoc plugin and add to PATH
id: build_step
# This runs all commands within the compiler/ directory
working-directory: ./compiler
run: |
bazel build //src:protoc-gen-rust-grpc

# Add the output directory to the GitHub PATH for subsequent steps
echo "$(pwd)/bazel-bin/src" >> $GITHUB_PATH
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --workspace --all-features --all-targets

Expand All @@ -55,6 +74,25 @@ jobs:
- uses: taiki-e/install-action@cargo-hack
- uses: taiki-e/install-action@cargo-udeps
- uses: taiki-e/install-action@protoc
- name: Install Bazel
uses: bazel-contrib/[email protected]
with:
# Avoid downloading Bazel every time.
bazelisk-cache: true
# Store build cache per workflow.
disk-cache: ${{ github.workflow }}
# Share repository cache between workflows.
repository-cache: true
module-root: ./compiler
- name: Build and protoc plugin and add to PATH
id: build_step
# This runs all commands within the compiler/ directory
working-directory: ./compiler
run: |
bazel build //src:protoc-gen-rust-grpc

# Add the output directory to the GitHub PATH for subsequent steps
echo "$(pwd)/bazel-bin/src" >> $GITHUB_PATH
- uses: Swatinem/rust-cache@v2
- run: cargo hack udeps --workspace --exclude-features=_tls-any,tls,tls-aws-lc,tls-ring --each-feature
- run: cargo udeps --package tonic --features tls-ring,transport
Expand All @@ -76,6 +114,25 @@ jobs:
- uses: hecrj/setup-rust-action@v2
- uses: taiki-e/install-action@cargo-hack
- uses: taiki-e/install-action@protoc
- name: Install Bazel
uses: bazel-contrib/[email protected]
with:
# Avoid downloading Bazel every time.
bazelisk-cache: true
# Store build cache per workflow.
disk-cache: ${{ github.workflow }}
# Share repository cache between workflows.
repository-cache: true
module-root: ./compiler
- name: Build and protoc plugin and add to PATH
id: build_step
# This runs all commands within the compiler/ directory
working-directory: ./compiler
run: |
bazel build //src:protoc-gen-rust-grpc

# Add the output directory to the GitHub PATH for subsequent steps
echo "$(pwd)/bazel-bin/src" >> $GITHUB_PATH
- uses: Swatinem/rust-cache@v2
- name: Check features
run: cargo hack check --workspace --no-private --each-feature --no-dev-deps
Expand Down Expand Up @@ -115,6 +172,25 @@ jobs:
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
- uses: taiki-e/install-action@protoc
- name: Install Bazel
uses: bazel-contrib/[email protected]
with:
# Avoid downloading Bazel every time.
bazelisk-cache: true
# Store build cache per workflow.
disk-cache: ${{ github.workflow }}
# Share repository cache between workflows.
repository-cache: true
module-root: ./compiler
- name: Build and protoc plugin and add to PATH
id: build_step
# This runs all commands within the compiler/ directory
working-directory: ./compiler
run: |
bazel build //src:protoc-gen-rust-grpc

# Add the output directory to the GitHub PATH for subsequent steps
echo "$(pwd)/bazel-bin/src" >> $GITHUB_PATH
- uses: taiki-e/install-action@cargo-hack
- uses: taiki-e/install-action@cargo-nextest
- uses: Swatinem/rust-cache@v2
Expand All @@ -141,6 +217,25 @@ jobs:
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
- uses: taiki-e/install-action@protoc
- name: Install Bazel
uses: bazel-contrib/[email protected]
with:
# Avoid downloading Bazel every time.
bazelisk-cache: true
# Store build cache per workflow.
disk-cache: ${{ github.workflow }}
# Share repository cache between workflows.
repository-cache: true
module-root: ./compiler
- name: Build and protoc plugin and add to PATH
id: build_step
# This runs all commands within the compiler/ directory
working-directory: ./compiler
run: |
bazel build //src:protoc-gen-rust-grpc

# Add the output directory to the GitHub PATH for subsequent steps
echo "$(pwd)/bazel-bin/src" >> $GITHUB_PATH
- uses: Swatinem/rust-cache@v2
- name: Run interop tests
run: ./interop/test.sh
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"examples",
"codegen",
"grpc",
"grpc-build",
"interop", # Tests
"tests/disable_comments",
"tests/included_service",
Expand Down
9 changes: 9 additions & 0 deletions compiler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Bazel
bazel-bin
bazel-examples
bazel-genfiles
bazel-grpc-java
bazel-out
bazel-testlogs
MODULE.bazel.lock

15 changes: 15 additions & 0 deletions compiler/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bazel_dep(name = "protobuf", repo_name = "com_google_protobuf", version = "31.1")

# Hedron's Compile Commands Extractor for bazel
# This is used to generate a compile_commands.json file which can be used by
# LSP servers like clangd.
# https://github.com/hedronvision/bazel-compile-commands-extractor
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
git_override(
module_name = "hedron_compile_commands",
# Using a commit from a fork to workaround failures while using absl.
# TODO: replace with a commit on the official repo once the following PR is
# merged: https://github.com/hedronvision/bazel-compile-commands-extractor/pull/219
remote = "https://github.com/mikael-s-persson/bazel-compile-commands-extractor",
commit = "f5fbd4cee671d8d908f37c83abaf70fba5928fc7"
)
29 changes: 29 additions & 0 deletions compiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Usage example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename this containg folder? Compiler seems quite ambiguous?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is. I was using the same name as Java and the core repo. I've changed it to the name of protoc plugin, protoc-gen-rust-grpc now.

```sh
# Build the plugin with Bazel
bazel build //src:protoc-gen-rust-grpc

# Set the plugin path
PLUGIN_PATH="$(pwd)/bazel-bin/src/protoc-gen-rust-grpc"

# Run protoc with the Rust and gRPC plugins
protoc \
--plugin=protoc-gen-grpc-rust="$PLUGIN_PATH" \
--rust_opt="experimental-codegen=enabled,kernel=upb" \
--rust_out=./tmp \
--rust-grpc_opt="experimental-codegen=enabled" \
--rust-grpc_out=./tmp \
routeguide.proto
```

## Build
```sh
bazel build //src:protoc-gen-rust-grpc
```

## Language Server Support for development
Generate compile_commands.json using bazel plugin. Configure the language
server to use the generate json file.
```sh
bazel run @hedron_compile_commands//:refresh_all
```
13 changes: 13 additions & 0 deletions compiler/src/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cc_binary(
name = "protoc-gen-rust-grpc",
srcs = [
"grpc_rust_plugin.cc",
"grpc_rust_generator.h",
"grpc_rust_generator.cc",
],
visibility = ["//visibility:public"],
deps = [
"@com_google_protobuf//:protoc_lib",
],
)

Loading
Loading