Skip to content

Commit

Permalink
schema: Conditionally enable serde support
Browse files Browse the repository at this point in the history
  • Loading branch information
dflemstr committed Nov 18, 2024
1 parent c900210 commit ce5e5ca
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 7 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@ jobs:
run: cargo build --all --features=tokio
- name: Run tests
run: cargo test --all --features=tokio
build-serde:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Protoc
uses: arduino/setup-protoc@v3
- name: Build
run: cargo build --all --features=serde
- name: Run tests
run: cargo test --all --features=serde
build-serde-tokio:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Protoc
uses: arduino/setup-protoc@v3
- name: Build
run: cargo build --all --features=serde,tokio
- name: Run tests
run: cargo test --all --features=serde,tokio
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pbjson-build = "0.6.2"
pbjson-types = "0.6.0"
prost = "0.12.6"
prost-build = "0.12.6"
prost-types = "0.12.6"
serde = "1.0.211"
serde_yaml = "0.9.34"
thiserror = "1.0.64"
Expand Down
13 changes: 13 additions & 0 deletions crates/layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,16 @@ tracing-appender.workspace = true

[features]
tokio = ["dep:tokio"]
serde = ["tracing-perfetto-sdk-schema/serde"]

[[example]]
name = "native-layer"
required-features = ["serde"]

[[example]]
name = "perfetto-dump-trace"
required-features = ["serde"]

[[example]]
name = "sdk-layer"
required-features = ["serde"]
12 changes: 8 additions & 4 deletions crates/schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ version.workspace = true
edition.workspace = true

[dependencies]
pbjson.workspace = true
pbjson-types.workspace = true
pbjson = { workspace = true, optional = true }
pbjson-types = { workspace = true, optional = true }
prost.workspace = true
serde.workspace = true
prost-types.workspace = true
serde = { workspace = true, optional = true }

[build-dependencies]
anyhow.workspace = true
pbjson-build.workspace = true
pbjson-build = { workspace = true, optional = true }
prost-build.workspace = true

[features]
serde = ["dep:pbjson", "dep:pbjson-types", "dep:pbjson-build", "dep:serde"]
28 changes: 26 additions & 2 deletions crates/schema/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,43 @@ fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed={}", proto_file.display());
}

compile(proto_files, &[proto_root])?;

Ok(())
}

#[cfg(not(feature = "serde"))]
// Compile for `serde`: emit a file descriptor set, substitute `prost_types` for
// `pbjson_types`, and use `pbjson_build` to generate serde traits.
fn compile(
proto_files: &[impl AsRef<path::Path>],
includes: &[impl AsRef<path::Path>],
) -> anyhow::Result<()> {
prost_build::Config::new()
.bytes(["."])
.compile_protos(proto_files, includes)?;
Ok(())
}

#[cfg(feature = "serde")]
// Compile for `serde`: emit a file descriptor set, substitute `prost_types` for
// `pbjson_types`, and use `pbjson_build` to generate serde traits.
fn compile(
proto_files: &[impl AsRef<path::Path>],
includes: &[impl AsRef<path::Path>],
) -> anyhow::Result<()> {
let descriptor_path = path::PathBuf::from(env::var("OUT_DIR")?).join("proto_descriptor.bin");

prost_build::Config::new()
.bytes(["."])
.file_descriptor_set_path(&descriptor_path)
.compile_well_known_types()
.extern_path(".google.protobuf", "::pbjson_types")
.compile_protos(proto_files, &[proto_root])?;
.compile_protos(proto_files, includes)?;

let descriptor_set = std::fs::read(descriptor_path)?;
pbjson_build::Builder::new()
.register_descriptors(&descriptor_set)?
.build(&[".perfetto"])?;

Ok(())
}
7 changes: 6 additions & 1 deletion crates/schema/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! # `tracing-perfetto-sdk-schema`: Internal crate containing the raw Perfetto proto schemata.
include!(concat!(env!("OUT_DIR"), "/perfetto.protos.rs"));
include!(concat!(env!("OUT_DIR"), "/perfetto.protos.serde.rs"));
#[cfg(feature = "serde")]
mod serde_impls {
use super::*;
// Only contains trait impls so no need to re-export
include!(concat!(env!("OUT_DIR"), "/perfetto.protos.serde.rs"));
}

0 comments on commit ce5e5ca

Please sign in to comment.