Skip to content
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

Merged
merged 10 commits into from
May 23, 2018

Conversation

flier
Copy link
Contributor

@flier flier commented May 11, 2018

The pre-generated binding file doesn't work for protobuf v1.6, we should generate it in the build script.

error[E0433]: failed to resolve. Could not find `MessageStatic` in `protobuf`
   --> /Users/fl0120/.cargo/registry/src/github.com-1ecc6299db9ec823/prometheus-0.4.1/src/../proto/metrics.rs:213:21
    |
213 |         ::protobuf::MessageStatic::descriptor_static(None::<Self>)
    |                     ^^^^^^^^^^^^^ Could not find `MessageStatic` in `protobuf`

@breezewish
Copy link
Member

Thanks! but CI failed. Could you make a fix for that?

@siddontang
Copy link
Contributor

any update @flier

@flier
Copy link
Contributor Author

flier commented May 14, 2018

It seems the protoc-rust depend on the protoc program which need be installed before build the crate.

Cargo.toml Outdated
@@ -27,13 +27,14 @@ dev = ["clippy"]
nightly = ["libc", "spin/unstable"]
push = ["hyper", "libc"]
process = ["libc", "procinfo"]
gen = []
Copy link
Member

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?

Copy link
Contributor Author

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 the protoc 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.

Copy link
Contributor

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?

Copy link
Contributor Author

@flier flier May 22, 2018

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");
Copy link
Member

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 {
Copy link
Member

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?

Copy link
Contributor Author

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

acdha added a commit to acdha/mountstatus that referenced this pull request May 16, 2018
@siddontang
Copy link
Contributor

LGTM

PTAL @overvenus

@breezewish
Copy link
Member

LGTM!

@breezewish
Copy link
Member

CI failed. Seems that protobuf 1.7 comes, and introduced new breaking changes..

@flier flier changed the title upgrade protobuf to v1.6 and gen binding file in the build script upgrade protobuf to v1.7 and gen binding file in the build script May 22, 2018
@flier
Copy link
Contributor Author

flier commented May 22, 2018

@breeswish That's why we need a build script to generate the binding file on building :)

@breezewish
Copy link
Member

@flier Agree.. Honestly I prefer to opt-in dynamic generation. However it requires protobuf to be installed, which is not very ideal..

@breezewish
Copy link
Member

I took a look at protobuf's changelog, and it seems that they republished the breaking change into 2.0 version, and 1.7 should be the same as 1.5. I think it would be better to target at protobuf 2.0 for us? @flier

@breezewish
Copy link
Member

breezewish commented May 22, 2018

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 proto/metrics.rs on the fly for all users?

@breezewish
Copy link
Member

LGTM! What will happen if we don't provide a pre-generated file, instead we always generate one at build?

@flier
Copy link
Contributor Author

flier commented May 22, 2018

sure, after we changed to use pure Rust protoc, it will be fine; on the other hand, the pre-generated binding file will make building a little faster and reduce the troubleshooting cost.

@flier flier changed the title upgrade protobuf to v1.7 and gen binding file in the build script upgrade protobuf to v2.0 and gen binding file in the build script May 22, 2018
Copy link
Member

@overvenus overvenus left a 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",
Copy link
Member

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

Copy link
Contributor Author

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;

Copy link
Member

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! {}. 😉

Copy link
Contributor Author

@flier flier May 23, 2018

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"
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

@overvenus
Copy link
Member

@flier Thank you!

@overvenus overvenus merged commit 8724e5a into tikv:master May 23, 2018
@flier
Copy link
Contributor Author

flier commented May 23, 2018

@overvenus @breeswish Thanks

azdlowry pushed a commit to azdlowry/rust-prometheus that referenced this pull request Sep 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants