Skip to content

Commit 6d93c1d

Browse files
authored
chore: Make codegen independent from protoc (#1953)
1 parent f074f13 commit 6d93c1d

File tree

8 files changed

+22
-9
lines changed

8 files changed

+22
-9
lines changed

.github/workflows/CI.yml

-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ jobs:
4646
steps:
4747
- uses: actions/checkout@v4
4848
- uses: hecrj/setup-rust-action@v2
49-
- name: Install protoc
50-
uses: taiki-e/install-action@v2
51-
with:
52-
5349
- uses: Swatinem/rust-cache@v2
5450
- run: cargo run --package codegen
5551
- run: git diff --exit-code

CONTRIBUTING.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ example would explicitly use `Timeout::new`. For example:
211211
When making changes to `tonic-build` that affects the generated code you will
212212
need to ensure that each of the sub crates gets updated as well. Each of the sub
213213
crates like, for example `tonic-health`, generate their gRPC code via `codegen`
214-
crate. This requires `Protocol Buffers Compiler` of which version is same as the
215-
one used in the GitHub Action (see [`codegen` job](./.github/workflows/CI.yml)).
214+
crate.
216215

217216
```
218217
cargo run --package codegen

codegen/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ version = "0.1.0"
88

99
[dependencies]
1010
tempfile = "3.8.0"
11+
protox = "0.7"
1112
tonic-build = {path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"]}

codegen/src/main.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
use std::path::{Path, PathBuf};
1+
use std::{
2+
fs::File,
3+
io::{BufWriter, Write as _},
4+
path::{Path, PathBuf},
5+
};
6+
7+
use protox::prost::{bytes::BytesMut, Message as _};
8+
use tonic_build::FileDescriptorSet;
29

310
fn main() {
411
// tonic-health
@@ -82,12 +89,15 @@ fn codegen(
8289
let out_dir = root_dir.join(out_dir);
8390
let file_descriptor_set_path = root_dir.join(file_descriptor_set_path);
8491

92+
let fds = protox::compile(&iface_files, &include_dirs).unwrap();
93+
94+
write_fds(&fds, &file_descriptor_set_path);
95+
8596
tonic_build::configure()
8697
.build_client(build_client)
8798
.build_server(build_server)
8899
.out_dir(&tempdir)
89-
.file_descriptor_set_path(file_descriptor_set_path)
90-
.compile_protos(&iface_files, &include_dirs)
100+
.compile_fds(fds)
91101
.unwrap();
92102

93103
for path in std::fs::read_dir(tempdir.path()).unwrap() {
@@ -105,3 +115,10 @@ fn codegen(
105115
std::fs::copy(&path, &to).unwrap();
106116
}
107117
}
118+
119+
fn write_fds(fds: &FileDescriptorSet, path: &Path) {
120+
let mut writer = BufWriter::new(File::create(path).unwrap());
121+
let mut buf = BytesMut::with_capacity(fds.encoded_len());
122+
fds.encode(&mut buf).unwrap();
123+
writer.write_all(&buf).unwrap();
124+
}
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

tonic-types/src/generated/types.bin

-234 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)