Skip to content

tools: Migrate to Protobuf RPC sessions #104

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tools/src/proto/gen/* linguist-generated=true
3 changes: 3 additions & 0 deletions tools/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

# These are backup files generated by rustfmt
**/*.rs.bk

# Local folder or symlink created to regenerate Protobuf files
/flipperzero-protobuf
137 changes: 131 additions & 6 deletions tools/Cargo.lock

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

5 changes: 5 additions & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ elf = "0.7"
hex = "0.4"
md-5 = "0.10"
once_cell = "1.17.1"
prost = "0.12"
rand = "0.8"
regex = "1.7.1"
serde = { version = "1.0.152", features = ["derive"] }
Expand All @@ -23,3 +24,7 @@ serialport = "4.2.0"
shlex = "1.1.0"
tempfile = "3"
which = "4"

[build-dependencies]
prost-build = "0.12"
which = "4"
69 changes: 69 additions & 0 deletions tools/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

macro_rules! flipper_proto {
($name:literal) => {
concat!("flipperzero-protobuf/", $name, ".proto")
};
}

macro_rules! stage_generated_proto {
($from:ident, $name:literal) => {
fs::copy($from.join($name), concat!("src/proto/gen/", $name))
};
}

const FLIPPER_PROTO_DIR: &str = "flipperzero-protobuf/";
const FLIPPER_PROTO: &str = flipper_proto!("flipper");

fn main() -> io::Result<()> {
// - We don't include the proto files in the repository so that downstreams do not
// need to regenerate the bindings even if protoc is present.
// - We check for the existence of protoc in the same way as prost-build, so that
// people building from source do not need to have protoc installed.
if Path::new(FLIPPER_PROTO).exists()
&& env::var_os("PROTOC")
.map(PathBuf::from)
.or_else(|| which::which("protoc").ok())
.is_some()
{
gen_protobufs()?;
}

Ok(())
}

fn gen_protobufs() -> io::Result<()> {
let out: PathBuf = env::var_os("OUT_DIR")
.expect("Cannot find OUT_DIR environment variable")
.into();

// Build the compact format types.
prost_build::compile_protos(
&[
FLIPPER_PROTO,
flipper_proto!("application"),
flipper_proto!("desktop"),
flipper_proto!("gpio"),
flipper_proto!("gui"),
flipper_proto!("property"),
flipper_proto!("storage"),
flipper_proto!("system"),
],
&[FLIPPER_PROTO_DIR],
)?;

// Copy the generated types into the source tree so changes can be committed.
stage_generated_proto!(out, "pb.rs")?;
stage_generated_proto!(out, "pb_app.rs")?;
stage_generated_proto!(out, "pb_desktop.rs")?;
stage_generated_proto!(out, "pb_gpio.rs")?;
stage_generated_proto!(out, "pb_gui.rs")?;
stage_generated_proto!(out, "pb_property.rs")?;
stage_generated_proto!(out, "pb_storage.rs")?;
stage_generated_proto!(out, "pb_system.rs")?;

Ok(())
}
Loading