tafia / quick-protobuf

A rust implementation of protobuf parser
MIT License
452 stars 87 forks source link

It's hard for a build script to put pb-rs output in OUT_DIR #122

Closed jimblandy closed 6 years ago

jimblandy commented 6 years ago

The cargo package command checks that the build script doesn't modify the source directory; as the Cargo book explains, build scripts are not supposed to modify anything outside the directory given in the OUT_DIR environment variable.

This rule is hard to follow when calling pb-rs from build.rs: the only way I'm aware of to incorporate code from OUT_DIR is via an include! directive, but that chokes on the //! comments that pb-rs places at the top of its generated files. When I write:

mod mozilla {
include!(concat!(env!("OUT_DIR"), "/mozilla/mod.rs"))
}

rustc complains:

error: macros that expand to items must either be surrounded with braces or followed by a semicolon
 --> src/dump/mod.rs:7:9
  |
7 | include!(concat!(env!("OUT_DIR"), "/mozilla/mod.rs"))
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
jimblandy commented 6 years ago

Note that the #[path] attribute on modules doesn't seem to do macro expansion on its argument, so something like

#[path=concat!(env!("OUT_DIR"), "/mozilla/mod.rs")]
mod mozilla;

doesn't work either.

jimblandy commented 6 years ago

It's arguably a Rust bug that include! is incapable of incorporating text with //! comments. Fixing that would make this problem go away.

tafia commented 6 years ago

We can probably (optionally) remove the comments from generated file. There are not strictly required.

tafia commented 6 years ago

Alright, so I made it work by not generating all the headers at all. So I'll put all the write_headers function as optional in config. I'll also update the readme for build.rs