-
Notifications
You must be signed in to change notification settings - Fork 188
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
upgrade protobuf to v2.0 and gen binding file in the build script #171
Conversation
Thanks! but CI failed. Could you make a fix for that? |
any update @flier |
It seems the |
Cargo.toml
Outdated
@@ -27,13 +27,14 @@ dev = ["clippy"] | |||
nightly = ["libc", "spin/unstable"] | |||
push = ["hyper", "libc"] | |||
process = ["libc", "procinfo"] | |||
gen = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to know why do we need to add a new feature? What will happen if user enables / disables this feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the
protoc-rust
depend on theprotoc
program which need be installed before build the crate.
The feature provide to the library developer who need use the latest protobuf
version and installed the protoc
program, and it should be disabled to make more easier with pre-generated binding file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add this to the README or a comment in the Cargo.toml
so that people will know about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
The crate has a pre-generated protobuf binding file for protobuf
v1.6, if you need use the latest version of protobuf
, you can generate the binding file on building with the gen
feature after you download and install the protocol buffer compiler.
[dependencies.prometheus]
git = "https://github.com/pingcap/rust-prometheus.git"
features = ["gen"]
build.rs
Outdated
customize: protoc_rust::Customize { | ||
..Default::default() | ||
}, | ||
}).expect("protoc"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be simplified to .unwrap()
build.rs
Outdated
out_dir: "proto", | ||
input: &["proto/metrics.proto"], | ||
includes: &["proto"], | ||
customize: protoc_rust::Customize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use protoc_rust::Customize::default()
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, fixed, just copy from rust-protobuf
example code
This will allow builds until tikv/rust-prometheus#171 lands
LGTM PTAL @overvenus |
LGTM! |
CI failed. Seems that protobuf 1.7 comes, and introduced new breaking changes.. |
@breeswish That's why we need a build script to generate the binding file on building :) |
@flier Agree.. Honestly I prefer to opt-in dynamic generation. However it requires protobuf to be installed, which is not very ideal.. |
Also I noticed that there is a pure rust codegen now. Maybe we can utilize that so that no more C++ protobuf dependency is required and we can safely generate |
LGTM! What will happen if we don't provide a pre-generated file, instead we always generate one at build? |
sure, after we changed to use pure Rust |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR!
build.rs
Outdated
fn main() { | ||
if cfg!(feature = "gen") { | ||
protobuf_codegen_pure::run(protobuf_codegen_pure::Args { | ||
out_dir: "proto", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not use the "proto", because it will modify the metrics.rs
in the local registry, also if we build multiple projects that deps on the same version prometheus, the file may be corrupted due to multiple writers. Instead you can try OUT_DIR
[0]. Here is an example [1].
[0] https://doc.rust-lang.org/cargo/reference/build-scripts.html#case-study-code-generation
[1] tikv/tikv#952
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is the generated binding file contains a lot of module attributes, that means we can't use the include!
macro to include the automatic generated binding file on the building time. The path
attribute could do it, but it doesn't accept a environment variable or concat!
macro, it only accept the static relative path.
#[path = "../proto/metrics.rs"]
pub mod proto;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try include!(concat!(env!("OUT_DIR"), "/metrics.rs"));
+ cfg_if! {}
. 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[cfg(feature = "gen")]
pub mod proto {
include!(concat!(env!("OUT_DIR"), "/metrics.rs"));
}
that's will cause a compile error, include! macro fails if included file has top-level inner attributes
error: an inner attribute is not permitted following an outer attribute
--> /Users/fl0120/github/rust-prometheus/target/debug/build/prometheus-52580fdca4c6c951/out/metrics.rs:12:3
|
12 | #![allow(missing_docs)]
| ^
|
= note: inner attributes, like#![no_std]
, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like#[test]
, annotate the item following them.
Cargo.toml
Outdated
@@ -56,5 +57,8 @@ optional = true | |||
getopts = "0.2" | |||
hyper = {version = "0.9", default-features = false} | |||
|
|||
[build-dependencies] | |||
protobuf-codegen-pure = "2.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make it an optional dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
@flier Thank you! |
@overvenus @breeswish Thanks |
The pre-generated binding file doesn't work for protobuf v1.6, we should generate it in the build script.